@lytjs/reactivity 6.0.0 → 6.4.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/index.cjs CHANGED
@@ -224,7 +224,7 @@ function notifySubscribers(subscribers, store, signalKey, newValue) {
224
224
  next = it.next();
225
225
  }
226
226
  if (store && signalKey !== void 0) {
227
- trigger(store, TriggerOpTypes.SET, signalKey);
227
+ trigger(store, TriggerOpTypes.SET, signalKey, newValue);
228
228
  }
229
229
  }
230
230
  function flushPendingNotifications() {
@@ -280,6 +280,71 @@ function readonlySignal(sig) {
280
280
  Object.defineProperty(readonlyFn, SignalSymbol, { value: true });
281
281
  return readonlyFn;
282
282
  }
283
+ var activeEffectScope;
284
+ function getActiveEffectScope() {
285
+ return activeEffectScope;
286
+ }
287
+ function effectScope(options) {
288
+ const detached = typeof options === "boolean" ? options : options?.detached;
289
+ const scope = {
290
+ active: true,
291
+ effects: [],
292
+ cleanups: [],
293
+ parent: activeEffectScope,
294
+ detached: !!detached,
295
+ run(fn) {
296
+ if (!this.active) {
297
+ return;
298
+ }
299
+ const prevScope = activeEffectScope;
300
+ activeEffectScope = this;
301
+ try {
302
+ return fn();
303
+ } finally {
304
+ activeEffectScope = prevScope;
305
+ }
306
+ },
307
+ stop() {
308
+ if (!this.active) return;
309
+ this.active = false;
310
+ for (const effect2 of this.effects) {
311
+ try {
312
+ effect2.stop();
313
+ } catch (e) {
314
+ commonError.error(`Error stopping effect in scope: ${String(e)}`);
315
+ }
316
+ }
317
+ for (const cleanup of this.cleanups) {
318
+ try {
319
+ cleanup();
320
+ } catch (e) {
321
+ commonError.error(`Error running cleanup in scope: ${String(e)}`);
322
+ }
323
+ }
324
+ if (this.parent) {
325
+ const idx = this.parent.effects.indexOf(commonAssertions.unsafeCast(this));
326
+ if (idx !== -1) {
327
+ this.parent.effects.splice(idx, 1);
328
+ }
329
+ }
330
+ this.effects.length = 0;
331
+ this.cleanups.length = 0;
332
+ this.parent = void 0;
333
+ }
334
+ };
335
+ if (!scope.detached && activeEffectScope) {
336
+ activeEffectScope.effects.push(commonAssertions.unsafeCast(scope));
337
+ }
338
+ return scope;
339
+ }
340
+ function getCurrentScope() {
341
+ return activeEffectScope;
342
+ }
343
+ function onScopeDispose(fn) {
344
+ if (activeEffectScope) {
345
+ activeEffectScope.cleanups.push(fn);
346
+ }
347
+ }
283
348
  var activeEffect;
284
349
  var _trackDepth = 0;
285
350
  var targetMap = /* @__PURE__ */ new WeakMap();
@@ -340,7 +405,7 @@ function trackEffect(dep) {
340
405
  activeEffect.deps.push(dep);
341
406
  }
342
407
  }
343
- function trigger(target, type, key, _newValue, _oldValue) {
408
+ function trigger(target, type, key, newValue, oldValue) {
344
409
  const depsMap = targetMap.get(target);
345
410
  if (!depsMap) return;
346
411
  const deps = [];
@@ -374,9 +439,9 @@ function trigger(target, type, key, _newValue, _oldValue) {
374
439
  }
375
440
  }
376
441
  }
377
- triggerEffects([...new Set(effects)]);
442
+ triggerEffects([...new Set(effects)], target, type, key, newValue, oldValue);
378
443
  }
