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