Last active
August 15, 2023 02:34
-
-
Save artalar/b630837f32a9138eff0630a1be087baa to your computer and use it in GitHub Desktop.
How to add extra logic to Reatom context
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 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