Skip to content

Instantly share code, notes, and snippets.

@mendes5
Created September 9, 2022 19:36
Show Gist options
  • Select an option

  • Save mendes5/6b0d9fdc2c0788da9ea246b73c152235 to your computer and use it in GitHub Desktop.

Select an option

Save mendes5/6b0d9fdc2c0788da9ea246b73c152235 to your computer and use it in GitHub Desktop.
Poor mans version of S.js
const effects = [];
let isCollectingEffects = false;
const data = (init) => {
const deps = [];
let oldValue = init;
return (value) => {
if (value === undefined) {
if (isCollectingEffects) {
effects.push(deps);
}
return oldValue;
} else {
oldValue = value;
deps.forEach((cb) => cb());
}
};
};
const S = (callback) => {
isCollectingEffects = true;
effects.splice(0);
callback();
isCollectingEffects = false;
effects.forEach((effect) => {
effect.push(callback);
});
};
const name = data("mendes");
S(() => {
document.body.innerText = name();
});
S(() => {
console.log(name());
});
setTimeout(() => {
name("Phellipe");
}, 3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment