@whitesev/utils 2.10.0 → 2.11.1

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.esm.js CHANGED
@@ -237,7 +237,7 @@ const clearTimeout$1 = (timerId) => loadOrReturnBroker().clearTimeout(timerId);
237
237
  const setInterval$1 = (...args) => loadOrReturnBroker().setInterval(...args);
238
238
  const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
239
239
 
240
- const version = "2.10.0";
240
+ const version = "2.11.1";
241
241
 
242
242
  /* eslint-disable */
243
243
  // ==UserScript==
@@ -5334,7 +5334,6 @@ class GMMenu {
5334
5334
  }
5335
5335
  }
5336
5336
 
5337
- // @ts-nocheck
5338
5337
  const VueUtils = {
5339
5338
  /** 标签 */
5340
5339
  ReactiveFlags: {
@@ -5374,9 +5373,11 @@ class ReactiveEffect {
5374
5373
  active = true;
5375
5374
  fn;
5376
5375
  scheduler;
5377
- constructor(fn, scheduler) {
5376
+ options;
5377
+ constructor(fn, scheduler, options) {
5378
5378
  this.fn = fn;
5379
5379
  this.scheduler = scheduler;
5380
+ this.options = options; // 默认值为'same'
5380
5381
  }
5381
5382
  run(cb) {
5382
5383
  if (!this.active) {
@@ -5394,6 +5395,18 @@ class ReactiveEffect {
5394
5395
  }
5395
5396
  }
5396
5397
  }
5398
+ stop() {
5399
+ if (this.active) {
5400
+ // 清除依赖关系
5401
+ if (this.deps && this.deps.length) {
5402
+ this.deps.forEach((dep) => {
5403
+ dep.delete(this);
5404
+ });
5405
+ this.deps.length = 0;
5406
+ }
5407
+ this.active = false;
5408
+ }
5409
+ }
5397
5410
  }
5398
5411
  class RefImpl {
5399
5412
  _value;
@@ -5463,9 +5476,7 @@ class Vue {
5463
5476
  set(target, key, value, receiver) {
5464
5477
  const oldValue = target[key];
5465
5478
  const result = Reflect.set(target, key, value, receiver);
5466
- if (oldValue !== value) {
5467
- that.trigger(target, "set", key, oldValue, value);
5468
- }
5479
+ that.trigger(target, "set", key, oldValue, value);
5469
5480
  return result;
5470
5481
  },
5471
5482
  });
@@ -5476,8 +5487,9 @@ class Vue {
5476
5487
  * 观察被reactive的对象值改变
5477
5488
  * @param source 被观察的对象,这里采用函数返回对象
5478
5489
  * @param changeCallBack 值改变的回调
5490
+ * @param options 配置项
5479
5491
  */
5480
- watch(source, changeCallBack) {
5492
+ watch(source, changeCallBack, options) {
5481
5493
  let getter;
5482
5494
  if (VueUtils.isReactive(source)) {
5483
5495
  getter = () => this.traversal(source);
@@ -5489,17 +5501,35 @@ class Vue {
5489
5501
  return;
5490
5502
  }
5491
5503
  let oldValue;
5504
+ const unwatch = () => {
5505
+ effect.stop();
5506
+ };
5492
5507
  const job = () => {
5493
5508
  const newValue = effect.run((activeEffect) => {
5494
5509
  this.activeEffect = activeEffect;
5495
5510
  });
5496
5511
  changeCallBack(newValue, oldValue);
5512
+ if (options?.once) {
5513
+ // 仅触发一次
5514
+ unwatch();
5515
+ }
5497
5516
  oldValue = newValue;
5498
5517
  };
5499
- const effect = new ReactiveEffect(getter, job);
5518
+ const effect = new ReactiveEffect(getter, job, {
5519
+ triggerMethod: "not-same",
5520
+ ...(options ?? {}),
5521
+ });
5500
5522
  oldValue = effect.run((activeEffect) => {
5501
5523
  this.activeEffect = activeEffect;
5502
5524
  });
5525
+ if (options) {
5526
+ if (options.immediate) {
5527
+ job();
5528
+ }
5529
+ }
5530
+ return {
5531
+ unwatch,
5532
+ };
5503
5533
  }
5504
5534
  toReactive(value) {
5505
5535
  return VueUtils.isObject(value) ? this.reactive(value) : value;
@@ -5517,28 +5547,40 @@ class Vue {
5517
5547
  }
5518
5548
  return result;
5519
5549
  }
5520
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
5521
5550
  trigger(target, type, key, oldValue, value) {
5522
5551
  const depsMap = this.targetMap.get(target);
5523
5552
  if (!depsMap)
5524
5553
  return;
5525
5554
  const effects = depsMap.get(key);
5526
- this.triggerEffect(effects, "effects");
5555
+ this.triggerEffect(effects, type, "effects", oldValue, value);
5527
5556
  }
5528
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
5529
- triggerEffect(effects, name) {
5557
+ triggerEffect(effects, _type, _name, oldValue, value) {
5530
5558
  if (effects) {
5559
+ const isSame = oldValue === value;
5531
5560
  effects.forEach((effect) => {
5532
- if (effect.scheduler) {
5533
- effect.scheduler();
5561
+ if (effect.options.triggerMethod === "not-same") {
5562
+ if (isSame) {
5563
+ return;
5564
+ }
5565
+ if (effect.scheduler) {
5566
+ effect.scheduler();
5567
+ }
5568
+ else {
5569
+ effect.run();
5570
+ }
5534
5571
  }
5535
- else {
5536
- effect.run();
5572
+ else if (effect.options.triggerMethod === "set") {
5573
+ if (effect.scheduler) {
5574
+ effect.scheduler();
5575
+ }
5576
+ else {
5577
+ effect.run();
5578
+ }
5537
5579
  }
5538
5580
  });
5539
5581
  }
5540
5582
  }
5541
- track(target, type, key) {
5583
+ track(target, _type, key) {
5542
5584
  if (!this.activeEffect)
5543
5585
  return;
5544
5586
  let depsMap = this.targetMap.get(target);
@@ -8220,14 +8262,39 @@ class Utils {
8220
8262
  /**
8221
8263
  * 自定义的动态响应对象
8222
8264
  * @example
8223
- * let vue = new Utils.Vue();
8224
- * let reactive = new vue.reactive({});
8225
- * vue.watch(()=>reactive["name"], (newValue, oldValue)=>{
8265
+ * const vue = new Utils.Vue();
8266
+ * const reactive = vue.reactive({
8267
+ * name: "",
8268
+ * });
8269
+ * vue.watch(()=>reactive.name, (newValue, oldValue)=>{
8226
8270
  * console.log("newValue ==> " + newValue);
8227
8271
  * console.log("oldValue ==> " + oldValue);
8228
8272
  * })
8229
- * vue["name"] = "测试";
8230
- * > "测试"
8273
+ * reactive.name = "测试";
8274
+ * > newValue ==> 测试
8275
+ * > oldValue ==>
8276
+ * reactive.name = "null";
8277
+ * > newValue ==> null
8278
+ * > oldValue ==> 测试
8279
+ * reactive.name = "null";
8280
+ * @example
8281
+ * const vue = new Utils.Vue();
8282
+ * const reactive = vue.reactive({
8283
+ * name: "",
8284
+ * });
8285
+ * vue.watch(()=>reactive.name, (newValue, oldValue)=>{
8286
+ * console.log("newValue ==> " + newValue);
8287
+ * console.log("oldValue ==> " + oldValue);
8288
+ * },{
8289
+ * triggerMethod: "set",
8290
+ * })
8291
+ * reactive.name = "测试";
8292
+ * > newValue ==> 测试
8293
+ * > oldValue ==>
8294
+ * reactive.name = "测试";
8295
+ * > newValue ==> 测试
8296
+ * > oldValue ==> 测试
8297
+ *
8231
8298
  */
8232
8299
  Vue = Vue;
8233
8300
  ModuleRaid = ModuleRaid;
@@ -8304,10 +8371,13 @@ class Utils {
8304
8371
  }
8305
8372
  /**
8306
8373
  * 判断页面中是否存在`worker-src`的CSP规则
8374
+ * @param timeout 超时时间,默认为`1500ms`
8307
8375
  */
8308
- hasWorkerCSP() {
8376
+ hasWorkerCSP(timeout = 1500) {
8309
8377
  return new Promise((resolve) => {
8310
8378
  let flag = true;
8379
+ let timeId = void 0;
8380
+ let worker = void 0;
8311
8381
  let workerBlobUrl = void 0;
8312
8382
  const workerJs = /*js*/ `
8313
8383
  (() => {
@@ -8323,11 +8393,26 @@ class Utils {
8323
8393
  }
8324
8394
  );
8325
8395
  })();`;
8396
+ /**
8397
+ * 返回结果
8398
+ */
8399
+ const finishCallBack = () => {
8400
+ clearTimeout(timeId);
8401
+ if (worker != null) {
8402
+ worker.terminate();
8403
+ }
8404
+ // 释放
8405
+ if (typeof workerBlobUrl === "string") {
8406
+ globalThis.URL.revokeObjectURL(workerBlobUrl);
8407
+ workerBlobUrl = void 0;
8408
+ }
8409
+ resolve(flag);
8410
+ };
8326
8411
  try {
8327
8412
  const workerScript = new Blob([workerJs], {
8328
8413
  type: "application/javascript",
8329
8414
  });
8330
- workerBlobUrl = window.URL.createObjectURL(workerScript);
8415
+ workerBlobUrl = globalThis.URL.createObjectURL(workerScript);
8331
8416
  // @ts-expect-error
8332
8417
  if (globalThis.trustedTypes && typeof globalThis.trustedTypes.createPolicy === "function") {
8333
8418
  // 使用这个后虽然不报错,但是仍会有blob错误
@@ -8339,25 +8424,27 @@ class Utils {
8339
8424
  });
8340
8425
  workerBlobUrl = workerPolicy.createScriptURL(workerBlobUrl);
8341
8426
  }
8342
- const worker = new Worker(workerBlobUrl);
8427
+ worker = new Worker(workerBlobUrl);
8343
8428
  worker.onmessage = (data) => {
8344
8429
  if (data.data.success) {
8345
8430
  flag = false;
8431
+ finishCallBack();
8346
8432
  }
8347
8433
  };
8348
- setTimeout(() => {
8349
- worker.terminate();
8350
- resolve(flag);
8351
- }, 500);
8434
+ timeId = setTimeout(() => {
8435
+ finishCallBack();
8436
+ }, timeout);
8352
8437
  worker.postMessage("test");
8353
8438
  }
8354
8439
  catch {
8355
8440
  flag = true;
8441
+ finishCallBack();
8356
8442
  }
8357
8443
  finally {
8358
8444
  // 释放
8359
8445
  if (typeof workerBlobUrl === "string") {
8360
8446
  globalThis.URL.revokeObjectURL(workerBlobUrl);
8447
+ workerBlobUrl = void 0;
8361
8448
  }
8362
8449
  }
8363
8450
  });