@whitesev/utils 2.10.0 → 2.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.
@@ -242,7 +242,7 @@ System.register('Utils', [], (function (exports) {
242
242
  const setInterval$1 = (...args) => loadOrReturnBroker().setInterval(...args);
243
243
  const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
244
244
 
245
- const version = "2.10.0";
245
+ const version = "2.11.0";
246
246
 
247
247
  /* eslint-disable */
248
248
  // ==UserScript==
@@ -5339,7 +5339,6 @@ System.register('Utils', [], (function (exports) {
5339
5339
  }
5340
5340
  }
5341
5341
 
5342
- // @ts-nocheck
5343
5342
  const VueUtils = {
5344
5343
  /** 标签 */
5345
5344
  ReactiveFlags: {
@@ -5379,9 +5378,11 @@ System.register('Utils', [], (function (exports) {
5379
5378
  active = true;
5380
5379
  fn;
5381
5380
  scheduler;
5382
- constructor(fn, scheduler) {
5381
+ options;
5382
+ constructor(fn, scheduler, options) {
5383
5383
  this.fn = fn;
5384
5384
  this.scheduler = scheduler;
5385
+ this.options = options; // 默认值为'same'
5385
5386
  }
5386
5387
  run(cb) {
5387
5388
  if (!this.active) {
@@ -5399,6 +5400,18 @@ System.register('Utils', [], (function (exports) {
5399
5400
  }
5400
5401
  }
5401
5402
  }
5403
+ stop() {
5404
+ if (this.active) {
5405
+ // 清除依赖关系
5406
+ if (this.deps && this.deps.length) {
5407
+ this.deps.forEach((dep) => {
5408
+ dep.delete(this);
5409
+ });
5410
+ this.deps.length = 0;
5411
+ }
5412
+ this.active = false;
5413
+ }
5414
+ }
5402
5415
  }
5403
5416
  class RefImpl {
5404
5417
  _value;
@@ -5468,9 +5481,7 @@ System.register('Utils', [], (function (exports) {
5468
5481
  set(target, key, value, receiver) {
5469
5482
  const oldValue = target[key];
5470
5483
  const result = Reflect.set(target, key, value, receiver);
5471
- if (oldValue !== value) {
5472
- that.trigger(target, "set", key, oldValue, value);
5473
- }
5484
+ that.trigger(target, "set", key, oldValue, value);
5474
5485
  return result;
5475
5486
  },
5476
5487
  });
@@ -5481,8 +5492,9 @@ System.register('Utils', [], (function (exports) {
5481
5492
  * 观察被reactive的对象值改变
5482
5493
  * @param source 被观察的对象,这里采用函数返回对象
5483
5494
  * @param changeCallBack 值改变的回调
5495
+ * @param options 配置项
5484
5496
  */
5485
- watch(source, changeCallBack) {
5497
+ watch(source, changeCallBack, options) {
5486
5498
  let getter;
5487
5499
  if (VueUtils.isReactive(source)) {
5488
5500
  getter = () => this.traversal(source);
@@ -5494,17 +5506,35 @@ System.register('Utils', [], (function (exports) {
5494
5506
  return;
5495
5507
  }
5496
5508
  let oldValue;
5509
+ const unwatch = () => {
5510
+ effect.stop();
5511
+ };
5497
5512
  const job = () => {
5498
5513
  const newValue = effect.run((activeEffect) => {
5499
5514
  this.activeEffect = activeEffect;
5500
5515
  });
5501
5516
  changeCallBack(newValue, oldValue);
5517
+ if (options?.once) {
5518
+ // 仅触发一次
5519
+ unwatch();
5520
+ }
5502
5521
  oldValue = newValue;
5503
5522
  };
5504
- const effect = new ReactiveEffect(getter, job);
5523
+ const effect = new ReactiveEffect(getter, job, {
5524
+ triggerMethod: "not-same",
5525
+ ...(options ?? {}),
5526
+ });
5505
5527
  oldValue = effect.run((activeEffect) => {
5506
5528
  this.activeEffect = activeEffect;
5507
5529
  });
5530
+ if (options) {
5531
+ if (options.immediate) {
5532
+ job();
5533
+ }
5534
+ }
5535
+ return {
5536
+ unwatch,
5537
+ };
5508
5538
  }
5509
5539
  toReactive(value) {
5510
5540
  return VueUtils.isObject(value) ? this.reactive(value) : value;
@@ -5522,28 +5552,40 @@ System.register('Utils', [], (function (exports) {
5522
5552
  }
5523
5553
  return result;
5524
5554
  }
5525
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
5526
5555
  trigger(target, type, key, oldValue, value) {
5527
5556
  const depsMap = this.targetMap.get(target);
5528
5557
  if (!depsMap)
5529
5558
  return;
5530
5559
  const effects = depsMap.get(key);
5531
- this.triggerEffect(effects, "effects");
5560
+ this.triggerEffect(effects, type, "effects", oldValue, value);
5532
5561
  }
5533
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
5534
- triggerEffect(effects, name) {
5562
+ triggerEffect(effects, _type, _name, oldValue, value) {
5535
5563
  if (effects) {
5564
+ const isSame = oldValue === value;
5536
5565
  effects.forEach((effect) => {
5537
- if (effect.scheduler) {
5538
- effect.scheduler();
5566
+ if (effect.options.triggerMethod === "not-same") {
5567
+ if (isSame) {
5568
+ return;
5569
+ }
5570
+ if (effect.scheduler) {
5571
+ effect.scheduler();
5572
+ }
5573
+ else {
5574
+ effect.run();
5575
+ }
5539
5576
  }
5540
- else {
5541
- effect.run();
5577
+ else if (effect.options.triggerMethod === "set") {
5578
+ if (effect.scheduler) {
5579
+ effect.scheduler();
5580
+ }
5581
+ else {
5582
+ effect.run();
5583
+ }
5542
5584
  }
5543
5585
  });
5544
5586
  }
5545
5587
  }
5546
- track(target, type, key) {
5588
+ track(target, _type, key) {
5547
5589
  if (!this.activeEffect)
5548
5590
  return;
5549
5591
  let depsMap = this.targetMap.get(target);
@@ -8225,14 +8267,39 @@ System.register('Utils', [], (function (exports) {
8225
8267
  /**
8226
8268
  * 自定义的动态响应对象
8227
8269
  * @example
8228
- * let vue = new Utils.Vue();
8229
- * let reactive = new vue.reactive({});
8230
- * vue.watch(()=>reactive["name"], (newValue, oldValue)=>{
8270
+ * const vue = new Utils.Vue();
8271
+ * const reactive = vue.reactive({
8272
+ * name: "",
8273
+ * });
8274
+ * vue.watch(()=>reactive.name, (newValue, oldValue)=>{
8275
+ * console.log("newValue ==> " + newValue);
8276
+ * console.log("oldValue ==> " + oldValue);
8277
+ * })
8278
+ * reactive.name = "测试";
8279
+ * > newValue ==> 测试
8280
+ * > oldValue ==>
8281
+ * reactive.name = "null";
8282
+ * > newValue ==> null
8283
+ * > oldValue ==> 测试
8284
+ * reactive.name = "null";
8285
+ * @example
8286
+ * const vue = new Utils.Vue();
8287
+ * const reactive = vue.reactive({
8288
+ * name: "",
8289
+ * });
8290
+ * vue.watch(()=>reactive.name, (newValue, oldValue)=>{
8231
8291
  * console.log("newValue ==> " + newValue);
8232
8292
  * console.log("oldValue ==> " + oldValue);
8293
+ * },{
8294
+ * triggerMethod: "set",
8233
8295
  * })
8234
- * vue["name"] = "测试";
8235
- * > "测试"
8296
+ * reactive.name = "测试";
8297
+ * > newValue ==> 测试
8298
+ * > oldValue ==>
8299
+ * reactive.name = "测试";
8300
+ * > newValue ==> 测试
8301
+ * > oldValue ==> 测试
8302
+ *
8236
8303
  */
8237
8304
  Vue = Vue;
8238
8305
  ModuleRaid = ModuleRaid;