提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
7.8 KiB

  1. <p align="center">
  2. <img src="assets/logo.png" width="250"><br>
  3. <p>
  4. <p align="center">
  5. <a href="https://npmjs.com/package/alien-signals"><img src="https://badgen.net/npm/v/alien-signals" alt="npm package"></a>
  6. <a href="https://deepwiki.com/stackblitz/alien-signals"><img src="https://deepwiki.com/badge.svg" alt="Ask DeepWiki"></a>
  7. </p>
  8. # alien-signals
  9. This project explores a push-pull based signal algorithm. Its current implementation is similar to or related to certain other frontend projects:
  10. - Propagation algorithm of Vue 3
  11. - Preact’s double-linked-list approach (https://preactjs.com/blog/signal-boosting/)
  12. - Inner effects scheduling of Svelte
  13. - Graph-coloring approach of Reactively (https://milomg.dev/2022-12-01/reactivity)
  14. We 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.
  15. Even 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.)
  16. <img width="1210" alt="Image" src="https://github.com/user-attachments/assets/88448f6d-4034-4389-89aa-9edf3da77254" />
  17. > Benchmark repo: https://github.com/transitive-bullshit/js-reactivity-benchmark
  18. ## Background
  19. I 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.
  20. ## Other Language Implementations
  21. - **Dart:** [medz/alien-signals-dart](https://github.com/medz/alien-signals-dart)
  22. - **Lua:** [YanqingXu/alien-signals-in-lua](https://github.com/YanqingXu/alien-signals-in-lua)
  23. - **Lua 5.4:** [xuhuanzy/alien-signals-lua](https://github.com/xuhuanzy/alien-signals-lua)
  24. - **Luau:** [Nicell/alien-signals-luau](https://github.com/Nicell/alien-signals-luau)
  25. - **Java:** [CTRL-Neo-Studios/java-alien-signals](https://github.com/CTRL-Neo-Studios/java-alien-signals)
  26. - **C#:** [CTRL-Neo-Studios/csharp-alien-signals](https://github.com/CTRL-Neo-Studios/csharp-alien-signals)
  27. - **Go:** [delaneyj/alien-signals-go](https://github.com/delaneyj/alien-signals-go)
  28. ## Derived Projects
  29. - [Rajaniraiyn/react-alien-signals](https://github.com/Rajaniraiyn/react-alien-signals): React bindings for the alien-signals API
  30. - [CCherry07/alien-deepsignals](https://github.com/CCherry07/alien-deepsignals): Use alien-signals with the interface of a plain JavaScript object
  31. - [hunghg255/reactjs-signal](https://github.com/hunghg255/reactjs-signal): Share Store State with Signal Pattern
  32. - [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
  33. - [WebReflection/alien-signals](https://github.com/WebReflection/alien-signals): Preact signals like API and a class based approach for easy brand check
  34. - [@lift-html/alien](https://github.com/JLarky/lift-html/tree/main/packages/alien): Integrating alien-signals into lift-html
  35. ## Adoption
  36. - [vuejs/core](https://github.com/vuejs/core): The core algorithm has been ported to v3.6 (PR: https://github.com/vuejs/core/pull/12349)
  37. - [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)
  38. - [flamrdevs/xignal](https://github.com/flamrdevs/xignal): Infrastructure for the reactive system
  39. - [vuejs/language-tools](https://github.com/vuejs/language-tools): Used in the language-core package for virtual code generation
  40. - [unuse](https://github.com/un-ts/unuse): A framework-agnostic `use` library inspired by `VueUse`
  41. ## Usage
  42. #### Basic APIs
  43. ```ts
  44. import { signal, computed, effect } from 'alien-signals';
  45. const count = signal(1);
  46. const doubleCount = computed(() => count() * 2);
  47. effect(() => {
  48. console.log(`Count is: ${count()}`);
  49. }); // Console: Count is: 1
  50. console.log(doubleCount()); // 2
  51. count(2); // Console: Count is: 2
  52. console.log(doubleCount()); // 4
  53. ```
  54. #### Effect Scope
  55. ```ts
  56. import { signal, effect, effectScope } from 'alien-signals';
  57. const count = signal(1);
  58. const stopScope = effectScope(() => {
  59. effect(() => {
  60. console.log(`Count in scope: ${count()}`);
  61. }); // Console: Count in scope: 1
  62. });
  63. count(2); // Console: Count in scope: 2
  64. stopScope();
  65. count(3); // No console output
  66. ```
  67. #### Creating Your Own Surface API
  68. You can reuse alien-signals’ core algorithm via `createReactiveSystem()` to build your own signal API. For implementation examples, see:
  69. - [Starter template](https://github.com/johnsoncodehk/alien-signals-starter) (implements `.get()` & `.set()` methods like the [Signals proposal](https://github.com/tc39/proposal-signals))
  70. - [stackblitz/alien-signals/src/index.ts](https://github.com/stackblitz/alien-signals/blob/master/src/index.ts)
  71. - [proposal-signals/signal-polyfill#44](https://github.com/proposal-signals/signal-polyfill/pull/44)
  72. ## About `propagate` and `checkDirty` functions
  73. In 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.
  74. This 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.
  75. #### `propagate`
  76. ```ts
  77. function propagate(link: Link): void {
  78. do {
  79. const sub = link.sub;
  80. let flags = sub.flags;
  81. if (!(flags & (ReactiveFlags.RecursedCheck | ReactiveFlags.Recursed | ReactiveFlags.Dirty | ReactiveFlags.Pending))) {
  82. sub.flags = flags | ReactiveFlags.Pending;
  83. } else if (!(flags & (ReactiveFlags.RecursedCheck | ReactiveFlags.Recursed))) {
  84. flags = ReactiveFlags.None;
  85. } else if (!(flags & ReactiveFlags.RecursedCheck)) {
  86. sub.flags = (flags & ~ReactiveFlags.Recursed) | ReactiveFlags.Pending;
  87. } else if (!(flags & (ReactiveFlags.Dirty | ReactiveFlags.Pending)) && isValidLink(link, sub)) {
  88. sub.flags = flags | ReactiveFlags.Recursed | ReactiveFlags.Pending;
  89. flags &= ReactiveFlags.Mutable;
  90. } else {
  91. flags = ReactiveFlags.None;
  92. }
  93. if (flags & ReactiveFlags.Watching) {
  94. notify(sub);
  95. }
  96. if (flags & ReactiveFlags.Mutable) {
  97. const subSubs = sub.subs;
  98. if (subSubs !== undefined) {
  99. propagate(subSubs);
  100. }
  101. }
  102. link = link.nextSub!;
  103. } while (link !== undefined);
  104. }
  105. ```
  106. #### `checkDirty`
  107. ```ts
  108. function checkDirty(link: Link, sub: ReactiveNode): boolean {
  109. do {
  110. const dep = link.dep;
  111. const depFlags = dep.flags;
  112. if (sub.flags & ReactiveFlags.Dirty) {
  113. return true;
  114. } else if ((depFlags & (ReactiveFlags.Mutable | ReactiveFlags.Dirty)) === (ReactiveFlags.Mutable | ReactiveFlags.Dirty)) {
  115. if (update(dep)) {
  116. const subs = dep.subs!;
  117. if (subs.nextSub !== undefined) {
  118. shallowPropagate(subs);
  119. }
  120. return true;
  121. }
  122. } else if ((depFlags & (ReactiveFlags.Mutable | ReactiveFlags.Pending)) === (ReactiveFlags.Mutable | ReactiveFlags.Pending)) {
  123. if (checkDirty(dep.deps!, dep)) {
  124. if (update(dep)) {
  125. const subs = dep.subs!;
  126. if (subs.nextSub !== undefined) {
  127. shallowPropagate(subs);
  128. }
  129. return true;
  130. }
  131. } else {
  132. dep.flags = depFlags & ~ReactiveFlags.Pending;
  133. }
  134. }
  135. link = link.nextDep!;
  136. } while (link !== undefined);
  137. return false;
  138. }
  139. ```