@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.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.1";
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)=>{
8232
8276
  * console.log("newValue ==> " + newValue);
8233
8277
  * console.log("oldValue ==> " + oldValue);
8234
8278
  * })
8235
- * vue["name"] = "测试";
8236
- * > "测试"
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)=>{
8292
+ * console.log("newValue ==> " + newValue);
8293
+ * console.log("oldValue ==> " + oldValue);
8294
+ * },{
8295
+ * triggerMethod: "set",
8296
+ * })
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;
@@ -8310,10 +8377,13 @@
8310
8377
  }
8311
8378
  /**
8312
8379
  * 判断页面中是否存在`worker-src`的CSP规则
8380
+ * @param timeout 超时时间,默认为`1500ms`
8313
8381
  */
8314
- hasWorkerCSP() {
8382
+ hasWorkerCSP(timeout = 1500) {
8315
8383
  return new Promise((resolve) => {
8316
8384
  let flag = true;
8385
+ let timeId = void 0;
8386
+ let worker = void 0;
8317
8387
  let workerBlobUrl = void 0;
8318
8388
  const workerJs = /*js*/ `
8319
8389
  (() => {
@@ -8329,11 +8399,26 @@
8329
8399
  }
8330
8400
  );
8331
8401
  })();`;
8402
+ /**
8403
+ * 返回结果
8404
+ */
8405
+ const finishCallBack = () => {
8406
+ clearTimeout(timeId);
8407
+ if (worker != null) {
8408
+ worker.terminate();
8409
+ }
8410
+ // 释放
8411
+ if (typeof workerBlobUrl === "string") {
8412
+ globalThis.URL.revokeObjectURL(workerBlobUrl);
8413
+ workerBlobUrl = void 0;
8414
+ }
8415
+ resolve(flag);
8416
+ };
8332
8417
  try {
8333
8418
  const workerScript = new Blob([workerJs], {
8334
8419
  type: "application/javascript",
8335
8420
  });
8336
- workerBlobUrl = window.URL.createObjectURL(workerScript);
8421
+ workerBlobUrl = globalThis.URL.createObjectURL(workerScript);
8337
8422
  // @ts-expect-error
8338
8423
  if (globalThis.trustedTypes && typeof globalThis.trustedTypes.createPolicy === "function") {
8339
8424
  // 使用这个后虽然不报错,但是仍会有blob错误
@@ -8345,25 +8430,27 @@
8345
8430
  });
8346
8431
  workerBlobUrl = workerPolicy.createScriptURL(workerBlobUrl);
8347
8432
  }
8348
- const worker = new Worker(workerBlobUrl);
8433
+ worker = new Worker(workerBlobUrl);
8349
8434
  worker.onmessage = (data) => {
8350
8435
  if (data.data.success) {
8351
8436
  flag = false;
8437
+ finishCallBack();
8352
8438
  }
8353
8439
  };
8354
- setTimeout(() => {
8355
- worker.terminate();
8356
- resolve(flag);
8357
- }, 500);
8440
+ timeId = setTimeout(() => {
8441
+ finishCallBack();
8442
+ }, timeout);
8358
8443
  worker.postMessage("test");
8359
8444
  }
8360
8445
  catch {
8361
8446
  flag = true;
8447
+ finishCallBack();
8362
8448
  }
8363
8449
  finally {
8364
8450
  // 释放
8365
8451
  if (typeof workerBlobUrl === "string") {
8366
8452
  globalThis.URL.revokeObjectURL(workerBlobUrl);
8453
+ workerBlobUrl = void 0;
8367
8454
  }
8368
8455
  }
8369
8456
  });