Skip to content

Instantly share code, notes, and snippets.

@damusix
Last active May 2, 2019 20:39
Show Gist options
  • Save damusix/78fa468672d3dfd198d939ed67c2198f to your computer and use it in GitHub Desktop.
Save damusix/78fa468672d3dfd198d939ed67c2198f to your computer and use it in GitHub Desktop.
No-stream implementation of meiosis pattern http://meiosis.js.org
const store = {
// Initial state
state: {
working: true,
items: [],
},
actions: (update) => ({
getThings: () => {
update({
...store.state,
items: [1,2,3]
});
},
breakThings: () => {
update({
...store.state,
working: false
});
},
fixThings: () => {
update({
...store.state,
working: true
});
}
})
};
// Need a place to store listeners bc of nature of ReadableStream
const listeners = [];
// Attaches functions to listeners
const states = (fn) => listeners.push(fn);
// Merges state and pushes to stream
update = (state) => {
store.state = {
...store.state,
...state
};
listeners.forEach(fn => fn(store.state))
};
// Bind update function
const actions = store.actions(update);
// Add log listener
states(console.log);
const el = document.createElement('div');
document.body.appendChild(el);
// Write to document listener
states((state) => {
const pre = document.createElement('pre');
pre.innerHTML = JSON.stringify(state, null, 4);
el.appendChild(pre);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment