Skip to content

Instantly share code, notes, and snippets.

@metruzanca
Last active July 17, 2025 20:13
Show Gist options
  • Save metruzanca/76b704484daa7f43b177d1af6f179bab to your computer and use it in GitHub Desktop.
Save metruzanca/76b704484daa7f43b177d1af6f179bab to your computer and use it in GitHub Desktop.
The poor man's SolidJS implemented in 40 lines
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