nexa-reactivity 0.10.0 → 0.11.0

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.
package/dist/core.d.ts CHANGED
@@ -4,6 +4,7 @@ export interface NexaReactivityState {
4
4
  pendingEffects: Set<Effect>;
5
5
  targetToEffectsMap: WeakMap<object, Map<number | string | symbol, Set<Effect>>>;
6
6
  postFlushCallback: (() => void) | null;
7
+ signalDeps: Map<object, Set<Effect>>;
7
8
  }
8
9
  export declare class Effect {
9
10
  fn: () => any;
package/dist/core.js CHANGED
@@ -9,7 +9,8 @@ if (!globalObj.__NEXA_REACTIVITY_STATE__) {
9
9
  batchDepth: 0,
10
10
  pendingEffects: new Set(),
11
11
  targetToEffectsMap: new WeakMap(),
12
- postFlushCallback: null
12
+ postFlushCallback: null,
13
+ signalDeps: new Map()
13
14
  };
14
15
  }
15
16
  const state = globalObj.__NEXA_REACTIVITY_STATE__;
@@ -151,16 +152,34 @@ export function untracked(fn) {
151
152
  }
152
153
  }
153
154
  export function signal(value, label) {
155
+ let deps = null;
154
156
  const obj = {
155
157
  _nexa_label: label,
156
158
  get value() {
157
- track(obj, 'value');
159
+ if (state.activeEffect) {
160
+ if (!deps)
161
+ deps = new Set();
162
+ if (!deps.has(state.activeEffect)) {
163
+ deps.add(state.activeEffect);
164
+ state.activeEffect.deps.push(deps);
165
+ }
166
+ }
158
167
  return value;
159
168
  },
160
169
  set value(newValue) {
161
170
  if (!Object.is(value, newValue)) {
162
171
  value = newValue;
163
- trigger(obj, 'value');
172
+ if (deps) {
173
+ for (const e of deps) {
174
+ if (e !== state.activeEffect && !e.isQueued) {
175
+ e.isQueued = true;
176
+ state.pendingEffects.add(e);
177
+ }
178
+ }
179
+ if (state.batchDepth === 0) {
180
+ flushEffects();
181
+ }
182
+ }
164
183
  }
165
184
  },
166
185
  peek() {
@@ -179,16 +198,17 @@ export function signal(value, label) {
179
198
  export function computed(fn) {
180
199
  let cachedValue;
181
200
  let dirty = true;
182
- const runner = new Effect(fn, () => {
183
- if (!dirty) {
184
- dirty = true;
185
- trigger(wrapper, 'value');
186
- }
187
- });
188
- runner.priority = 100;
201
+ let deps = null;
189
202
  const wrapper = {
190
203
  get value() {
191
- track(wrapper, 'value');
204
+ if (state.activeEffect) {
205
+ if (!deps)
206
+ deps = new Set();
207
+ if (!deps.has(state.activeEffect)) {
208
+ deps.add(state.activeEffect);
209
+ state.activeEffect.deps.push(deps);
210
+ }
211
+ }
192
212
  if (dirty) {
193
213
  cachedValue = runner.run();
194
214
  dirty = false;
@@ -203,6 +223,23 @@ export function computed(fn) {
203
223
  return cachedValue;
204
224
  }
205
225
  };
226
+ const runner = new Effect(fn, () => {
227
+ if (!dirty) {
228
+ dirty = true;
229
+ if (deps) {
230
+ for (const e of deps) {
231
+ if (e !== state.activeEffect && !e.isQueued) {
232
+ e.isQueued = true;
233
+ state.pendingEffects.add(e);
234
+ }
235
+ }
236
+ if (state.batchDepth === 0) {
237
+ flushEffects();
238
+ }
239
+ }
240
+ }
241
+ });
242
+ runner.priority = 100;
206
243
  return wrapper;
207
244
  }
208
245
  export function setPostFlushCallback(cb) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nexa-reactivity",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",