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