@signaltree/core 5.1.6 → 6.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 (84) hide show
  1. package/README.md +76 -76
  2. package/dist/enhancers/batching/{lib/batching.js → batching.js} +45 -17
  3. package/dist/enhancers/devtools/{lib/devtools.js → devtools.js} +83 -98
  4. package/dist/enhancers/effects/effects.js +66 -0
  5. package/dist/enhancers/entities/entities.js +51 -0
  6. package/dist/enhancers/memoization/{lib/memoization.js → memoization.js} +33 -23
  7. package/dist/enhancers/presets/lib/presets.js +5 -65
  8. package/dist/enhancers/serialization/{lib/serialization.js → serialization.js} +12 -18
  9. package/dist/enhancers/time-travel/{lib/time-travel.js → time-travel.js} +62 -24
  10. package/dist/enhancers/time-travel/utils.js +11 -0
  11. package/dist/index.js +8 -8
  12. package/dist/lib/async-helpers.js +6 -6
  13. package/dist/lib/presets.js +21 -0
  14. package/dist/lib/signal-tree.js +156 -496
  15. package/dist/lib/types.js +1 -1
  16. package/dist/lib/utils.js +40 -2
  17. package/package.json +1 -6
  18. package/src/enhancers/batching/batching.d.ts +11 -0
  19. package/src/enhancers/batching/index.d.ts +1 -1
  20. package/src/enhancers/devtools/{lib/devtools.d.ts → devtools.d.ts} +10 -19
  21. package/src/enhancers/devtools/devtools.types.d.ts +1 -0
  22. package/src/enhancers/devtools/index.d.ts +1 -1
  23. package/src/enhancers/effects/effects.d.ts +9 -0
  24. package/src/enhancers/effects/effects.types.d.ts +1 -0
  25. package/src/enhancers/effects/index.d.ts +1 -0
  26. package/src/enhancers/entities/entities.d.ts +11 -0
  27. package/src/enhancers/entities/entities.types.d.ts +1 -0
  28. package/src/enhancers/entities/index.d.ts +1 -1
  29. package/src/enhancers/index.d.ts +3 -3
  30. package/src/enhancers/memoization/index.d.ts +1 -1
  31. package/src/enhancers/memoization/memoization.d.ts +54 -0
  32. package/src/enhancers/memoization/memoization.types.d.ts +1 -0
  33. package/src/enhancers/presets/lib/presets.d.ts +3 -6
  34. package/src/enhancers/serialization/index.d.ts +1 -1
  35. package/src/{serialization.d.ts → enhancers/serialization/serialization.d.ts} +17 -8
  36. package/src/enhancers/test-helpers/types-equals.d.ts +2 -0
  37. package/src/enhancers/time-travel/index.d.ts +1 -1
  38. package/src/enhancers/time-travel/time-travel.d.ts +10 -0
  39. package/src/enhancers/time-travel/time-travel.types.d.ts +1 -0
  40. package/src/enhancers/time-travel/utils.d.ts +2 -0
  41. package/src/enhancers/types.d.ts +1 -74
  42. package/src/enhancers/typing/helpers-types.d.ts +2 -0
  43. package/src/index.d.ts +7 -8
  44. package/src/lib/async-helpers.d.ts +2 -2
  45. package/src/lib/dev-proxy.d.ts +3 -0
  46. package/src/lib/presets.d.ts +34 -0
  47. package/src/lib/signal-tree.d.ts +2 -7
  48. package/src/lib/types.d.ts +121 -90
  49. package/src/lib/utils.d.ts +4 -0
  50. package/dist/deep-clone.js +0 -80
  51. package/dist/enhancers/computed/lib/computed.js +0 -21
  52. package/dist/enhancers/entities/lib/entities.js +0 -66
  53. package/dist/lib/performance/diff-engine.js +0 -156
  54. package/dist/lib/performance/path-index.js +0 -156
  55. package/dist/lib/performance/update-engine.js +0 -188
  56. package/src/async-helpers.d.ts +0 -8
  57. package/src/batching.d.ts +0 -16
  58. package/src/computed.d.ts +0 -12
  59. package/src/constants.d.ts +0 -14
  60. package/src/devtools.d.ts +0 -77
  61. package/src/diff-engine.d.ts +0 -33
  62. package/src/enhancers/batching/lib/batching.d.ts +0 -16
  63. package/src/enhancers/computed/index.d.ts +0 -1
  64. package/src/enhancers/computed/lib/computed.d.ts +0 -12
  65. package/src/enhancers/entities/lib/entities.d.ts +0 -17
  66. package/src/enhancers/memoization/lib/memoization.d.ts +0 -65
  67. package/src/enhancers/presets/test-setup.d.ts +0 -3
  68. package/src/enhancers/serialization/lib/serialization.d.ts +0 -59
  69. package/src/enhancers/time-travel/lib/time-travel.d.ts +0 -36
  70. package/src/enhancers/time-travel/lib/utils.d.ts +0 -1
  71. package/src/entities.d.ts +0 -28
  72. package/src/memoization.d.ts +0 -65
  73. package/src/memory-manager.d.ts +0 -30
  74. package/src/path-index.d.ts +0 -25
  75. package/src/path-notifier.d.ts +0 -12
  76. package/src/presets.d.ts +0 -11
  77. package/src/security-validator.d.ts +0 -33
  78. package/src/signal-tree.d.ts +0 -8
  79. package/src/test-setup.d.ts +0 -3
  80. package/src/time-travel.d.ts +0 -36
  81. package/src/types.d.ts +0 -436
  82. package/src/update-engine.d.ts +0 -32
  83. package/src/utils.d.ts +0 -1
  84. /package/src/{entity-signal.d.ts → enhancers/batching/batching.types.d.ts} +0 -0
