{"name":"alien-signals","version":"0.4.7","license":"MIT","_id":"alien-signals@0.4.7","homepage":"https://github.com/johnsoncodehk/signals#readme","bugs":{"url":"https://github.com/johnsoncodehk/signals/issues"},"dist":{"shasum":"901758689e41bc8c4251dc972d8d555c526842b3","tarball":"http://registry-npm.ywwl.com/alien-signals/download/alien-signals-0.4.7.tgz","fileCount":27,"integrity":"sha512-RXX8MsER25M4Dlq49b1aT8vGz3vjGwdYONy3be4toZSAOok2eVePTtjiJwpL8eRuLnrMhGyR2+ds75m2avTLoA==","signatures":[{"sig":"MEUCIDX/Mk73Q6KLIctDKFEWygbfBY3b15egsHnm9NIvxsI9AiEA9513Dnmz9iW/WMVaB4nOU+lrRYXHT7bYBUEvZ3JYluE=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":53007,"size":9476,"noattachment":false,"key":"/alien-signals/-/alien-signals-0.4.7.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"}},"gitHead":"5771ffbfbcd4832f89260f96055f23dcc34e6db7","scripts":{"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/index.ts --bundle --format=cjs --outfile=cjs/index.cjs","build:esm":"esbuild src/index.ts --bundle --format=esm --outfile=esm/index.mjs","prepublishOnly":"npm run build"},"_npmUser":{"name":"johnsoncodehk","email":"johnsoncodehk@gmail.com"},"repository":{"url":"git+https://github.com/johnsoncodehk/signals.git","type":"git"},"_npmVersion":"10.9.0","description":"The lightest signal library.","directories":{},"_nodeVersion":"22.11.0","_hasShrinkwrap":false,"packageManager":"pnpm@9.12.0","devDependencies":{"mitata":"latest","vitest":"latest","esbuild":"latest","typescript":"latest"},"_npmOperationalInternal":{"tmp":"tmp/alien-signals_0.4.7_1734875392109_0.8014381623717126","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</p>\n\n# alien-signals\n\nThis project explores a push-pull based signal algorithm. Its current implementation is similar to or related to certain other 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) to ensure performance. We found that under these conditions, maintaining algorithmic simplicity offers more significant improvements than complex scheduling strategies.\n\nEven though Vue 3.4 is already optimized, alien-signals is still noticeably faster. (I wrote code for both, and since they share similar algorithms, they’re quite comparable.)\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. Our end goal is to implement fully incremental AST parsing and virtual code generation in Vue language tools, based on alien-signals.\n\n## Derived Projects\n\n- [YanqingXu/alien-signals-in-lua](https://github.com/YanqingXu/alien-signals-in-lua): Lua implementation of alien-signals\n- [medz/alien-signals-dart](https://github.com/medz/alien-signals-dart): Dart implementation of alien-signals\n- [delaneyj/alien-signals-go](https://github.com/delaneyj/alien-signals-go): Go implementation of alien-signals\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\n## Adoption\n\n- [vuejs/core](https://github.com/vuejs/core): The core algorithm has been ported to 3.6 or higher (PR：https://github.com/vuejs/core/pull/12349)\n- [vuejs/language-tools](https://github.com/vuejs/language-tools): Used in the language-core package for virtual code generation\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, 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  count(2); // Console: Count in scope: 2\n});\n\nstopScope();\n\ncount(3); // No console output\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\nIn order to eliminate recursive calls and improve performance, we record the last link node of the previous loop in `propagate` and `checkDirty` functions, and implement the rollback logic to return to this node.\n\nThis results in code that is difficult to understand, and you don't necessarily get the same performance improvements in other languages, so we record the original implementation without eliminating recursive calls here for reference.\n\n#### `propagate`\n\n```ts\nfunction propagate(link: Link, targetFlag = SubscriberFlags.Dirty): void {\n\tdo {\n\t\tconst sub = link.sub;\n\t\tconst subFlags = sub.flags;\n\n\t\tif (\n\t\t\t(\n\t\t\t\t!(subFlags & (SubscriberFlags.Tracking | SubscriberFlags.Recursed | SubscriberFlags.Propagated))\n\t\t\t\t&& (sub.flags = subFlags | targetFlag | SubscriberFlags.Notified, true)\n\t\t\t)\n\t\t\t|| (\n\t\t\t\t(subFlags & SubscriberFlags.Recursed)\n\t\t\t\t&& !(subFlags & SubscriberFlags.Tracking)\n\t\t\t\t&& (sub.flags = (subFlags & ~SubscriberFlags.Recursed) | targetFlag | SubscriberFlags.Notified, true)\n\t\t\t)\n\t\t\t|| (\n\t\t\t\t!(subFlags & SubscriberFlags.Propagated)\n\t\t\t\t&& isValidLink(link, sub)\n\t\t\t\t&& (\n\t\t\t\t\tsub.flags = subFlags | SubscriberFlags.Recursed | targetFlag | SubscriberFlags.Notified,\n\t\t\t\t\t(sub as Dependency).subs !== undefined\n\t\t\t\t)\n\t\t\t)\n\t\t) {\n\t\t\tconst subSubs = (sub as Dependency).subs;\n\t\t\tif (subSubs !== undefined) {\n\t\t\t\tpropagate(\n\t\t\t\t\tsubSubs,\n\t\t\t\t\tsubFlags & SubscriberFlags.Effect\n\t\t\t\t\t\t? SubscriberFlags.PendingEffect\n\t\t\t\t\t\t: SubscriberFlags.PendingComputed\n\t\t\t\t);\n\t\t\t} else if (subFlags & SubscriberFlags.Effect) {\n\t\t\t\tif (queuedEffectsTail !== undefined) {\n\t\t\t\t\tqueuedEffectsTail.depsTail!.nextDep = sub.deps;\n\t\t\t\t} else {\n\t\t\t\t\tqueuedEffects = sub;\n\t\t\t\t}\n\t\t\t\tqueuedEffectsTail = sub;\n\t\t\t}\n\t\t} else if (!(subFlags & (SubscriberFlags.Tracking | targetFlag))) {\n\t\t\tsub.flags = subFlags | targetFlag | SubscriberFlags.Notified;\n\t\t\tif ((subFlags & (SubscriberFlags.Effect | SubscriberFlags.Notified)) === SubscriberFlags.Effect) {\n\t\t\t\tif (queuedEffectsTail !== undefined) {\n\t\t\t\t\tqueuedEffectsTail.depsTail!.nextDep = sub.deps;\n\t\t\t\t} else {\n\t\t\t\t\tqueuedEffects = sub;\n\t\t\t\t}\n\t\t\t\tqueuedEffectsTail = sub;\n\t\t\t}\n\t\t} else if (\n\t\t\t!(subFlags & targetFlag)\n\t\t\t&& (subFlags & SubscriberFlags.Propagated)\n\t\t\t&& isValidLink(link, sub)\n\t\t) {\n\t\t\tsub.flags = subFlags | targetFlag;\n\t\t}\n\n\t\tlink = link.nextSub!;\n\t} while (link !== undefined);\n}\n```\n\n#### `checkDirty`\n\n```ts\nfunction checkDirty(link: Link): boolean {\n\tdo {\n\t\tconst dep = link.dep;\n\t\tif ('flags' in dep) {\n\t\t\tconst depFlags = dep.flags;\n\t\t\tif ((depFlags & (SubscriberFlags.Computed | SubscriberFlags.Dirty)) === (SubscriberFlags.Computed | SubscriberFlags.Dirty)) {\n\t\t\t\tif (updateComputed(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 if ((depFlags & (SubscriberFlags.Computed | SubscriberFlags.PendingComputed)) === (SubscriberFlags.Computed | SubscriberFlags.PendingComputed)) {\n\t\t\t\tif (checkDirty(dep.deps!)) {\n\t\t\t\t\tif (updateComputed(dep)) {\n\t\t\t\t\t\tconst subs = dep.subs!;\n\t\t\t\t\t\tif (subs.nextSub !== undefined) {\n\t\t\t\t\t\t\tshallowPropagate(subs);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tdep.flags = depFlags & ~SubscriberFlags.PendingComputed;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tlink = link.nextDep!;\n\t} while (link !== undefined);\n\n\treturn false;\n}\n```\n","publish_time":1734875392336,"maintainers":[{"name":"johnsoncodehk","email":"johnsoncodehk@gmail.com"}],"_cnpm_publish_time":1734875392336,"dist-tags":{"latest":"3.2.1"}}