379
- function triggerEffects(effects) {
444
+ function triggerEffects(effects, target, type, key, newValue, oldValue) {
380
445
  if (triggerDepth > commonConstants.REACTIVITY_MAX_TRIGGER_DEPTH) {
381
446
  return;
382
447
  }
@@ -384,19 +449,19 @@ function triggerEffects(effects) {
384
449
  try {
385
450
  for (const effect2 of effects) {
386
451
  if (effect2.computed) {
387
- triggerEffect(effect2);
452
+ triggerEffect(effect2, target, type, key, newValue, oldValue);
388
453
  }
389
454
  }
390
455
  for (const effect2 of effects) {
391
456
  if (!effect2.computed) {
392
- triggerEffect(effect2);
457
+ triggerEffect(effect2, target, type, key, newValue, oldValue);
393
458
  }
394
459
  }
395
460
  } finally {
396
461
  triggerDepth--;
397
462
  }
398
463
  }
399
- function triggerEffect(effect2) {
464
+ function triggerEffect(effect2, target, type, key, newValue, oldValue) {
400
465
  if (effect2 !== activeEffect || effect2.allowRecurse) {
401
466
  if (effect2.scheduler) {
402
467
  effect2.scheduler();
@@ -414,6 +479,10 @@ var ReactiveEffect = class {
414
479
  this.parent = void 0;
415
480
  // 运行前清理(onEffectCleanup 注册的)
416
481
  this._cleanups = [];
482
+ const scope = getActiveEffectScope();
483
+ if (scope && scope.active) {
484
+ scope.effects.push(this);
485
+ }
417
486
  }
418
487
  run() {
419
488
  if (!this.active) {
@@ -709,9 +778,9 @@ function createMutableHandler(isReadonly2, isShallow) {
709
778
  const result = Reflect.set(target, key, value, receiver);
710
779
  if (target === toRaw(receiver)) {
711
780
  if (!hadKey) {
712
- trigger(target, TriggerOpTypes.ADD, key);
781
+ trigger(target, TriggerOpTypes.ADD, key, value);
713
782
  } else if (commonIs.hasChanged(value, oldValue)) {
714
- trigger(target, TriggerOpTypes.SET, key);
783
+ trigger(target, TriggerOpTypes.SET, key, value, oldValue);
715
784
  }
716
785
  }
717
786
  return result;
@@ -721,10 +790,10 @@ function createMutableHandler(isReadonly2, isShallow) {
721
790
  return true;
722
791
  }
723
792
  const hadKey = commonIs.hasOwn(target, key);
724
- Reflect.get(target, key);
793
+ const oldValue = Reflect.get(target, key);
725
794
  const result = Reflect.deleteProperty(target, key);
726
795
  if (result && hadKey) {
727
- trigger(target, TriggerOpTypes.DELETE, key);
796
+ trigger(target, TriggerOpTypes.DELETE, key, void 0, oldValue);
728
797
  }
729
798
  return result;
730
799
  },
@@ -786,7 +855,7 @@ function createCollectionHandler(isReadonly2, isShallow) {
786
855
  if (!hadKey || !Object.is(toRaw(oldValue), toRaw(args[1]))) {
787
856
  const triggerKey = toTriggerKey(args[0]);
788
857
  if (triggerKey !== void 0) {
789
- trigger(target, TriggerOpTypes.SET, triggerKey, args[1]);
858
+ trigger(target, TriggerOpTypes.SET, triggerKey, args[1], oldValue);
790
859
  }
791
860
  }
792
861
  return result2;
@@ -807,7 +876,7 @@ function createCollectionHandler(isReadonly2, isShallow) {
807
876
  if (hadKey) {
808
877
  const triggerKey = toTriggerKey(args[0]);
809
878
  if (triggerKey !== void 0) {
810
- trigger(target, TriggerOpTypes.DELETE, triggerKey);
879
+ trigger(target, TriggerOpTypes.DELETE, triggerKey, void 0, void 0);
811
880
  }
812
881
  trigger(target, TriggerOpTypes.DELETE, ITERATE_KEY);
813
882
  }
@@ -816,7 +885,7 @@ function createCollectionHandler(isReadonly2, isShallow) {
816
885
  const hadItems = rawTarget.size > 0;
817
886
  const result2 = res.apply(target, args);
818
887
  if (hadItems) {
819
- trigger(target, TriggerOpTypes.CLEAR, void 0);
888
+ trigger(target, TriggerOpTypes.CLEAR, void 0, void 0, void 0);
820
889
  }
821
890
  return result2;
822
891
  }
@@ -934,10 +1003,10 @@ var RefImpl = class {
934
1003
  const useDirectValue = this.__v_isShallow;
935
1004
  newVal = useDirectValue ? newVal : toRaw(newVal);
936
1005
  if (commonIs.hasChanged(newVal, this._rawValue)) {
937
- this._rawValue;
1006
+ const oldVal = this._rawValue;
938
1007
  this._rawValue = newVal;
939
1008
  this._value = useDirectValue ? newVal : toReactive(newVal);
940
- triggerRefValue(this);
1009
+ triggerRefValue(this, newVal, oldVal);
941
1010
  }
942
1011
  }
943
1012
  };
@@ -956,10 +1025,10 @@ var ShallowRefImpl = class {
956
1025
  }
957
1026
  set value(newVal) {
958
1027
  if (commonIs.hasChanged(newVal, this._rawValue)) {
959
- this._rawValue;
1028
+ const oldVal = this._rawValue;
960
1029
  this._rawValue = newVal;
961
1030
  this._value = newVal;
962
- triggerRefValue(this);
1031
+ triggerRefValue(this, newVal, oldVal);
963
1032
  }
964
1033
  }
965
1034
  };
@@ -969,7 +1038,7 @@ function trackRefValue(ref2) {
969
1038
  }
970
1039
  }
971
1040
  function triggerRefValue(ref2, newVal, oldVal) {
972
- trigger(ref2, TriggerOpTypes.SET, "value");
1041
+ trigger(ref2, TriggerOpTypes.SET, "value", newVal, oldVal);
973
1042
  }
974
1043
  function ref(value) {
975
1044
  if (isRef(value)) {
@@ -1556,10 +1625,12 @@ exports.computed = computed2;
1556
1625
  exports.computedSignal = computedSignal;
1557
1626
  exports.customRef = customRef;
1558
1627
  exports.effect = effect;
1628
+ exports.effectScope = effectScope;
1559
1629
  exports.enableTracking = enableTracking;
1560
1630
  exports.flushBatchScopes = flushBatchScopes;
1561
1631
  exports.getBatchScopeDepth = getBatchScopeDepth;
1562
1632
  exports.getCurrentBatchScopeStack = getCurrentBatchScopeStack;
1633
+ exports.getCurrentScope = getCurrentScope;
1563
1634
  exports.getSkippedTrackingCount = getSkippedTrackingCount;
1564
1635
  exports.isComputedRef = isComputedRef;
1565
1636
  exports.isInBatchScope = isInBatchScope;
@@ -1570,6 +1641,7 @@ exports.isRef = isRef;
1570
1641
  exports.isShallowRef = isShallowRef;
1571
1642
  exports.markRaw = markRaw;
1572
1643
  exports.onEffectCleanup = onEffectCleanup;
1644
+ exports.onScopeDispose = onScopeDispose;
1573
1645
  exports.pauseTracking = pauseTracking;
1574
1646
  exports.reactive = reactive;
1575
1647
  exports.readonly = readonly;