@cmtlyt/lingshu-toolkit 0.2.0 → 0.4.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/607.js +737 -0
- package/dist/707.js +13 -3
- package/dist/react/index.d.ts +7 -0
- package/dist/react/index.js +120 -3
- package/dist/react/use-force-update/index.d.ts +1 -0
- package/dist/react/use-force-update/index.js +6 -0
- package/dist/react/use-force-update/index.test.d.ts +1 -0
- package/dist/react/use-ref-state/index.d.ts +8 -0
- package/dist/react/use-ref-state/index.js +33 -0
- package/dist/react/use-ref-state/index.test.d.ts +1 -0
- package/dist/react/use-storage/index.d.ts +2 -0
- package/dist/react/use-storage/index.js +15 -0
- package/dist/react/use-storage/index.test.d.ts +1 -0
- package/dist/react/use-valid-data/index.d.ts +3 -3
- package/dist/shared/allx/__test__/allsettled.test.d.ts +1 -0
- package/dist/shared/allx/__test__/basic.test.d.ts +1 -0
- package/dist/shared/allx/__test__/circular-dependency.test.d.ts +1 -0
- package/dist/shared/allx/__test__/dependency.test.d.ts +1 -0
- package/dist/shared/allx/__test__/edge-cases.test.d.ts +1 -0
- package/dist/shared/allx/__test__/error-handling.test.d.ts +1 -0
- package/dist/shared/allx/__test__/execution-order.test.d.ts +1 -0
- package/dist/shared/allx/__test__/falsy-values.test.d.ts +1 -0
- package/dist/shared/allx/__test__/performance.test.d.ts +1 -0
- package/dist/shared/allx/__test__/type-checking.test.d.ts +1 -0
- package/dist/shared/allx/__test__/use-cases.test.d.ts +1 -0
- package/dist/shared/allx/index.d.ts +13 -0
- package/dist/shared/allx/index.js +44 -0
- package/dist/shared/allx/types.d.ts +13 -0
- package/dist/shared/allx/types.js +0 -0
- package/dist/shared/allx/utils.d.ts +9 -0
- package/dist/shared/allx/utils.js +94 -0
- package/dist/shared/animation/__test__/animation-pause-resume.test.d.ts +1 -0
- package/dist/shared/animation/__test__/animation.test.d.ts +1 -0
- package/dist/shared/animation/__test__/step-animation.test.d.ts +1 -0
- package/dist/shared/animation/__test__/utils.test.d.ts +1 -0
- package/dist/shared/animation/index.d.ts +3 -0
- package/dist/shared/animation/index.js +77 -0
- package/dist/shared/animation/types.d.ts +24 -0
- package/dist/shared/animation/types.js +0 -0
- package/dist/shared/animation/utils.d.ts +14 -0
- package/dist/shared/animation/utils.js +134 -0
- package/dist/shared/condition-merge/index.d.ts +1 -5
- package/dist/shared/create-storage-handler/index.browser.test.d.ts +1 -0
- package/dist/shared/create-storage-handler/index.d.ts +10 -0
- package/dist/shared/create-storage-handler/index.js +68 -0
- package/dist/shared/create-storage-handler/index.test.d.ts +1 -0
- package/dist/shared/data-handler/index.d.ts +7 -2
- package/dist/shared/data-handler/tools.d.ts +36 -12
- package/dist/shared/data-handler/tools.js +8 -4
- package/dist/shared/data-handler/types.d.ts +2 -2
- package/dist/shared/data-mixed-manager/__test__/basic.test.d.ts +1 -0
- package/dist/shared/data-mixed-manager/__test__/build-options.test.d.ts +1 -0
- package/dist/shared/data-mixed-manager/__test__/constructor-options.test.d.ts +1 -0
- package/dist/shared/data-mixed-manager/__test__/data-management.test.d.ts +1 -0
- package/dist/shared/data-mixed-manager/__test__/edge-cases.test.d.ts +1 -0
- package/dist/shared/data-mixed-manager/__test__/events.browser.test.d.ts +1 -0
- package/dist/shared/data-mixed-manager/__test__/events.test.d.ts +1 -0
- package/dist/shared/data-mixed-manager/__test__/fixed-slots.test.d.ts +1 -0
- package/dist/shared/data-mixed-manager/__test__/insert-mode.test.d.ts +1 -0
- package/dist/shared/data-mixed-manager/constants.d.ts +8 -0
- package/dist/shared/data-mixed-manager/constants.js +9 -0
- package/dist/shared/data-mixed-manager/index.d.ts +128 -0
- package/dist/shared/data-mixed-manager/index.js +226 -0
- package/dist/shared/data-mixed-manager/types.d.ts +90 -0
- package/dist/shared/data-mixed-manager/types.js +0 -0
- package/dist/shared/index.d.ts +8 -0
- package/dist/shared/index.js +2 -1
- package/dist/shared/throw-error/index.d.ts +1 -0
- package/dist/shared/throw-error/index.js +5 -2
- package/dist/shared/types/base.d.ts +6 -0
- package/dist/shared/utils/__test__/base.test.d.ts +1 -0
- package/dist/shared/utils/__test__/verify.test.d.ts +1 -0
- package/dist/shared/utils/base.d.ts +3 -0
- package/dist/shared/utils/base.js +6 -0
- package/dist/shared/utils/index.d.ts +2 -0
- package/dist/shared/utils/index.js +2 -0
- package/dist/shared/utils/verify.d.ts +53 -0
- package/dist/shared/utils/verify.js +67 -0
- package/dist/shared/with-resolvers/index.d.ts +6 -0
- package/dist/shared/with-resolvers/index.js +15 -0
- package/dist/shared/with-resolvers/index.test.d.ts +1 -0
- package/dist/test/utils.d.ts +13 -0
- package/package.json +9 -7
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { throwType } from "../throw-error/index.js";
|
|
2
|
+
import { getType } from "../utils/base.js";
|
|
3
|
+
import { withResolvers } from "../with-resolvers/index.js";
|
|
4
|
+
function getNextValueHandler(from, to, valueFormatter) {
|
|
5
|
+
const type = getType(from);
|
|
6
|
+
const context = {
|
|
7
|
+
progress: 0,
|
|
8
|
+
valueFormatter
|
|
9
|
+
};
|
|
10
|
+
const baseNextValue = (_from, _to)=>{
|
|
11
|
+
const { valueFormatter: formatter, progress } = context;
|
|
12
|
+
if ('number' !== getType(_from)) return _from;
|
|
13
|
+
return formatter(_from + (_to - _from) * progress);
|
|
14
|
+
};
|
|
15
|
+
const arrayHandler = (_from, _to)=>{
|
|
16
|
+
const result = Array.from(_from, (item, idx)=>{
|
|
17
|
+
if (Array.isArray(item)) return arrayHandler(item, _to[idx]);
|
|
18
|
+
if ('object' === getType(item)) return objectHandler(item, _to[idx]);
|
|
19
|
+
return baseNextValue(item, _to[idx]);
|
|
20
|
+
});
|
|
21
|
+
return result;
|
|
22
|
+
};
|
|
23
|
+
const objectHandler = (_from, _to)=>{
|
|
24
|
+
const result = {};
|
|
25
|
+
const keys = Reflect.ownKeys(_from);
|
|
26
|
+
for(let i = 0; i < keys.length; i++){
|
|
27
|
+
const key = keys[i];
|
|
28
|
+
const fromValue = _from[key];
|
|
29
|
+
const toValue = _to[key];
|
|
30
|
+
if (Array.isArray(_from[key])) result[key] = arrayHandler(fromValue, toValue);
|
|
31
|
+
else if ('object' === getType(fromValue)) result[key] = objectHandler(fromValue, toValue);
|
|
32
|
+
else result[key] = baseNextValue(fromValue, toValue);
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
};
|
|
36
|
+
let nextValueHandler = baseNextValue;
|
|
37
|
+
if ('array' === type) nextValueHandler = arrayHandler;
|
|
38
|
+
else if ('object' === type) nextValueHandler = objectHandler;
|
|
39
|
+
return (progress)=>{
|
|
40
|
+
context.progress = progress;
|
|
41
|
+
return nextValueHandler(from, to);
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function matchValid(from, to, valueParser) {
|
|
45
|
+
const fromType = getType(from);
|
|
46
|
+
const toType = getType(to);
|
|
47
|
+
if (fromType !== toType) throwType('animation/stepAnimation', 'from and to must be the same type');
|
|
48
|
+
if ('array' === fromType) {
|
|
49
|
+
if (from.length !== to.length) throwType('animation/stepAnimation', 'from and to must be the same length');
|
|
50
|
+
const result = [
|
|
51
|
+
Array.from({
|
|
52
|
+
length: from.length
|
|
53
|
+
}),
|
|
54
|
+
Array.from({
|
|
55
|
+
length: to.length
|
|
56
|
+
})
|
|
57
|
+
];
|
|
58
|
+
for(let i = 0; i < from.length; i++){
|
|
59
|
+
const [fromItem, toItem] = matchValid(from[i], to[i], valueParser);
|
|
60
|
+
result[0][i] = fromItem;
|
|
61
|
+
result[1][i] = toItem;
|
|
62
|
+
}
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
if ('object' === fromType) {
|
|
66
|
+
const toKeys = Reflect.ownKeys(to);
|
|
67
|
+
const fromKeys = new Set(Reflect.ownKeys(from));
|
|
68
|
+
const result = [
|
|
69
|
+
{},
|
|
70
|
+
{}
|
|
71
|
+
];
|
|
72
|
+
for(let i = 0; i < toKeys.length; i++){
|
|
73
|
+
const key = toKeys[i];
|
|
74
|
+
if (!fromKeys.has(key)) throwType('animation/stepAnimation', `from does not have this key: ${String(key)}`);
|
|
75
|
+
const [fromItem, toItem] = matchValid(from[key], to[key], valueParser);
|
|
76
|
+
result[0][key] = fromItem;
|
|
77
|
+
result[1][key] = toItem;
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
if ('number' !== fromType) return [
|
|
82
|
+
valueParser(from),
|
|
83
|
+
valueParser(to)
|
|
84
|
+
];
|
|
85
|
+
return [
|
|
86
|
+
from,
|
|
87
|
+
to
|
|
88
|
+
];
|
|
89
|
+
}
|
|
90
|
+
function createNextTick(resolvers, rcSignal) {
|
|
91
|
+
const nextTick = (()=>{
|
|
92
|
+
if ('function' == typeof globalThis.requestAnimationFrame) return globalThis.requestAnimationFrame;
|
|
93
|
+
return (callback)=>setTimeout(callback, 16);
|
|
94
|
+
})();
|
|
95
|
+
return (callback)=>{
|
|
96
|
+
if (rcSignal.stopSignal) return true;
|
|
97
|
+
nextTick(()=>tryRun(callback, resolvers, (error)=>{
|
|
98
|
+
rcSignal.stop();
|
|
99
|
+
resolvers.reject(error);
|
|
100
|
+
}));
|
|
101
|
+
return false;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
async function tryRun(callback, resolvers, customErrorHandler) {
|
|
105
|
+
return new Promise((resolve, reject)=>{
|
|
106
|
+
const result = callback();
|
|
107
|
+
if (result && 'function' == typeof result.then) return result.then(resolve, reject);
|
|
108
|
+
resolve();
|
|
109
|
+
}).catch(customErrorHandler || resolvers.reject);
|
|
110
|
+
}
|
|
111
|
+
function createRunningControllerSignal(startFn, options) {
|
|
112
|
+
const { onStart, onStop, onClear } = options;
|
|
113
|
+
const resolvers = withResolvers();
|
|
114
|
+
const ctrl = {
|
|
115
|
+
stopSignal: true,
|
|
116
|
+
resolvers,
|
|
117
|
+
stop: ()=>{
|
|
118
|
+
ctrl.stopSignal = true;
|
|
119
|
+
onStop();
|
|
120
|
+
},
|
|
121
|
+
start: ()=>{
|
|
122
|
+
ctrl.stopSignal = false;
|
|
123
|
+
onStart();
|
|
124
|
+
startFn();
|
|
125
|
+
},
|
|
126
|
+
clear: ()=>{
|
|
127
|
+
ctrl.stopSignal = true;
|
|
128
|
+
onClear();
|
|
129
|
+
resolvers.resolve(true);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
return ctrl;
|
|
133
|
+
}
|
|
134
|
+
export { createNextTick, createRunningControllerSignal, getNextValueHandler, matchValid, tryRun };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { IsPrimitive, UnionToIntersection } from '../types';
|
|
1
|
+
import type { IsPrimitive, Printify, UnionToIntersection } from '../types';
|
|
2
2
|
type AssertValue = Record<PropertyKey, any> | any[];
|
|
3
3
|
type ConditionArrayItem = [boolean, AssertValue, AssertValue?];
|
|
4
4
|
type ConditionObjItem = {
|
|
@@ -25,11 +25,7 @@ type MergedResult<T extends ConditionItem[]> = T extends [
|
|
|
25
25
|
...infer Last extends [ConditionItem, ...ConditionItem[]]
|
|
26
26
|
] ? ParseConditionValue<First> & MergedResult<Last> : ParseConditionValue<T[0]>;
|
|
27
27
|
type CMInput = ConditionItem[];
|
|
28
|
-
type Printify<T> = T extends any[] ? T : [T] extends [never] ? T : {
|
|
29
|
-
[K in keyof T]: T[K];
|
|
30
|
-
};
|
|
31
28
|
type FormatResult<T extends any[]> = T[0] & Record<PropertyKey, any>;
|
|
32
29
|
export declare function conditionMerge<T extends CMInput>(...input: T): FormatResult<MergedResult<T>>;
|
|
33
30
|
export declare function conditionMerge<T extends CMInput>(input: T): FormatResult<MergedResult<T>>;
|
|
34
31
|
export {};
|
|
35
|
-
t {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface CreateStorageOptions {
|
|
2
|
+
storageType: 'local' | 'session' | 'memory';
|
|
3
|
+
autoSaveInterval: number;
|
|
4
|
+
}
|
|
5
|
+
export interface StorageHandler<T extends Record<string, any>> {
|
|
6
|
+
get: <K extends keyof T | (string & {})>(key?: K) => string extends K ? T : T[K & keyof T];
|
|
7
|
+
set: <K extends keyof T | (string & {})>(value: string extends K ? T : T[K & keyof T], key?: K) => void;
|
|
8
|
+
clear: () => void;
|
|
9
|
+
}
|
|
10
|
+
export declare function createStorageHandler<T extends Record<string, any>>(storageKey: string, initialData?: T, options?: Partial<CreateStorageOptions>): StorageHandler<T>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { $dt, $t, dataHandler } from "../data-handler/index.js";
|
|
2
|
+
import { logger } from "../logger/index.js";
|
|
3
|
+
import { throwError } from "../throw-error/index.js";
|
|
4
|
+
const validInfo = $dt({
|
|
5
|
+
storageKey: 'validString',
|
|
6
|
+
storageType: $t["enum"]([
|
|
7
|
+
'local',
|
|
8
|
+
'session',
|
|
9
|
+
'memory'
|
|
10
|
+
], 'local'),
|
|
11
|
+
autoSaveInterval: $t.number(0)
|
|
12
|
+
});
|
|
13
|
+
const memoryStorage = {
|
|
14
|
+
data: {},
|
|
15
|
+
getItem (key) {
|
|
16
|
+
return this.data[key];
|
|
17
|
+
},
|
|
18
|
+
setItem (key, value) {
|
|
19
|
+
this.data[key] = value;
|
|
20
|
+
},
|
|
21
|
+
removeItem (key) {
|
|
22
|
+
delete this.data[key];
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
function getStorage(storageType) {
|
|
26
|
+
try {
|
|
27
|
+
if ('memory' === storageType) return memoryStorage;
|
|
28
|
+
return 'local' === storageType ? localStorage : sessionStorage;
|
|
29
|
+
} catch {
|
|
30
|
+
logger.warn('createStorage', 'Failed to access localStorage or sessionStorage, using memoryStorage instead.');
|
|
31
|
+
return memoryStorage;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const CLEAR_FLAG = Symbol('cleared');
|
|
35
|
+
function createStorageHandler(storageKey, initialData, options = {}) {
|
|
36
|
+
const { storageKey: validStorageKey, storageType, autoSaveInterval } = dataHandler({
|
|
37
|
+
storageKey,
|
|
38
|
+
...options
|
|
39
|
+
}, validInfo, {
|
|
40
|
+
unwrap: true
|
|
41
|
+
});
|
|
42
|
+
const storage = getStorage(storageType);
|
|
43
|
+
const storageData = storage.getItem(validStorageKey);
|
|
44
|
+
const context = {
|
|
45
|
+
data: storageData ? JSON.parse(storageData) : initialData || {}
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
get (key) {
|
|
49
|
+
if (context.data === CLEAR_FLAG) throwError('createStorageHandler', 'Storage has been cleared.');
|
|
50
|
+
if (null == key) return context.data;
|
|
51
|
+
return context.data[key];
|
|
52
|
+
},
|
|
53
|
+
set (value, key) {
|
|
54
|
+
if (context.data === CLEAR_FLAG) throwError('createStorageHandler', 'Storage has been cleared.');
|
|
55
|
+
if (null == key) context.data = value;
|
|
56
|
+
else context.data[key] = value;
|
|
57
|
+
if (autoSaveInterval > 0) setTimeout(()=>{
|
|
58
|
+
storage.setItem(validStorageKey, JSON.stringify(context.data));
|
|
59
|
+
}, autoSaveInterval);
|
|
60
|
+
else storage.setItem(validStorageKey, JSON.stringify(context.data));
|
|
61
|
+
},
|
|
62
|
+
clear () {
|
|
63
|
+
context.data = CLEAR_FLAG;
|
|
64
|
+
storage.removeItem(validStorageKey);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
export { createStorageHandler };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
+
import type { Equal, Printify } from '../types';
|
|
2
|
+
import type { Transform2Type } from './tools';
|
|
1
3
|
import type { DataHandlerOptions, Handler } from './types';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
+
type MergeResult<BaseResult extends Record<PropertyKey, any>, HandlerResult extends Handler<any>> = Printify<HandlerResult extends (...args: any[]) => any ? BaseResult : BaseResult & {
|
|
5
|
+
[K in keyof HandlerResult]: Equal<HandlerResult[K], any> extends true ? Required<BaseResult>[K] : HandlerResult[K];
|
|
6
|
+
}>;
|
|
7
|
+
export declare function dataHandler<M extends Record<PropertyKey, any>, H extends Handler<M> = Handler<M>, O extends DataHandlerOptions<M> = DataHandlerOptions<M>>(data: M & Partial<O['defaultValue']>, handler: H, options?: O): O['unwrap'] extends true ? MergeResult<M & O['defaultValue'], Transform2Type<H>> : {
|
|
8
|
+
result: MergeResult<M & O['defaultValue'], Transform2Type<H>>;
|
|
4
9
|
errors: string[];
|
|
5
10
|
};
|
|
6
11
|
export * from './tools';
|
|
@@ -1,18 +1,42 @@
|
|
|
1
1
|
import type { Handler } from './types';
|
|
2
2
|
type TypeHandler = NonNullable<Exclude<Handler<any>, (...args: any[]) => any>[string]>;
|
|
3
|
-
type
|
|
3
|
+
type TypeHandlerParams = Parameters<TypeHandler> extends [any, ...infer Rest] ? Rest : never;
|
|
4
|
+
type ParseType<T extends string> = T extends keyof TypeMap ? TypeMap[T] : any;
|
|
5
|
+
type TypeHandlerInfo<T extends string> = (value: ParseType<T>, ...args: TypeHandlerParams) => ReturnType<TypeHandler>;
|
|
6
|
+
interface TypeMap {
|
|
7
|
+
notNullable: any & {};
|
|
8
|
+
string: string;
|
|
9
|
+
validString: string;
|
|
10
|
+
number: number;
|
|
11
|
+
validNumber: number;
|
|
12
|
+
boolean: boolean;
|
|
13
|
+
object: Record<PropertyKey, any>;
|
|
14
|
+
array: any[];
|
|
15
|
+
function: (...args: any[]) => any;
|
|
16
|
+
symbol: symbol;
|
|
17
|
+
enum: any & {};
|
|
18
|
+
}
|
|
4
19
|
export declare const $t: {
|
|
5
|
-
notNullable: (fullback?:
|
|
6
|
-
string: (fullback?:
|
|
7
|
-
validString: (fullback?:
|
|
8
|
-
number: (fullback?:
|
|
9
|
-
validNumber: (fullback?:
|
|
10
|
-
boolean: (fullback?:
|
|
11
|
-
object: (fullback?:
|
|
12
|
-
array: (fullback?:
|
|
13
|
-
function: (fullback?:
|
|
14
|
-
symbol: (fullback?:
|
|
20
|
+
notNullable: (fullback?: any) => TypeHandlerInfo<"notNullable">;
|
|
21
|
+
string: (fullback?: string | ((_v: any) => string) | undefined) => TypeHandlerInfo<"string">;
|
|
22
|
+
validString: (fullback?: string | ((_v: any) => string) | undefined) => TypeHandlerInfo<"validString">;
|
|
23
|
+
number: (fullback?: number | ((_v: any) => number) | undefined) => TypeHandlerInfo<"number">;
|
|
24
|
+
validNumber: (fullback?: number | ((_v: any) => number) | undefined) => TypeHandlerInfo<"validNumber">;
|
|
25
|
+
boolean: (fullback?: boolean | ((_v: any) => boolean) | undefined) => TypeHandlerInfo<"boolean">;
|
|
26
|
+
object: (fullback?: Record<PropertyKey, any> | ((_v: any) => Record<PropertyKey, any>) | undefined) => TypeHandlerInfo<"object">;
|
|
27
|
+
array: (fullback?: any[] | ((_v: any) => any[]) | undefined) => TypeHandlerInfo<"array">;
|
|
28
|
+
function: (fullback?: ((_v: any) => (...args: any[]) => any) | undefined) => TypeHandlerInfo<"function">;
|
|
29
|
+
symbol: (fullback?: symbol | ((_v: any) => symbol) | undefined) => TypeHandlerInfo<"symbol">;
|
|
30
|
+
enum: <T>(list: T[], fullback?: T) => TypeHandlerInfo<"enum">;
|
|
15
31
|
};
|
|
16
|
-
|
|
32
|
+
type TransformMap = typeof $t;
|
|
33
|
+
type TransformKey = Exclude<keyof TransformMap, 'enum'>;
|
|
34
|
+
type DataTransformResult<D extends Record<PropertyKey, TransformKey | TypeHandler | undefined>> = {
|
|
35
|
+
[K in keyof D]: D[K] extends TransformKey ? TransformMap[D[K]] : D[K];
|
|
36
|
+
};
|
|
37
|
+
export type Transform2Type<R extends DataTransformResult<any>> = {
|
|
38
|
+
[K in keyof R]: R[K] extends TypeHandlerInfo<infer T> ? ParseType<T> & {} : any & {};
|
|
39
|
+
};
|
|
40
|
+
export declare function defineTransform<T extends Record<PropertyKey, any>, D extends Partial<Record<keyof T, TransformKey | TypeHandler>> = Partial<Record<keyof T, TransformKey | TypeHandler>>>(dataInfo: D): DataTransformResult<D>;
|
|
17
41
|
export declare const $dt: typeof defineTransform;
|
|
18
42
|
export {};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { logger } from "../logger/index.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
2
|
+
import { throwType } from "../throw-error/index.js";
|
|
3
|
+
import { getType } from "../utils/base.js";
|
|
5
4
|
function typeHandler(type, verifyFn) {
|
|
6
5
|
return (fullback)=>(_v, actions)=>{
|
|
7
6
|
if (verifyFn ? verifyFn(_v) : getType(_v) === type) return true;
|
|
@@ -21,7 +20,12 @@ const $t = {
|
|
|
21
20
|
object: typeHandler('object'),
|
|
22
21
|
array: typeHandler('array'),
|
|
23
22
|
function: typeHandler('function'),
|
|
24
|
-
symbol: typeHandler('symbol')
|
|
23
|
+
symbol: typeHandler('symbol'),
|
|
24
|
+
enum: (list, fullback)=>{
|
|
25
|
+
if (!Array.isArray(list)) throwType('$t.enum', 'list must be an array');
|
|
26
|
+
const set = new Set(list);
|
|
27
|
+
return typeHandler('enum', (_v)=>set.has(_v))(fullback);
|
|
28
|
+
}
|
|
25
29
|
};
|
|
26
30
|
function defineTransform(dataInfo) {
|
|
27
31
|
const verifyInfo = {};
|
|
@@ -8,8 +8,8 @@ export interface Actions {
|
|
|
8
8
|
transform: <T>(value: T) => T;
|
|
9
9
|
}
|
|
10
10
|
export type Handler<M extends Record<PropertyKey, any>> = Partial<{
|
|
11
|
-
[K in keyof M]: (value:
|
|
12
|
-
}> | (<K extends keyof M>(value:
|
|
11
|
+
[K in keyof M]: (value: any, action: Actions, option: M) => false | (any & {});
|
|
12
|
+
}> | (<K extends keyof M>(value: any, key: K, action: Actions, option: M) => false | (any & {}));
|
|
13
13
|
export interface ActionContext {
|
|
14
14
|
errors: string[];
|
|
15
15
|
transforms: [PropertyKey, any][];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { PickRequired } from '../types/base';
|
|
2
|
+
import type { BuildOptions, DataMixedManagerOptions, DMMEventHandler, EventDetailMap, InputSlotConfig, MixedDataItem } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* 通用数据管理器类
|
|
5
|
+
* 支持定坑逻辑处理,不包含具体业务逻辑
|
|
6
|
+
*
|
|
7
|
+
* @template T - 数据类型
|
|
8
|
+
*/
|
|
9
|
+
declare class DataMixedManager<T> extends EventTarget {
|
|
10
|
+
addEventListener<E extends keyof EventDetailMap<T>>(type: E, listener: DMMEventHandler<T, E> | null, options?: AddEventListenerOptions | boolean): void;
|
|
11
|
+
removeEventListener<E extends keyof EventDetailMap<T>>(type: E, listener: DMMEventHandler<T, E> | null, options?: EventListenerOptions | boolean): void;
|
|
12
|
+
/** 实例配置 */
|
|
13
|
+
private readonly options;
|
|
14
|
+
/** 位置到定坑配置的映射 */
|
|
15
|
+
private readonly fixedSlots;
|
|
16
|
+
/** 普通数据列表 */
|
|
17
|
+
private readonly dataList;
|
|
18
|
+
/** 混合后的数据 */
|
|
19
|
+
private readonly mixedData;
|
|
20
|
+
/** 最后混合的定坑位置 */
|
|
21
|
+
private lastMixedSlotIdx;
|
|
22
|
+
/** 上次处理的数据长度 */
|
|
23
|
+
private prevDataLength;
|
|
24
|
+
/** 是否正在批量更新 */
|
|
25
|
+
private isBatching;
|
|
26
|
+
constructor(options?: DataMixedManagerOptions<T>);
|
|
27
|
+
/**
|
|
28
|
+
* 初始化事件监听
|
|
29
|
+
* @param listener - 事件监听
|
|
30
|
+
*/
|
|
31
|
+
private initListener;
|
|
32
|
+
private getTypeText;
|
|
33
|
+
private buildSlotConfig;
|
|
34
|
+
/**
|
|
35
|
+
* 添加定坑配置
|
|
36
|
+
* @param config - 定坑配置
|
|
37
|
+
* @param buildOptions - 构建选项
|
|
38
|
+
* @returns 返回添加后的定坑位置
|
|
39
|
+
*/
|
|
40
|
+
addFixedSlot(config: InputSlotConfig<T>, buildOptions?: BuildOptions): number;
|
|
41
|
+
/**
|
|
42
|
+
* 重新排序定坑位置
|
|
43
|
+
* 当插入模式为 before 或 after 时,需要调整后续定坑的位置
|
|
44
|
+
* @param config - 定坑配置
|
|
45
|
+
* @returns 返回调整后的定坑配置
|
|
46
|
+
*/
|
|
47
|
+
private reorderFixedSlots;
|
|
48
|
+
/**
|
|
49
|
+
* 批量添加定坑配置
|
|
50
|
+
* @param configs - 定坑配置数组
|
|
51
|
+
* @param buildOptions - 构建选项
|
|
52
|
+
* @returns 返回添加后的定坑位置数组
|
|
53
|
+
*/
|
|
54
|
+
addFixedSlots(configs: InputSlotConfig<T>[], buildOptions?: BuildOptions): number[];
|
|
55
|
+
/**
|
|
56
|
+
* 移除指定位置的定坑配置
|
|
57
|
+
* @param position - 要移除的定坑位置
|
|
58
|
+
* @param buildOptions - 构建选项
|
|
59
|
+
*/
|
|
60
|
+
deleteFixedSlot(position: number, buildOptions?: BuildOptions): void;
|
|
61
|
+
/**
|
|
62
|
+
* 批量移除定坑配置
|
|
63
|
+
* @param positions - 要移除的定坑位置数组
|
|
64
|
+
* @param buildOptions - 构建选项
|
|
65
|
+
*/
|
|
66
|
+
deleteFixedSlots(positions: number[], buildOptions?: BuildOptions): void;
|
|
67
|
+
/**
|
|
68
|
+
* 批量更新
|
|
69
|
+
* @param callback - 更新回调
|
|
70
|
+
*/
|
|
71
|
+
private batchUpdate;
|
|
72
|
+
/**
|
|
73
|
+
* 清除所有定坑配置
|
|
74
|
+
* @param buildOptions - 构建选项
|
|
75
|
+
*/
|
|
76
|
+
clearFixedSlots(buildOptions?: BuildOptions): void;
|
|
77
|
+
/**
|
|
78
|
+
* 追加新的数据列表到普通数据列表末尾
|
|
79
|
+
* @param list - 要追加的数据数组
|
|
80
|
+
* @param buildOptions - 构建选项,lazy 为 true 时延迟构建
|
|
81
|
+
*/
|
|
82
|
+
appendList(list: T[], buildOptions?: BuildOptions): void;
|
|
83
|
+
/**
|
|
84
|
+
* 清空普通数据列表
|
|
85
|
+
*/
|
|
86
|
+
clearList(): void;
|
|
87
|
+
/**
|
|
88
|
+
* 获取混合后的数据
|
|
89
|
+
* @param buildOptions - 构建选项
|
|
90
|
+
* @returns 返回混合后的数据项数组
|
|
91
|
+
*/
|
|
92
|
+
getMixedData(buildOptions?: Omit<BuildOptions, 'lazy'>): MixedDataItem<T>[];
|
|
93
|
+
/**
|
|
94
|
+
* 分发事件
|
|
95
|
+
* 同时在实例和 window 对象上触发事件
|
|
96
|
+
* @param name - 事件名称
|
|
97
|
+
* @param data - 事件数据
|
|
98
|
+
*/
|
|
99
|
+
private dispatch;
|
|
100
|
+
/**
|
|
101
|
+
* 重新构建混合数据
|
|
102
|
+
* 全局定坑:定坑位置始终固定在全局位置,普通数据在定坑位置之外填充
|
|
103
|
+
* 定坑位置不会因为普通数据的变化而改变
|
|
104
|
+
*/
|
|
105
|
+
private buildMixedData;
|
|
106
|
+
/**
|
|
107
|
+
* 切片获取指定范围内的定坑位置
|
|
108
|
+
* @param startIdx - 起始位置
|
|
109
|
+
* @param endIdx - 结束位置,默认为正无穷
|
|
110
|
+
* @returns 返回过滤后的定坑位置数组
|
|
111
|
+
*/
|
|
112
|
+
private sliceSlots;
|
|
113
|
+
/**
|
|
114
|
+
* 插入数据(插卡模式)
|
|
115
|
+
* 在指定位置插入数据,会触发全量重建
|
|
116
|
+
* @param config - 定坑配置,必须指定插入模式
|
|
117
|
+
* @returns 返回插入后的实际位置
|
|
118
|
+
*/
|
|
119
|
+
insertSlot(config: PickRequired<InputSlotConfig<T>, 'insertMode'>): number;
|
|
120
|
+
insertSlots(configs: PickRequired<InputSlotConfig<T>, 'insertMode'>[]): number[];
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* 创建数据管理器实例的工厂函数
|
|
124
|
+
* @template T - 数据类型
|
|
125
|
+
* @returns 返回一个新的 DataMixedManager 实例
|
|
126
|
+
*/
|
|
127
|
+
export declare function dataMixedManager<T>(options?: DataMixedManagerOptions<T>): DataMixedManager<T>;
|
|
128
|
+
export {};
|