@whitesev/utils 2.2.9 → 2.3.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.amd.js +244 -4
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +244 -4
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +244 -4
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +244 -4
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +244 -4
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +244 -4
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/Httpx.d.ts +1273 -1274
- package/dist/types/src/Log.d.ts +96 -97
- package/dist/types/src/Utils.d.ts +1752 -1747
- package/dist/types/src/Vue.d.ts +43 -0
- package/dist/types/src/VueObject.d.ts +110 -110
- package/package.json +2 -1
- package/src/Httpx.ts +3 -3
- package/src/Log.ts +1 -2
- package/src/Utils.ts +26 -25
- package/src/Vue.ts +241 -0
- package/src/VueObject.ts +29 -29
package/dist/index.cjs.js
CHANGED
|
@@ -3647,6 +3647,245 @@ class WindowApi {
|
|
|
3647
3647
|
}
|
|
3648
3648
|
}
|
|
3649
3649
|
|
|
3650
|
+
const VueUtils = {
|
|
3651
|
+
/** 标签 */
|
|
3652
|
+
ReactiveFlags: {
|
|
3653
|
+
IS_REACTIVE: Symbol("isReactive"),
|
|
3654
|
+
},
|
|
3655
|
+
/**
|
|
3656
|
+
* 判断是否是对象
|
|
3657
|
+
* @param value
|
|
3658
|
+
*/
|
|
3659
|
+
isObject(value) {
|
|
3660
|
+
return typeof value === "object" && value !== null;
|
|
3661
|
+
},
|
|
3662
|
+
/**
|
|
3663
|
+
* 判断是否是函数
|
|
3664
|
+
* @param val
|
|
3665
|
+
*/
|
|
3666
|
+
isFunction(val) {
|
|
3667
|
+
return typeof val === "function";
|
|
3668
|
+
},
|
|
3669
|
+
/**
|
|
3670
|
+
* 处理对象再次代理,可以直接返回
|
|
3671
|
+
* @param value
|
|
3672
|
+
*/
|
|
3673
|
+
isReactive(value) {
|
|
3674
|
+
return !!(value && value[VueUtils.ReactiveFlags.IS_REACTIVE]);
|
|
3675
|
+
},
|
|
3676
|
+
/**
|
|
3677
|
+
* 判断是否是数组
|
|
3678
|
+
* @param value
|
|
3679
|
+
*/
|
|
3680
|
+
isArray(value) {
|
|
3681
|
+
return Array.isArray(value);
|
|
3682
|
+
},
|
|
3683
|
+
};
|
|
3684
|
+
class ReactiveEffect {
|
|
3685
|
+
deps = [];
|
|
3686
|
+
active = true;
|
|
3687
|
+
fn;
|
|
3688
|
+
// @ts-ignore
|
|
3689
|
+
scheduler;
|
|
3690
|
+
constructor(fn, scheduler) {
|
|
3691
|
+
this.fn = fn;
|
|
3692
|
+
this.scheduler = scheduler;
|
|
3693
|
+
}
|
|
3694
|
+
run(cb) {
|
|
3695
|
+
if (!this.active) {
|
|
3696
|
+
this.fn();
|
|
3697
|
+
}
|
|
3698
|
+
try {
|
|
3699
|
+
if (typeof cb === "function") {
|
|
3700
|
+
cb(this);
|
|
3701
|
+
}
|
|
3702
|
+
return this.fn();
|
|
3703
|
+
}
|
|
3704
|
+
finally {
|
|
3705
|
+
if (typeof cb === "function") {
|
|
3706
|
+
cb(undefined);
|
|
3707
|
+
}
|
|
3708
|
+
}
|
|
3709
|
+
}
|
|
3710
|
+
}
|
|
3711
|
+
class RefImpl {
|
|
3712
|
+
_value;
|
|
3713
|
+
_isRef = true;
|
|
3714
|
+
_rawValue;
|
|
3715
|
+
_vue;
|
|
3716
|
+
constructor(vueIns, rawValue) {
|
|
3717
|
+
this._vue = vueIns;
|
|
3718
|
+
this._rawValue = rawValue;
|
|
3719
|
+
this._value = this._vue.toReactive(rawValue);
|
|
3720
|
+
}
|
|
3721
|
+
get value() {
|
|
3722
|
+
return this._value;
|
|
3723
|
+
}
|
|
3724
|
+
set value(newValue) {
|
|
3725
|
+
if (newValue !== this._rawValue) {
|
|
3726
|
+
this._value = this._vue.toReactive(newValue);
|
|
3727
|
+
this._rawValue = newValue;
|
|
3728
|
+
}
|
|
3729
|
+
}
|
|
3730
|
+
}
|
|
3731
|
+
class ObjectRefImpl {
|
|
3732
|
+
object;
|
|
3733
|
+
key;
|
|
3734
|
+
constructor(object, key) {
|
|
3735
|
+
this.object = object;
|
|
3736
|
+
this.key = key;
|
|
3737
|
+
}
|
|
3738
|
+
get value() {
|
|
3739
|
+
return this.object[this.key];
|
|
3740
|
+
}
|
|
3741
|
+
set value(newValue) {
|
|
3742
|
+
this.object[this.key] = newValue;
|
|
3743
|
+
}
|
|
3744
|
+
}
|
|
3745
|
+
class Vue {
|
|
3746
|
+
reactMap = new WeakMap();
|
|
3747
|
+
targetMap = new WeakMap();
|
|
3748
|
+
activeEffect = undefined;
|
|
3749
|
+
constructor() {
|
|
3750
|
+
// 将数据转化成响应式的数据,只能做对象的代理
|
|
3751
|
+
}
|
|
3752
|
+
/**
|
|
3753
|
+
* 生成一个被代理的对象
|
|
3754
|
+
* @param target 需要代理的对象
|
|
3755
|
+
*/
|
|
3756
|
+
reactive(target) {
|
|
3757
|
+
const that = this;
|
|
3758
|
+
if (!(typeof target === "object" && target !== null)) {
|
|
3759
|
+
// @ts-ignore
|
|
3760
|
+
return;
|
|
3761
|
+
}
|
|
3762
|
+
if (VueUtils.isReactive(target)) {
|
|
3763
|
+
return target;
|
|
3764
|
+
}
|
|
3765
|
+
let exisProxy = this.reactMap.get(target);
|
|
3766
|
+
if (exisProxy) {
|
|
3767
|
+
return exisProxy;
|
|
3768
|
+
}
|
|
3769
|
+
const proxy = new Proxy(target, {
|
|
3770
|
+
get(target, key, receiver) {
|
|
3771
|
+
if (key === VueUtils.ReactiveFlags.IS_REACTIVE) {
|
|
3772
|
+
return true;
|
|
3773
|
+
}
|
|
3774
|
+
that.track(target, "get", key);
|
|
3775
|
+
return Reflect.get(target, key, receiver);
|
|
3776
|
+
},
|
|
3777
|
+
set(target, key, value, receiver) {
|
|
3778
|
+
let oldValue = target[key];
|
|
3779
|
+
let result = Reflect.set(target, key, value, receiver);
|
|
3780
|
+
if (oldValue !== value) {
|
|
3781
|
+
that.trigger(target, "set", key, oldValue, value);
|
|
3782
|
+
}
|
|
3783
|
+
return result;
|
|
3784
|
+
},
|
|
3785
|
+
});
|
|
3786
|
+
that.reactMap.set(target, proxy);
|
|
3787
|
+
return proxy;
|
|
3788
|
+
}
|
|
3789
|
+
/**
|
|
3790
|
+
* 观察被reactive的对象值改变
|
|
3791
|
+
* @param source 被观察的对象,这里采用函数返回对象
|
|
3792
|
+
* @param changeCallBack 值改变的回调
|
|
3793
|
+
*/
|
|
3794
|
+
watch(source, changeCallBack) {
|
|
3795
|
+
let getter;
|
|
3796
|
+
if (VueUtils.isReactive(source)) {
|
|
3797
|
+
getter = () => this.traversal(source);
|
|
3798
|
+
}
|
|
3799
|
+
else if (VueUtils.isFunction(source)) {
|
|
3800
|
+
getter = source;
|
|
3801
|
+
}
|
|
3802
|
+
else {
|
|
3803
|
+
return;
|
|
3804
|
+
}
|
|
3805
|
+
let oldValue;
|
|
3806
|
+
const job = () => {
|
|
3807
|
+
const newValue = effect.run((activeEffect) => {
|
|
3808
|
+
this.activeEffect = activeEffect;
|
|
3809
|
+
});
|
|
3810
|
+
changeCallBack(newValue, oldValue);
|
|
3811
|
+
oldValue = newValue;
|
|
3812
|
+
};
|
|
3813
|
+
const effect = new ReactiveEffect(getter, job);
|
|
3814
|
+
oldValue = effect.run((activeEffect) => {
|
|
3815
|
+
this.activeEffect = activeEffect;
|
|
3816
|
+
});
|
|
3817
|
+
}
|
|
3818
|
+
toReactive(value) {
|
|
3819
|
+
return VueUtils.isObject(value) ? this.reactive(value) : value;
|
|
3820
|
+
}
|
|
3821
|
+
ref(value) {
|
|
3822
|
+
return new RefImpl(this, value);
|
|
3823
|
+
}
|
|
3824
|
+
toRef(object, key) {
|
|
3825
|
+
return new ObjectRefImpl(object, key);
|
|
3826
|
+
}
|
|
3827
|
+
toRefs(object) {
|
|
3828
|
+
const result = VueUtils.isArray(object) ? new Array(object.length) : {};
|
|
3829
|
+
for (let key in object) {
|
|
3830
|
+
// @ts-ignore
|
|
3831
|
+
result[key] = this.toRef(object, key);
|
|
3832
|
+
}
|
|
3833
|
+
return result;
|
|
3834
|
+
}
|
|
3835
|
+
trigger(target, type, key, oldValue, value) {
|
|
3836
|
+
const depsMap = this.targetMap.get(target);
|
|
3837
|
+
if (!depsMap)
|
|
3838
|
+
return;
|
|
3839
|
+
const effects = depsMap.get(key);
|
|
3840
|
+
this.triggerEffect(effects, "effects");
|
|
3841
|
+
}
|
|
3842
|
+
triggerEffect(effects, name) {
|
|
3843
|
+
effects &&
|
|
3844
|
+
effects.forEach((effect) => {
|
|
3845
|
+
if (effect.scheduler) {
|
|
3846
|
+
effect.scheduler();
|
|
3847
|
+
}
|
|
3848
|
+
else {
|
|
3849
|
+
effect.run();
|
|
3850
|
+
}
|
|
3851
|
+
});
|
|
3852
|
+
}
|
|
3853
|
+
track(target, type, key) {
|
|
3854
|
+
if (!this.activeEffect)
|
|
3855
|
+
return;
|
|
3856
|
+
let depsMap = this.targetMap.get(target);
|
|
3857
|
+
if (!depsMap) {
|
|
3858
|
+
this.targetMap.set(target, (depsMap = new Map()));
|
|
3859
|
+
}
|
|
3860
|
+
let dep = depsMap.get(key);
|
|
3861
|
+
if (!dep) {
|
|
3862
|
+
depsMap.set(key, (dep = new Set()));
|
|
3863
|
+
}
|
|
3864
|
+
this.trackEffect(dep);
|
|
3865
|
+
}
|
|
3866
|
+
trackEffect(dep) {
|
|
3867
|
+
if (this.activeEffect) {
|
|
3868
|
+
let shouldTrack = !dep.has(this.activeEffect);
|
|
3869
|
+
if (shouldTrack) {
|
|
3870
|
+
dep.add(this.activeEffect);
|
|
3871
|
+
this.activeEffect.deps.push(dep);
|
|
3872
|
+
}
|
|
3873
|
+
}
|
|
3874
|
+
}
|
|
3875
|
+
traversal(value, set = new Set()) {
|
|
3876
|
+
if (!VueUtils.isObject(value))
|
|
3877
|
+
return value;
|
|
3878
|
+
if (set.has(value)) {
|
|
3879
|
+
return value;
|
|
3880
|
+
}
|
|
3881
|
+
set.add(value);
|
|
3882
|
+
for (let key in value) {
|
|
3883
|
+
this.traversal(value[key], set);
|
|
3884
|
+
}
|
|
3885
|
+
return value;
|
|
3886
|
+
}
|
|
3887
|
+
}
|
|
3888
|
+
|
|
3650
3889
|
class Utils {
|
|
3651
3890
|
windowApi;
|
|
3652
3891
|
constructor(option) {
|
|
@@ -5766,7 +6005,6 @@ class Utils {
|
|
|
5766
6005
|
}
|
|
5767
6006
|
/**
|
|
5768
6007
|
* 申请剪贴板权限
|
|
5769
|
-
* @returns {Promise<boolean>}
|
|
5770
6008
|
*/
|
|
5771
6009
|
requestClipboardPermission() {
|
|
5772
6010
|
return new Promise((resolve, reject) => {
|
|
@@ -5779,9 +6017,7 @@ class Utils {
|
|
|
5779
6017
|
.then((permissionStatus) => {
|
|
5780
6018
|
resolve(true);
|
|
5781
6019
|
})
|
|
5782
|
-
.catch(
|
|
5783
|
-
/** @param {TypeError} error */
|
|
5784
|
-
(error) => {
|
|
6020
|
+
.catch((error) => {
|
|
5785
6021
|
console.error([
|
|
5786
6022
|
"申请剪贴板权限失败,尝试直接写入👉",
|
|
5787
6023
|
error.message ?? error.name ?? error.stack,
|
|
@@ -6714,6 +6950,10 @@ class Utils {
|
|
|
6714
6950
|
* Utils.generateUUID()
|
|
6715
6951
|
*/
|
|
6716
6952
|
generateUUID = GenerateUUID;
|
|
6953
|
+
/**
|
|
6954
|
+
* 自定义的动态响应对象
|
|
6955
|
+
*/
|
|
6956
|
+
Vue = Vue;
|
|
6717
6957
|
}
|
|
6718
6958
|
let utils = new Utils();
|
|
6719
6959
|
|