-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
84 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
.counter { | ||
align-items: center; | ||
display: flex; | ||
padding: 1rem; | ||
} | ||
|
||
.counter span, | ||
.counter button { | ||
font-size: 200%; | ||
} | ||
|
||
.counter span { | ||
display: inline-block; | ||
margin: auto 0.5rem; | ||
min-width: 4rem; | ||
text-align: center; | ||
} | ||
|
||
.counter button { | ||
width: 4rem; | ||
height: 4rem; | ||
border: none; | ||
border-radius: 10px; | ||
background-color: seagreen; | ||
color: white; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<script type="importmap"> | ||
{ "imports": { "help-es": "../lib/index.js", "help-es/": "../lib/" } } | ||
</script> | ||
<script type="esms-options"> | ||
{ "polyfillEnable": ["css-modules", "json-modules"] } | ||
</script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script> | ||
<script type="module" src="./counter.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { addStyle, createState, html, render } from 'help-es'; | ||
import styles from './counter.css' assert { type: 'css' }; | ||
|
||
addStyle(styles); | ||
|
||
const store = createState({ | ||
counter: 0, | ||
increment() { this.counter++ }, | ||
decrement() { this.counter-- }, | ||
}); | ||
|
||
function App() { | ||
return html` | ||
<div class="counter"> | ||
<button @click=${store.decrement}>–</button> | ||
<span>${store.counter}</span> | ||
<button @click=${store.increment}>+</button> | ||
</div> | ||
`; | ||
} | ||
|
||
const update = () => render(document.body, App()); | ||
store.observe(update); | ||
update(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,24 @@ | ||
import { Fn } from './types'; | ||
|
||
type Callback = Fn<[...any[]], any>; // eslint-disable-line | ||
|
||
type Callback = Fn<[...any[]], any>; | ||
const _ = new WeakMap(); | ||
const { defineProperty, getOwnPropertyDescriptor } = Object; | ||
|
||
export function bind<T extends object>(ctx: T, arg?: Callback | keyof T) { | ||
if (arg) { | ||
const fn = typeof arg === 'function' ? arg : <Callback>ctx[arg as never]; | ||
const fns = _.get(ctx) || _.set(ctx, new Map()).get(ctx); | ||
return fns.get(fn) || fns.set(fn, fn.bind(ctx)).get(fn); | ||
} | ||
|
||
for (const key in ctx) { | ||
const d = getOwnPropertyDescriptor(ctx, key); | ||
if (d?.get && d?.configurable) { | ||
defineProperty(ctx, key, { get: d.get.bind(ctx) }); | ||
} else if (typeof ctx[key] === 'function') { | ||
defineProperty(ctx, key, { value: (ctx[key] as any).bind(ctx) }); | ||
} | ||
} | ||
|
||
export function bind<T extends object>(ctx: T, cb: Callback | keyof T) { | ||
const fn = typeof cb === 'function' ? cb : <Callback>ctx[cb as never]; | ||
const fns = _.get(ctx) || _.set(ctx, new Map()).get(ctx); | ||
return fns.get(fn) || fns.set(fn, fn.bind(ctx)).get(fn); | ||
return ctx; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters