@cmtlyt/lingshu-toolkit 0.3.0 → 0.5.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.
Files changed (76) hide show
  1. package/dist/247.js +66 -0
  2. package/dist/707.js +8 -2
  3. package/dist/react/index.js +2 -4
  4. package/dist/react/use-mount/index.js +1 -1
  5. package/dist/react/use-ref-state/index.js +1 -3
  6. package/dist/shared/allx/__test__/allsettled.test.d.ts +1 -0
  7. package/dist/shared/allx/__test__/basic.test.d.ts +1 -0
  8. package/dist/shared/allx/__test__/circular-dependency.test.d.ts +1 -0
  9. package/dist/shared/allx/__test__/dependency.test.d.ts +1 -0
  10. package/dist/shared/allx/__test__/edge-cases.test.d.ts +1 -0
  11. package/dist/shared/allx/__test__/error-handling.test.d.ts +1 -0
  12. package/dist/shared/allx/__test__/execution-order.test.d.ts +1 -0
  13. package/dist/shared/allx/__test__/falsy-values.test.d.ts +1 -0
  14. package/dist/shared/allx/__test__/performance.test.d.ts +1 -0
  15. package/dist/shared/allx/__test__/type-checking.test.d.ts +1 -0
  16. package/dist/shared/allx/__test__/use-cases.test.d.ts +1 -0
  17. package/dist/shared/allx/index.d.ts +13 -0
  18. package/dist/shared/allx/index.js +44 -0
  19. package/dist/shared/allx/types.d.ts +13 -0
  20. package/dist/shared/allx/types.js +0 -0
  21. package/dist/shared/allx/utils.d.ts +9 -0
  22. package/dist/shared/allx/utils.js +94 -0
  23. package/dist/shared/animation/index.d.ts +2 -2
  24. package/dist/shared/animation/index.js +19 -13
  25. package/dist/shared/animation/types.d.ts +6 -4
  26. package/dist/shared/animation/utils.d.ts +3 -5
  27. package/dist/shared/animation/utils.js +2 -6
  28. package/dist/shared/api-controller/__test__/index.browser.test.d.ts +1 -0
  29. package/dist/shared/api-controller/__test__/index.node.test.d.ts +1 -0
  30. package/dist/shared/api-controller/create-api.d.ts +26 -0
  31. package/dist/shared/api-controller/create-api.js +79 -0
  32. package/dist/shared/api-controller/index.d.ts +3 -0
  33. package/dist/shared/api-controller/index.js +3 -0
  34. package/dist/shared/api-controller/request.d.ts +7 -0
  35. package/dist/shared/api-controller/request.js +66 -0
  36. package/dist/shared/api-controller/types.d.ts +141 -0
  37. package/dist/shared/api-controller/types.js +0 -0
  38. package/dist/shared/api-controller/utils.d.ts +22 -0
  39. package/dist/shared/api-controller/utils.js +96 -0
  40. package/dist/shared/data-handler/tools.js +1 -3
  41. package/dist/shared/data-mixed-manager/__test__/basic.test.d.ts +1 -0
  42. package/dist/shared/data-mixed-manager/__test__/build-options.test.d.ts +1 -0
  43. package/dist/shared/data-mixed-manager/__test__/constructor-options.test.d.ts +1 -0
  44. package/dist/shared/data-mixed-manager/__test__/data-management.test.d.ts +1 -0
  45. package/dist/shared/data-mixed-manager/__test__/edge-cases.test.d.ts +1 -0
  46. package/dist/shared/data-mixed-manager/__test__/events.browser.test.d.ts +1 -0
  47. package/dist/shared/data-mixed-manager/__test__/events.test.d.ts +1 -0
  48. package/dist/shared/data-mixed-manager/__test__/fixed-slots.test.d.ts +1 -0
  49. package/dist/shared/data-mixed-manager/__test__/insert-mode.test.d.ts +1 -0
  50. package/dist/shared/data-mixed-manager/constants.d.ts +8 -0
  51. package/dist/shared/data-mixed-manager/constants.js +9 -0
  52. package/dist/shared/data-mixed-manager/index.d.ts +128 -0
  53. package/dist/shared/data-mixed-manager/index.js +226 -0
  54. package/dist/shared/data-mixed-manager/types.d.ts +90 -0
  55. package/dist/shared/data-mixed-manager/types.js +0 -0
  56. package/dist/shared/index.d.ts +5 -0
  57. package/dist/shared/index.js +957 -2
  58. package/dist/shared/logger/index.d.ts +5 -0
  59. package/dist/shared/logger/index.js +1 -0
  60. package/dist/shared/throw-error/index.d.ts +1 -0
  61. package/dist/shared/throw-error/index.js +5 -2
  62. package/dist/shared/try-call/index.d.ts +22 -0
  63. package/dist/shared/try-call/index.js +59 -0
  64. package/dist/shared/try-call/index.test.d.ts +1 -0
  65. package/dist/shared/types/base.d.ts +5 -0
  66. package/dist/shared/types/pack.d.ts +1 -1
  67. package/dist/shared/utils/__test__/base.test.d.ts +1 -0
  68. package/dist/shared/utils/__test__/verify.test.d.ts +1 -0
  69. package/dist/shared/utils/base.d.ts +3 -0
  70. package/dist/shared/utils/base.js +6 -0
  71. package/dist/shared/utils/index.d.ts +2 -0
  72. package/dist/shared/utils/index.js +2 -0
  73. package/dist/shared/utils/verify.d.ts +53 -0
  74. package/dist/shared/utils/verify.js +67 -0
  75. package/package.json +10 -7
  76. package/dist/607.js +0 -311
