Skip to content

Instantly share code, notes, and snippets.

@artalar
Last active August 15, 2023 02:34
Show Gist options
  • Save artalar/b630837f32a9138eff0630a1be087baa to your computer and use it in GitHub Desktop.
Save artalar/b630837f32a9138eff0630a1be087baa to your computer and use it in GitHub Desktop.
How to add extra logic to Reatom context
const ctxBase = createCtx();
const listeners = new Set();
const { subscribe } = ctx;
export const ctx = Object.assign({}, ctxBase, {
subscribe(...a) {
const un = subscribe.apply(this, a);
// logs
if (a.length === 1) return un;
listeners.add(un);
return () => {
listeners.delete(un);
un();
};
},
});
export const reset = () => {
listeners.forEach((un) => un());
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment