Last active
July 17, 2025 20:13
-
-
Save metruzanca/76b704484daa7f43b177d1af6f179bab to your computer and use it in GitHub Desktop.
The poor man's SolidJS implemented in 40 lines
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 $ = document.querySelector.bind(document), | |
$$ = document.querySelectorAll.bind(document), | |
h = (name, props) => Object.assign(document.createElement(name), props); | |
const context = [] | |
const getCurrentObserver = () => context[context.length - 1] | |
function createEffect(fn, name) { | |
const execute = () => { | |
context.push(execute) | |
try { | |
fn() | |
} finally { | |
context.pop() | |
} | |
} | |
execute() | |
} | |
function createSignal(value) { | |
const subscribers = new Set() | |
const read = () => { | |
const current = getCurrentObserver() | |
if (current) subscribers.add(current) | |
return value | |
} | |
const write = (nextValue) => { | |
if (typeof nextValue === 'function') { | |
value = nextValue(value) | |
} else { | |
value = nextValue | |
} | |
for (const sub of subscribers) sub() | |
} | |
return [read, write] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment