signalium 0.3.8 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (287) hide show
  1. package/.turbo/turbo-build.log +3 -3
  2. package/CHANGELOG.md +21 -0
  3. package/build/react.js +19 -0
  4. package/build/transform.js +19 -0
  5. package/dist/cjs/config.d.ts +8 -3
  6. package/dist/cjs/config.d.ts.map +1 -1
  7. package/dist/cjs/config.js +14 -8
  8. package/dist/cjs/config.js.map +1 -1
  9. package/dist/cjs/debug.d.ts +2 -2
  10. package/dist/cjs/debug.d.ts.map +1 -1
  11. package/dist/cjs/debug.js +3 -3
  12. package/dist/cjs/debug.js.map +1 -1
  13. package/dist/cjs/hooks.d.ts +14 -42
  14. package/dist/cjs/hooks.d.ts.map +1 -1
  15. package/dist/cjs/hooks.js +19 -240
  16. package/dist/cjs/hooks.js.map +1 -1
  17. package/dist/cjs/index.d.ts +5 -3
  18. package/dist/cjs/index.d.ts.map +1 -1
  19. package/dist/cjs/index.js +18 -18
  20. package/dist/cjs/index.js.map +1 -1
  21. package/dist/cjs/internals/async.d.ts +52 -0
  22. package/dist/cjs/internals/async.d.ts.map +1 -0
  23. package/dist/cjs/internals/async.js +394 -0
  24. package/dist/cjs/internals/async.js.map +1 -0
  25. package/dist/cjs/internals/connect.d.ts +4 -0
  26. package/dist/cjs/internals/connect.d.ts.map +1 -0
  27. package/dist/cjs/internals/connect.js +37 -0
  28. package/dist/cjs/internals/connect.js.map +1 -0
  29. package/dist/cjs/internals/consumer.d.ts +6 -0
  30. package/dist/cjs/internals/consumer.d.ts.map +1 -0
  31. package/dist/cjs/internals/consumer.js +13 -0
  32. package/dist/cjs/internals/consumer.js.map +1 -0
  33. package/dist/cjs/internals/contexts.d.ts +33 -0
  34. package/dist/cjs/internals/contexts.d.ts.map +1 -0
  35. package/dist/cjs/internals/contexts.js +103 -0
  36. package/dist/cjs/internals/contexts.js.map +1 -0
  37. package/dist/cjs/internals/derived.d.ts +66 -0
  38. package/dist/cjs/internals/derived.d.ts.map +1 -0
  39. package/dist/cjs/internals/derived.js +128 -0
  40. package/dist/cjs/internals/derived.js.map +1 -0
  41. package/dist/cjs/internals/dirty.d.ts +5 -0
  42. package/dist/cjs/internals/dirty.d.ts.map +1 -0
  43. package/dist/cjs/internals/dirty.js +79 -0
  44. package/dist/cjs/internals/dirty.js.map +1 -0
  45. package/dist/cjs/internals/edge.d.ts +32 -0
  46. package/dist/cjs/internals/edge.d.ts.map +1 -0
  47. package/dist/cjs/internals/edge.js +59 -0
  48. package/dist/cjs/internals/edge.js.map +1 -0
  49. package/dist/cjs/internals/get.d.ts +10 -0
  50. package/dist/cjs/internals/get.d.ts.map +1 -0
  51. package/dist/cjs/internals/get.js +255 -0
  52. package/dist/cjs/internals/get.js.map +1 -0
  53. package/dist/cjs/internals/scheduling.d.ts +12 -0
  54. package/dist/cjs/internals/scheduling.d.ts.map +1 -0
  55. package/dist/cjs/internals/scheduling.js +117 -0
  56. package/dist/cjs/internals/scheduling.js.map +1 -0
  57. package/dist/cjs/internals/state.d.ts +18 -0
  58. package/dist/cjs/internals/state.d.ts.map +1 -0
  59. package/dist/cjs/internals/state.js +88 -0
  60. package/dist/cjs/internals/state.js.map +1 -0
  61. package/dist/cjs/internals/utils/debug-name.d.ts +2 -0
  62. package/dist/cjs/internals/utils/debug-name.d.ts.map +1 -0
  63. package/dist/cjs/internals/utils/debug-name.js +14 -0
  64. package/dist/cjs/internals/utils/debug-name.js.map +1 -0
  65. package/dist/cjs/internals/utils/equals.d.ts +3 -0
  66. package/dist/cjs/internals/utils/equals.d.ts.map +1 -0
  67. package/dist/cjs/internals/utils/equals.js +13 -0
  68. package/dist/cjs/internals/utils/equals.js.map +1 -0
  69. package/dist/cjs/internals/utils/hash.d.ts +7 -0
  70. package/dist/cjs/internals/utils/hash.d.ts.map +1 -0
  71. package/dist/cjs/internals/utils/hash.js +181 -0
  72. package/dist/cjs/internals/utils/hash.js.map +1 -0
  73. package/dist/cjs/internals/utils/stringify.d.ts +3 -0
  74. package/dist/cjs/internals/utils/stringify.d.ts.map +1 -0
  75. package/dist/cjs/{utils.js → internals/utils/stringify.js} +5 -27
  76. package/dist/cjs/internals/utils/stringify.js.map +1 -0
  77. package/dist/cjs/internals/utils/type-utils.d.ts +6 -0
  78. package/dist/cjs/internals/utils/type-utils.d.ts.map +1 -0
  79. package/dist/cjs/internals/utils/type-utils.js +22 -0
  80. package/dist/cjs/internals/utils/type-utils.js.map +1 -0
  81. package/dist/cjs/react/context.d.ts +1 -1
  82. package/dist/cjs/react/context.d.ts.map +1 -1
  83. package/dist/cjs/react/provider.d.ts +4 -3
  84. package/dist/cjs/react/provider.d.ts.map +1 -1
  85. package/dist/cjs/react/provider.js +7 -3
  86. package/dist/cjs/react/provider.js.map +1 -1
  87. package/dist/cjs/react/setup.d.ts.map +1 -1
  88. package/dist/cjs/react/setup.js +2 -1
  89. package/dist/cjs/react/setup.js.map +1 -1
  90. package/dist/cjs/react/signal-value.d.ts +5 -1
  91. package/dist/cjs/react/signal-value.d.ts.map +1 -1
  92. package/dist/cjs/react/signal-value.js +35 -45
  93. package/dist/cjs/react/signal-value.js.map +1 -1
  94. package/dist/cjs/trace.d.ts +32 -28
  95. package/dist/cjs/trace.d.ts.map +1 -1
  96. package/dist/cjs/trace.js +14 -16
  97. package/dist/cjs/trace.js.map +1 -1
  98. package/dist/cjs/transform.d.ts +6 -0
  99. package/dist/cjs/transform.d.ts.map +1 -0
  100. package/dist/cjs/transform.js +92 -0
  101. package/dist/cjs/transform.js.map +1 -0
  102. package/dist/cjs/types.d.ts +32 -40
  103. package/dist/cjs/types.d.ts.map +1 -1
  104. package/dist/esm/config.d.ts +8 -3
  105. package/dist/esm/config.d.ts.map +1 -1
  106. package/dist/esm/config.js +12 -7
  107. package/dist/esm/config.js.map +1 -1
  108. package/dist/esm/debug.d.ts +2 -2
  109. package/dist/esm/debug.d.ts.map +1 -1
  110. package/dist/esm/debug.js +2 -2
  111. package/dist/esm/debug.js.map +1 -1
  112. package/dist/esm/hooks.d.ts +14 -42
  113. package/dist/esm/hooks.d.ts.map +1 -1
  114. package/dist/esm/hooks.js +17 -226
  115. package/dist/esm/hooks.js.map +1 -1
  116. package/dist/esm/index.d.ts +5 -3
  117. package/dist/esm/index.d.ts.map +1 -1
  118. package/dist/esm/index.js +5 -3
  119. package/dist/esm/index.js.map +1 -1
  120. package/dist/esm/internals/async.d.ts +52 -0
  121. package/dist/esm/internals/async.d.ts.map +1 -0
  122. package/dist/esm/internals/async.js +387 -0
  123. package/dist/esm/internals/async.js.map +1 -0
  124. package/dist/esm/internals/connect.d.ts +4 -0
  125. package/dist/esm/internals/connect.d.ts.map +1 -0
  126. package/dist/esm/internals/connect.js +33 -0
  127. package/dist/esm/internals/connect.js.map +1 -0
  128. package/dist/esm/internals/consumer.d.ts +6 -0
  129. package/dist/esm/internals/consumer.d.ts.map +1 -0
  130. package/dist/esm/internals/consumer.js +9 -0
  131. package/dist/esm/internals/consumer.js.map +1 -0
  132. package/dist/esm/internals/contexts.d.ts +33 -0
  133. package/dist/esm/internals/contexts.d.ts.map +1 -0
  134. package/dist/esm/internals/contexts.js +92 -0
  135. package/dist/esm/internals/contexts.js.map +1 -0
  136. package/dist/esm/internals/derived.d.ts +66 -0
  137. package/dist/esm/internals/derived.d.ts.map +1 -0
  138. package/dist/esm/internals/derived.js +118 -0
  139. package/dist/esm/internals/derived.js.map +1 -0
  140. package/dist/esm/internals/dirty.d.ts +5 -0
  141. package/dist/esm/internals/dirty.d.ts.map +1 -0
  142. package/dist/esm/internals/dirty.js +75 -0
  143. package/dist/esm/internals/dirty.js.map +1 -0
  144. package/dist/esm/internals/edge.d.ts +32 -0
  145. package/dist/esm/internals/edge.d.ts.map +1 -0
  146. package/dist/esm/internals/edge.js +54 -0
  147. package/dist/esm/internals/edge.js.map +1 -0
  148. package/dist/esm/internals/get.d.ts +10 -0
  149. package/dist/esm/internals/get.d.ts.map +1 -0
  150. package/dist/esm/internals/get.js +247 -0
  151. package/dist/esm/internals/get.js.map +1 -0
  152. package/dist/esm/internals/scheduling.d.ts +12 -0
  153. package/dist/esm/internals/scheduling.d.ts.map +1 -0
  154. package/dist/esm/internals/scheduling.js +106 -0
  155. package/dist/esm/internals/scheduling.js.map +1 -0
  156. package/dist/esm/internals/state.d.ts +18 -0
  157. package/dist/esm/internals/state.d.ts.map +1 -0
  158. package/dist/esm/internals/state.js +82 -0
  159. package/dist/esm/internals/state.js.map +1 -0
  160. package/dist/esm/internals/utils/debug-name.d.ts +2 -0
  161. package/dist/esm/internals/utils/debug-name.d.ts.map +1 -0
  162. package/dist/esm/internals/utils/debug-name.js +11 -0
  163. package/dist/esm/internals/utils/debug-name.js.map +1 -0
  164. package/dist/esm/internals/utils/equals.d.ts +3 -0
  165. package/dist/esm/internals/utils/equals.d.ts.map +1 -0
  166. package/dist/esm/internals/utils/equals.js +9 -0
  167. package/dist/esm/internals/utils/equals.js.map +1 -0
  168. package/dist/esm/internals/utils/hash.d.ts +7 -0
  169. package/dist/esm/internals/utils/hash.d.ts.map +1 -0
  170. package/dist/esm/internals/utils/hash.js +174 -0
  171. package/dist/esm/internals/utils/hash.js.map +1 -0
  172. package/dist/esm/internals/utils/stringify.d.ts +3 -0
  173. package/dist/esm/internals/utils/stringify.d.ts.map +1 -0
  174. package/dist/esm/{utils.js → internals/utils/stringify.js} +4 -25
  175. package/dist/esm/internals/utils/stringify.js.map +1 -0
  176. package/dist/esm/internals/utils/type-utils.d.ts +6 -0
  177. package/dist/esm/internals/utils/type-utils.d.ts.map +1 -0
  178. package/dist/esm/internals/utils/type-utils.js +15 -0
  179. package/dist/esm/internals/utils/type-utils.js.map +1 -0
  180. package/dist/esm/react/context.d.ts +1 -1
  181. package/dist/esm/react/context.d.ts.map +1 -1
  182. package/dist/esm/react/provider.d.ts +4 -3
  183. package/dist/esm/react/provider.d.ts.map +1 -1
  184. package/dist/esm/react/provider.js +6 -2
  185. package/dist/esm/react/provider.js.map +1 -1
  186. package/dist/esm/react/setup.d.ts.map +1 -1
  187. package/dist/esm/react/setup.js +3 -2
  188. package/dist/esm/react/setup.js.map +1 -1
  189. package/dist/esm/react/signal-value.d.ts +5 -1
  190. package/dist/esm/react/signal-value.d.ts.map +1 -1
  191. package/dist/esm/react/signal-value.js +34 -45
  192. package/dist/esm/react/signal-value.js.map +1 -1
  193. package/dist/esm/trace.d.ts +32 -28
  194. package/dist/esm/trace.d.ts.map +1 -1
  195. package/dist/esm/trace.js +13 -15
  196. package/dist/esm/trace.js.map +1 -1
  197. package/dist/esm/transform.d.ts +6 -0
  198. package/dist/esm/transform.d.ts.map +1 -0
  199. package/dist/esm/transform.js +89 -0
  200. package/dist/esm/transform.js.map +1 -0
  201. package/dist/esm/types.d.ts +32 -40
  202. package/dist/esm/types.d.ts.map +1 -1
  203. package/package.json +23 -4
  204. package/src/__tests__/__snapshots__/context.test.ts.snap +2101 -0
  205. package/src/__tests__/__snapshots__/nesting.test.ts.snap +16201 -0
  206. package/src/__tests__/__snapshots__/params-and-state.test.ts.snap +1879 -0
  207. package/src/__tests__/async-task.test.ts +327 -0
  208. package/src/__tests__/context.test.ts +517 -0
  209. package/src/__tests__/nesting.test.ts +298 -0
  210. package/src/__tests__/params-and-state.test.ts +230 -0
  211. package/src/__tests__/reactive-async.test.ts +548 -0
  212. package/src/__tests__/reactive-sync.test.ts +130 -0
  213. package/src/__tests__/subscription.test.ts +510 -0
  214. package/src/__tests__/utils/async.ts +1 -1
  215. package/src/__tests__/utils/instrumented-hooks.ts +229 -124
  216. package/src/__tests__/utils/permute.ts +25 -14
  217. package/src/config.ts +19 -9
  218. package/src/debug.ts +2 -2
  219. package/src/hooks.ts +46 -380
  220. package/src/index.ts +7 -24
  221. package/src/internals/async.ts +558 -0
  222. package/src/internals/connect.ts +41 -0
  223. package/src/internals/consumer.ts +13 -0
  224. package/src/internals/contexts.ts +133 -0
  225. package/src/internals/derived.ts +208 -0
  226. package/src/internals/dirty.ts +91 -0
  227. package/src/internals/edge.ts +109 -0
  228. package/src/internals/get.ts +298 -0
  229. package/src/internals/scheduling.ts +140 -0
  230. package/src/internals/state.ts +111 -0
  231. package/src/internals/utils/debug-name.ts +14 -0
  232. package/src/internals/utils/equals.ts +12 -0
  233. package/src/internals/utils/hash.ts +221 -0
  234. package/src/{utils.ts → internals/utils/stringify.ts} +3 -29
  235. package/src/internals/utils/type-utils.ts +19 -0
  236. package/src/react/__tests__/async.test.tsx +704 -0
  237. package/src/react/__tests__/basic.test.tsx +95 -0
  238. package/src/react/__tests__/contexts.test.tsx +99 -0
  239. package/src/react/__tests__/subscriptions.test.tsx +49 -0
  240. package/src/react/__tests__/utils.tsx +40 -0
  241. package/src/react/context.ts +1 -1
  242. package/src/react/provider.tsx +12 -4
  243. package/src/react/setup.ts +3 -2
  244. package/src/react/signal-value.ts +47 -67
  245. package/src/trace.ts +43 -38
  246. package/src/transform.ts +113 -0
  247. package/src/types.ts +56 -46
  248. package/transform.js +19 -0
  249. package/vitest.workspace.ts +38 -2
  250. package/dist/cjs/scheduling.d.ts +0 -11
  251. package/dist/cjs/scheduling.d.ts.map +0 -1
  252. package/dist/cjs/scheduling.js +0 -108
  253. package/dist/cjs/scheduling.js.map +0 -1
  254. package/dist/cjs/signals.d.ts +0 -73
  255. package/dist/cjs/signals.d.ts.map +0 -1
  256. package/dist/cjs/signals.js +0 -632
  257. package/dist/cjs/signals.js.map +0 -1
  258. package/dist/cjs/utils.d.ts +0 -4
  259. package/dist/cjs/utils.d.ts.map +0 -1
  260. package/dist/cjs/utils.js.map +0 -1
  261. package/dist/esm/scheduling.d.ts +0 -11
  262. package/dist/esm/scheduling.d.ts.map +0 -1
  263. package/dist/esm/scheduling.js +0 -97
  264. package/dist/esm/scheduling.js.map +0 -1
  265. package/dist/esm/signals.d.ts +0 -73
  266. package/dist/esm/signals.d.ts.map +0 -1
  267. package/dist/esm/signals.js +0 -614
  268. package/dist/esm/signals.js.map +0 -1
  269. package/dist/esm/utils.d.ts +0 -4
  270. package/dist/esm/utils.d.ts.map +0 -1
  271. package/dist/esm/utils.js.map +0 -1
  272. package/src/__tests__/hooks/async-computed.test.ts +0 -190
  273. package/src/__tests__/hooks/async-task.test.ts +0 -334
  274. package/src/__tests__/hooks/computed.test.ts +0 -126
  275. package/src/__tests__/hooks/context.test.ts +0 -527
  276. package/src/__tests__/hooks/nesting.test.ts +0 -303
  277. package/src/__tests__/hooks/params-and-state.test.ts +0 -168
  278. package/src/__tests__/hooks/subscription.test.ts +0 -97
  279. package/src/__tests__/signals/async.test.ts +0 -416
  280. package/src/__tests__/signals/basic.test.ts +0 -399
  281. package/src/__tests__/signals/subscription.test.ts +0 -632
  282. package/src/__tests__/signals/watcher.test.ts +0 -253
  283. package/src/__tests__/utils/builders.ts +0 -22
  284. package/src/__tests__/utils/instrumented-signals.ts +0 -291
  285. package/src/react/__tests__/react.test.tsx +0 -227
  286. package/src/scheduling.ts +0 -130
  287. package/src/signals.ts +0 -824