@@ -1,5 +1,5 @@
1
- import { deepClone } from '../../../deep-clone.js';
2
- import { deepEqual } from '../../../deep-equal.js';
1
+ import { snapshotState } from '../../lib/utils.js';
2
+ import { deepClone, deepEqual } from './utils.js';
3
3
 
4
4
  class TimeTravelManager {
5
5
  tree;
@@ -28,8 +28,10 @@ class TimeTravelManager {
28
28
  if (this.currentIndex < this.history.length - 1) {
29
29
  this.history = this.history.slice(0, this.currentIndex + 1);
30
30
  }
31
+ const plain = snapshotState(this.tree.state);
32
+ const cloned = typeof structuredClone !== 'undefined' ? structuredClone(plain) : JSON.parse(JSON.stringify(plain));
31
33
  const entry = {
32
- state: deepClone(state),
34
+ state: cloned,
33
35
  timestamp: Date.now(),
34
36
  action: this.actionNames[action] || action,
35
37
  ...(this.includePayload && payload !== undefined && {
@@ -99,8 +101,33 @@ class TimeTravelManager {
99
101
  }
100
102
  }
101
103
  }
102
- function withTimeTravel(config = {}) {
104
+ function timeTravel(config = {}) {
105
+ const {
106
+ enabled = true
107
+ } = config;
103
108
  return tree => {
109
+ if (!enabled) {
110
+ const noopMethods = {
111
+ undo() {},
112
+ redo() {},
113
+ canUndo() {
114
+ return false;
115
+ },
116
+ canRedo() {
117
+ return false;
118
+ },
119
+ getHistory() {
120
+ return [];
121
+ },
122
+ resetHistory() {},
123
+ jumpTo(_index) {
124
+ },
125
+ getCurrentIndex() {
126
+ return -1;
127
+ }
128
+ };
129
+ return Object.assign(tree, noopMethods);
130
+ }
104
131
  const originalTreeCall = tree.bind(tree);
105
132
  let isRestoring = false;
106
133
  const timeTravelManager = new TimeTravelManager(tree, config, state => {
@@ -137,8 +164,7 @@ function withTimeTravel(config = {}) {
137
164
  }
138
165
  }
139
166
  const afterState = originalTreeCall();
140
- const statesEqual = deepEqual(beforeState, afterState);
141
- if (!statesEqual) {
167
+ if (!deepEqual(beforeState, afterState)) {
142
168
  timeTravelManager.addEntry('update', afterState);
143
169
  }
144
170
  return result;
@@ -155,39 +181,51 @@ function withTimeTravel(config = {}) {
155
181
  }
156
182
  if ('$' in tree) {
157
183
  Object.defineProperty(enhancedTree, '$', {
158
- value: tree['$'],
184
+ value: tree.$,
159
185
  enumerable: false,
160
186
  configurable: true
161
187
  });
162
188
  }
163
- enhancedTree.undo = () => {
189
+ enhancedTree['undo'] = () => {
164
190
  timeTravelManager.undo();
165
191
  };
166
- enhancedTree.redo = () => {
192
+ enhancedTree['redo'] = () => {
167
193
  timeTravelManager.redo();
168
194
  };
169
- enhancedTree.getHistory = () => timeTravelManager.getHistory();
170
- enhancedTree.resetHistory = () => {
195
+ enhancedTree['getHistory'] = () => timeTravelManager.getHistory();
196
+ enhancedTree['resetHistory'] = () => {
171
197
  timeTravelManager.resetHistory();
172
198
  };
173
- enhancedTree.jumpTo = index => {
199
+ enhancedTree['jumpTo'] = index => {
174
200
  timeTravelManager.jumpTo(index);
175
201
  };
176
- enhancedTree.canUndo = () => timeTravelManager.canUndo();
177
- enhancedTree.canRedo = () => timeTravelManager.canRedo();
178
- enhancedTree.getCurrentIndex = () => timeTravelManager.getCurrentIndex();
179
- return Object.assign(enhancedTree, {
180
- __timeTravel: timeTravelManager
181
- });
202
+ enhancedTree['canUndo'] = () => timeTravelManager.canUndo();
203
+ enhancedTree['canRedo'] = () => timeTravelManager.canRedo();
204
+ enhancedTree['getCurrentIndex'] = () => timeTravelManager.getCurrentIndex();
205
+ enhancedTree['__timeTravel'] = timeTravelManager;
206
+ return enhancedTree;
182
207
  };
183
208
  }
184
- function enableTimeTravel(maxHistorySize) {
185
- return withTimeTravel({
186
- maxHistorySize
209
+ function enableTimeTravel() {
210
+ return timeTravel({
211
+ enabled: true
187
212
  });
188
213
  }
189
- function getTimeTravel(tree) {
190
- return tree.__timeTravel;
214
+ function timeTravelHistory(maxHistorySize) {
215
+ return timeTravel({
216
+ maxHistorySize
217
+ });
191
218
  }
219
+ Object.assign((config = {}) => timeTravel(config), {
220
+ minimal: () => timeTravel({
221
+ maxHistorySize: 20,
222
+ includePayload: false
223
+ }),
224
+ debug: () => timeTravel({
225
+ maxHistorySize: 200,
226
+ includePayload: true
227
+ }),
228
+ history: timeTravelHistory
229
+ });
192
230
 
193
- export { enableTimeTravel, getTimeTravel, withTimeTravel };
231
+ export { enableTimeTravel, timeTravel, timeTravelHistory };
@@ -0,0 +1,11 @@
1
+ function deepClone(obj) {
2
+ if (typeof structuredClone === 'function') {
3
+ return structuredClone(obj);
4
+ }
5
+ return JSON.parse(JSON.stringify(obj));
6
+ }
7
+ function deepEqual(a, b) {
8
+ return JSON.stringify(a) === JSON.stringify(b);
9
+ }
10
+
11
+ export { deepClone, deepEqual };
package/dist/index.js CHANGED
@@ -4,16 +4,16 @@ export { composeEnhancers, createLazySignalTree, isAnySignal, isNodeAccessor, to
4
4
  export { getPathNotifier } from './lib/path-notifier.js';
5
5
  export { SecurityPresets, SecurityValidator } from './lib/security/security-validator.js';
6
6
  export { createEnhancer, resolveEnhancerOrder } from './enhancers/index.js';
7
- export { flushBatchedUpdates, getBatchQueueSize, hasPendingUpdates, withBatching, withHighPerformanceBatching } from './enhancers/batching/lib/batching.js';
8
- export { cleanupMemoizationCache, clearAllCaches, getGlobalCacheStats, memoize, memoizeReference, memoizeShallow, withComputedMemoization, withDeepStateMemoization, withHighFrequencyMemoization, withHighPerformanceMemoization, withLightweightMemoization, withMemoization, withSelectorMemoization, withShallowMemoization } from './enhancers/memoization/lib/memoization.js';
9
- export { enableTimeTravel, getTimeTravel, withTimeTravel } from './enhancers/time-travel/lib/time-travel.js';
10
- export { enableEntities, withEntities, withHighPerformanceEntities } from './enhancers/entities/lib/entities.js';
11
- export { applyPersistence, applySerialization, createIndexedDBAdapter, createStorageAdapter, enableSerialization, withPersistence, withSerialization } from './enhancers/serialization/lib/serialization.js';
12
- export { enableDevTools, withDevTools, withFullDevTools, withProductionDevTools } from './enhancers/devtools/lib/devtools.js';
7
+ export { batching, flushBatchedUpdates, getBatchQueueSize, hasPendingUpdates, highPerformanceBatching } from './enhancers/batching/batching.js';
8
+ export { clearAllCaches, computedMemoization, deepStateMemoization, getGlobalCacheStats, highFrequencyMemoization, highPerformanceMemoization, lightweightMemoization, memoization, memoize, memoizeReference, memoizeShallow, selectorMemoization, shallowMemoization } from './enhancers/memoization/memoization.js';
9
+ export { enableTimeTravel, timeTravel } from './enhancers/time-travel/time-travel.js';
10
+ export { enableEntities, entities, highPerformanceEntities } from './enhancers/entities/entities.js';
11
+ export { applyPersistence, applySerialization, createIndexedDBAdapter, createStorageAdapter, enableSerialization, persistence, serialization } from './enhancers/serialization/serialization.js';
12
+ export { devTools, enableDevTools, fullDevTools, productionDevTools } from './enhancers/devtools/devtools.js';
13
13
  export { createAsyncOperation, trackAsync } from './lib/async-helpers.js';
14
- export { TREE_PRESETS, combinePresets, createDevTree, createPresetConfig, getAvailablePresets, validatePreset } from './enhancers/presets/lib/presets.js';
15
- export { computedEnhancer, createComputed } from './enhancers/computed/lib/computed.js';
14
+ export { TREE_PRESETS, combinePresets, createPresetConfig, getAvailablePresets, validatePreset } from './enhancers/presets/lib/presets.js';
16
15
  export { SIGNAL_TREE_CONSTANTS, SIGNAL_TREE_MESSAGES } from './lib/constants.js';
17
16
  export { deepEqual, deepEqual as equal } from './deep-equal.js';
18
17
  export { parsePath } from './parse-path.js';
19
18
  export { isBuiltInObject } from './is-built-in-object.js';
19
+ export { createDevTree } from './lib/presets.js';
@@ -2,11 +2,11 @@ import { signal } from '@angular/core';
2
2
 
3
3
  function createAsyncOperation(name, operation) {
4
4
  return async tree => {
5
- if (typeof tree.batchUpdate === 'function') {
5
+ if (typeof tree['batchUpdate'] === 'function') {
6
6
  const pendingPatch = {
7
7
  [`${name}_PENDING`]: true
8
8
  };
9
- tree.batchUpdate(() => pendingPatch);
9
+ tree['batchUpdate'](() => pendingPatch);
10
10
  } else if ('$' in tree) {
11
11
  try {
12
12
  tree['$'][`${name}_PENDING`]?.set?.(true);
@@ -15,12 +15,12 @@ function createAsyncOperation(name, operation) {
15
15
  }
16
16
  try {
17
17
  const result = await operation();
18
- if (typeof tree.batchUpdate === 'function') {
18
+ if (typeof tree['batchUpdate'] === 'function') {
19
19
  const resultPatch = {
20
20
  [`${name}_RESULT`]: result,
21
21
  [`${name}_PENDING`]: false
22
22
  };
23
- tree.batchUpdate(() => resultPatch);
23
+ tree['batchUpdate'](() => resultPatch);
24
24
  } else if ('$' in tree) {
25
25
  try {
26
26
  tree['$'][`${name}_RESULT`]?.set?.(result);
@@ -31,12 +31,12 @@ function createAsyncOperation(name, operation) {
31
31
  }
32
32
  return result;
33
33
  } catch (error) {
34
- if (typeof tree.batchUpdate === 'function') {
34
+ if (typeof tree['batchUpdate'] === 'function') {
35
35
  const errorPatch = {
36
36
  [`${name}_ERROR`]: error,
37
37
  [`${name}_PENDING`]: false
38
38
  };
39
- tree.batchUpdate(() => errorPatch);
39
+ tree['batchUpdate'](() => errorPatch);
40
40
  } else if ('$' in tree) {
41
41
  try {
42
42
  tree['$'][`${name}_ERROR`]?.set?.(error);
@@ -0,0 +1,21 @@
1
+ import { signalTree } from './signal-tree.js';
2
+ import { effects } from '../enhancers/effects/effects.js';
3
+ import { batching } from '../enhancers/batching/batching.js';
4
+ import { memoization } from '../enhancers/memoization/memoization.js';
5
+ import { entities } from '../enhancers/entities/entities.js';
6
+ import { timeTravel } from '../enhancers/time-travel/time-travel.js';
7
+ import { devTools } from '../enhancers/devtools/devtools.js';
8
+
9
+ function createDevTree(initialState, config = {}) {
10
+ if (arguments.length === 0) {
11
+ const enhancer = tree => tree.with(effects()).with(batching()).with(memoization()).with(entities()).with(timeTravel()).with(devTools());
12
+ return {
13
+ enhancer
14
+ };
15
+ }
16
+ const base = signalTree(initialState, config);
17
+ const enhanced = base.with(effects()).with(batching(config.batching)).with(memoization(config.memoization)).with(entities()).with(timeTravel(config.timeTravel)).with(devTools(config.devTools));
18
+ return enhanced;
19
+ }
20
+
21
+ export { createDevTree };