@akanjs/store 0.9.47 → 0.9.49
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/cjs/src/baseSt.js +7 -18
- package/cjs/src/index.js +1 -0
- package/cjs/src/storeDecorators.js +180 -192
- package/cjs/src/storeInfo.js +65 -0
- package/esm/src/baseSt.js +8 -21
- package/esm/src/index.js +1 -0
- package/esm/src/storeDecorators.js +182 -196
- package/esm/src/storeInfo.js +46 -0
- package/package.json +1 -1
- package/src/baseSt.d.ts +5 -77
- package/src/index.d.ts +1 -0
- package/src/storeDecorators.d.ts +35 -25
- package/src/storeInfo.d.ts +20 -0
|
@@ -12,19 +12,18 @@ import {
|
|
|
12
12
|
pathSet
|
|
13
13
|
} from "@akanjs/common";
|
|
14
14
|
import {
|
|
15
|
-
|
|
16
|
-
getFieldMetas
|
|
17
|
-
getFullModelRef
|
|
15
|
+
constantInfo,
|
|
16
|
+
getFieldMetas
|
|
18
17
|
} from "@akanjs/constant";
|
|
19
18
|
import { msg } from "@akanjs/dictionary";
|
|
20
19
|
import {
|
|
21
|
-
getGqlOnStorage,
|
|
22
20
|
immerify
|
|
23
21
|
} from "@akanjs/signal";
|
|
24
22
|
import { useEffect, useRef } from "react";
|
|
25
23
|
import { create } from "zustand";
|
|
26
24
|
import { devtools, subscribeWithSelector } from "zustand/middleware";
|
|
27
25
|
import { immer } from "zustand/middleware/immer";
|
|
26
|
+
import { storeInfo } from "./storeInfo";
|
|
28
27
|
const baseSt = {};
|
|
29
28
|
class StoreStorage {
|
|
30
29
|
}
|
|
@@ -37,14 +36,9 @@ const getStoreMeta = (storeName) => {
|
|
|
37
36
|
const setStoreMeta = (storeName, storeMeta) => {
|
|
38
37
|
Reflect.defineMetadata(storeName, storeMeta, StoreStorage.prototype);
|
|
39
38
|
};
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
throw new Error(`storeNames is not defined`);
|
|
44
|
-
return storeNames;
|
|
45
|
-
};
|
|
46
|
-
const createState = (gql) => {
|
|
47
|
-
const [fieldName, className] = [lowerlize(gql.refName), capitalize(gql.refName)];
|
|
39
|
+
const createDatabaseState = (refName) => {
|
|
40
|
+
const cnst = constantInfo.getDatabase(refName);
|
|
41
|
+
const [fieldName, className] = [refName, capitalize(refName)];
|
|
48
42
|
const names = {
|
|
49
43
|
model: fieldName,
|
|
50
44
|
Model: className,
|
|
@@ -54,7 +48,26 @@ const createState = (gql) => {
|
|
|
54
48
|
modelSubmit: `${fieldName}Submit`,
|
|
55
49
|
modelViewAt: `${fieldName}ViewAt`,
|
|
56
50
|
modelModal: `${fieldName}Modal`,
|
|
57
|
-
modelOperation: `${fieldName}Operation
|
|
51
|
+
modelOperation: `${fieldName}Operation`
|
|
52
|
+
};
|
|
53
|
+
const baseState = {
|
|
54
|
+
[names.model]: null,
|
|
55
|
+
[names.modelLoading]: true,
|
|
56
|
+
[names.modelForm]: cnst.getDefault(),
|
|
57
|
+
[names.modelFormLoading]: true,
|
|
58
|
+
[names.modelSubmit]: { disabled: true, loading: false, times: 0 },
|
|
59
|
+
[names.modelViewAt]: /* @__PURE__ */ new Date(0),
|
|
60
|
+
[names.modelModal]: null,
|
|
61
|
+
[names.modelOperation]: "sleep"
|
|
62
|
+
};
|
|
63
|
+
return baseState;
|
|
64
|
+
};
|
|
65
|
+
const createSliceState = (refName, slices) => {
|
|
66
|
+
const cnst = constantInfo.getDatabase(refName);
|
|
67
|
+
const [fieldName, className] = [refName, capitalize(refName)];
|
|
68
|
+
const names = {
|
|
69
|
+
model: fieldName,
|
|
70
|
+
Model: className,
|
|
58
71
|
defaultModel: `default${className}`,
|
|
59
72
|
defaultModelInsight: `default${className}Insight`,
|
|
60
73
|
modelList: `${fieldName}List`,
|
|
@@ -69,17 +82,7 @@ const createState = (gql) => {
|
|
|
69
82
|
queryArgsOfModel: `queryArgsOf${className}`,
|
|
70
83
|
sortOfModel: `sortOf${className}`
|
|
71
84
|
};
|
|
72
|
-
const
|
|
73
|
-
[names.model]: null,
|
|
74
|
-
[names.modelLoading]: true,
|
|
75
|
-
[names.modelForm]: { ...gql[names.defaultModel] },
|
|
76
|
-
[names.modelFormLoading]: true,
|
|
77
|
-
[names.modelSubmit]: { disabled: true, loading: false, times: 0 },
|
|
78
|
-
[names.modelViewAt]: /* @__PURE__ */ new Date(0),
|
|
79
|
-
[names.modelModal]: null,
|
|
80
|
-
[names.modelOperation]: "sleep"
|
|
81
|
-
};
|
|
82
|
-
const sliceState = gql.slices.reduce((acc, { sliceName, defaultArgs }) => {
|
|
85
|
+
const sliceState = slices.reduce((acc, { sliceName, defaultArgs }) => {
|
|
83
86
|
const SliceName = capitalize(sliceName);
|
|
84
87
|
const namesOfSlice = {
|
|
85
88
|
defaultModel: SliceName.replace(names.Model, names.defaultModel),
|
|
@@ -97,13 +100,13 @@ const createState = (gql) => {
|
|
|
97
100
|
sortOfModel: SliceName.replace(names.Model, names.sortOfModel)
|
|
98
101
|
};
|
|
99
102
|
const singleSliceState = {
|
|
100
|
-
[namesOfSlice.defaultModel]:
|
|
103
|
+
[namesOfSlice.defaultModel]: cnst.getDefault(),
|
|
101
104
|
[namesOfSlice.modelList]: new DataList(),
|
|
102
105
|
[namesOfSlice.modelListLoading]: true,
|
|
103
106
|
[namesOfSlice.modelInitList]: new DataList(),
|
|
104
107
|
[namesOfSlice.modelInitAt]: /* @__PURE__ */ new Date(0),
|
|
105
108
|
[namesOfSlice.modelSelection]: new DataList(),
|
|
106
|
-
[namesOfSlice.modelInsight]:
|
|
109
|
+
[namesOfSlice.modelInsight]: cnst.getDefaultInsight(),
|
|
107
110
|
[namesOfSlice.lastPageOfModel]: 1,
|
|
108
111
|
[namesOfSlice.pageOfModel]: 1,
|
|
109
112
|
[namesOfSlice.limitOfModel]: 20,
|
|
@@ -112,20 +115,11 @@ const createState = (gql) => {
|
|
|
112
115
|
};
|
|
113
116
|
return Object.assign(acc, singleSliceState);
|
|
114
117
|
}, {});
|
|
115
|
-
return
|
|
116
|
-
...baseState,
|
|
117
|
-
...sliceState
|
|
118
|
-
};
|
|
118
|
+
return sliceState;
|
|
119
119
|
};
|
|
120
|
-
const
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
return { ...formSetterActions, ...baseActions };
|
|
124
|
-
};
|
|
125
|
-
const makeFormSetter = (gql) => {
|
|
126
|
-
const fileGql = getGqlOnStorage("file");
|
|
127
|
-
const [fieldName, className] = [lowerlize(gql.refName), capitalize(gql.refName)];
|
|
128
|
-
const modelRef = getFullModelRef(gql.refName);
|
|
120
|
+
const makeFormSetter = (refName) => {
|
|
121
|
+
const [fieldName, className] = [refName, capitalize(refName)];
|
|
122
|
+
const modelRef = constantInfo.getDatabase(refName).full;
|
|
129
123
|
const fieldMetas = getFieldMetas(modelRef);
|
|
130
124
|
const names = {
|
|
131
125
|
model: fieldName,
|
|
@@ -190,12 +184,12 @@ const makeFormSetter = (gql) => {
|
|
|
190
184
|
this[namesOfField.subFieldOnModel](index);
|
|
191
185
|
}
|
|
192
186
|
} : {},
|
|
193
|
-
...fieldMeta.
|
|
187
|
+
...fieldMeta.isClass && constantInfo.getRefName(fieldMeta.modelRef) === "file" ? {
|
|
194
188
|
[namesOfField.uploadFieldOnModel]: async function(fileList, index) {
|
|
195
189
|
const form = this.get()[names.modelForm];
|
|
196
190
|
if (!fileList.length)
|
|
197
191
|
return;
|
|
198
|
-
const files = await
|
|
192
|
+
const files = await fetch[names.addModelFiles](
|
|
199
193
|
fileList,
|
|
200
194
|
form.id
|
|
201
195
|
);
|
|
@@ -216,7 +210,7 @@ const makeFormSetter = (gql) => {
|
|
|
216
210
|
files.map((file) => {
|
|
217
211
|
const intervalKey = setInterval(() => {
|
|
218
212
|
void (async () => {
|
|
219
|
-
const currentFile = await
|
|
213
|
+
const currentFile = await fetch.file(file.id);
|
|
220
214
|
if (fieldMeta.isArray)
|
|
221
215
|
this.set((state) => {
|
|
222
216
|
state[names.modelForm][namesOfField.field] = state[names.modelForm][namesOfField.field].map(
|
|
@@ -239,17 +233,14 @@ const makeFormSetter = (gql) => {
|
|
|
239
233
|
}, {});
|
|
240
234
|
return Object.assign(fieldSetAction, baseSetAction);
|
|
241
235
|
};
|
|
242
|
-
const makeActions = (
|
|
243
|
-
const [fieldName, className] = [
|
|
244
|
-
const
|
|
236
|
+
const makeActions = (refName, slices) => {
|
|
237
|
+
const [fieldName, className] = [refName, capitalize(refName)];
|
|
238
|
+
const cnst = constantInfo.getDatabase(refName);
|
|
239
|
+
const modelRef = cnst.full;
|
|
245
240
|
const names = {
|
|
246
241
|
model: fieldName,
|
|
247
242
|
_model: `_${fieldName}`,
|
|
248
243
|
Model: className,
|
|
249
|
-
purifyModel: `purify${className}`,
|
|
250
|
-
crystalizeModel: `crystalize${className}`,
|
|
251
|
-
lightCrystalizeModel: `lightCrystalize${className}`,
|
|
252
|
-
crystalizeInsight: `crystalize${className}Insight`,
|
|
253
244
|
modelOperation: `${fieldName}Operation`,
|
|
254
245
|
defaultModel: `default${className}`,
|
|
255
246
|
modelInsight: `${fieldName}Insight`,
|
|
@@ -306,13 +297,13 @@ const makeActions = (gql) => {
|
|
|
306
297
|
const modelListLoading = currentState[namesOfSlice.modelListLoading];
|
|
307
298
|
const modelInsight = currentState[namesOfSlice.modelInsight];
|
|
308
299
|
const defaultModel = currentState[namesOfSlice.defaultModel];
|
|
309
|
-
const modelInput =
|
|
300
|
+
const modelInput = cnst.purify(modelForm);
|
|
310
301
|
if (!modelInput)
|
|
311
302
|
return;
|
|
312
303
|
this.set({ [names.modelLoading]: true });
|
|
313
|
-
const model = await
|
|
304
|
+
const model = await fetch[names.createModel](modelInput, { onError });
|
|
314
305
|
const newModelList = modelListLoading ? modelList : new DataList([...modelList.slice(0, idx ?? 0), model, ...modelList.slice(idx ?? 0)]);
|
|
315
|
-
const newModelInsight =
|
|
306
|
+
const newModelInsight = cnst.crystalizeInsight({
|
|
316
307
|
...modelInsight,
|
|
317
308
|
count: modelInsight.count + 1
|
|
318
309
|
});
|
|
@@ -337,12 +328,12 @@ const makeActions = (gql) => {
|
|
|
337
328
|
const model = currentState[names.model];
|
|
338
329
|
const modelForm = currentState[names.modelForm];
|
|
339
330
|
const defaultModel = currentState[namesOfSlice.defaultModel];
|
|
340
|
-
const modelInput =
|
|
331
|
+
const modelInput = cnst.purify(modelForm);
|
|
341
332
|
if (!modelInput)
|
|
342
333
|
return;
|
|
343
334
|
if (model?.id === modelForm.id)
|
|
344
335
|
this.set({ [names.modelLoading]: modelForm.id });
|
|
345
|
-
const updatedModel = await
|
|
336
|
+
const updatedModel = await fetch[names.updateModel](modelForm.id, modelInput, {
|
|
346
337
|
onError
|
|
347
338
|
});
|
|
348
339
|
this.set({
|
|
@@ -351,8 +342,8 @@ const makeActions = (gql) => {
|
|
|
351
342
|
[names.modelModal]: modal ?? null,
|
|
352
343
|
...typeof path === "string" && path ? { [path]: updatedModel } : {}
|
|
353
344
|
});
|
|
354
|
-
const updatedLightModel =
|
|
355
|
-
|
|
345
|
+
const updatedLightModel = cnst.lightCrystalize(updatedModel);
|
|
346
|
+
slices.forEach(({ sliceName: sliceName2 }) => {
|
|
356
347
|
const namesOfSlice2 = {
|
|
357
348
|
modelList: sliceName2.replace(names.model, names.modelList),
|
|
358
349
|
modelListLoading: sliceName2.replace(names.model, names.modelListLoading)
|
|
@@ -379,13 +370,13 @@ const makeActions = (gql) => {
|
|
|
379
370
|
const modelList = currentState[namesOfSlice.modelList];
|
|
380
371
|
const modelListLoading = currentState[namesOfSlice.modelListLoading];
|
|
381
372
|
const modelInsight = currentState[namesOfSlice.modelInsight];
|
|
382
|
-
const modelInput =
|
|
373
|
+
const modelInput = cnst.purify(data);
|
|
383
374
|
if (!modelInput)
|
|
384
375
|
return;
|
|
385
376
|
this.set({ [names.modelLoading]: true });
|
|
386
|
-
const model = await
|
|
377
|
+
const model = await fetch[names.createModel](modelInput, { onError });
|
|
387
378
|
const newModelList = modelListLoading ? modelList : new DataList([...modelList.slice(0, idx ?? 0), model, ...modelList.slice(idx ?? 0)]);
|
|
388
|
-
const newModelInsight =
|
|
379
|
+
const newModelInsight = cnst.crystalizeInsight({
|
|
389
380
|
...modelInsight,
|
|
390
381
|
count: modelInsight.count + 1
|
|
391
382
|
});
|
|
@@ -403,19 +394,19 @@ const makeActions = (gql) => {
|
|
|
403
394
|
[names.updateModel]: async function(id, data, { idx, path, modal, sliceName = names.model, onError, onSuccess } = {}) {
|
|
404
395
|
const currentState = this.get();
|
|
405
396
|
const model = currentState[names.model];
|
|
406
|
-
const modelInput =
|
|
397
|
+
const modelInput = cnst.purify(data);
|
|
407
398
|
if (!modelInput)
|
|
408
399
|
return;
|
|
409
400
|
if (model?.id === id)
|
|
410
401
|
this.set({ [names.modelLoading]: id });
|
|
411
|
-
const updatedModel = await
|
|
402
|
+
const updatedModel = await fetch[names.updateModel](id, modelInput, { onError });
|
|
412
403
|
this.set({
|
|
413
404
|
...model?.id === updatedModel.id ? { [names.model]: updatedModel, [names.modelLoading]: false, [names.modelViewAt]: /* @__PURE__ */ new Date() } : {},
|
|
414
405
|
[names.modelModal]: modal ?? null,
|
|
415
406
|
...typeof path === "string" && path ? { [path]: updatedModel } : {}
|
|
416
407
|
});
|
|
417
|
-
const updatedLightModel =
|
|
418
|
-
|
|
408
|
+
const updatedLightModel = cnst.lightCrystalize(updatedModel);
|
|
409
|
+
slices.forEach(({ sliceName: sliceName2 }) => {
|
|
419
410
|
const namesOfSlice = {
|
|
420
411
|
modelList: sliceName2.replace(names.model, names.modelList),
|
|
421
412
|
modelListLoading: sliceName2.replace(names.model, names.modelListLoading)
|
|
@@ -432,12 +423,12 @@ const makeActions = (gql) => {
|
|
|
432
423
|
},
|
|
433
424
|
[names.removeModel]: async function(id, options) {
|
|
434
425
|
const { modal, ...fetchPolicyOptions } = options ?? {};
|
|
435
|
-
const model = await
|
|
426
|
+
const model = await fetch[names.removeModel](
|
|
436
427
|
id,
|
|
437
428
|
fetchPolicyOptions
|
|
438
429
|
);
|
|
439
|
-
const lightModel =
|
|
440
|
-
|
|
430
|
+
const lightModel = cnst.lightCrystalize(model);
|
|
431
|
+
slices.forEach(({ sliceName }) => {
|
|
441
432
|
const namesOfSlice = {
|
|
442
433
|
modelList: sliceName.replace(names.model, names.modelList),
|
|
443
434
|
modelListLoading: sliceName.replace(names.model, names.modelListLoading),
|
|
@@ -454,7 +445,7 @@ const makeActions = (gql) => {
|
|
|
454
445
|
const newModelList = new DataList(modelList);
|
|
455
446
|
if (model.removedAt) {
|
|
456
447
|
newModelList.delete(id);
|
|
457
|
-
const newModelInsight =
|
|
448
|
+
const newModelInsight = cnst.crystalizeInsight({
|
|
458
449
|
...modelInsight,
|
|
459
450
|
count: modelInsight.count - 1
|
|
460
451
|
});
|
|
@@ -479,7 +470,7 @@ const makeActions = (gql) => {
|
|
|
479
470
|
const currentState = this.get();
|
|
480
471
|
const modelForm = currentState[names.modelForm];
|
|
481
472
|
const modelSubmit = currentState[names.modelSubmit];
|
|
482
|
-
const modelInput =
|
|
473
|
+
const modelInput = cnst.purify(modelForm);
|
|
483
474
|
this.set({ [names.modelSubmit]: { ...modelSubmit, disabled: !modelInput || disabled } });
|
|
484
475
|
},
|
|
485
476
|
[names.submitModel]: async function(option) {
|
|
@@ -511,7 +502,7 @@ const makeActions = (gql) => {
|
|
|
511
502
|
[names.editModel]: async function(modelOrId, { modal, onError } = {}) {
|
|
512
503
|
const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
|
|
513
504
|
this.set({ [names.modelFormLoading]: id, [names.modelModal]: modal ?? "edit" });
|
|
514
|
-
const model = await
|
|
505
|
+
const model = await fetch[names.model](id, { onError });
|
|
515
506
|
const modelForm = deepObjectify(model);
|
|
516
507
|
this.set({
|
|
517
508
|
[names.model]: model,
|
|
@@ -526,13 +517,13 @@ const makeActions = (gql) => {
|
|
|
526
517
|
const model = currentState[names.model];
|
|
527
518
|
if (id === model?.id)
|
|
528
519
|
this.set({ modelLoading: id });
|
|
529
|
-
const updatedModel = await
|
|
520
|
+
const updatedModel = await fetch[names.mergeModel](modelOrId, data, options);
|
|
530
521
|
this.set({
|
|
531
522
|
[names.model]: id === model?.id ? updatedModel : model,
|
|
532
523
|
[names.modelLoading]: false
|
|
533
524
|
});
|
|
534
|
-
const updatedLightModel =
|
|
535
|
-
|
|
525
|
+
const updatedLightModel = cnst.lightCrystalize(updatedModel);
|
|
526
|
+
slices.forEach(({ sliceName }) => {
|
|
536
527
|
const namesOfSlice = {
|
|
537
528
|
modelList: sliceName.replace(names.model, names.modelList),
|
|
538
529
|
modelListLoading: sliceName.replace(names.model, names.modelListLoading)
|
|
@@ -549,7 +540,7 @@ const makeActions = (gql) => {
|
|
|
549
540
|
[names.viewModel]: async function(modelOrId, { modal, onError } = {}) {
|
|
550
541
|
const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
|
|
551
542
|
this.set({ [names.modelModal]: modal ?? "view", [names.modelLoading]: id });
|
|
552
|
-
const model = await
|
|
543
|
+
const model = await fetch[names.model](id, { onError });
|
|
553
544
|
this.set({ [names.model]: model, [names.modelViewAt]: /* @__PURE__ */ new Date(), [names.modelLoading]: false });
|
|
554
545
|
},
|
|
555
546
|
[names.setModel]: function(fullOrLightModel) {
|
|
@@ -557,14 +548,14 @@ const makeActions = (gql) => {
|
|
|
557
548
|
const model = currentState[names.model];
|
|
558
549
|
const isFull = fullOrLightModel instanceof modelRef;
|
|
559
550
|
if (isFull) {
|
|
560
|
-
const crystalizedModel =
|
|
551
|
+
const crystalizedModel = cnst.crystalize(fullOrLightModel);
|
|
561
552
|
this.set({ [names.model]: crystalizedModel });
|
|
562
553
|
} else if (model?.id === fullOrLightModel.id) {
|
|
563
|
-
const crystalizedModel =
|
|
554
|
+
const crystalizedModel = cnst.crystalize({ ...model, ...fullOrLightModel });
|
|
564
555
|
this.set({ [names.model]: crystalizedModel });
|
|
565
556
|
}
|
|
566
|
-
const lightModel =
|
|
567
|
-
|
|
557
|
+
const lightModel = cnst.lightCrystalize(fullOrLightModel);
|
|
558
|
+
slices.forEach(({ sliceName }) => {
|
|
568
559
|
const namesOfSlice = {
|
|
569
560
|
modelList: sliceName.replace(names.model, names.modelList),
|
|
570
561
|
modelListLoading: sliceName.replace(names.model, names.modelListLoading)
|
|
@@ -588,7 +579,7 @@ const makeActions = (gql) => {
|
|
|
588
579
|
return model ?? null;
|
|
589
580
|
}
|
|
590
581
|
};
|
|
591
|
-
const sliceAction =
|
|
582
|
+
const sliceAction = slices.reduce((acc, { sliceName, argLength }) => {
|
|
592
583
|
const SliceName = capitalize(sliceName);
|
|
593
584
|
const namesOfSlice = {
|
|
594
585
|
defaultModel: SliceName.replace(names.Model, names.defaultModel),
|
|
@@ -617,7 +608,7 @@ const makeActions = (gql) => {
|
|
|
617
608
|
const initArgLength = Math.min(args.length, argLength);
|
|
618
609
|
const initForm = { invalidate: false, ...args[argLength] ?? {} };
|
|
619
610
|
const queryArgs = new Array(initArgLength).fill(null).map((_, i) => args[i]);
|
|
620
|
-
const defaultModel = immerify(modelRef, { ...
|
|
611
|
+
const defaultModel = immerify(modelRef, { ...cnst.getDefault(), ...initForm.default ?? {} });
|
|
621
612
|
this.set({ [names.defaultModel]: defaultModel });
|
|
622
613
|
await this[namesOfSlice.refreshModel](
|
|
623
614
|
...initArgLength === argLength ? [...queryArgs, initForm] : queryArgs
|
|
@@ -648,14 +639,14 @@ const makeActions = (gql) => {
|
|
|
648
639
|
else
|
|
649
640
|
this.set({ [namesOfSlice.modelListLoading]: true });
|
|
650
641
|
const [modelDataList, modelInsight] = await Promise.all([
|
|
651
|
-
|
|
642
|
+
fetch[namesOfSlice.modelList](
|
|
652
643
|
...queryArgs,
|
|
653
644
|
(page - 1) * limit,
|
|
654
645
|
limit,
|
|
655
646
|
sort,
|
|
656
647
|
{ onError: initForm.onError }
|
|
657
648
|
),
|
|
658
|
-
|
|
649
|
+
fetch[namesOfSlice.modelInsight](...queryArgs, {
|
|
659
650
|
onError: initForm.onError
|
|
660
651
|
})
|
|
661
652
|
]);
|
|
@@ -697,7 +688,7 @@ const makeActions = (gql) => {
|
|
|
697
688
|
if (pageOfModel === page)
|
|
698
689
|
return;
|
|
699
690
|
this.set({ [namesOfSlice.modelListLoading]: true });
|
|
700
|
-
const modelDataList = await
|
|
691
|
+
const modelDataList = await fetch[namesOfSlice.modelList](
|
|
701
692
|
...queryArgsOfModel,
|
|
702
693
|
(page - 1) * limitOfModel,
|
|
703
694
|
limitOfModel,
|
|
@@ -721,7 +712,7 @@ const makeActions = (gql) => {
|
|
|
721
712
|
if (pageOfModel === page)
|
|
722
713
|
return;
|
|
723
714
|
const addFront = page < pageOfModel;
|
|
724
|
-
const modelDataList = await
|
|
715
|
+
const modelDataList = await fetch[namesOfSlice.modelList](
|
|
725
716
|
...queryArgsOfModel,
|
|
726
717
|
(page - 1) * limitOfModel,
|
|
727
718
|
limitOfModel,
|
|
@@ -744,7 +735,7 @@ const makeActions = (gql) => {
|
|
|
744
735
|
return;
|
|
745
736
|
const skip = (pageOfModel - 1) * limitOfModel;
|
|
746
737
|
const page = Math.max(Math.floor((skip - 1) / limit) + 1, 1);
|
|
747
|
-
const modelDataList = await
|
|
738
|
+
const modelDataList = await fetch[namesOfSlice.modelList](
|
|
748
739
|
...queryArgsOfModel,
|
|
749
740
|
(page - 1) * limit,
|
|
750
741
|
limit,
|
|
@@ -772,14 +763,17 @@ const makeActions = (gql) => {
|
|
|
772
763
|
}
|
|
773
764
|
this.set({ [namesOfSlice.modelListLoading]: true });
|
|
774
765
|
const [modelDataList, modelInsight] = await Promise.all([
|
|
775
|
-
|
|
766
|
+
fetch[namesOfSlice.modelList](
|
|
776
767
|
...queryArgs,
|
|
777
768
|
0,
|
|
778
769
|
limitOfModel,
|
|
779
770
|
sortOfModel,
|
|
780
771
|
options
|
|
781
772
|
),
|
|
782
|
-
|
|
773
|
+
fetch[namesOfSlice.modelInsight](
|
|
774
|
+
...queryArgs,
|
|
775
|
+
options
|
|
776
|
+
)
|
|
783
777
|
]);
|
|
784
778
|
const modelList = new DataList(modelDataList);
|
|
785
779
|
this.set({
|
|
@@ -800,7 +794,7 @@ const makeActions = (gql) => {
|
|
|
800
794
|
if (sortOfModel === sort)
|
|
801
795
|
return;
|
|
802
796
|
this.set({ [namesOfSlice.modelListLoading]: true });
|
|
803
|
-
const modelDataList = await
|
|
797
|
+
const modelDataList = await fetch[namesOfSlice.modelList](
|
|
804
798
|
...queryArgsOfModel,
|
|
805
799
|
0,
|
|
806
800
|
limitOfModel,
|
|
@@ -820,27 +814,25 @@ const makeActions = (gql) => {
|
|
|
820
814
|
}, {});
|
|
821
815
|
return { ...baseAction, ...sliceAction };
|
|
822
816
|
};
|
|
817
|
+
function store(refNameOrGql, state, ...libStores) {
|
|
818
|
+
const storeRef = libStores.at(0) ?? class StateStore {
|
|
819
|
+
};
|
|
820
|
+
storeInfo.setState(storeRef, state);
|
|
821
|
+
return storeRef;
|
|
822
|
+
}
|
|
823
823
|
const stateOf = (gql, state) => {
|
|
824
|
-
const applyState =
|
|
825
|
-
const applyAction =
|
|
826
|
-
setStoreMeta(gql.refName, {
|
|
827
|
-
refName: gql.refName,
|
|
828
|
-
useKeys: Object.keys(applyState),
|
|
829
|
-
doKeys: Object.keys(applyAction),
|
|
830
|
-
slices: gql.slices
|
|
831
|
-
});
|
|
824
|
+
const applyState = state;
|
|
825
|
+
const applyAction = {};
|
|
832
826
|
const applyStore = { ...applyState, ...applyAction };
|
|
833
827
|
class StateStore {
|
|
834
828
|
get;
|
|
835
829
|
set;
|
|
836
830
|
pick;
|
|
837
831
|
}
|
|
838
|
-
Object.
|
|
839
|
-
(key) => Object.defineProperty(StateStore.prototype, key, { value: applyStore[key] })
|
|
840
|
-
);
|
|
832
|
+
Object.assign(StateStore.prototype, applyStore);
|
|
841
833
|
return StateStore;
|
|
842
834
|
};
|
|
843
|
-
const
|
|
835
|
+
const serviceStateOf = (refName, state) => {
|
|
844
836
|
const applyState = state;
|
|
845
837
|
setStoreMeta(refName, { refName, useKeys: Object.keys(applyState), doKeys: [], slices: [] });
|
|
846
838
|
class StateStore {
|
|
@@ -848,19 +840,12 @@ const scalarStateOf = (refName, state) => {
|
|
|
848
840
|
Object.keys(applyState).forEach(
|
|
849
841
|
(key) => Object.defineProperty(StateStore.prototype, key, { value: applyState[key] })
|
|
850
842
|
);
|
|
843
|
+
storeInfo.setRefNameTemp(refName, StateStore);
|
|
851
844
|
return StateStore;
|
|
852
845
|
};
|
|
853
|
-
const
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
return function(target) {
|
|
857
|
-
const customDoKeys = Object.getOwnPropertyNames(target.prototype).filter((key) => key !== "constructor");
|
|
858
|
-
setStoreMeta(refName, { ...storeMeta, doKeys: [...storeMeta.doKeys, ...customDoKeys] });
|
|
859
|
-
};
|
|
860
|
-
};
|
|
861
|
-
const createSelectors = (_store, store = {}) => {
|
|
862
|
-
store.get = _store.getState;
|
|
863
|
-
store.set = (s) => {
|
|
846
|
+
const createSelectors = (_store, store2 = {}, slices) => {
|
|
847
|
+
store2.get = _store.getState;
|
|
848
|
+
store2.set = (s) => {
|
|
864
849
|
if (typeof s === "function")
|
|
865
850
|
_store.setState((st) => {
|
|
866
851
|
s(st);
|
|
@@ -868,36 +853,36 @@ const createSelectors = (_store, store = {}) => {
|
|
|
868
853
|
else
|
|
869
854
|
_store.setState(s);
|
|
870
855
|
};
|
|
871
|
-
|
|
872
|
-
const state =
|
|
873
|
-
|
|
856
|
+
store2.sel = (selectFn, equals) => _store(selectFn, equals);
|
|
857
|
+
const state = store2.get();
|
|
858
|
+
store2.sub = _store.subscribe;
|
|
874
859
|
const useReference = (selectFn) => {
|
|
875
|
-
const ref = useRef(selectFn(
|
|
860
|
+
const ref = useRef(selectFn(store2.get()));
|
|
876
861
|
useEffect(() => {
|
|
877
|
-
return
|
|
862
|
+
return store2.sub(selectFn, (val) => ref.current = val);
|
|
878
863
|
}, []);
|
|
879
864
|
return ref;
|
|
880
865
|
};
|
|
881
|
-
|
|
882
|
-
const existingUse =
|
|
883
|
-
const existingDo =
|
|
884
|
-
const existingSlice =
|
|
866
|
+
store2.ref = useReference;
|
|
867
|
+
const existingUse = store2.use;
|
|
868
|
+
const existingDo = store2.do;
|
|
869
|
+
const existingSlice = store2.slice;
|
|
885
870
|
if (!existingUse)
|
|
886
|
-
Object.assign(
|
|
871
|
+
Object.assign(store2, { use: {} });
|
|
887
872
|
if (!existingDo)
|
|
888
|
-
Object.assign(
|
|
873
|
+
Object.assign(store2, { do: {} });
|
|
889
874
|
if (!existingSlice)
|
|
890
|
-
Object.assign(
|
|
875
|
+
Object.assign(store2, { slice: {} });
|
|
891
876
|
for (const k of Object.keys(state)) {
|
|
892
877
|
if (typeof state[k] !== "function") {
|
|
893
|
-
|
|
878
|
+
store2.use[k] = () => store2.sel((s) => s[k]);
|
|
894
879
|
const setKey = `set${capitalize(k)}`;
|
|
895
880
|
if (!state[setKey])
|
|
896
|
-
|
|
897
|
-
|
|
881
|
+
store2.do[setKey] = (value) => {
|
|
882
|
+
store2.set({ [k]: value });
|
|
898
883
|
};
|
|
899
884
|
} else {
|
|
900
|
-
|
|
885
|
+
store2.do[k] = async (...args) => {
|
|
901
886
|
try {
|
|
902
887
|
Logger.verbose(`${k} action loading...`);
|
|
903
888
|
const start = Date.now();
|
|
@@ -912,9 +897,8 @@ const createSelectors = (_store, store = {}) => {
|
|
|
912
897
|
};
|
|
913
898
|
}
|
|
914
899
|
}
|
|
915
|
-
const
|
|
916
|
-
|
|
917
|
-
const [fieldName, className] = [lowerlize(storeName), capitalize(storeName)];
|
|
900
|
+
for (const slice of slices) {
|
|
901
|
+
const [fieldName, className] = [slice.refName, capitalize(slice.refName)];
|
|
918
902
|
const names = {
|
|
919
903
|
model: fieldName,
|
|
920
904
|
Model: className,
|
|
@@ -939,55 +923,52 @@ const createSelectors = (_store, store = {}) => {
|
|
|
939
923
|
setSortOfModel: `setSortOf${className}`,
|
|
940
924
|
lastPageOfModel: `lastPageOf${className}`
|
|
941
925
|
};
|
|
942
|
-
const
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
Object.keys(namesOfSliceAction).forEach((key) => {
|
|
972
|
-
targetSlice.do[names[key]] = store.do[namesOfSliceAction[key]];
|
|
973
|
-
});
|
|
974
|
-
Object.keys(namesOfSliceState).map((key) => {
|
|
975
|
-
targetSlice.use[names[key]] = store.use[namesOfSliceState[key]];
|
|
976
|
-
targetSlice.do[`set${capitalize(names[key])}`] = store.do[`set${capitalize(namesOfSliceState[key])}`];
|
|
977
|
-
});
|
|
978
|
-
targetSlice.get = () => {
|
|
979
|
-
const state2 = store.get();
|
|
980
|
-
const stateOfSlice = Object.fromEntries(
|
|
981
|
-
Object.entries(namesOfSliceState).map(([key, value]) => [names[key], state2[value]])
|
|
982
|
-
);
|
|
983
|
-
return stateOfSlice;
|
|
984
|
-
};
|
|
985
|
-
targetSlice.sliceName = sliceName;
|
|
986
|
-
targetSlice.refName = refName;
|
|
987
|
-
targetSlice.argLength = argLength;
|
|
926
|
+
const SliceName = capitalize(slice.sliceName);
|
|
927
|
+
const namesOfSliceState = {
|
|
928
|
+
defaultModel: SliceName.replace(names.Model, names.defaultModel),
|
|
929
|
+
modelInitList: SliceName.replace(names.Model, names.modelInitList),
|
|
930
|
+
modelInsight: slice.sliceName.replace(names.model, names.modelInsight),
|
|
931
|
+
modelList: slice.sliceName.replace(names.model, names.modelList),
|
|
932
|
+
modelListLoading: slice.sliceName.replace(names.model, names.modelListLoading),
|
|
933
|
+
modelInitAt: SliceName.replace(names.Model, names.modelInitAt),
|
|
934
|
+
lastPageOfModel: SliceName.replace(names.Model, names.lastPageOfModel),
|
|
935
|
+
pageOfModel: SliceName.replace(names.Model, names.pageOfModel),
|
|
936
|
+
limitOfModel: SliceName.replace(names.Model, names.limitOfModel),
|
|
937
|
+
queryArgsOfModel: SliceName.replace(names.Model, names.queryArgsOfModel),
|
|
938
|
+
sortOfModel: SliceName.replace(names.Model, names.sortOfModel),
|
|
939
|
+
modelSelection: SliceName.replace(names.Model, names.modelSelection)
|
|
940
|
+
};
|
|
941
|
+
const namesOfSliceAction = {
|
|
942
|
+
initModel: SliceName.replace(names.Model, names.initModel),
|
|
943
|
+
refreshModel: SliceName.replace(names.Model, names.refreshModel),
|
|
944
|
+
selectModel: SliceName.replace(names.Model, names.selectModel),
|
|
945
|
+
setPageOfModel: SliceName.replace(names.Model, names.setPageOfModel),
|
|
946
|
+
addPageOfModel: SliceName.replace(names.Model, names.addPageOfModel),
|
|
947
|
+
setLimitOfModel: SliceName.replace(names.Model, names.setLimitOfModel),
|
|
948
|
+
setQueryArgsOfModel: SliceName.replace(names.Model, names.setQueryArgsOfModel),
|
|
949
|
+
setSortOfModel: SliceName.replace(names.Model, names.setSortOfModel)
|
|
950
|
+
};
|
|
951
|
+
store2.slice[slice.sliceName] = { do: {}, use: {} };
|
|
952
|
+
const targetSlice = store2.slice[slice.sliceName];
|
|
953
|
+
Object.keys(namesOfSliceAction).forEach((key) => {
|
|
954
|
+
targetSlice.do[names[key]] = store2.do[namesOfSliceAction[key]];
|
|
988
955
|
});
|
|
956
|
+
Object.keys(namesOfSliceState).map((key) => {
|
|
957
|
+
targetSlice.use[names[key]] = store2.use[namesOfSliceState[key]];
|
|
958
|
+
targetSlice.do[`set${capitalize(names[key])}`] = store2.do[`set${capitalize(namesOfSliceState[key])}`];
|
|
959
|
+
});
|
|
960
|
+
targetSlice.get = () => {
|
|
961
|
+
const state2 = store2.get();
|
|
962
|
+
const stateOfSlice = Object.fromEntries(
|
|
963
|
+
Object.entries(namesOfSliceState).map(([key, value]) => [names[key], state2[value]])
|
|
964
|
+
);
|
|
965
|
+
return stateOfSlice;
|
|
966
|
+
};
|
|
967
|
+
targetSlice.sliceName = slice.sliceName;
|
|
968
|
+
targetSlice.refName = slice.refName;
|
|
969
|
+
targetSlice.argLength = slice.argLength;
|
|
989
970
|
}
|
|
990
|
-
return
|
|
971
|
+
return store2;
|
|
991
972
|
};
|
|
992
973
|
const makePicker = (set, get) => (...fields) => {
|
|
993
974
|
const state = get();
|
|
@@ -1004,28 +985,39 @@ const makePicker = (set, get) => (...fields) => {
|
|
|
1004
985
|
}
|
|
1005
986
|
return ret;
|
|
1006
987
|
};
|
|
1007
|
-
const makeStore = (st,
|
|
1008
|
-
if (library)
|
|
1009
|
-
return st;
|
|
988
|
+
const makeStore = (st, signals) => {
|
|
1010
989
|
const zustandStore = create(
|
|
1011
990
|
devtools(
|
|
1012
991
|
subscribeWithSelector(
|
|
1013
992
|
immer((set, get) => {
|
|
1014
|
-
const
|
|
993
|
+
const store2 = {};
|
|
1015
994
|
const pick = makePicker(set, get);
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
995
|
+
const sliceSet = new Map(signals.map((signal) => [signal.refName, signal.slices]));
|
|
996
|
+
storeInfo.store.forEach((storeRef, refName) => {
|
|
997
|
+
const state = storeInfo.getState(storeRef);
|
|
998
|
+
const action = storeInfo.getAction(storeRef);
|
|
999
|
+
const isDatabase = constantInfo.database.has(refName);
|
|
1000
|
+
const slices = sliceSet.get(refName) ?? [];
|
|
1001
|
+
Object.assign(
|
|
1002
|
+
store2,
|
|
1003
|
+
state,
|
|
1004
|
+
action,
|
|
1005
|
+
...isDatabase ? [
|
|
1006
|
+
createDatabaseState(refName),
|
|
1007
|
+
createSliceState(refName, slices),
|
|
1008
|
+
makeFormSetter(refName),
|
|
1009
|
+
makeActions(refName, slices)
|
|
1010
|
+
] : []
|
|
1011
|
+
);
|
|
1020
1012
|
});
|
|
1021
|
-
Object.assign(
|
|
1022
|
-
return
|
|
1013
|
+
Object.assign(store2, { set, get, pick });
|
|
1014
|
+
return store2;
|
|
1023
1015
|
})
|
|
1024
1016
|
),
|
|
1025
1017
|
{ name: "root", anonymousActionType: "root", type: "root" }
|
|
1026
1018
|
)
|
|
1027
1019
|
);
|
|
1028
|
-
return createSelectors(zustandStore, st);
|
|
1020
|
+
return createSelectors(zustandStore, st, signals.map((signal) => signal.slices).flat());
|
|
1029
1021
|
};
|
|
1030
1022
|
const MixStore = (...stores) => {
|
|
1031
1023
|
if (stores.length === 0)
|
|
@@ -1035,9 +1027,6 @@ const MixStore = (...stores) => {
|
|
|
1035
1027
|
applyMixins(Mix, stores);
|
|
1036
1028
|
return Mix;
|
|
1037
1029
|
};
|
|
1038
|
-
const rootStoreOf = (store) => {
|
|
1039
|
-
return Object.getPrototypeOf(store);
|
|
1040
|
-
};
|
|
1041
1030
|
const Toast = ({ root, duration = 3 } = {}) => {
|
|
1042
1031
|
return function(target, key, descriptor) {
|
|
1043
1032
|
const originMethod = descriptor.value;
|
|
@@ -1057,13 +1046,10 @@ const Toast = ({ root, duration = 3 } = {}) => {
|
|
|
1057
1046
|
};
|
|
1058
1047
|
export {
|
|
1059
1048
|
MixStore,
|
|
1060
|
-
Store,
|
|
1061
1049
|
Toast,
|
|
1062
1050
|
baseSt,
|
|
1063
|
-
|
|
1064
|
-
createState,
|
|
1051
|
+
getStoreMeta,
|
|
1065
1052
|
makeStore,
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
stateOf
|
|
1053
|
+
setStoreMeta,
|
|
1054
|
+
store
|
|
1069
1055
|
};
|