@@ -1,303 +0,0 @@
1
- import { describe, expect, test, vi } from 'vitest';
2
- import { state } from '../../index.js';
3
- import { nextTick } from '../utils/async.js';
4
- import { permute } from '../utils/permute.js';
5
-
6
- describe('nesting', () => {
7
- permute(2, (create1, create2) => {
8
- test('simple nesting', () => {
9
- const inner = create2((a: number, b: number) => {
10
- return a + b;
11
- });
12
-
13
- const outer = create1((a: number) => {
14
- return inner(a, 2);
15
- });
16
-
17
- expect(outer).withParams(1).toHaveValueAndCounts(3, { compute: 1 });
18
- expect(outer).withParams(2).toHaveValueAndCounts(4, { compute: 2 });
19
-
20
- expect(outer).withParams(1).toHaveValueAndCounts(3, { compute: 2 });
21
- expect(outer).withParams(2).toHaveValueAndCounts(4, { compute: 2 });
22
- });
23
-
24
- test('outer state + params', () => {
25
- const val = state(1);
26
-
27
- const inner = create2((a: number, b: number) => {
28
- return a + b;
29
- });
30
-
31
- const outer = create1((a: number) => {
32
- if (a > 1) {
33
- return inner(a, 2)! + val.get();
34
- }
35
-
36
- return inner(a, 2);
37
- });
38
-
39
- expect(outer).withParams(1).toHaveValueAndCounts(3, { compute: 1 });
40
- expect(outer).withParams(2).toHaveValueAndCounts(5, { compute: 2 });
41
-
42
- val.set(2);
43
- expect(outer).withParams(1).toHaveValueAndCounts(3, { compute: 2 });
44
- expect(outer).withParams(2).toHaveValueAndCounts(6, { compute: 3 });
45
- });
46
-
47
- test('inner state + params', async () => {
48
- const val = state(1);
49
-
50
- const inner = create2((a: number, b: number) => {
51
- if (a > 1) {
52
- return a + b + val.get();
53
- }
54
-
55
- return a + b;
56
- });
57
-
58
- const outer = create1((a: number) => {
59
- return inner(a, 2);
60
- });
61
-
62
- expect(outer).withParams(1).toHaveValueAndCounts(3, { compute: 1 });
63
- expect(outer).withParams(2).toHaveValueAndCounts(5, { compute: 2 });
64
-
65
- val.set(2);
66
-
67
- expect(outer).withParams(1).toHaveValueAndCounts(3, { compute: 2 });
68
-
69
- // Wait for async with subscriptions
70
- await nextTick();
71
-
72
- expect(outer).withParams(2).toHaveValueAndCounts(6, { compute: 3 });
73
- });
74
- });
75
-
76
- permute(3, (create1, create2, create3) => {
77
- test('simple nesting', () => {
78
- const inner = create3((a: number, b: number, c: number) => {
79
- return a + b + c;
80
- });
81
-
82
- const middle = create2((a: number, b: number) => {
83
- return inner(a, b, 3);
84
- });
85
-
86
- const outer = create1((a: number) => {
87
- return middle(a, 2);
88
- });
89
-
90
- expect(outer).withParams(1).toHaveValueAndCounts(6, { compute: 1 });
91
- });
92
-
93
- test('state + params one level deep', async () => {
94
- const val = state(1);
95
-
96
- const inner = create3((a: number, b: number, c: number) => {
97
- return a + b + c;
98
- });
99
-
100
- const middle = create2((a: number, b: number) => {
101
- if (a > 1) {
102
- return inner(a, b, 3)! + val.get();
103
- }
104
-
105
- return inner(a, b, 3);
106
- });
107
-
108
- const outer = create1((a: number) => {
109
- return middle(a, 2);
110
- });
111
-
112
- expect(outer).withParams(1).toHaveValueAndCounts(6, { compute: 1 });
113
- expect(middle).toHaveCounts({ compute: 1 });
114
- expect(inner).toHaveCounts({ compute: 1 });
115
-
116
- expect(outer).withParams(2).toHaveValueAndCounts(8, { compute: 2 });
117
- expect(middle).toHaveCounts({ compute: 2 });
118
- expect(inner).toHaveCounts({ compute: 2 });
119
-
120
- val.set(2);
121
-
122
- expect(outer).withParams(1).toHaveValueAndCounts(6, { compute: 2 });
123
- expect(middle).toHaveCounts({ compute: 2 });
124
- expect(inner).toHaveCounts({ compute: 2 });
125
-
126
- // Wait for async with subscriptions
127
- await nextTick();
128
-
129
- expect(outer).withParams(2).toHaveValueAndCounts(9, { compute: 3 });
130
- expect(middle).toHaveCounts({ compute: 3 });
131
- expect(inner).toHaveCounts({ compute: 2 });
132
- });
133
-
134
- test('state + params two levels deep', async () => {
135
- const val = state(1);
136
-
137
- const inner = create3((a: number, b: number, c: number) => {
138
- if (a > 1) {
139
- return a + b + c + val.get();
140
- }
141
-
142
- return a + b + c;
143
- });
144
-
145
- const middle = create2((a: number, b: number) => {
146
- return inner(a, b, 3);
147
- });
148
-
149
- const outer = create1((a: number) => {
150
- return middle(a, 2);
151
- });
152
-
153
- expect(outer).withParams(1).toHaveValueAndCounts(6, { compute: 1 });
154
- expect(middle).toHaveCounts({ compute: 1 });
155
- expect(inner).toHaveCounts({ compute: 1 });
156
-
157
- expect(outer).withParams(2).toHaveValueAndCounts(8, { compute: 2 });
158
- expect(middle).toHaveCounts({ compute: 2 });
159
- expect(inner).toHaveCounts({ compute: 2 });
160
-
161
- val.set(2);
162
-
163
- // Wait for async with subscriptions
164
- await nextTick();
165
-
166
- // Flush all first
167
- expect(outer).withParams(1).toHaveHookValue(6);
168
- expect(outer).withParams(2).toHaveHookValue(9);
169
-
170
- // Then check counts
171
- expect(outer).toHaveCounts({ compute: 3 });
172
- expect(middle).toHaveCounts({ compute: 3 });
173
- expect(inner).toHaveCounts({ compute: 3 });
174
- });
175
-
176
- test('params + multiple children', async () => {
177
- const inner1 = create3((a: number, b: number, c: number) => {
178
- return a + b + c;
179
- });
180
-
181
- const inner2 = create2((a: number, b: number) => {
182
- return a + b + inner1(a, b, 3)!;
183
- });
184
-
185
- const outer = create1((a: number, b: number) => {
186
- return inner1(a, 2, 3)! + inner2(b, 2)!;
187
- });
188
-
189
- expect(outer).withParams(1, 2).toHaveValueAndCounts(17, { compute: 1 });
190
- expect(outer).withParams(1, 2).toHaveValueAndCounts(17, { compute: 1 });
191
- expect(inner1).toHaveCounts({ compute: 2 });
192
- expect(inner2).toHaveCounts({ compute: 1 });
193
-
194
- expect(outer).withParams(2, 2).toHaveValueAndCounts(18, { compute: 2 });
195
- expect(outer).withParams(2, 2).toHaveValueAndCounts(18, { compute: 2 });
196
- expect(inner1).toHaveCounts({ compute: 2 });
197
- expect(inner2).toHaveCounts({ compute: 1 });
198
-
199
- expect(outer).withParams(2, 3).toHaveValueAndCounts(20, { compute: 3 });
200
- expect(outer).withParams(2, 3).toHaveValueAndCounts(20, { compute: 3 });
201
- expect(inner1).toHaveCounts({ compute: 3 });
202
- expect(inner2).toHaveCounts({ compute: 2 });
203
- });
204
-
205
- test('params + state + multiple children', async () => {
206
- const val = state(1);
207
-
208
- const inner1 = create3((a: number, b: number, c: number) => {
209
- return a + b + c;
210
- });
211
-
212
- const inner2 = create2((a: number, b: number) => {
213
- if (a > 1) {
214
- return a + b + inner1(a, b, 3)! + val.get();
215
- }
216
-
217
- return a + b + inner1(a, b, 3)!;
218
- });
219
-
220
- const outer = create1((a: number, b: number) => {
221
- return inner1(a, 2, 3)! + inner2(b, 2)!;
222
- });
223
-
224
- expect(outer).withParams(1, 2).toHaveValueAndCounts(18, { compute: 1 });
225
- expect(inner1).toHaveCounts({ compute: 2 });
226
- expect(inner2).toHaveCounts({ compute: 1 });
227
-
228
- expect(outer).withParams(2, 2).toHaveValueAndCounts(19, { compute: 2 });
229
- expect(inner1).toHaveCounts({ compute: 2 });
230
- expect(inner2).toHaveCounts({ compute: 1 });
231
-
232
- expect(outer).withParams(2, 3).toHaveValueAndCounts(21, { compute: 3 });
233
- expect(inner1).toHaveCounts({ compute: 3 });
234
- expect(inner2).toHaveCounts({ compute: 2 });
235
-
236
- expect(outer).withParams(3, 3).toHaveValueAndCounts(22, { compute: 4 });
237
- expect(inner1).toHaveCounts({ compute: 3 });
238
- expect(inner2).toHaveCounts({ compute: 2 });
239
-
240
- val.set(2);
241
-
242
- // Wait for async with subscriptions
243
- await nextTick();
244
-
245
- // Flush all first
246
- expect(outer).withParams(1, 2).toHaveHookValue(19);
247
- expect(outer).withParams(2, 2).toHaveHookValue(20);
248
- expect(outer).withParams(2, 3).toHaveHookValue(22);
249
- expect(outer).withParams(3, 3).toHaveHookValue(23);
250
-
251
- // Then check counts
252
- expect(outer).toHaveCounts({ compute: 8 });
253
- expect(inner1).toHaveCounts({ compute: 3 });
254
- expect(inner2).toHaveCounts({ compute: 4 });
255
- });
256
-
257
- test('passing state as params + multiple children', async () => {
258
- const stateA = state(1);
259
- const stateB = state(2);
260
-
261
- const inner1 = create2((a: number, s: typeof stateA) => {
262
- return a + s.get();
263
- });
264
-
265
- const inner2 = create3((b: number, s: typeof stateB) => {
266
- return b * s.get();
267
- });
268
-
269
- const outer = create1((x: number) => {
270
- if (x > 2) {
271
- return inner1(x, stateA)! + inner2(x, stateB)!;
272
- }
273
- return inner1(x, stateA);
274
- });
275
-
276
- expect(outer).withParams(1).toHaveValueAndCounts(2, { compute: 1 });
277
- expect(inner1).toHaveCounts({ compute: 1 });
278
- expect(inner2).toHaveCounts({ compute: 0 });
279
-
280
- expect(outer).withParams(3).toHaveValueAndCounts(10, { compute: 2 });
281
- expect(inner1).toHaveCounts({ compute: 2 });
282
- expect(inner2).toHaveCounts({ compute: 1 });
283
-
284
- stateA.set(2);
285
- await nextTick();
286
-
287
- expect(outer).withParams(1).toHaveHookValue(3);
288
- expect(outer).withParams(3).toHaveHookValue(11);
289
- expect(outer).toHaveCounts({ compute: 4 });
290
- expect(inner1).toHaveCounts({ compute: 4 });
291
- expect(inner2).toHaveCounts({ compute: 1 });
292
-
293
- stateB.set(3);
294
-
295
- await nextTick();
296
- expect(outer).withParams(1).toHaveHookValue(3);
297
- expect(outer).withParams(3).toHaveHookValue(14);
298
- expect(outer).toHaveCounts({ compute: 5 });
299
- expect(inner1).toHaveCounts({ compute: 4 });
300
- expect(inner2).toHaveCounts({ compute: 2 });
301
- });
302
- });
303
- });
@@ -1,168 +0,0 @@
1
- import { describe, expect, test } from 'vitest';
2
- import { state } from '../../index.js';
3
- import { permute } from '../utils/permute.js';
4
-
5
- describe('parameters and state reactivity', () => {
6
- permute(1, createHook => {
7
- test('Parameters can be passed to computed', async () => {
8
- const getC = createHook((a: number, b: number) => {
9
- return a + b;
10
- });
11
-
12
- expect(getC).withParams(1, 2).toHaveValueAndCounts(3, { compute: 1 });
13
- expect(getC).withParams(2, 2).toHaveValueAndCounts(4, { compute: 2 });
14
- });
15
-
16
- test('Computed is not recomputed when the same parameters are passed', () => {
17
- const getC = createHook((a: number, b: number) => {
18
- return a + b;
19
- });
20
-
21
- expect(getC).withParams(1, 2).toHaveValueAndCounts(3, { compute: 1 });
22
- expect(getC).withParams(1, 2).toHaveValueAndCounts(3, { compute: 1 });
23
- });
24
-
25
- test('Computed is recomputed when state changes', () => {
26
- const stateValue = state(1);
27
- const getC = createHook((a: number) => {
28
- return a + stateValue.get();
29
- });
30
-
31
- expect(getC).withParams(1).toHaveValueAndCounts(2, { compute: 1 });
32
- stateValue.set(2);
33
- expect(getC).withParams(1).toHaveValueAndCounts(3, { compute: 2 });
34
- });
35
-
36
- test('Computed can return complex objects', () => {
37
- const getC = createHook((a: number, b: number) => {
38
- return {
39
- sum: a + b,
40
- product: a * b,
41
- };
42
- });
43
-
44
- expect(getC).withParams(2, 3).toHaveValueAndCounts(
45
- {
46
- sum: 5,
47
- product: 6,
48
- },
49
- { compute: 1 },
50
- );
51
-
52
- expect(getC).withParams(2, 3).toHaveValueAndCounts(
53
- {
54
- sum: 5,
55
- product: 6,
56
- },
57
- { compute: 1 },
58
- );
59
- });
60
-
61
- test('Computed can take array arguments', () => {
62
- const getC = createHook((nums: number[]) => {
63
- return nums.reduce((a, b) => a + b, 0);
64
- });
65
-
66
- expect(getC).withParams([1, 2, 3]).toHaveValueAndCounts(6, { compute: 1 });
67
- expect(getC).withParams([1, 2, 3]).toHaveValueAndCounts(6, { compute: 1 });
68
- expect(getC).withParams([4, 5, 6]).toHaveValueAndCounts(15, { compute: 2 });
69
- });
70
-
71
- test('Computed can take object arguments', () => {
72
- const getC = createHook((obj: { x: number; y: number }) => {
73
- return obj.x + obj.y;
74
- });
75
-
76
- expect(getC).withParams({ x: 1, y: 2 }).toHaveValueAndCounts(3, { compute: 1 });
77
- expect(getC).withParams({ x: 1, y: 2 }).toHaveValueAndCounts(3, { compute: 1 });
78
- expect(getC).withParams({ x: 3, y: 4 }).toHaveValueAndCounts(7, { compute: 2 });
79
- });
80
-
81
- test('Computed memoizes based on deep equality of arguments', () => {
82
- const getC = createHook((obj: { x: number; y: number; nested: { a: number; b: number }; arr: number[] }) => {
83
- return obj.x + obj.y + obj.nested.a + obj.nested.b + obj.arr.reduce((sum, n) => sum + n, 0);
84
- });
85
-
86
- expect(getC)
87
- .withParams({ x: 1, y: 2, nested: { a: 3, b: 4 }, arr: [1, 2] })
88
- .toHaveValueAndCounts(13, { compute: 1 });
89
-
90
- expect(getC)
91
- .withParams({ x: 1, y: 2, nested: { a: 3, b: 4 }, arr: [1, 2] })
92
- .toHaveValueAndCounts(13, { compute: 1 });
93
-
94
- expect(getC)
95
- .withParams({ x: 2, y: 2, nested: { a: 3, b: 4 }, arr: [1, 2] })
96
- .toHaveValueAndCounts(14, { compute: 2 });
97
-
98
- expect(getC)
99
- .withParams({ x: 2, y: 2, nested: { a: 3, b: 5 }, arr: [1, 2] })
100
- .toHaveValueAndCounts(15, { compute: 3 });
101
-
102
- expect(getC)
103
- .withParams({ x: 2, y: 2, nested: { a: 3, b: 5 }, arr: [2, 2] })
104
- .toHaveValueAndCounts(16, { compute: 4 });
105
- });
106
-
107
- test('Computed can use custom memoization function', () => {
108
- const getC = createHook(
109
- (obj: { x: number; y: number; nested: { a: number; b: number }; arr: number[] }) => {
110
- return obj.x + obj.y + obj.nested.a + obj.nested.b + obj.arr.reduce((sum, n) => sum + n, 0);
111
- },
112
- {
113
- paramKey: obj => {
114
- return Object.entries(obj)
115
- .map(([key, value]) => `${key}:${Array.isArray(value) ? 'array' : String(value)}`)
116
- .join(',');
117
- },
118
- },
119
- );
120
-
121
- expect(getC)
122
- .withParams({ x: 1, y: 2, nested: { a: 3, b: 4 }, arr: [1, 2] })
123
- .toHaveValueAndCounts(13, { compute: 1 });
124
-
125
- expect(getC)
126
- .withParams({ x: 1, y: 2, nested: { a: 3, b: 4 }, arr: [1, 2] })
127
- .toHaveValueAndCounts(13, { compute: 1 });
128
-
129
- expect(getC)
130
- .withParams({ x: 2, y: 2, nested: { a: 3, b: 4 }, arr: [1, 2] })
131
- .toHaveValueAndCounts(14, { compute: 2 });
132
-
133
- expect(getC)
134
- .withParams({ x: 2, y: 2, nested: { a: 3, b: 5 }, arr: [1, 2] })
135
- .toHaveValueAndCounts(14, { compute: 2 });
136
-
137
- expect(getC)
138
- .withParams({ x: 2, y: 2, nested: { a: 3, b: 5 }, arr: [2, 2] })
139
- .toHaveValueAndCounts(14, { compute: 2 });
140
- });
141
-
142
- test('Computed can handle undefined arguments', () => {
143
- const getC = createHook((a?: number, b?: number) => {
144
- return (a ?? 0) + (b ?? 0);
145
- });
146
-
147
- expect(getC).withParams(undefined, 2).toHaveValueAndCounts(2, { compute: 1 });
148
- expect(getC).withParams(undefined, 2).toHaveValueAndCounts(2, { compute: 1 });
149
- expect(getC).withParams(1, undefined).toHaveValueAndCounts(1, { compute: 2 });
150
- });
151
-
152
- test('Computed can take state as argument and handle updates', () => {
153
- const stateValue = state(1);
154
- const getC = createHook((s: typeof stateValue) => {
155
- return s.get() * 2;
156
- });
157
-
158
- expect(getC).withParams(stateValue).toHaveValueAndCounts(2, { compute: 1 });
159
- expect(getC).withParams(stateValue).toHaveValueAndCounts(2, { compute: 1 });
160
-
161
- stateValue.set(2);
162
- expect(getC).withParams(stateValue).toHaveValueAndCounts(4, { compute: 2 });
163
-
164
- stateValue.set(3);
165
- expect(getC).withParams(stateValue).toHaveValueAndCounts(6, { compute: 3 });
166
- });
167
- });
168
- });
@@ -1,97 +0,0 @@
1
- import { describe, expect, test } from 'vitest';
2
- import { state } from '../../index.js';
3
- import { subscription } from '../utils/instrumented-hooks.js';
4
- import { nextTick } from '../utils/async.js';
5
-
6
- describe('subscriptions', () => {
7
- test('Subscription can set initial value', () => {
8
- const sub = subscription(({ set }) => {
9
- set(1);
10
- });
11
-
12
- expect(sub).toHaveValueAndCounts(1, { compute: 1, internalSet: 1 });
13
- });
14
-
15
- test('Subscription can update value', () => {
16
- const value = state(1);
17
- const sub = subscription(({ set }) => {
18
- set(value.get());
19
-
20
- return {
21
- update: () => {
22
- set(value.get());
23
- },
24
- };
25
- });
26
-
27
- expect(sub).toHaveValueAndCounts(1, { compute: 1, internalSet: 1 });
28
-
29
- value.set(2);
30
- expect(sub).toHaveValueAndCounts(2, { compute: 2, internalSet: 2 });
31
- });
32
-
33
- test('Subscription can set multiple times', () => {
34
- const sub = subscription(({ set }) => {
35
- set(1);
36
- set(2);
37
- set(3);
38
- });
39
-
40
- expect(sub).toHaveValueAndCounts(3, { compute: 1, internalSet: 3 });
41
- });
42
-
43
- test('Subscription can access parameters', () => {
44
- const sub = subscription(({ set }, value: number) => {
45
- set(value);
46
- });
47
-
48
- expect(sub).withParams(1).toHaveValueAndCounts(1, { compute: 1, internalSet: 1 });
49
- expect(sub).withParams(2).toHaveValueAndCounts(2, { compute: 2, internalSet: 2 });
50
- });
51
-
52
- test('Subscription is recomputed when parameters change', () => {
53
- const sub = subscription(({ set }, value: number) => {
54
- set(value);
55
-
56
- return {
57
- update: () => {
58
- set(value * 2);
59
- },
60
- };
61
- });
62
-
63
- expect(sub).withParams(1).toHaveValueAndCounts(1, { compute: 1, internalSet: 1 });
64
- expect(sub).withParams(2).toHaveValueAndCounts(2, { compute: 2, internalSet: 2 });
65
- });
66
-
67
- test('Subscription is not recomputed when same parameters are passed', () => {
68
- const sub = subscription(({ set }, value: number) => {
69
- set(value);
70
- });
71
-
72
- expect(sub).withParams(1).toHaveValueAndCounts(1, { compute: 1, internalSet: 1 });
73
- expect(sub).withParams(1).toHaveValueAndCounts(1, { compute: 1, internalSet: 1 });
74
- });
75
-
76
- test('Subscription updates automatically when state changes', async () => {
77
- const value = state(1);
78
- const sub = subscription(({ set }) => {
79
- set(value.get());
80
-
81
- return {
82
- update: () => {
83
- set(value.get());
84
- },
85
- };
86
- });
87
-
88
- expect(sub).toHaveValueAndCounts(1, { compute: 1, internalSet: 1 });
89
-
90
- value.set(2);
91
-
92
- await nextTick();
93
-
94
- expect(sub).toHaveCounts({ compute: 2, internalSet: 2 });
95
- expect(sub).toHaveHookValue(2);
96
- });
97
- });