Skip to content

Instantly share code, notes, and snippets.

View vdsabev's full-sized avatar

Vlad Sabev vdsabev

View GitHub Profile
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) {
// 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);
@vdsabev
vdsabev / 02-hyperapp-counter.jsx
Last active August 15, 2017 13:27
Exploring Unidirectional Components in Mithril
app({
state: {
count: 0
},
actions: {
decrement: ({ count }) => ({ count: count - 1 }),
increment: ({ count }) => ({ count: count + 1 })
},
view: ({ count }, { decrement, increment }) =>
<div>
@vdsabev
vdsabev / 03-hyperapp-counter-component.jsx
Last active August 15, 2017 13:27
Exploring Unidirectional Components in Mithril
const Counter = ({ count, decrement, increment }) => (
<div>...</div>
);
app({
state: { ... },
actions: { ... },
view: (state, actions) =>
<Counter
count={state.count}
@vdsabev
vdsabev / 04-mithril-counter-trimmed.jsx
Last active August 15, 2017 13:27
Exploring Unidirectional Components in Mithril
const Counter = ({ attrs: { count = 0 } }) => {
const decrement = () => count--;
const increment = () => count++;
return {
view: () => <div>...</div>
};
};
@vdsabev
vdsabev / 06-hypermithril-counter.jsx
Last active August 15, 2017 13:28
Exploring Unidirectional Components in Mithril
const Counter = component({
state: {
count: 0
},
actions: {
decrement: ({ count }) => ({ count: count - 1 }),
increment: ({ count }) => ({ count: count + 1 })
},
view: ({ count }, { decrement, increment }) => <div>...</div>
});
@vdsabev
vdsabev / 01-mithril-counter-class.jsx
Last active August 17, 2017 09:17
Exploring Unidirectional Components in Mithril
class Counter {
oninit({ attrs }) {
this.count = attrs.count || 0;
}
decrement = () => this.count--;
increment = () => this.count++;
view() {
return (
@vdsabev
vdsabev / 01-mithril-counter-closure.jsx
Last active August 17, 2017 10:20
Exploring Unidirectional Components in Mithril
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>
@vdsabev
vdsabev / 05-hypermithril-basic.jsx
Last active August 17, 2017 10:28
Exploring Unidirectional Components in Mithril
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 };
}
@vdsabev
vdsabev / awesome.md
Last active October 26, 2017 15:17
An awesome list of awesome things

Development

Animation & Interactivity

  • IntersectionObserver - get notified when element properties change, e.g. when an element becomes visible or invisible

Authentication

Components

  • Web components - encapsulate HTML / CSS / JS components and use them across the whole web