This file contains hidden or 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
| const Counter = component({ | |
| state: { | |
| count: 0 | |
| }, | |
| actions: { | |
| decrement: ({ count }) => ({ count: count - 1 }), | |
| increment: ({ count }) => ({ count: count + 1 }) | |
| }, | |
| view: ({ count }, { decrement, increment }) => <div>...</div> | |
| }); |
This file contains hidden or 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
| const component = (options = {}) => (vnode) { | |
| let state = { ...options.state, ...vnode.attrs }; | |
| const proxyActions = {}; | |
| Object.keys(options.actions).forEach((key) => { | |
| proxyActions[key] = (...args) => { | |
| const newState = options.actions[key](state, proxyActions, ...args); | |
| if (newState) { | |
| state = { ...state, ...newState }; | |
| } |
This file contains hidden or 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
| const Counter = ({ attrs: { count = 0 } }) => { | |
| const decrement = () => count--; | |
| const increment = () => count++; | |
| return { | |
| view: () => <div>...</div> | |
| }; | |
| }; |
This file contains hidden or 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
| const Counter = ({ count, decrement, increment }) => ( | |
| <div>...</div> | |
| ); | |
| app({ | |
| state: { ... }, | |
| actions: { ... }, | |
| view: (state, actions) => | |
| <Counter | |
| count={state.count} |
This file contains hidden or 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
| app({ | |
| state: { | |
| count: 0 | |
| }, | |
| actions: { | |
| decrement: ({ count }) => ({ count: count - 1 }), | |
| increment: ({ count }) => ({ count: count + 1 }) | |
| }, | |
| view: ({ count }, { decrement, increment }) => | |
| <div> |
This file contains hidden or 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
| const Counter = ({ attrs: { count = 0 } }) => { | |
| const decrement = () => count--; | |
| const increment = () => count++; | |
| return { | |
| view: () => | |
| <div> | |
| <h1>{count}</h1> | |
| <button onclick={decrement} disabled={count <= 0}>-</button> | |
| <button onclick={increment}>+</button> |
This file contains hidden or 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
| // map | |
| const double = x => x * 2; | |
| map(double, [1, 2, 3]); // --> [2, 4, 6] | |
| // filter | |
| const isEven = x => x % 2 === 0; | |
| filter(isEven, [1, 2, 3, 4]); // --> [2, 4] | |
| // complement | |
| const isOdd = complement(isEven); |
This file contains hidden or 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
| class EventBus { | |
| private handlers: Record<string, ((...args: any[]) => void)[]> = {}; | |
| trigger(eventName: string, ...args: any[]) { | |
| if (this.handlers[eventName]) { | |
| this.handlers[eventName].forEach((handler) => handler(...args)); | |
| } | |
| } | |
| on(eventName: string, handler: (...args: any[]) => void) { |
NewerOlder