@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.cjs.js CHANGED
@@ -239,7 +239,7 @@ const clearTimeout$1 = (timerId) => loadOrReturnBroker().clearTimeout(timerId);
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.1";
243
243
 
244
244
  /* eslint-disable */
245
245
  // ==UserScript==
@@ -5336,7 +5336,6 @@ class GMMenu {
5336
5336
  }
5337
5337
  }
5338
5338
 
5339
- // @ts-nocheck
5340
5339
  const VueUtils = {
5341
5340
  /** 标签 */
5342
5341
  ReactiveFlags: {
@@ -5376,9 +5375,11 @@ class ReactiveEffect {
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 @@ class ReactiveEffect {
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 @@ class Vue {
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 @@ class Vue {
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 @@ class Vue {
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 @@ class Vue {
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 @@ class Utils {
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)=>{
8228
8272
  * console.log("newValue ==> " + newValue);
8229
8273
  * console.log("oldValue ==> " + oldValue);
8230
8274
  * })
8231
- * vue["name"] = "测试";
8232
- * > "测试"
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)=>{
8288
+ * console.log("newValue ==> " + newValue);
8289
+ * console.log("oldValue ==> " + oldValue);
8290
+ * },{
8291
+ * triggerMethod: "set",
8292
+ * })
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;
@@ -8306,10 +8373,13 @@ class Utils {
8306
8373
  }
8307
8374
  /**
8308
8375
  * 判断页面中是否存在`worker-src`的CSP规则
8376
+ * @param timeout 超时时间,默认为`1500ms`
8309
8377
  */
8310
- hasWorkerCSP() {
8378
+ hasWorkerCSP(timeout = 1500) {
8311
8379
  return new Promise((resolve) => {
8312
8380
  let flag = true;
8381
+ let timeId = void 0;
8382
+ let worker = void 0;
8313
8383
  let workerBlobUrl = void 0;
8314
8384
  const workerJs = /*js*/ `
8315
8385
  (() => {
@@ -8325,11 +8395,26 @@ class Utils {
8325
8395
  }
8326
8396
  );
8327
8397
  })();`;
8398
+ /**
8399
+ * 返回结果
8400
+ */
8401
+ const finishCallBack = () => {
8402
+ clearTimeout(timeId);
8403
+ if (worker != null) {
8404
+ worker.terminate();
8405
+ }
8406
+ // 释放
8407
+ if (typeof workerBlobUrl === "string") {
8408
+ globalThis.URL.revokeObjectURL(workerBlobUrl);
8409
+ workerBlobUrl = void 0;
8410
+ }
8411
+ resolve(flag);
8412
+ };
8328
8413
  try {
8329
8414
  const workerScript = new Blob([workerJs], {
8330
8415
  type: "application/javascript",
8331
8416
  });
8332
- workerBlobUrl = window.URL.createObjectURL(workerScript);
8417
+ workerBlobUrl = globalThis.URL.createObjectURL(workerScript);
8333
8418
  // @ts-expect-error
8334
8419
  if (globalThis.trustedTypes && typeof globalThis.trustedTypes.createPolicy === "function") {
8335
8420
  // 使用这个后虽然不报错,但是仍会有blob错误
@@ -8341,25 +8426,27 @@ class Utils {
8341
8426
  });
8342
8427
  workerBlobUrl = workerPolicy.createScriptURL(workerBlobUrl);
8343
8428
  }
8344
- const worker = new Worker(workerBlobUrl);
8429
+ worker = new Worker(workerBlobUrl);
8345
8430
  worker.onmessage = (data) => {
8346
8431
  if (data.data.success) {
8347
8432
  flag = false;
8433
+ finishCallBack();
8348
8434
  }
8349
8435
  };
8350
- setTimeout(() => {
8351
- worker.terminate();
8352
- resolve(flag);
8353
- }, 500);
8436
+ timeId = setTimeout(() => {
8437
+ finishCallBack();
8438
+ }, timeout);
8354
8439
  worker.postMessage("test");
8355
8440
  }
8356
8441
  catch {
8357
8442
  flag = true;
8443
+ finishCallBack();
8358
8444
  }
8359
8445
  finally {
8360
8446
  // 释放
8361
8447
  if (typeof workerBlobUrl === "string") {
8362
8448
  globalThis.URL.revokeObjectURL(workerBlobUrl);
8449
+ workerBlobUrl = void 0;
8363
8450
  }
8364
8451
  }
8365
8452
  });