{"name":"alien-signals","version":"2.0.3","license":"MIT","_id":"alien-signals@2.0.3","maintainers":[{"name":"johnsoncodehk","email":"johnsoncodehk@gmail.com"}],"homepage":"https://github.com/johnsoncodehk/signals#readme","bugs":{"url":"https://github.com/johnsoncodehk/signals/issues"},"dist":{"shasum":"79f5938f426abe22cbc07d661cd8b429118f00fa","tarball":"http://registry-npm.ywwl.com/alien-signals/download/alien-signals-2.0.3.tgz","fileCount":9,"integrity":"sha512-s7dxjvnAFC9hHpvpAQvAKSRyo8viERBF76EblcGpicxOhs1BbCc3XLI7Sozr6HXaTBeK8v760jZU1M4MkNdtQg==","signatures":[{"sig":"MEQCIG9UWgw0HWQBEqlE2PcDEMleGrFCs8AuUtIbOb482thUAiA6FmNUgx2Nz2QC504w1PFzV+djMZ4YNJp+yoUW70jd3A==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":40978,"size":8050,"noattachment":false,"key":"/alien-signals/-/alien-signals-2.0.3.tgz"},"types":"./types/index.d.ts","exports":{".":{"types":"./types/index.d.ts","import":"./esm/index.mjs","require":"./cjs/index.cjs"},"./cjs":{"types":"./types/index.d.ts","import":"./cjs/index.cjs","require":"./cjs/index.cjs"},"./esm":{"types":"./types/index.d.ts","import":"./esm/index.mjs","require":"./esm/index.mjs"},"./system":{"types":"./types/system.d.ts","import":"./esm/system.mjs","require":"./cjs/system.cjs"},"./cjs/system":{"types":"./types/system.d.ts","import":"./cjs/system.cjs","require":"./cjs/system.cjs"},"./esm/system":{"types":"./types/system.d.ts","import":"./esm/system.mjs","require":"./esm/system.mjs"}},"gitHead":"8c47400adc17dcc4eed5ef808a7bc4771e7feff7","scripts":{"lint":"tsslint --project tsconfig.json","test":"vitest run","bench":"npm run build:esm && node --jitless --expose-gc benchs/propagate.mjs","build":"tsc && npm run build:esm && npm run build:cjs","build:cjs":"esbuild src/*.ts --format=cjs --outdir=cjs --out-extension:.js=.cjs","build:esm":"esbuild src/*.ts --format=esm --outdir=esm --out-extension:.js=.mjs","bench:memory":"npm run build:esm && node --expose-gc benchs/memoryUsage.mjs","prepublishOnly":"npm run build"},"_npmUser":{"name":"johnsoncodehk","email":"johnsoncodehk@gmail.com"},"deprecated":"Invalid packaging. Use 2.0.4 instead.","repository":{"url":"git+https://github.com/johnsoncodehk/signals.git","type":"git"},"_npmVersion":"10.9.0","description":"The lightest signal library.","directories":{},"sideEffects":false,"_nodeVersion":"22.11.0","_hasShrinkwrap":false,"packageManager":"pnpm@9.12.0","devDependencies":{"mitata":"latest","vitest":"latest","esbuild":"latest","typescript":"latest","@tsslint/cli":"latest","jest-extended":"latest","@tsslint/config":"latest"},"_npmOperationalInternal":{"tmp":"tmp/alien-signals_2.0.3_1746640833271_0.6476940082350926","host":"s3://npm-registry-packages-npm-production"},"readme":"<p align=\"center\">\n\t<img src=\"assets/logo.png\" width=\"250\"><br>\n<p>\n\n<p align=\"center\">\n\t<a href=\"https://npmjs.com/package/alien-signals\"><img src=\"https://badgen.net/npm/v/alien-signals\" alt=\"npm package\"></a>\n\t<a href=\"https://deepwiki.com/stackblitz/alien-signals\"><img src=\"https://deepwiki.com/badge.svg\" alt=\"Ask DeepWiki\"></a>\n</p>\n\n# alien-signals\n\nThis project explores a push-pull based signal algorithm. The implementation is related to the following frontend projects:\n\n- Propagation algorithm of Vue 3\n- Preact’s double-linked-list approach (https://preactjs.com/blog/signal-boosting/)\n- Inner effects scheduling of Svelte\n- Graph-coloring approach of Reactively (https://milomg.dev/2022-12-01/reactivity)\n\nWe impose some constraints (such as not using Array/Set/Map and disallowing function recursion in [the algorithmic core](https://github.com/stackblitz/alien-signals/blob/master/src/system.ts)) to ensure performance. We found that under these conditions, maintaining algorithmic simplicity offers more significant improvements than complex scheduling strategies.\n\nI wrote the reactivity code for both Vue and alien-signals. Below is a benchmark comparison against Vue 3.4 and other frameworks. The core algorithm has since been [ported back to Vue 3.6](https://github.com/vuejs/core/pull/12349).\n\n<img width=\"1210\" alt=\"Image\" src=\"https://github.com/user-attachments/assets/88448f6d-4034-4389-89aa-9edf3da77254\" />\n\n> Benchmark repo: https://github.com/transitive-bullshit/js-reactivity-benchmark\n\n## Background\n\nI spent considerable time [optimizing Vue 3.4’s reactivity system](https://github.com/vuejs/core/pull/5912), gaining experience along the way. Since Vue 3.5 [switched to a pull-based algorithm similar to Preact](https://github.com/vuejs/core/pull/10397), I decided to continue researching a push-pull based implementation in a separate project. The algorithm is used in Vue language tools for incremental AST parsing and virtual code generation.\n\n## Other Language Implementations\n\n- **Dart:** [medz/alien-signals-dart](https://github.com/medz/alien-signals-dart)\n- **Dart:** [void-signals/void_signals](https://github.com/void-signals/void_signals)\n- **Lua:** [YanqingXu/alien-signals-in-lua](https://github.com/YanqingXu/alien-signals-in-lua)\n- **Lua 5.4:** [xuhuanzy/alien-signals-lua](https://github.com/xuhuanzy/alien-signals-lua)\n- **Luau:** [Nicell/alien-signals-luau](https://github.com/Nicell/alien-signals-luau)\n- **Java:** [CTRL-Neo-Studios/java-alien-signals](https://github.com/CTRL-Neo-Studios/java-alien-signals)\n- **C#:** [CTRL-Neo-Studios/csharp-alien-signals](https://github.com/CTRL-Neo-Studios/csharp-alien-signals)\n- **Go:** [delaneyj/alien-signals-go](https://github.com/delaneyj/alien-signals-go)\n- **Rust:** [wuzekang/samara-signals](https://github.com/wuzekang/samara/tree/main/crates/signals)\n- **Rust:** [ohkami-rs/alien-signals-rs](https://github.com/ohkami-rs/alien-signals-rs)\n\n## Derived Projects\n\n- [Rajaniraiyn/react-alien-signals](https://github.com/Rajaniraiyn/react-alien-signals): React bindings for the alien-signals API\n- [CCherry07/alien-deepsignals](https://github.com/CCherry07/alien-deepsignals): Use alien-signals with the interface of a plain JavaScript object\n- [hunghg255/reactjs-signal](https://github.com/hunghg255/reactjs-signal): Share Store State with Signal Pattern\n- [gn8-ai/universe-alien-signals](https://github.com/gn8-ai/universe-alien-signals): Enables simple use of the Alien Signals state management system in modern frontend frameworks\n- [WebReflection/alien-signals](https://github.com/WebReflection/alien-signals): Preact signals like API and a class based approach for easy brand check\n- [@lift-html/alien](https://github.com/JLarky/lift-html/tree/main/packages/alien): Integrating alien-signals into lift-html\n- [ilha](https://github.com/ilhajs/ilha): A tiny web UI library built around the islands architecture\n\n## Adoption\n\n- [vuejs/core](https://github.com/vuejs/core): The core algorithm has been ported to v3.6 (PR: https://github.com/vuejs/core/pull/12349)\n- [statelyai/xstate](https://github.com/statelyai/xstate): The core algorithm has been ported to implement the atom architecture (PR: https://github.com/statelyai/xstate/pull/5250)\n- [flamrdevs/xignal](https://github.com/flamrdevs/xignal): Infrastructure for the reactive system\n- [vuejs/language-tools](https://github.com/vuejs/language-tools): Used in the language-core package for virtual code generation\n- [unuse](https://github.com/un-ts/unuse): A framework-agnostic `use` library inspired by `VueUse`\n\n## Usage\n\n#### Basic APIs\n\n```ts\nimport { signal, computed, effect } from 'alien-signals';\n\nconst count = signal(1);\nconst doubleCount = computed(() => count() * 2);\n\neffect(() => {\n  console.log(`Count is: ${count()}`);\n}); // Console: Count is: 1\n\nconsole.log(doubleCount()); // 2\n\ncount(2); // Console: Count is: 2\n\nconsole.log(doubleCount()); // 4\n```\n\n#### Effect Scope\n\n```ts\nimport { signal, effect, effectScope } from 'alien-signals';\n\nconst count = signal(1);\n\nconst stopScope = effectScope(() => {\n  effect(() => {\n    console.log(`Count in scope: ${count()}`);\n  }); // Console: Count in scope: 1\n});\n\ncount(2); // Console: Count in scope: 2\n\nstopScope();\n\ncount(3); // No console output\n```\n\n#### Nested Effects\n\nEffects can be nested inside other effects. When the outer effect re-runs, inner effects from the previous run are automatically cleaned up, and new inner effects are created if needed. The system ensures proper execution order — outer effects always run before their inner effects:\n\n```ts\nimport { signal, effect } from 'alien-signals';\n\nconst show = signal(true);\nconst count = signal(1);\n\neffect(() => {\n  if (show()) {\n    // This inner effect is created when show() is true\n    effect(() => {\n      console.log(`Count is: ${count()}`);\n    });\n  }\n}); // Console: Count is: 1\n\ncount(2); // Console: Count is: 2\n\n// When show becomes false, the inner effect is cleaned up\nshow(false); // No output\n\ncount(3); // No output (inner effect no longer exists)\n```\n\n#### Manual Triggering\n\nThe `trigger()` function allows you to manually trigger updates for downstream dependencies when you've directly mutated a signal's value without using the signal setter:\n\n```ts\nimport { signal, computed, trigger } from 'alien-signals';\n\nconst arr = signal<number[]>([]);\nconst length = computed(() => arr().length);\n\nconsole.log(length()); // 0\n\n// Direct mutation doesn't automatically trigger updates\narr().push(1);\nconsole.log(length()); // Still 0\n\n// Manually trigger updates\ntrigger(arr);\nconsole.log(length()); // 1\n```\n\nYou can also trigger multiple signals at once:\n\n```ts\nimport { signal, computed, trigger } from 'alien-signals';\n\nconst src1 = signal<number[]>([]);\nconst src2 = signal<number[]>([]);\nconst total = computed(() => src1().length + src2().length);\n\nsrc1().push(1);\nsrc2().push(2);\n\ntrigger(() => {\n  src1();\n  src2();\n});\n\nconsole.log(total()); // 2\n```\n\n#### Creating Your Own Surface API\n\nYou can reuse alien-signals’ core algorithm via `createReactiveSystem()` to build your own signal API. For implementation examples, see:\n\n- [Starter template](https://github.com/johnsoncodehk/alien-signals-starter) (implements  `.get()` & `.set()` methods like the [Signals proposal](https://github.com/tc39/proposal-signals))\n- [stackblitz/alien-signals/src/index.ts](https://github.com/stackblitz/alien-signals/blob/master/src/index.ts)\n- [proposal-signals/signal-polyfill#44](https://github.com/proposal-signals/signal-polyfill/pull/44)\n\n\n## About `propagate` and `checkDirty` functions\n\nThe actual implementations of `propagate` and `checkDirty` in [system.ts](https://github.com/stackblitz/alien-signals/blob/master/src/system.ts) replace recursive calls with iterative stack-based traversal for performance. The recursive versions below are equivalent and easier to follow — useful as a reference when porting to other languages where the iterative optimization may not help.\n\n<details>\n<summary><code>propagate</code></summary>\n\n```ts\nfunction propagate(link: Link, innerWrite: boolean): void {\n\tdo {\n\t\tconst sub = link.sub;\n\n\t\tlet flags = sub.flags;\n\n\t\tif (!(flags & (ReactiveFlags.RecursedCheck | ReactiveFlags.Recursed | ReactiveFlags.Dirty | ReactiveFlags.Pending))) {\n\t\t\tsub.flags = flags | ReactiveFlags.Pending;\n\t\t\tif (innerWrite) {\n\t\t\t\tsub.flags |= ReactiveFlags.Recursed;\n\t\t\t}\n\t\t} else if (!(flags & (ReactiveFlags.RecursedCheck | ReactiveFlags.Recursed))) {\n\t\t\tflags = ReactiveFlags.None;\n\t\t} else if (!(flags & ReactiveFlags.RecursedCheck)) {\n\t\t\tsub.flags = (flags & ~ReactiveFlags.Recursed) | ReactiveFlags.Pending;\n\t\t} else if (!(flags & (ReactiveFlags.Dirty | ReactiveFlags.Pending)) && isValidLink(link, sub)) {\n\t\t\tsub.flags = flags | ReactiveFlags.Recursed | ReactiveFlags.Pending;\n\t\t\tflags &= ReactiveFlags.Mutable;\n\t\t} else {\n\t\t\tflags = ReactiveFlags.None;\n\t\t}\n\n\t\tif (flags & ReactiveFlags.Watching) {\n\t\t\tnotify(sub);\n\t\t}\n\n\t\tif (flags & ReactiveFlags.Mutable) {\n\t\t\tconst subSubs = sub.subs;\n\t\t\tif (subSubs !== undefined) {\n\t\t\t\tpropagate(subSubs, innerWrite);\n\t\t\t}\n\t\t}\n\n\t\tlink = link.nextSub!;\n\t} while (link !== undefined);\n}\n```\n</details>\n\n<details>\n<summary><code>checkDirty</code></summary>\n\n```ts\nfunction checkDirty(link: Link, sub: ReactiveNode): boolean {\n\tdo {\n\t\tconst dep = link.dep;\n\t\tconst depFlags = dep.flags;\n\n\t\tif (sub.flags & ReactiveFlags.Dirty) {\n\t\t\treturn true;\n\t\t} else if ((depFlags & (ReactiveFlags.Mutable | ReactiveFlags.Dirty)) === (ReactiveFlags.Mutable | ReactiveFlags.Dirty)) {\n\t\t\tif (update(dep)) {\n\t\t\t\tconst subs = dep.subs!;\n\t\t\t\tif (subs.nextSub !== undefined) {\n\t\t\t\t\tshallowPropagate(subs);\n\t\t\t\t}\n\t\t\t\treturn true;\n\t\t\t}\n\t\t} else if ((depFlags & (ReactiveFlags.Mutable | ReactiveFlags.Pending)) === (ReactiveFlags.Mutable | ReactiveFlags.Pending)) {\n\t\t\tif (checkDirty(dep.deps!, dep)) {\n\t\t\t\tif (update(dep)) {\n\t\t\t\t\tconst subs = dep.subs!;\n\t\t\t\t\tif (subs.nextSub !== undefined) {\n\t\t\t\t\t\tshallowPropagate(subs);\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tdep.flags = depFlags & ~ReactiveFlags.Pending;\n\t\t\t}\n\t\t}\n\n\t\tlink = link.nextDep!;\n\t} while (link !== undefined);\n\n\treturn false;\n}\n```\n</details>\n","publish_time":1746640833465,"_cnpm_publish_time":1746640833465,"dist-tags":{"latest":"3.2.1"}}