@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.amd.js +116 -29
- package/dist/index.amd.js.map +1 -1
- package/dist/index.amd.min.js +1 -1
- package/dist/index.amd.min.js.map +1 -1
- package/dist/index.cjs.js +116 -29
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +1 -1
- package/dist/index.cjs.min.js.map +1 -1
- package/dist/index.esm.js +116 -29
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/index.iife.js +116 -29
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +1 -1
- package/dist/index.iife.min.js.map +1 -1
- package/dist/index.system.js +116 -29
- package/dist/index.system.js.map +1 -1
- package/dist/index.system.min.js +1 -1
- package/dist/index.system.min.js.map +1 -1
- package/dist/index.umd.js +116 -29
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/dist/types/src/Utils.d.ts +32 -6
- package/dist/types/src/Vue.d.ts +24 -1
- package/package.json +1 -1
- package/src/Utils.ts +57 -12
- package/src/Vue.ts +92 -21
package/dist/index.iife.js
CHANGED
|
@@ -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.
|
|
243
|
+
const version = "2.11.1";
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
5536
|
-
|
|
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.
|
|
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,
|
|
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
|
-
*
|
|
8227
|
-
*
|
|
8228
|
-
*
|
|
8268
|
+
* const vue = new Utils.Vue();
|
|
8269
|
+
* const reactive = vue.reactive({
|
|
8270
|
+
* name: "",
|
|
8271
|
+
* });
|
|
8272
|
+
* vue.watch(()=>reactive.name, (newValue, oldValue)=>{
|
|
8229
8273
|
* console.log("newValue ==> " + newValue);
|
|
8230
8274
|
* console.log("oldValue ==> " + oldValue);
|
|
8231
8275
|
* })
|
|
8232
|
-
*
|
|
8233
|
-
* >
|
|
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)=>{
|
|
8289
|
+
* console.log("newValue ==> " + newValue);
|
|
8290
|
+
* console.log("oldValue ==> " + oldValue);
|
|
8291
|
+
* },{
|
|
8292
|
+
* triggerMethod: "set",
|
|
8293
|
+
* })
|
|
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;
|
|
@@ -8307,10 +8374,13 @@ var Utils = (function () {
|
|
|
8307
8374
|
}
|
|
8308
8375
|
/**
|
|
8309
8376
|
* 判断页面中是否存在`worker-src`的CSP规则
|
|
8377
|
+
* @param timeout 超时时间,默认为`1500ms`
|
|
8310
8378
|
*/
|
|
8311
|
-
hasWorkerCSP() {
|
|
8379
|
+
hasWorkerCSP(timeout = 1500) {
|
|
8312
8380
|
return new Promise((resolve) => {
|
|
8313
8381
|
let flag = true;
|
|
8382
|
+
let timeId = void 0;
|
|
8383
|
+
let worker = void 0;
|
|
8314
8384
|
let workerBlobUrl = void 0;
|
|
8315
8385
|
const workerJs = /*js*/ `
|
|
8316
8386
|
(() => {
|
|
@@ -8326,11 +8396,26 @@ var Utils = (function () {
|
|
|
8326
8396
|
}
|
|
8327
8397
|
);
|
|
8328
8398
|
})();`;
|
|
8399
|
+
/**
|
|
8400
|
+
* 返回结果
|
|
8401
|
+
*/
|
|
8402
|
+
const finishCallBack = () => {
|
|
8403
|
+
clearTimeout(timeId);
|
|
8404
|
+
if (worker != null) {
|
|
8405
|
+
worker.terminate();
|
|
8406
|
+
}
|
|
8407
|
+
// 释放
|
|
8408
|
+
if (typeof workerBlobUrl === "string") {
|
|
8409
|
+
globalThis.URL.revokeObjectURL(workerBlobUrl);
|
|
8410
|
+
workerBlobUrl = void 0;
|
|
8411
|
+
}
|
|
8412
|
+
resolve(flag);
|
|
8413
|
+
};
|
|
8329
8414
|
try {
|
|
8330
8415
|
const workerScript = new Blob([workerJs], {
|
|
8331
8416
|
type: "application/javascript",
|
|
8332
8417
|
});
|
|
8333
|
-
workerBlobUrl =
|
|
8418
|
+
workerBlobUrl = globalThis.URL.createObjectURL(workerScript);
|
|
8334
8419
|
// @ts-expect-error
|
|
8335
8420
|
if (globalThis.trustedTypes && typeof globalThis.trustedTypes.createPolicy === "function") {
|
|
8336
8421
|
// 使用这个后虽然不报错,但是仍会有blob错误
|
|
@@ -8342,25 +8427,27 @@ var Utils = (function () {
|
|
|
8342
8427
|
});
|
|
8343
8428
|
workerBlobUrl = workerPolicy.createScriptURL(workerBlobUrl);
|
|
8344
8429
|
}
|
|
8345
|
-
|
|
8430
|
+
worker = new Worker(workerBlobUrl);
|
|
8346
8431
|
worker.onmessage = (data) => {
|
|
8347
8432
|
if (data.data.success) {
|
|
8348
8433
|
flag = false;
|
|
8434
|
+
finishCallBack();
|
|
8349
8435
|
}
|
|
8350
8436
|
};
|
|
8351
|
-
setTimeout(() => {
|
|
8352
|
-
|
|
8353
|
-
|
|
8354
|
-
}, 500);
|
|
8437
|
+
timeId = setTimeout(() => {
|
|
8438
|
+
finishCallBack();
|
|
8439
|
+
}, timeout);
|
|
8355
8440
|
worker.postMessage("test");
|
|
8356
8441
|
}
|
|
8357
8442
|
catch {
|
|
8358
8443
|
flag = true;
|
|
8444
|
+
finishCallBack();
|
|
8359
8445
|
}
|
|
8360
8446
|
finally {
|
|
8361
8447
|
// 释放
|
|
8362
8448
|
if (typeof workerBlobUrl === "string") {
|
|
8363
8449
|
globalThis.URL.revokeObjectURL(workerBlobUrl);
|
|
8450
|
+
workerBlobUrl = void 0;
|
|
8364
8451
|
}
|
|
8365
8452
|
}
|
|
8366
8453
|
});
|