@@ -0,0 +1,226 @@
1
+ import { $dt, $t, dataHandler } from "../data-handler/index.js";
2
+ import { throwError } from "../throw-error/index.js";
3
+ import { SLOT_TYPE } from "./constants.js";
4
+ const validInfo = $dt({
5
+ name: $t.string('default'),
6
+ fixedSlots: $t.array([]),
7
+ dataList: $t.array([]),
8
+ listener: $t.object({})
9
+ });
10
+ class DataMixedManager extends EventTarget {
11
+ addEventListener(...args) {
12
+ return super.addEventListener.apply(this, args);
13
+ }
14
+ removeEventListener(...args) {
15
+ return super.removeEventListener.apply(this, args);
16
+ }
17
+ options;
18
+ fixedSlots = new Map();
19
+ dataList = [];
20
+ mixedData = [];
21
+ lastMixedSlotIdx = -1;
22
+ prevDataLength = 0;
23
+ isBatching = false;
24
+ constructor(options){
25
+ super();
26
+ const validOptions = dataHandler(options || {}, validInfo, {
27
+ unwrap: true
28
+ });
29
+ const { fixedSlots, dataList, listener } = validOptions;
30
+ this.options = validOptions;
31
+ this.addFixedSlots(fixedSlots, {
32
+ lazy: true
33
+ });
34
+ this.appendList(dataList);
35
+ try {
36
+ this.initListener(listener);
37
+ } catch (error) {
38
+ throwError('dataMixedManager', error.message, error.constructor);
39
+ }
40
+ }
41
+ initListener(listener) {
42
+ const listenerNames = Object.keys(listener);
43
+ for(let i = 0, name = listenerNames[i], handler = listener[name]; i < listenerNames.length; name = listenerNames[++i], handler = listener[name])this.addEventListener(name, handler);
44
+ }
45
+ getTypeText(_type) {
46
+ switch(_type){
47
+ case SLOT_TYPE.fixed:
48
+ return 'fixed';
49
+ case SLOT_TYPE.insert:
50
+ return 'insert';
51
+ }
52
+ }
53
+ buildSlotConfig(config, type) {
54
+ const typeText = this.getTypeText(config.type || type);
55
+ return {
56
+ ...config,
57
+ type: typeText,
58
+ inputPosition: config.position,
59
+ insertMode: config.insertMode || 'cover'
60
+ };
61
+ }
62
+ addFixedSlot(config, buildOptions) {
63
+ const realConfig = this.reorderFixedSlots(this.buildSlotConfig(config, SLOT_TYPE.fixed));
64
+ this.fixedSlots.set(realConfig.position, realConfig);
65
+ this.buildMixedData(buildOptions);
66
+ return realConfig.position;
67
+ }
68
+ reorderFixedSlots(config) {
69
+ const { position: oldPosition, insertMode } = config;
70
+ if (null == insertMode || 'cover' === insertMode || !this.fixedSlots.has(oldPosition)) return {
71
+ ...config,
72
+ inputPosition: oldPosition
73
+ };
74
+ const position = 'after' === insertMode ? oldPosition + 1 : oldPosition;
75
+ for(let i = position + 1, preItem = this.fixedSlots.get(i - 1), currItem = this.fixedSlots.get(i); preItem; ++i, preItem = currItem, currItem = this.fixedSlots.get(i)){
76
+ preItem.position = i;
77
+ this.fixedSlots.set(i, preItem);
78
+ }
79
+ return {
80
+ ...config,
81
+ position,
82
+ inputPosition: oldPosition
83
+ };
84
+ }
85
+ addFixedSlots(configs, buildOptions) {
86
+ if (!configs.length) return [];
87
+ const positions = this.batchUpdate(()=>configs.map((config)=>this.addFixedSlot(config)));
88
+ this.buildMixedData(buildOptions);
89
+ return positions;
90
+ }
91
+ deleteFixedSlot(position, buildOptions) {
92
+ this.fixedSlots.delete(position);
93
+ this.buildMixedData(buildOptions);
94
+ }
95
+ deleteFixedSlots(positions, buildOptions) {
96
+ if (!positions.length) return;
97
+ this.batchUpdate(()=>positions.forEach((position)=>void this.deleteFixedSlot(position)));
98
+ this.buildMixedData(buildOptions);
99
+ }
100
+ batchUpdate(callback) {
101
+ try {
102
+ this.isBatching = true;
103
+ const result = callback();
104
+ return result;
105
+ } finally{
106
+ this.isBatching = false;
107
+ }
108
+ }
109
+ clearFixedSlots(buildOptions) {
110
+ this.fixedSlots.clear();
111
+ this.buildMixedData(buildOptions);
112
+ }
113
+ appendList(list, buildOptions) {
114
+ if (!list.length) return;
115
+ this.dataList.push(...list);
116
+ this.buildMixedData({
117
+ ...buildOptions,
118
+ lazy: true === (buildOptions || {}).lazy
119
+ });
120
+ }
121
+ clearList() {
122
+ this.dataList.length = 0;
123
+ this.mixedData.length = 0;
124
+ this.dispatch('change', {
125
+ mode: 'clear',
126
+ mixedData: this.getMixedData({
127
+ mode: 'rebuild'
128
+ })
129
+ });
130
+ this.dispatch('clear');
131
+ }
132
+ getMixedData(buildOptions) {
133
+ this.buildMixedData({
134
+ ...buildOptions,
135
+ lazy: false
136
+ });
137
+ return this.mixedData.slice();
138
+ }
139
+ dispatch(name, data) {
140
+ const detail = {
141
+ name: this.options.name,
142
+ ...data
143
+ };
144
+ this.dispatchEvent(new CustomEvent(name, {
145
+ detail
146
+ }));
147
+ if ("u" > typeof window) window.dispatchEvent(new CustomEvent(`[DMM]:${name}`, {
148
+ detail
149
+ }));
150
+ }
151
+ buildMixedData(buildOptions) {
152
+ if (this.isBatching) return;
153
+ const { lazy, mode } = buildOptions || {};
154
+ if ('rebuild' === mode) {
155
+ this.prevDataLength = 0;
156
+ this.lastMixedSlotIdx = -1;
157
+ }
158
+ if (false !== lazy || this.dataList.length <= this.prevDataLength) return;
159
+ let dataStartIdx = this.prevDataLength;
160
+ const dataEndIdx = this.dataList.length;
161
+ const newItemCount = dataEndIdx - dataStartIdx;
162
+ this.prevDataLength = dataEndIdx;
163
+ const isPatchMode = dataStartIdx > 0;
164
+ const filteredSlots = this.sliceSlots(isPatchMode ? this.mixedData.length : this.lastMixedSlotIdx, this.mixedData.length + newItemCount);
165
+ this.lastMixedSlotIdx = filteredSlots.at(-1) ?? this.lastMixedSlotIdx;
166
+ let mixedStartIdx = isPatchMode ? this.mixedData.length : 0;
167
+ this.mixedData.length = (isPatchMode ? newItemCount + mixedStartIdx : dataEndIdx) + filteredSlots.length;
168
+ for(let fpIdx = 0, fpItem = filteredSlots[fpIdx]; dataStartIdx < dataEndIdx; ++mixedStartIdx)if (mixedStartIdx === fpItem) {
169
+ const fixedSlot = this.fixedSlots.get(fpItem);
170
+ this.mixedData[mixedStartIdx] = {
171
+ isFixed: true,
172
+ type: fixedSlot.type,
173
+ data: fixedSlot.data
174
+ };
175
+ fpItem = filteredSlots[++fpIdx];
176
+ } else {
177
+ this.mixedData[mixedStartIdx] = {
178
+ isFixed: false,
179
+ type: 'plain',
180
+ data: this.dataList[dataStartIdx]
181
+ };
182
+ ++dataStartIdx;
183
+ }
184
+ this.dispatch('change', {
185
+ mode: isPatchMode ? 'patch' : 'rebuild',
186
+ mixedData: this.mixedData.slice()
187
+ });
188
+ }
189
+ sliceSlots(startIdx, endIdx = 1 / 0) {
190
+ let prevItem = -2;
191
+ let count = 0;
192
+ return Array.from(this.fixedSlots.keys()).sort((ai, bi)=>ai - bi).filter((item)=>{
193
+ if (item >= startIdx && item < endIdx + count || item - 1 === prevItem) {
194
+ ++count;
195
+ prevItem = item;
196
+ return true;
197
+ }
198
+ return false;
199
+ });
200
+ }
201
+ insertSlot(config) {
202
+ const realPosition = this.addFixedSlot({
203
+ ...config,
204
+ type: SLOT_TYPE.insert
205
+ });
206
+ if (realPosition > this.mixedData.length) return realPosition;
207
+ this.buildMixedData({
208
+ lazy: false,
209
+ mode: 'rebuild'
210
+ });
211
+ return realPosition;
212
+ }
213
+ insertSlots(configs) {
214
+ if (!configs.length) return [];
215
+ const positions = this.batchUpdate(()=>configs.map((config)=>this.insertSlot(config)));
216
+ this.buildMixedData({
217
+ lazy: false,
218
+ mode: 'rebuild'
219
+ });
220
+ return positions;
221
+ }
222
+ }
223
+ function dataMixedManager(options) {
224
+ return new DataMixedManager(options);
225
+ }
226
+ export { dataMixedManager };
@@ -0,0 +1,90 @@
1
+ import type { SLOT_TYPE } from './constants';
2
+ export type SlotType = (typeof SLOT_TYPE)[keyof typeof SLOT_TYPE];
3
+ export interface InputSlotConfig<T> {
4
+ /** 坑位位置(从1开始) */
5
+ position: number;
6
+ /** 定坑数据 */
7
+ data: T;
8
+ /** 插入模式:cover-覆盖,before-在指定位置前插入,after-在指定位置后插入 */
9
+ insertMode?: 'cover' | 'before' | 'after';
10
+ }
11
+ /**
12
+ * 定坑配置接口
13
+ */
14
+ export interface SlotConfig<T> {
15
+ /** 坑位位置(从1开始) */
16
+ position: number;
17
+ /** 定坑数据 */
18
+ data: T;
19
+ /** 定坑类型 */
20
+ type: 'fixed' | 'insert';
21
+ /** 插入模式:cover-覆盖,before-在指定位置前插入,after-在指定位置后插入 */
22
+ insertMode: 'cover' | 'before' | 'after';
23
+ /** 输入位置(用于记录原始输入的位置) */
24
+ inputPosition: number;
25
+ }
26
+ export interface BaseDataItem<T> {
27
+ /** 数据内容 */
28
+ data: T;
29
+ }
30
+ /**
31
+ * 普通数据项接口
32
+ */
33
+ export interface PlainDataItem<T> extends BaseDataItem<T> {
34
+ /** 是否为定坑数据 */
35
+ isFixed: false;
36
+ /** 数据类型 */
37
+ type: 'plain';
38
+ }
39
+ /**
40
+ * 定坑数据项接口
41
+ */
42
+ export interface FixedDataItem<T> extends BaseDataItem<T> {
43
+ /** 是否为定坑数据 */
44
+ isFixed: true;
45
+ /** 定坑类型 */
46
+ type: SlotConfig<T>['type'];
47
+ }
48
+ /**
49
+ * 混合数据项类型
50
+ * 可以是定坑数据项或普通数据项
51
+ */
52
+ export type MixedDataItem<T> = FixedDataItem<T> | PlainDataItem<T>;
53
+ /**
54
+ * 构建选项接口
55
+ */
56
+ export interface BuildOptions {
57
+ /** 是否延迟构建,为 true 时不立即构建混合数据 */
58
+ lazy?: boolean;
59
+ /** 构建模式:rebuild-全量重建,patch-增量更新 */
60
+ mode?: 'rebuild' | 'patch';
61
+ }
62
+ interface DiffEventDetailMap<T> {
63
+ change: {
64
+ mode: NonNullable<BuildOptions['mode']> | 'clear';
65
+ mixedData: MixedDataItem<T>[];
66
+ };
67
+ clear: Record<PropertyKey, never>;
68
+ }
69
+ export interface BaseEventDetail {
70
+ name: string;
71
+ }
72
+ export type EventDetailMap<T> = {
73
+ [K in keyof DiffEventDetailMap<T>]: BaseEventDetail & DiffEventDetailMap<T>[K];
74
+ };
75
+ export type DMMEventHandler<T, K extends keyof EventDetailMap<T>> = ((event: CustomEvent<EventDetailMap<T>[K]>) => void) | {
76
+ handleEvent: (event: CustomEvent<EventDetailMap<T>[K]>) => void;
77
+ };
78
+ export interface DataMixedManagerOptions<T> {
79
+ /** 实例名称 */
80
+ name?: string;
81
+ /** 定坑配置数组 */
82
+ fixedSlots?: InputSlotConfig<T>[];
83
+ /** 普通数据列表 */
84
+ dataList?: T[];
85
+ /** 事件监听 */
86
+ listener?: {
87
+ [K in keyof EventDetailMap<T>]?: DMMEventHandler<T, K>;
88
+ };
89
+ }
90
+ export {};
File without changes
@@ -1,7 +1,12 @@
1
+ export * from './allx';
1
2
  export * from './animation';
3
+ export * from './api-controller';
2
4
  export * from './condition-merge';
3
5
  export * from './create-storage-handler';
4
6
  export * from './data-handler';
7
+ export * from './data-mixed-manager';
5
8
  export * from './throw-error';
9
+ export * from './try-call';
6
10
  export * from './types';
11
+ export * from './utils';
7
12
  export * from './with-resolvers';