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