aoye 0.0.52 → 0.0.54
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/aoye.cjs.js +224 -13
- package/dist/aoye.cjs.js.map +1 -1
- package/dist/aoye.esm.js +224 -14
- package/dist/aoye.esm.js.map +1 -1
- package/dist/index.d.ts +16 -2
- package/dist/index.umd.js +224 -13
- package/dist/index.umd.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -132,6 +132,10 @@ declare class Signal<T = any> {
|
|
|
132
132
|
|
|
133
133
|
declare const batchStart: () => number;
|
|
134
134
|
declare const batchEnd: () => void;
|
|
135
|
+
/** effect、computed 回调执行的唯一 id
|
|
136
|
+
* 用于判断重复依赖属于同一 effect、effect、computed
|
|
137
|
+
*/
|
|
138
|
+
declare const batchDeep: () => number;
|
|
135
139
|
declare function clean(onClean: OnClean): void;
|
|
136
140
|
|
|
137
141
|
declare const execIdInc: () => number;
|
|
@@ -202,7 +206,17 @@ declare class Store {
|
|
|
202
206
|
static Current: Store;
|
|
203
207
|
constructor();
|
|
204
208
|
parent: () => Store | null;
|
|
205
|
-
|
|
209
|
+
/**
|
|
210
|
+
* 创建子 Store 实例并传递属性:
|
|
211
|
+
* Store.new({ prop1: `v1+v2`, prop2: (p) => p.v3 * p.v4 }, { prop3: 123 })
|
|
212
|
+
*
|
|
213
|
+
* keyMap:childKey → 表达式
|
|
214
|
+
* - string:解析为 with(parentStore) { ... } 表达式,结果包装为 Computed
|
|
215
|
+
* - Function:直接包装为 Computed(() => fn(parentStore))
|
|
216
|
+
*
|
|
217
|
+
* staticMap:childKey → 静态值,直接赋值
|
|
218
|
+
*/
|
|
219
|
+
static new<T extends Store = any, P extends Store = any, O extends string = ''>(this: new (...args: any[]) => T, keyMap?: PRecord<keyof T, keyof Omit<P, O> | DeepOmitPath<P, O>> | Record<string, string | Function>, staticMap?: PRecord<keyof T, any>): T;
|
|
206
220
|
map<P extends Store = any, O extends string = ''>(keyMap?: PRecord<keyof this, keyof Omit<P, O> | DeepOmitPath<P, O>>): void;
|
|
207
221
|
}
|
|
208
222
|
|
|
@@ -229,5 +243,5 @@ declare function effectUt(callback: (...args: ValueDiff[]) => void, depOrOpt?: a
|
|
|
229
243
|
declare function effect(callback: (...args: ValueDiff[]) => void, depOrOpt?: any[] | CustomEffectOpt, opt?: CustomEffectOpt): Effect;
|
|
230
244
|
declare function scope(...args: any[]): any;
|
|
231
245
|
|
|
232
|
-
export { $, Computed, Effect, EffectStrType2Enum, IsStore, Keys, NoopEffect, ScheduleStatus, ScheduleType, Scope, Signal, Store, StoreIgnoreKeys, batchEnd, batchStart, clean, deepSignal, effect, effectUt, execId, execIdInc, getPulling, ide, macro, micro, noopEffect, now, runWithPulling, scope, setExecId, setPulling, shareSignal, toRaw };
|
|
246
|
+
export { $, Computed, Effect, EffectStrType2Enum, IsStore, Keys, NoopEffect, ScheduleStatus, ScheduleType, Scope, Signal, Store, StoreIgnoreKeys, batchDeep, batchEnd, batchStart, clean, deepSignal, effect, effectUt, execId, execIdInc, getPulling, ide, macro, micro, noopEffect, now, runWithPulling, scope, setExecId, setPulling, shareSignal, toRaw };
|
|
233
247
|
export type { CreateScope, CreateTaskProps, CustomEffectOpt, DeepOmitPath, DeepPath, DeepValue, Dispose, Key, Link, MatchValue, Mix, OnClean, OutLink, PRecord, ScheduleTypeStr, SideEffect, SignalNode, SignalType, Task, TaskControlReturn, ValueDiff };
|
package/dist/index.umd.js
CHANGED
|
@@ -486,7 +486,11 @@
|
|
|
486
486
|
}
|
|
487
487
|
up.emitTail = line;
|
|
488
488
|
if (recHead) {
|
|
489
|
-
recTail
|
|
489
|
+
if (recTail) {
|
|
490
|
+
recTail.nextRecLine = line;
|
|
491
|
+
} else {
|
|
492
|
+
down.recHead = line;
|
|
493
|
+
}
|
|
490
494
|
} else {
|
|
491
495
|
down.recHead = line;
|
|
492
496
|
}
|
|
@@ -549,7 +553,11 @@
|
|
|
549
553
|
}
|
|
550
554
|
up.emitTail = line;
|
|
551
555
|
if (recHead) {
|
|
552
|
-
recTail
|
|
556
|
+
if (recTail) {
|
|
557
|
+
recTail.nextRecLine = line;
|
|
558
|
+
} else {
|
|
559
|
+
down.recHead = line;
|
|
560
|
+
}
|
|
553
561
|
} else {
|
|
554
562
|
down.recHead = line;
|
|
555
563
|
}
|
|
@@ -752,11 +760,195 @@
|
|
|
752
760
|
State.ScopeReady | State.ScopeAbort;
|
|
753
761
|
State.ScopeAbort;
|
|
754
762
|
|
|
763
|
+
const trackIterator = (cells, scope) => {
|
|
764
|
+
let iter = cells.get(Keys.Iterator);
|
|
765
|
+
if (!iter) {
|
|
766
|
+
iter = new Signal(0);
|
|
767
|
+
iter.scope = scope;
|
|
768
|
+
cells.set(Keys.Iterator, iter);
|
|
769
|
+
}
|
|
770
|
+
iter.get();
|
|
771
|
+
};
|
|
772
|
+
const triggerIterator = cells => {
|
|
773
|
+
const iter = cells.get(Keys.Iterator);
|
|
774
|
+
if (iter) {
|
|
775
|
+
iter.set((iter.value || 0) + 1);
|
|
776
|
+
}
|
|
777
|
+
};
|
|
778
|
+
const trackKey = (cells, scope, key) => {
|
|
779
|
+
let cell = cells.get(key);
|
|
780
|
+
if (!cell) {
|
|
781
|
+
cell = new Signal(undefined);
|
|
782
|
+
cell.scope = scope;
|
|
783
|
+
cells.set(key, cell);
|
|
784
|
+
}
|
|
785
|
+
cell.get();
|
|
786
|
+
};
|
|
787
|
+
const triggerKey = (cells, key) => {
|
|
788
|
+
const cell = cells.get(key);
|
|
789
|
+
if (cell) {
|
|
790
|
+
cell.set((cell.value === undefined ? 0 : cell.value) + 1);
|
|
791
|
+
}
|
|
792
|
+
};
|
|
793
|
+
const createSharedHandler = (cells, scope, deep, targetIsMap) => {
|
|
794
|
+
const iterate = (rawTarget, iteratorFn) => {
|
|
795
|
+
trackIterator(cells, scope);
|
|
796
|
+
const rawIter = rawTarget[iteratorFn]();
|
|
797
|
+
if (!deep) return rawIter;
|
|
798
|
+
const rawNext = rawIter.next.bind(rawIter);
|
|
799
|
+
rawIter.next = () => {
|
|
800
|
+
const result = rawNext();
|
|
801
|
+
if (!result.done) {
|
|
802
|
+
if (iteratorFn === 'entries') {
|
|
803
|
+
result.value[0] = deepSignal(result.value[0], scope);
|
|
804
|
+
result.value[1] = deepSignal(result.value[1], scope);
|
|
805
|
+
} else {
|
|
806
|
+
result.value = deepSignal(result.value, scope);
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
return result;
|
|
810
|
+
};
|
|
811
|
+
return rawIter;
|
|
812
|
+
};
|
|
813
|
+
return {
|
|
814
|
+
has(key) {
|
|
815
|
+
const target = this[Keys.Raw];
|
|
816
|
+
trackKey(cells, scope, key);
|
|
817
|
+
return target.has(key);
|
|
818
|
+
},
|
|
819
|
+
delete(key) {
|
|
820
|
+
const target = this[Keys.Raw];
|
|
821
|
+
batchStart();
|
|
822
|
+
const had = target.has(key);
|
|
823
|
+
const result = target.delete(key);
|
|
824
|
+
if (had) {
|
|
825
|
+
triggerKey(cells, key);
|
|
826
|
+
cells.delete(key);
|
|
827
|
+
triggerIterator(cells);
|
|
828
|
+
}
|
|
829
|
+
batchEnd();
|
|
830
|
+
return result;
|
|
831
|
+
},
|
|
832
|
+
clear() {
|
|
833
|
+
const target = this[Keys.Raw];
|
|
834
|
+
batchStart();
|
|
835
|
+
const hadItems = target.size > 0;
|
|
836
|
+
target.clear();
|
|
837
|
+
const iterCell = cells.get(Keys.Iterator);
|
|
838
|
+
cells.clear();
|
|
839
|
+
if (iterCell) cells.set(Keys.Iterator, iterCell);
|
|
840
|
+
if (hadItems) {
|
|
841
|
+
triggerIterator(cells);
|
|
842
|
+
}
|
|
843
|
+
batchEnd();
|
|
844
|
+
},
|
|
845
|
+
forEach(callback, thisArg) {
|
|
846
|
+
const target = this[Keys.Raw];
|
|
847
|
+
trackIterator(cells, scope);
|
|
848
|
+
const wrap = val => deep ? deepSignal(val, scope) : val;
|
|
849
|
+
target.forEach((v, k) => {
|
|
850
|
+
callback.call(thisArg, wrap(v), wrap(k), this);
|
|
851
|
+
});
|
|
852
|
+
},
|
|
853
|
+
keys() {
|
|
854
|
+
return iterate(this[Keys.Raw], 'keys');
|
|
855
|
+
},
|
|
856
|
+
values() {
|
|
857
|
+
return iterate(this[Keys.Raw], 'values');
|
|
858
|
+
},
|
|
859
|
+
entries() {
|
|
860
|
+
return iterate(this[Keys.Raw], 'entries');
|
|
861
|
+
},
|
|
862
|
+
[Symbol.iterator]() {
|
|
863
|
+
return iterate(this[Keys.Raw], targetIsMap ? 'entries' : 'values');
|
|
864
|
+
}
|
|
865
|
+
};
|
|
866
|
+
};
|
|
867
|
+
const createMapHandler = (cells, scope, deep) => ({
|
|
868
|
+
get(key) {
|
|
869
|
+
const target = this[Keys.Raw];
|
|
870
|
+
trackKey(cells, scope, key);
|
|
871
|
+
const value = target.get(key);
|
|
872
|
+
return deep ? deepSignal(value, scope) : value;
|
|
873
|
+
},
|
|
874
|
+
set(key, value) {
|
|
875
|
+
const target = this[Keys.Raw];
|
|
876
|
+
batchStart();
|
|
877
|
+
target.set(key, value);
|
|
878
|
+
let cell = cells.get(key);
|
|
879
|
+
if (!cell) {
|
|
880
|
+
cell = new Signal(value);
|
|
881
|
+
cell.scope = scope;
|
|
882
|
+
cells.set(key, cell);
|
|
883
|
+
} else {
|
|
884
|
+
cell.set(value);
|
|
885
|
+
}
|
|
886
|
+
triggerIterator(cells);
|
|
887
|
+
batchEnd();
|
|
888
|
+
return this;
|
|
889
|
+
}
|
|
890
|
+
});
|
|
891
|
+
const createSetHandler = (cells, scope, _deep) => ({
|
|
892
|
+
add(value) {
|
|
893
|
+
const target = this[Keys.Raw];
|
|
894
|
+
batchStart();
|
|
895
|
+
const had = target.has(value);
|
|
896
|
+
target.add(value);
|
|
897
|
+
let cell = cells.get(value);
|
|
898
|
+
if (!cell) {
|
|
899
|
+
cell = new Signal(value);
|
|
900
|
+
cell.scope = scope;
|
|
901
|
+
cells.set(value, cell);
|
|
902
|
+
} else {
|
|
903
|
+
cell.set(value);
|
|
904
|
+
}
|
|
905
|
+
if (!had) {
|
|
906
|
+
triggerIterator(cells);
|
|
907
|
+
}
|
|
908
|
+
batchEnd();
|
|
909
|
+
return this;
|
|
910
|
+
}
|
|
911
|
+
});
|
|
912
|
+
const mergeHandlers = (...handlers) => Object.assign({}, ...handlers);
|
|
913
|
+
|
|
755
914
|
const deepSignal = (target, scope, deep = true) => {
|
|
756
915
|
const isObj = typeof target === 'object' && target !== null;
|
|
757
916
|
if (!isObj || target[Keys.Raw] || target[Keys.ProxyFreeObject]) return target;
|
|
758
917
|
const p = rawToProxy.get(target);
|
|
759
918
|
if (p) return p;
|
|
919
|
+
const targetIsMap = target instanceof Map;
|
|
920
|
+
const targetIsSet = target instanceof Set;
|
|
921
|
+
if (targetIsMap || targetIsSet) {
|
|
922
|
+
const cells = new Map();
|
|
923
|
+
const meta = {
|
|
924
|
+
deep,
|
|
925
|
+
scope,
|
|
926
|
+
cells
|
|
927
|
+
};
|
|
928
|
+
const shared = createSharedHandler(cells, scope, deep, targetIsMap);
|
|
929
|
+
const specific = targetIsMap ? createMapHandler(cells, scope, deep) : createSetHandler(cells, scope);
|
|
930
|
+
const instrumentations = mergeHandlers(shared, specific);
|
|
931
|
+
const proxy = new Proxy(target, {
|
|
932
|
+
get(_obj, prop, receiver) {
|
|
933
|
+
if (prop === Keys.Raw) return target;
|
|
934
|
+
if (prop === Keys.Meta) return meta;
|
|
935
|
+
if (prop === Symbol.toStringTag) return targetIsMap ? 'Map' : 'Set';
|
|
936
|
+
if (prop === 'size') {
|
|
937
|
+
trackIterator(cells, scope);
|
|
938
|
+
return Reflect.get(target, 'size', target);
|
|
939
|
+
}
|
|
940
|
+
if (prop in instrumentations) {
|
|
941
|
+
const fn = instrumentations[prop];
|
|
942
|
+
return typeof fn === 'function' ? fn.bind(receiver) : fn;
|
|
943
|
+
}
|
|
944
|
+
const value = Reflect.get(target, prop, receiver);
|
|
945
|
+
if (typeof value === 'function') return value;
|
|
946
|
+
return deep ? deepSignal(value, scope) : value;
|
|
947
|
+
}
|
|
948
|
+
});
|
|
949
|
+
rawToProxy.set(target, proxy);
|
|
950
|
+
return proxy;
|
|
951
|
+
}
|
|
760
952
|
const cells = new Map();
|
|
761
953
|
const targetIsArray = Array.isArray(target);
|
|
762
954
|
const targetIsStore = Boolean(target.constructor?.[IsStore]);
|
|
@@ -805,6 +997,7 @@
|
|
|
805
997
|
if (targetIsStore && isIgnoreKey(obj.constructor[StoreIgnoreKeys], prop) || typeof value === 'function') {
|
|
806
998
|
return Reflect.set(obj, prop, value, receiver);
|
|
807
999
|
}
|
|
1000
|
+
const isNewKey = !Reflect.has(obj, prop);
|
|
808
1001
|
batchStart();
|
|
809
1002
|
const success = Reflect.set(obj, prop, value, receiver);
|
|
810
1003
|
const cell = cells.get(prop);
|
|
@@ -813,8 +1006,8 @@
|
|
|
813
1006
|
}
|
|
814
1007
|
if (targetIsArray) {
|
|
815
1008
|
handleArraySet(obj, prop, value, receiver);
|
|
816
|
-
} else {
|
|
817
|
-
|
|
1009
|
+
} else if (isNewKey) {
|
|
1010
|
+
receiver[Keys.Iterator] = (receiver[Keys.Raw][Keys.Iterator] || 0) + 1;
|
|
818
1011
|
}
|
|
819
1012
|
batchEnd();
|
|
820
1013
|
return success;
|
|
@@ -824,8 +1017,11 @@
|
|
|
824
1017
|
return Reflect.deleteProperty(obj, prop);
|
|
825
1018
|
}
|
|
826
1019
|
cells.delete(prop);
|
|
827
|
-
|
|
828
|
-
|
|
1020
|
+
const result = Reflect.deleteProperty(obj, prop);
|
|
1021
|
+
if (!targetIsArray) {
|
|
1022
|
+
proxy[Keys.Iterator] = (obj[Keys.Iterator] || 0) + 1;
|
|
1023
|
+
}
|
|
1024
|
+
return result;
|
|
829
1025
|
},
|
|
830
1026
|
ownKeys(obj) {
|
|
831
1027
|
if (targetIsArray) {
|
|
@@ -833,7 +1029,7 @@
|
|
|
833
1029
|
} else {
|
|
834
1030
|
proxy[Keys.Iterator];
|
|
835
1031
|
}
|
|
836
|
-
return Reflect.ownKeys(obj);
|
|
1032
|
+
return Reflect.ownKeys(obj).filter(key => key !== Keys.Iterator);
|
|
837
1033
|
}
|
|
838
1034
|
});
|
|
839
1035
|
rawToProxy.set(target, proxy);
|
|
@@ -1210,6 +1406,7 @@
|
|
|
1210
1406
|
args[0] = wrapCb;
|
|
1211
1407
|
}
|
|
1212
1408
|
|
|
1409
|
+
const isParentKey = (parentStore, expr) => expr in parentStore[Keys.Raw];
|
|
1213
1410
|
class Store {
|
|
1214
1411
|
static [IsStore] = true;
|
|
1215
1412
|
static [StoreIgnoreKeys] = ['ui', 'raw'];
|
|
@@ -1220,18 +1417,31 @@
|
|
|
1220
1417
|
return proxy;
|
|
1221
1418
|
}
|
|
1222
1419
|
parent = () => null;
|
|
1223
|
-
static new(keyMap
|
|
1420
|
+
static new(keyMap, staticMap) {
|
|
1224
1421
|
const parentStore = Store.Current;
|
|
1225
1422
|
const child = new this();
|
|
1226
|
-
if (parentStore) {
|
|
1423
|
+
if (parentStore && keyMap) {
|
|
1424
|
+
const cells = child[Keys.Meta].cells;
|
|
1227
1425
|
for (const childKey in keyMap) {
|
|
1228
|
-
const
|
|
1229
|
-
|
|
1426
|
+
const expr = keyMap[childKey];
|
|
1427
|
+
if (typeof expr === 'function') {
|
|
1428
|
+
cells.set(childKey, new Computed(() => expr(parentStore)));
|
|
1429
|
+
child[Keys.Raw][childKey] = undefined;
|
|
1430
|
+
} else if (typeof expr === 'string') {
|
|
1431
|
+
if (isParentKey(parentStore, expr)) {
|
|
1432
|
+
shareSignal(parentStore, expr, child, childKey);
|
|
1433
|
+
} else {
|
|
1434
|
+
const fn = new Function('data', `let v;with(data){v=${expr};}return v;`);
|
|
1435
|
+
cells.set(childKey, new Computed(() => fn(parentStore)));
|
|
1436
|
+
child[Keys.Raw][childKey] = undefined;
|
|
1437
|
+
}
|
|
1438
|
+
} else {
|
|
1439
|
+
shareSignal(parentStore, expr, child, childKey);
|
|
1440
|
+
}
|
|
1230
1441
|
}
|
|
1231
1442
|
}
|
|
1232
1443
|
for (const key in staticMap) {
|
|
1233
|
-
|
|
1234
|
-
child[key] = value;
|
|
1444
|
+
child[key] = staticMap[key];
|
|
1235
1445
|
}
|
|
1236
1446
|
child.parent = () => parentStore;
|
|
1237
1447
|
Store.Current = parentStore;
|
|
@@ -1378,6 +1588,7 @@
|
|
|
1378
1588
|
exports.Signal = Signal;
|
|
1379
1589
|
exports.Store = Store;
|
|
1380
1590
|
exports.StoreIgnoreKeys = StoreIgnoreKeys;
|
|
1591
|
+
exports.batchDeep = batchDeep;
|
|
1381
1592
|
exports.batchEnd = batchEnd;
|
|
1382
1593
|
exports.batchStart = batchStart;
|
|
1383
1594
|
exports.clean = clean;
|