akanjs 3.2.0-rc.2 → 3.2.0-rc.4
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/ARCHITECTURE.md +3 -3
- package/CHANGELOG.md +105 -1
- package/client/csrTypes.ts +13 -1
- package/client/frameConfig.ts +4 -2
- package/client/route/RouteHead.ts +11 -2
- package/client/router.ts +8 -4
- package/client/rscNavigation.ts +2 -3
- package/client/rscRoutePatch.ts +42 -0
- package/common/akanGraph.ts +1 -1
- package/common/types.ts +5 -0
- package/dictionary/base.dictionary.ts +1 -0
- package/fetch/client/fetchClient.ts +11 -16
- package/fetch/client/fetchHandle.ts +11 -1
- package/fetch/client/inflightQueries.ts +17 -0
- package/fetch/index.ts +1 -0
- package/fetch/staleReads.ts +27 -0
- package/fetch/streamedRead.ts +23 -0
- package/package.json +1 -1
- package/server/akanServer.ts +1 -1
- package/server/artifact/builderRpc.ts +22 -7
- package/server/artifact/ipcTypes.ts +9 -3
- package/server/devtools/README.md +4 -3
- package/server/devtools/devtoolsRouter.ts +9 -5
- package/server/devtools/studio/studioRoute.ts +18 -11
- package/server/head.tsx +4 -19
- package/server/routeElementComposer.tsx +86 -3
- package/server/routeState.ts +5 -1
- package/server/routeTreeBuilder.ts +1 -0
- package/server/rscClient.tsx +118 -21
- package/server/rscClientChunks.ts +62 -0
- package/server/rscClientFetch.ts +6 -1
- package/server/rscHeadPatch.ts +17 -56
- package/server/rscParkedRedirect.ts +23 -0
- package/server/rscPatchPlanner.ts +220 -0
- package/server/rscPrefetchCache.ts +39 -17
- package/server/rscResponse.ts +78 -0
- package/server/rscSegmentOutlet.tsx +24 -5
- package/server/rscSegmentOutletReference.ts +10 -12
- package/server/rscWorker.tsx +109 -154
- package/server/rscWorkerCache.ts +56 -0
- package/server/rscWorkerHost.ts +6 -8
- package/server/ssrTypes.ts +9 -0
- package/server/webRouter.ts +12 -89
- package/store/action.ts +282 -299
- package/store/actionErrorKey.ts +9 -0
- package/store/draftFlush.ts +5 -0
- package/store/draftStore.ts +2 -0
- package/store/formDrafts.ts +115 -0
- package/store/modelListPatch.ts +53 -0
- package/store/modelWriteGuard.ts +91 -0
- package/store/rootStore.ts +2 -0
- package/store/storeInstance.ts +9 -10
- package/store/uploadPoll.ts +52 -0
- package/store/withSelector.ts +6 -4
- package/types/client/csrTypes.d.ts +13 -1
- package/types/client/route/RouteHead.d.ts +5 -1
- package/types/client/router.d.ts +3 -2
- package/types/client/rscNavigation.d.ts +2 -2
- package/types/client/rscRoutePatch.d.ts +22 -0
- package/types/common/akanGraph.d.ts +1 -1
- package/types/common/types.d.ts +5 -0
- package/types/dictionary/base.dictionary.d.ts +1 -1
- package/types/dictionary/dictionary.d.ts +8 -8
- package/types/fetch/client/fetchHandle.d.ts +1 -0
- package/types/fetch/client/inflightQueries.d.ts +2 -0
- package/types/fetch/index.d.ts +1 -0
- package/types/fetch/staleReads.d.ts +8 -0
- package/types/fetch/streamedRead.d.ts +8 -0
- package/types/server/artifact/builderRpc.d.ts +8 -0
- package/types/server/artifact/ipcTypes.d.ts +8 -0
- package/types/server/devtools/devtoolsRouter.d.ts +4 -1
- package/types/server/devtools/studio/studioRoute.d.ts +2 -1
- package/types/server/head.d.ts +2 -2
- package/types/server/logging/rotatingLogWriter.d.ts +1 -1
- package/types/server/routeElementComposer.d.ts +24 -1
- package/types/server/routeState.d.ts +2 -0
- package/types/server/rscClient.d.ts +1 -0
- package/types/server/rscClientChunks.d.ts +10 -0
- package/types/server/rscHeadPatch.d.ts +5 -4
- package/types/server/rscParkedRedirect.d.ts +12 -0
- package/types/server/rscPatchPlanner.d.ts +49 -0
- package/types/server/rscPrefetchCache.d.ts +2 -2
- package/types/server/rscResponse.d.ts +9 -0
- package/types/server/rscSegmentOutlet.d.ts +5 -0
- package/types/server/rscSegmentOutletReference.d.ts +2 -1
- package/types/server/rscWorkerCache.d.ts +33 -1
- package/types/server/rscWorkerHost.d.ts +2 -6
- package/types/server/ssrTypes.d.ts +8 -0
- package/types/server/webRouter.d.ts +1 -7
- package/types/store/action.d.ts +10 -6
- package/types/store/actionErrorKey.d.ts +1 -0
- package/types/store/baseSt.d.ts +1 -295
- package/types/store/draftFlush.d.ts +4 -0
- package/types/store/formDrafts.d.ts +21 -0
- package/types/store/modelListPatch.d.ts +34 -0
- package/types/store/modelWriteGuard.d.ts +45 -0
- package/types/store/rootStore.d.ts +6 -0
- package/types/store/uploadPoll.d.ts +16 -0
- package/types/store/withSelector.d.ts +1 -1
- package/types/ui/Link/inputModality.d.ts +5 -0
- package/types/ui/Link/viewportPrefetch.d.ts +4 -0
- package/types/ui/Load/Edit_Client.d.ts +2 -0
- package/types/ui/Load/Pagination.d.ts +4 -1
- package/types/ui/Load/Stream.d.ts +7 -1
- package/types/ui/Load/StreamBoundary.d.ts +20 -0
- package/types/ui/Load/Units.d.ts +2 -0
- package/types/ui/Load/View.d.ts +2 -0
- package/types/webkit/nativeCore.d.ts +1 -0
- package/types/webkit/nativePurchase.d.ts +2 -1
- package/types/webkit/useFrameRuntime.d.ts +1 -0
- package/types/webkit/useGeoLocation.d.ts +1 -1
- package/types/webkit/usePurchase.d.ts +1 -1
- package/types/webkit/useTransitionStore.d.ts +1 -0
- package/ui/Button.tsx +1 -0
- package/ui/Link/inputModality.ts +21 -0
- package/ui/Link/prefetchIntent.ts +7 -25
- package/ui/Link/viewportPrefetch.ts +30 -0
- package/ui/Load/Edit_Client.tsx +7 -1
- package/ui/Load/Pagination.tsx +10 -2
- package/ui/Load/Stream.tsx +24 -4
- package/ui/Load/StreamBoundary.tsx +70 -0
- package/ui/Load/Units.tsx +3 -0
- package/ui/Load/View.tsx +3 -0
- package/ui/System/Client.tsx +28 -7
- package/webkit/nativeCore.ts +1 -0
- package/webkit/nativePurchase.ts +6 -3
- package/webkit/useCamera.tsx +22 -21
- package/webkit/useGeoLocation.tsx +4 -1
- package/webkit/usePurchase.tsx +4 -2
- package/webkit/useTransitionStore.ts +13 -0
package/store/action.ts
CHANGED
|
@@ -23,7 +23,6 @@ import {
|
|
|
23
23
|
MAX_PAGE_SIZE,
|
|
24
24
|
type ProtoFile,
|
|
25
25
|
type ProtoLightFile,
|
|
26
|
-
withSharedInstances,
|
|
27
26
|
} from "akanjs/constant";
|
|
28
27
|
import type { BaseFilterSortKey, ExtractSort, FilterInstance } from "akanjs/document";
|
|
29
28
|
import { type FetchInitForm, type FetchProxy, restoreRemoteError } from "akanjs/fetch";
|
|
@@ -41,17 +40,22 @@ import type {
|
|
|
41
40
|
SlceDbSort,
|
|
42
41
|
SliceCls,
|
|
43
42
|
} from "akanjs/signal";
|
|
43
|
+
import { produce } from "immer";
|
|
44
44
|
import { expandQueryArgs, normalizeQueryArgs } from "../fetch/client/sliceQueryArgs";
|
|
45
45
|
import { tagAction } from "./actionTag";
|
|
46
|
-
import {
|
|
46
|
+
import { makeFormDrafts } from "./formDrafts";
|
|
47
47
|
import { type LiveSortableRow, livePlacementIndex } from "./liveInsert";
|
|
48
|
+
import { makeModelListPatches } from "./modelListPatch";
|
|
49
|
+
import { makeModelWriteGuard } from "./modelWriteGuard";
|
|
48
50
|
import { SliceRequest } from "./SliceRequest";
|
|
49
51
|
import { servedEndpointOf } from "./servedEndpointOf";
|
|
50
52
|
import { sliceKeysOf } from "./sliceKeys";
|
|
51
53
|
import type { DraftState } from "./state";
|
|
52
54
|
import type { SetGet, StoreSliceArgs, StoreSliceMap, StoreSliceSuffixCap } from "./types";
|
|
55
|
+
import { pollUploadedFiles, releasedUpload, type Uploading } from "./uploadPoll";
|
|
53
56
|
|
|
54
57
|
type _SliceMap<S extends SliceCls> = StoreSliceMap<S>;
|
|
58
|
+
type StateRecord = { [key: string]: unknown };
|
|
55
59
|
type _ActionRefName<S extends SliceCls> = SlceCnstRefName<S>;
|
|
56
60
|
type _ActionCap<S extends SliceCls> = SlceCnstCapitalizedRefName<S>;
|
|
57
61
|
type _ActionInput<S extends SliceCls> = SlceCnstInput<S>;
|
|
@@ -66,14 +70,15 @@ type SliceRefreshForm<S extends SliceCls, Suffix extends keyof _SliceMap<S>> = S
|
|
|
66
70
|
queryArgs?: StoreSliceArgs<S, Suffix>;
|
|
67
71
|
};
|
|
68
72
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
export interface ErrorReportOption {
|
|
74
|
+
/** Called with the failed request's dictionary key, as a `Button`'s `onError` takes it; the action still rejects. */
|
|
75
|
+
onError?: (key: string) => void;
|
|
76
|
+
}
|
|
77
|
+
type ReportingFetchPolicy = Omit<FetchPolicy, "onError"> & ErrorReportOption;
|
|
78
|
+
export interface CreateOption<Full extends { id: string }> extends ErrorReportOption {
|
|
73
79
|
path?: string;
|
|
74
80
|
modal?: string;
|
|
75
81
|
sliceName?: string;
|
|
76
|
-
onError?: (e: string) => void;
|
|
77
82
|
onSuccess?: (model: Full) => void | Promise<void>;
|
|
78
83
|
}
|
|
79
84
|
export interface NewOption {
|
|
@@ -109,7 +114,7 @@ type BaseAction<
|
|
|
109
114
|
} & {
|
|
110
115
|
[K in `remove${_CapitalizedRefName}`]: (
|
|
111
116
|
id: string,
|
|
112
|
-
options?:
|
|
117
|
+
options?: ReportingFetchPolicy & { modal?: string | null },
|
|
113
118
|
) => Promise<void>;
|
|
114
119
|
} & {
|
|
115
120
|
[K in `check${_CapitalizedRefName}Submitable`]: (disabled?: boolean) => Promise<void>;
|
|
@@ -118,7 +123,10 @@ type BaseAction<
|
|
|
118
123
|
} & {
|
|
119
124
|
[K in `new${_CapitalizedRefName}`]: (partial?: PartialOrNull<Full>, options?: NewOption) => void;
|
|
120
125
|
} & {
|
|
121
|
-
[K in `edit${_CapitalizedRefName}`]: (
|
|
126
|
+
[K in `edit${_CapitalizedRefName}`]: (
|
|
127
|
+
model: Full | string,
|
|
128
|
+
options?: EditOption & ReportingFetchPolicy,
|
|
129
|
+
) => Promise<void>;
|
|
122
130
|
} & {
|
|
123
131
|
[K in `load${_CapitalizedRefName}FormDraft`]: (draftScope?: string) => Promise<void>;
|
|
124
132
|
} & {
|
|
@@ -129,12 +137,12 @@ type BaseAction<
|
|
|
129
137
|
[K in `merge${_CapitalizedRefName}`]: (
|
|
130
138
|
model: Full | string,
|
|
131
139
|
data: Partial<Full>,
|
|
132
|
-
options?:
|
|
140
|
+
options?: ReportingFetchPolicy,
|
|
133
141
|
) => Promise<void>;
|
|
134
142
|
} & {
|
|
135
143
|
[K in `view${_CapitalizedRefName}`]: (
|
|
136
144
|
model: Full | string,
|
|
137
|
-
options?: { modal?: string | null } &
|
|
145
|
+
options?: { modal?: string | null } & ReportingFetchPolicy,
|
|
138
146
|
) => Promise<void>;
|
|
139
147
|
} & { [K in `set${_CapitalizedRefName}`]: (...models: (Full | Light)[]) => void } & {
|
|
140
148
|
[K in `reset${_CapitalizedRefName}`]: (model?: Full) => void;
|
|
@@ -311,20 +319,24 @@ export const makeFormSetter = (refName: string, fetch: FetchProxy<any>) => {
|
|
|
311
319
|
const fileUploadRefName = resolveFileUploadCapability(fetch.serializedSignal)?.refName;
|
|
312
320
|
|
|
313
321
|
const names = modelNames(refName);
|
|
314
|
-
type
|
|
322
|
+
type FormState = { [key: string]: unknown };
|
|
315
323
|
let uploadSeq = 0;
|
|
316
|
-
const
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
324
|
+
const formPatchOf = (
|
|
325
|
+
state: StateRecord,
|
|
326
|
+
recipe: (form: FormState) => void,
|
|
327
|
+
uploading = state[names.modelFormUploading] as Uploading,
|
|
328
|
+
) => ({
|
|
329
|
+
[names.modelForm]: produce(state[names.modelForm] as FormState, recipe),
|
|
330
|
+
|
|
331
|
+
[names.modelFormUploading]: uploading,
|
|
332
|
+
});
|
|
323
333
|
const baseSetAction = {
|
|
324
334
|
[names.writeOnModel]: function (this: SetGet, path: string | (string | number)[], value: any) {
|
|
325
|
-
this.set(
|
|
326
|
-
|
|
327
|
-
|
|
335
|
+
this.set(
|
|
336
|
+
formPatchOf(this.get(), (form) => {
|
|
337
|
+
pathSet(form, path, value);
|
|
338
|
+
}),
|
|
339
|
+
);
|
|
328
340
|
},
|
|
329
341
|
};
|
|
330
342
|
const fieldSetAction = Object.entries(modelRef[FIELD_META]).reduce((acc, [key, field]) => {
|
|
@@ -333,17 +345,20 @@ export const makeFormSetter = (refName: string, fetch: FetchProxy<any>) => {
|
|
|
333
345
|
field.isClass && !!fileUploadRefName && ConstantRegistry.getRefName(field.modelRef) === fileUploadRefName;
|
|
334
346
|
const singleFieldSetAction = {
|
|
335
347
|
[namesOfField.setFieldOnModel]: function (this: SetGet, value: any | null) {
|
|
336
|
-
this.
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
348
|
+
const state = this.get() as StateRecord;
|
|
349
|
+
const setValue =
|
|
350
|
+
value === null ? null : field.isClass ? immerify<object>(field.modelRef, value as object) : (value as object);
|
|
351
|
+
|
|
352
|
+
const uploading = state[names.modelFormUploading] as Uploading;
|
|
353
|
+
this.set(
|
|
354
|
+
formPatchOf(
|
|
355
|
+
state,
|
|
356
|
+
(form) => {
|
|
357
|
+
form[namesOfField.field] = setValue;
|
|
358
|
+
},
|
|
359
|
+
isFileField && !field.isArray ? releasedUpload(uploading, namesOfField.field) : uploading,
|
|
360
|
+
),
|
|
361
|
+
);
|
|
347
362
|
|
|
348
363
|
const postSet = (this as unknown as { [key: string]: ((value: unknown) => unknown) | undefined })[
|
|
349
364
|
namesOfField.postSetField
|
|
@@ -362,22 +377,26 @@ export const makeFormSetter = (refName: string, fetch: FetchProxy<any>) => {
|
|
|
362
377
|
if (options.limit && options.limit <= length) return;
|
|
363
378
|
const idx = options.idx ?? length;
|
|
364
379
|
const setValue = field.isClass ? immerify<Light | Light[]>(field.modelRef, value) : value;
|
|
365
|
-
this.set(
|
|
366
|
-
(
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
380
|
+
this.set(
|
|
381
|
+
formPatchOf(this.get(), (draft) => {
|
|
382
|
+
draft[namesOfField.field] = [
|
|
383
|
+
...(form[namesOfField.field] as object[]).slice(0, idx),
|
|
384
|
+
...(Array.isArray(setValue) ? setValue : [setValue]),
|
|
385
|
+
...(form[namesOfField.field] as object[]).slice(idx),
|
|
386
|
+
];
|
|
387
|
+
}),
|
|
388
|
+
);
|
|
372
389
|
},
|
|
373
390
|
[namesOfField.subFieldOnModel]: function (this: SetGet, idx: number | number[]) {
|
|
374
391
|
const form = (this.get() as { [key: string]: any })[names.modelForm] as { [key: string]: object[] };
|
|
375
|
-
this.set(
|
|
376
|
-
(
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
392
|
+
this.set(
|
|
393
|
+
formPatchOf(this.get(), (draft) => {
|
|
394
|
+
draft[namesOfField.field] =
|
|
395
|
+
typeof idx === "number"
|
|
396
|
+
? form[namesOfField.field].filter((_, i) => i !== idx)
|
|
397
|
+
: form[namesOfField.field].filter((_, i) => !idx.includes(i));
|
|
398
|
+
}),
|
|
399
|
+
);
|
|
381
400
|
},
|
|
382
401
|
[namesOfField.addOrSubFieldOnModel]: function (
|
|
383
402
|
this: SetGet,
|
|
@@ -406,37 +425,45 @@ export const makeFormSetter = (refName: string, fetch: FetchProxy<any>) => {
|
|
|
406
425
|
if (!fileList.length) return;
|
|
407
426
|
|
|
408
427
|
const ticket = `${++uploadSeq}`;
|
|
409
|
-
this.
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
428
|
+
const uploading = (this.get() as StateRecord)[names.modelFormUploading] as Uploading;
|
|
429
|
+
const tickets = uploading[namesOfField.field] ?? [];
|
|
430
|
+
this.set({
|
|
431
|
+
[names.modelFormUploading]: {
|
|
432
|
+
...uploading,
|
|
413
433
|
[namesOfField.field]: field.isArray ? [...tickets, ticket] : [ticket],
|
|
414
|
-
}
|
|
434
|
+
},
|
|
415
435
|
});
|
|
416
|
-
const
|
|
417
|
-
|
|
418
|
-
(state[names.modelForm] as { id?: string }).id === formId;
|
|
436
|
+
const releasedIn = (state: StateRecord) =>
|
|
437
|
+
releasedUpload(state[names.modelFormUploading] as Uploading, namesOfField.field, ticket);
|
|
419
438
|
let files: ProtoFile[];
|
|
420
439
|
try {
|
|
421
440
|
files = await (fetch[names.addModelFiles] as (...args: any) => Promise<ProtoFile[]>)(fileList, formId);
|
|
422
441
|
} catch (error) {
|
|
423
|
-
this.set(
|
|
442
|
+
this.set({ [names.modelFormUploading]: releasedIn(this.get() as StateRecord) });
|
|
424
443
|
throw error;
|
|
425
444
|
}
|
|
426
|
-
|
|
445
|
+
const state = this.get() as StateRecord;
|
|
446
|
+
const isWritten =
|
|
447
|
+
!!(state[names.modelFormUploading] as Uploading)[namesOfField.field]?.includes(ticket) &&
|
|
448
|
+
(state[names.modelForm] as { id?: string }).id === formId;
|
|
449
|
+
if (!isWritten) {
|
|
450
|
+
this.set({ [names.modelFormUploading]: releasedIn(state) });
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
427
453
|
|
|
428
|
-
this.set(
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
454
|
+
this.set(
|
|
455
|
+
formPatchOf(
|
|
456
|
+
state,
|
|
457
|
+
(form) => {
|
|
458
|
+
if (field.isArray) {
|
|
459
|
+
const current = (form[namesOfField.field] as ProtoLightFile[] | null) ?? [];
|
|
460
|
+
const idx = Math.min(index ?? current.length, current.length);
|
|
461
|
+
form[namesOfField.field] = [...current.slice(0, idx), ...files, ...current.slice(idx)];
|
|
462
|
+
} else form[namesOfField.field] = files[0] ?? null;
|
|
463
|
+
},
|
|
464
|
+
releasedIn(state),
|
|
465
|
+
),
|
|
466
|
+
);
|
|
440
467
|
const postSet = (this as unknown as { [key: string]: ((value: unknown) => unknown) | undefined })[
|
|
441
468
|
namesOfField.postSetField
|
|
442
469
|
];
|
|
@@ -452,42 +479,20 @@ export const makeFormSetter = (refName: string, fetch: FetchProxy<any>) => {
|
|
|
452
479
|
] as ProtoLightFile | ProtoLightFile[] | null;
|
|
453
480
|
return Array.isArray(value) ? value.some((heldFile) => heldFile.id === id) : value?.id === id;
|
|
454
481
|
};
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
clearInterval(intervalKey);
|
|
470
|
-
return;
|
|
471
|
-
}
|
|
472
|
-
this.set(
|
|
473
|
-
(state: { [key: string]: { [key: string]: ProtoLightFile[] | ProtoLightFile | null } }) => {
|
|
474
|
-
const value = state[names.modelForm][namesOfField.field];
|
|
475
|
-
state[names.modelForm][namesOfField.field] = Array.isArray(value)
|
|
476
|
-
? value.map((heldFile) => (heldFile.id === currentFile.id ? currentFile : heldFile))
|
|
477
|
-
: currentFile;
|
|
478
|
-
},
|
|
479
|
-
);
|
|
480
|
-
if (currentFile.status !== "uploading" || attemptsLeft <= 0) clearInterval(intervalKey);
|
|
481
|
-
} catch (error) {
|
|
482
|
-
clearInterval(intervalKey);
|
|
483
|
-
Logger.warn(
|
|
484
|
-
`Upload poll for ${fileReadName} ${file.id} stopped: ${
|
|
485
|
-
error instanceof Error ? error.message : String(error)
|
|
486
|
-
}`,
|
|
487
|
-
);
|
|
488
|
-
}
|
|
489
|
-
})();
|
|
490
|
-
}, UPLOAD_POLL_INTERVAL_MS);
|
|
482
|
+
pollUploadedFiles({
|
|
483
|
+
files,
|
|
484
|
+
readName: fileReadName,
|
|
485
|
+
read: (fetch as { [key: string]: any })[fileReadName] as (id: string) => Promise<ProtoLightFile>,
|
|
486
|
+
holds: holdsFile,
|
|
487
|
+
land: (currentFile) =>
|
|
488
|
+
this.set(
|
|
489
|
+
formPatchOf(this.get(), (form) => {
|
|
490
|
+
const value = form[namesOfField.field] as ProtoLightFile[] | ProtoLightFile | null;
|
|
491
|
+
form[namesOfField.field] = Array.isArray(value)
|
|
492
|
+
? value.map((heldFile) => (heldFile.id === currentFile.id ? currentFile : heldFile))
|
|
493
|
+
: currentFile;
|
|
494
|
+
}),
|
|
495
|
+
),
|
|
491
496
|
});
|
|
492
497
|
},
|
|
493
498
|
}
|
|
@@ -514,8 +519,6 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
|
|
|
514
519
|
const className = capitalize(refName);
|
|
515
520
|
const cnst = ConstantRegistry.getDatabase(refName);
|
|
516
521
|
const modelRef = cnst.full;
|
|
517
|
-
|
|
518
|
-
let openFormBase: object | null = null;
|
|
519
522
|
const slices = Object.entries(slice).map(([suffix, serializedSlice]) => {
|
|
520
523
|
const keys = sliceKeysOf(refName, suffix);
|
|
521
524
|
return { sliceName: keys.slice, keys, slice: serializedSlice };
|
|
@@ -540,95 +543,10 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
|
|
|
540
543
|
if (fn) return fn;
|
|
541
544
|
throw restoreRemoteError({ error: "base.error.endpointClosed", data: { endpoint: key } }, 404);
|
|
542
545
|
};
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
void DraftStore.reconcileIdentity();
|
|
548
|
-
return {
|
|
549
|
-
key: DraftStore.keyOf(refName, draftScope),
|
|
550
|
-
baseHash: DraftStore.formHash(refName, form),
|
|
551
|
-
baseUpdatedAt,
|
|
552
|
-
pending: null,
|
|
553
|
-
appliedAt: null,
|
|
554
|
-
};
|
|
555
|
-
}
|
|
556
|
-
async function applyDraft(this: SetGet, { auto }: { auto: boolean }) {
|
|
557
|
-
const draft = (this.get() as { [key: string]: any })[names.modelDraft] as DraftState | null;
|
|
558
|
-
if (!draft) return;
|
|
559
|
-
const record = await DraftStore.read(draft.key);
|
|
560
|
-
if (!record) return;
|
|
561
|
-
|
|
562
|
-
const current = (this.get() as { [key: string]: any })[names.modelDraft] as DraftState | null;
|
|
563
|
-
if (current?.key !== draft.key) return;
|
|
564
|
-
|
|
565
|
-
const openedForm = (this.get() as { [key: string]: any })[names.modelForm] as object;
|
|
566
|
-
if (DraftStore.contentHash(record.form) === DraftStore.contentHash(DraftStore.encodeForm(refName, openedForm))) {
|
|
567
|
-
await DraftStore.remove(draft.key);
|
|
568
|
-
return;
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
const draftedId = record.form.id;
|
|
572
|
-
if (typeof draftedId === "string" && draftedId && draftedId !== (openedForm as { id?: unknown }).id) {
|
|
573
|
-
await DraftStore.remove(draft.key);
|
|
574
|
-
return;
|
|
575
|
-
}
|
|
576
|
-
let form: object;
|
|
577
|
-
try {
|
|
578
|
-
form = DraftStore.decodeForm(refName, record.form);
|
|
579
|
-
} catch {
|
|
580
|
-
|
|
581
|
-
await DraftStore.remove(draft.key);
|
|
582
|
-
return;
|
|
583
|
-
}
|
|
584
|
-
const savedAt = new Date(record.savedAt);
|
|
585
|
-
if (auto || record.baseUpdatedAt === draft.baseUpdatedAt) {
|
|
586
|
-
this.set({ [names.modelForm]: form, [names.modelDraft]: { ...draft, appliedAt: savedAt } });
|
|
587
|
-
return;
|
|
588
|
-
}
|
|
589
|
-
this.set({ [names.modelDraft]: { ...draft, pending: { savedAt, form } } });
|
|
590
|
-
}
|
|
591
|
-
const clearDraft = (draft: DraftState | null) => {
|
|
592
|
-
openFormBase = null;
|
|
593
|
-
if (draft) void DraftStore.remove(draft.key);
|
|
594
|
-
return null;
|
|
595
|
-
};
|
|
596
|
-
const isLiveSlice = (sliceName: string) => !!slices.find((entry) => entry.sliceName === sliceName)?.slice.live;
|
|
597
|
-
|
|
598
|
-
const createdListPatchOf = (sliceName: string, model: Full, seen: { [key: string]: unknown }) => {
|
|
599
|
-
const listKey = sliceName.replace(names.model, names.modelList);
|
|
600
|
-
if (isLiveSlice(sliceName) || seen[sliceName.replace(names.model, names.modelListLoading)] || !seen[listKey])
|
|
601
|
-
return {};
|
|
602
|
-
const insightKey = sliceName.replace(names.model, names.modelInsight);
|
|
603
|
-
const modelInsight = seen[insightKey] as Insight & BaseInsight;
|
|
604
|
-
return {
|
|
605
|
-
[listKey]: new DataList([model, ...(seen[listKey] as DataList<Light>)]),
|
|
606
|
-
[insightKey]: new cnst.insight().set({ ...modelInsight, count: modelInsight.count + 1 }),
|
|
607
|
-
};
|
|
608
|
-
};
|
|
609
|
-
function applyLiveCreate(this: SetGet, sliceName: string, model: Full) {
|
|
610
|
-
const target = slices.find((entry) => entry.sliceName === sliceName);
|
|
611
|
-
if (!target?.slice.live) return;
|
|
612
|
-
const applyLive = (this as unknown as DynamicRecord)[target.keys.action.applyLiveModel] as
|
|
613
|
-
| ((event: LiveEventPayload) => void)
|
|
614
|
-
| undefined;
|
|
615
|
-
applyLive?.({ op: "enter", id: model.id, light: new cnst.light().set(model) as unknown as object });
|
|
616
|
-
}
|
|
617
|
-
const updatedListPatchOf = (state: { [key: string]: unknown }, ...updatedModels: Full[]) => {
|
|
618
|
-
const lightModels = withSharedInstances(() =>
|
|
619
|
-
updatedModels.map((updatedModel) => new cnst.light().set(updatedModel) as unknown as Light),
|
|
620
|
-
);
|
|
621
|
-
const patch: { [key: string]: DataList<Light> } = {};
|
|
622
|
-
for (const { keys } of slices) {
|
|
623
|
-
const namesOfSlice = keys.state;
|
|
624
|
-
const modelList = state[namesOfSlice.modelList] as DataList<Light>;
|
|
625
|
-
if (state[namesOfSlice.modelListLoading] || !lightModels.some(({ id }) => modelList.has(id))) continue;
|
|
626
|
-
const nextList = new DataList(modelList);
|
|
627
|
-
for (const lightModel of lightModels) if (nextList.has(lightModel.id)) nextList.set(lightModel);
|
|
628
|
-
patch[namesOfSlice.modelList] = nextList;
|
|
629
|
-
}
|
|
630
|
-
return patch;
|
|
631
|
-
};
|
|
546
|
+
const { openSeqOf, markOpened, isOvertaken, isSavedFormOpen, withFlag, closedUnlessShown, openModelPatchOf } =
|
|
547
|
+
makeModelWriteGuard(names);
|
|
548
|
+
const { formBase, armDraft, applyDraft, forgetDraft, clearDraft, savingDraft } = makeFormDrafts(refName, names);
|
|
549
|
+
const { createdListPatchOf, applyLiveCreate, updatedListPatchOf } = makeModelListPatches({ cnst, names, slices });
|
|
632
550
|
const baseAction = {
|
|
633
551
|
[names.createModelInForm]: async function (
|
|
634
552
|
this: SetGet,
|
|
@@ -640,18 +558,26 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
|
|
|
640
558
|
const modelInput = (cnst.input.purify as (form: any) => DefaultOf<Input> | null)(modelForm);
|
|
641
559
|
|
|
642
560
|
if (!modelInput || isUploadingIn(currentState)) return;
|
|
643
|
-
|
|
644
|
-
const
|
|
645
|
-
this
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
561
|
+
const openSeq = openSeqOf(this);
|
|
562
|
+
const draft = currentState[names.modelDraft] as DraftState | null;
|
|
563
|
+
const model = await withFlag(this, {
|
|
564
|
+
flag: { key: names.modelLoading, value: true },
|
|
565
|
+
onError,
|
|
566
|
+
request: () => savingDraft(this, draft, modelForm, () => servedFetch<Full>(names.createModel)(modelInput)),
|
|
567
|
+
land: (model, ownsFlag) => {
|
|
568
|
+
this.set({
|
|
569
|
+
...(isSavedFormOpen(this, openSeq, modelForm)
|
|
570
|
+
? { [names.modelForm]: immerify(modelRef, defaultModel), [names.modelDraft]: clearDraft(this, draft) }
|
|
571
|
+
: {}),
|
|
572
|
+
...(isOvertaken(this, openSeq)
|
|
573
|
+
? {}
|
|
574
|
+
: { [names.model]: model, [names.modelViewAt]: new Date(), [names.modelModal]: modal ?? null }),
|
|
575
|
+
...(ownsFlag ? { [names.modelLoading]: false } : {}),
|
|
576
|
+
...staleAtOfSlices(),
|
|
577
|
+
...(typeof path === "string" && path ? { [path]: model } : {}),
|
|
578
|
+
...createdListPatchOf(sliceName, model, this.get()),
|
|
579
|
+
});
|
|
580
|
+
},
|
|
655
581
|
});
|
|
656
582
|
applyLiveCreate.call(this, sliceName, model);
|
|
657
583
|
await onSuccess?.(model);
|
|
@@ -666,18 +592,26 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
|
|
|
666
592
|
const defaultModel = currentState[defaultKeyOf(sliceName)] as Full;
|
|
667
593
|
const modelInput = (cnst.input.purify as (form: any) => DefaultOf<Input> | null)(modelForm);
|
|
668
594
|
if (!modelInput || isUploadingIn(currentState)) return;
|
|
669
|
-
|
|
670
|
-
const
|
|
671
|
-
this
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
595
|
+
const openSeq = openSeqOf(this);
|
|
596
|
+
const draft = currentState[names.modelDraft] as DraftState | null;
|
|
597
|
+
const updatedModel = await withFlag(this, {
|
|
598
|
+
flag: model?.id === modelForm.id ? { key: names.modelLoading, value: modelForm.id } : null,
|
|
599
|
+
onError,
|
|
600
|
+
request: () =>
|
|
601
|
+
savingDraft(this, draft, modelForm, () => servedFetch<Full>(names.updateModel)(modelForm.id, modelInput)),
|
|
602
|
+
land: (updatedModel, ownsFlag) => {
|
|
603
|
+
const latest = this.get() as StateRecord;
|
|
604
|
+
this.set({
|
|
605
|
+
...openModelPatchOf(latest, updatedModel, ownsFlag, { [names.modelViewAt]: new Date() }),
|
|
606
|
+
...(isSavedFormOpen(this, openSeq, modelForm)
|
|
607
|
+
? { [names.modelForm]: immerify(modelRef, defaultModel), [names.modelDraft]: clearDraft(this, draft) }
|
|
608
|
+
: {}),
|
|
609
|
+
...(isOvertaken(this, openSeq) ? {} : { [names.modelModal]: modal ?? null }),
|
|
610
|
+
...staleAtOfSlices(),
|
|
611
|
+
...(typeof path === "string" && path ? { [path]: updatedModel } : {}),
|
|
612
|
+
...updatedListPatchOf(latest, updatedModel),
|
|
613
|
+
});
|
|
614
|
+
},
|
|
681
615
|
});
|
|
682
616
|
await onSuccess?.(updatedModel);
|
|
683
617
|
},
|
|
@@ -688,16 +622,22 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
|
|
|
688
622
|
) {
|
|
689
623
|
const modelInput = (cnst.input.purify as (data: any) => Input | null)(data);
|
|
690
624
|
if (!modelInput) return;
|
|
691
|
-
|
|
692
|
-
const model = await
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
625
|
+
const openSeq = openSeqOf(this);
|
|
626
|
+
const model = await withFlag(this, {
|
|
627
|
+
flag: { key: names.modelLoading, value: true },
|
|
628
|
+
onError,
|
|
629
|
+
request: () => servedFetch<Full>(names.createModel)(modelInput),
|
|
630
|
+
land: (model, ownsFlag) => {
|
|
631
|
+
this.set({
|
|
632
|
+
...(isOvertaken(this, openSeq)
|
|
633
|
+
? {}
|
|
634
|
+
: { [names.model]: model, [names.modelViewAt]: new Date(), [names.modelModal]: modal ?? null }),
|
|
635
|
+
...(ownsFlag ? { [names.modelLoading]: false } : {}),
|
|
636
|
+
...staleAtOfSlices(),
|
|
637
|
+
...(typeof path === "string" && path ? { [path]: model } : {}),
|
|
638
|
+
...createdListPatchOf(sliceName, model, this.get()),
|
|
639
|
+
});
|
|
640
|
+
},
|
|
701
641
|
});
|
|
702
642
|
applyLiveCreate.call(this, sliceName, model);
|
|
703
643
|
await onSuccess?.(model);
|
|
@@ -712,51 +652,64 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
|
|
|
712
652
|
const model = currentState[names.model] as Full | null;
|
|
713
653
|
const modelInput = (cnst.input.purify as (data: any) => DefaultOf<Input> | null)(data);
|
|
714
654
|
if (!modelInput) return;
|
|
715
|
-
|
|
716
|
-
const updatedModel = await
|
|
655
|
+
const openSeq = openSeqOf(this);
|
|
656
|
+
const updatedModel = await withFlag(this, {
|
|
657
|
+
flag: model?.id === id ? { key: names.modelLoading, value: id } : null,
|
|
717
658
|
onError,
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
659
|
+
request: () => servedFetch<Full>(names.updateModel)(id, modelInput),
|
|
660
|
+
land: (updatedModel, ownsFlag) => {
|
|
661
|
+
const latest = this.get() as StateRecord;
|
|
662
|
+
this.set({
|
|
663
|
+
...openModelPatchOf(latest, updatedModel, ownsFlag, { [names.modelViewAt]: new Date() }),
|
|
664
|
+
...(isOvertaken(this, openSeq) ? {} : { [names.modelModal]: modal ?? null }),
|
|
665
|
+
...staleAtOfSlices(),
|
|
666
|
+
...(typeof path === "string" && path ? { [path]: updatedModel } : {}),
|
|
667
|
+
...updatedListPatchOf(latest, updatedModel),
|
|
668
|
+
});
|
|
669
|
+
},
|
|
727
670
|
});
|
|
728
671
|
await onSuccess?.(updatedModel);
|
|
729
672
|
},
|
|
730
|
-
[names.removeModel]: async function (
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
673
|
+
[names.removeModel]: async function (
|
|
674
|
+
this: SetGet,
|
|
675
|
+
id: string,
|
|
676
|
+
options?: ReportingFetchPolicy & { modal?: string | null },
|
|
677
|
+
) {
|
|
678
|
+
const { modal, onError, ...fetchPolicyOptions } = options ?? {};
|
|
679
|
+
await withFlag(this, {
|
|
680
|
+
flag: null,
|
|
681
|
+
onError,
|
|
682
|
+
request: () => servedFetch<Full & { removedAt: Dayjs | null }>(names.removeModel)(id, fetchPolicyOptions),
|
|
683
|
+
land: (model) => {
|
|
684
|
+
const currentState = this.get() as { [key: string]: unknown };
|
|
685
|
+
if (!model.removedAt) {
|
|
686
|
+
const listPatch = updatedListPatchOf(currentState, model);
|
|
687
|
+
this.set({
|
|
688
|
+
...listPatch,
|
|
689
|
+
...(modal !== undefined && Object.keys(listPatch).length ? { [names.modelModal]: modal } : {}),
|
|
690
|
+
...staleAtOfSlices(),
|
|
691
|
+
});
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
const removedPatch: { [key: string]: unknown } = {};
|
|
695
|
+
for (const { keys } of slices) {
|
|
696
|
+
const namesOfSlice = keys.state;
|
|
697
|
+
const modelList = currentState[namesOfSlice.modelList] as DataList<Light>;
|
|
698
|
+
const modelSelection = currentState[namesOfSlice.modelSelection] as DataList<Light>;
|
|
699
|
+
const modelInsight = currentState[namesOfSlice.modelInsight] as Insight & BaseInsight;
|
|
700
|
+
if (currentState[namesOfSlice.modelListLoading] || !modelList.has(model.id)) continue;
|
|
701
|
+
removedPatch[namesOfSlice.modelList] = new DataList(modelList).delete(id);
|
|
702
|
+
removedPatch[namesOfSlice.modelInsight] = new cnst.insight().set({
|
|
703
|
+
...modelInsight,
|
|
704
|
+
count: modelInsight.count - 1,
|
|
705
|
+
});
|
|
706
|
+
if (modelSelection.has(model.id))
|
|
707
|
+
removedPatch[namesOfSlice.modelSelection] = new DataList(modelSelection).delete(id);
|
|
708
|
+
if (modal !== undefined) removedPatch[names.modelModal] = modal;
|
|
709
|
+
}
|
|
710
|
+
this.set({ ...removedPatch, ...staleAtOfSlices() });
|
|
711
|
+
},
|
|
712
|
+
});
|
|
760
713
|
},
|
|
761
714
|
[names.checkModelSubmitable]: function (this: SetGet, disabled?: boolean) {
|
|
762
715
|
const currentState = this.get() as { [key: string]: any };
|
|
@@ -786,6 +739,7 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
|
|
|
786
739
|
partial: Partial<Full> = {},
|
|
787
740
|
{ modal, setDefault, sliceName = names.model, draftScope }: NewOption = {},
|
|
788
741
|
) {
|
|
742
|
+
markOpened(this, { form: true });
|
|
789
743
|
const defaultModelKey = defaultKeyOf(sliceName);
|
|
790
744
|
const currentState = this.get() as { [key: string]: any };
|
|
791
745
|
const defaultModel = currentState[defaultModelKey] as Full;
|
|
@@ -805,49 +759,78 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
|
|
|
805
759
|
[names.editModel]: async function (
|
|
806
760
|
this: SetGet,
|
|
807
761
|
modelOrId: Full | string,
|
|
808
|
-
{ modal, onError, draftScope }: EditOption &
|
|
762
|
+
{ modal, onError, draftScope }: EditOption & ReportingFetchPolicy = {},
|
|
809
763
|
) {
|
|
810
764
|
const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
765
|
+
const openSeq = markOpened(this, { form: true });
|
|
766
|
+
await withFlag(this, {
|
|
767
|
+
flag: { key: names.modelFormLoading, value: id },
|
|
768
|
+
opening: { [names.modelModal]: modal ?? "edit" },
|
|
769
|
+
failed: () => closedUnlessShown(this, openSeq, names.modelForm, id),
|
|
770
|
+
onError,
|
|
771
|
+
request: () => servedFetch<Full>(names.model)(id),
|
|
772
|
+
land: (model, ownsFlag) => {
|
|
773
|
+
const released = ownsFlag ? { [names.modelFormLoading]: false } : {};
|
|
774
|
+
if (isOvertaken(this, openSeq)) {
|
|
775
|
+
if (ownsFlag) this.set(released);
|
|
776
|
+
return;
|
|
777
|
+
}
|
|
778
|
+
const modelForm = immerify(modelRef, deepObjectify<Input>(model as unknown as Input) as object) as Input;
|
|
779
|
+
this.set({
|
|
780
|
+
...released,
|
|
781
|
+
[names.model]: model,
|
|
782
|
+
[names.modelViewAt]: new Date(),
|
|
783
|
+
[names.modelForm]: modelForm,
|
|
784
|
+
[names.modelDraft]: armDraft.call(this, draftScope, modelForm, updatedAtOf(model)),
|
|
785
|
+
});
|
|
786
|
+
|
|
787
|
+
if (draftScope) void applyDraft.call(this, { auto: false });
|
|
788
|
+
},
|
|
820
789
|
});
|
|
821
|
-
|
|
822
|
-
if (draftScope) void applyDraft.call(this, { auto: false });
|
|
823
790
|
},
|
|
824
791
|
[names.mergeModel]: async function (
|
|
825
792
|
this: SetGet,
|
|
826
793
|
modelOrId: Full | string,
|
|
827
794
|
data: Partial<Full>,
|
|
828
|
-
options?:
|
|
795
|
+
options?: ReportingFetchPolicy,
|
|
829
796
|
) {
|
|
797
|
+
const { onError, ...fetchPolicyOptions } = options ?? {};
|
|
830
798
|
const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
|
|
831
|
-
const
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
799
|
+
const model = (this.get() as StateRecord)[names.model] as Full | null;
|
|
800
|
+
await withFlag(this, {
|
|
801
|
+
flag: id === model?.id ? { key: names.modelLoading, value: id } : null,
|
|
802
|
+
onError,
|
|
803
|
+
request: () => servedFetch<Full>(names.mergeModel)(modelOrId, data, fetchPolicyOptions),
|
|
804
|
+
land: (updatedModel, ownsFlag) => {
|
|
805
|
+
const latest = this.get() as StateRecord;
|
|
806
|
+
this.set({
|
|
807
|
+
...openModelPatchOf(latest, updatedModel, ownsFlag),
|
|
808
|
+
...staleAtOfSlices(),
|
|
809
|
+
...updatedListPatchOf(latest, updatedModel),
|
|
810
|
+
});
|
|
811
|
+
},
|
|
840
812
|
});
|
|
841
813
|
},
|
|
842
814
|
[names.viewModel]: async function (
|
|
843
815
|
this: SetGet,
|
|
844
816
|
modelOrId: Full | string,
|
|
845
|
-
{ modal, onError }: { modal?: string | null } &
|
|
817
|
+
{ modal, onError }: { modal?: string | null } & ReportingFetchPolicy = {},
|
|
846
818
|
) {
|
|
847
819
|
const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
820
|
+
const openSeq = markOpened(this, { form: false });
|
|
821
|
+
await withFlag(this, {
|
|
822
|
+
flag: { key: names.modelLoading, value: id },
|
|
823
|
+
opening: { [names.modelModal]: modal ?? "view" },
|
|
824
|
+
failed: () => closedUnlessShown(this, openSeq, names.model, id),
|
|
825
|
+
onError,
|
|
826
|
+
request: () => servedFetch<Full>(names.model)(id),
|
|
827
|
+
land: (model, ownsFlag) => {
|
|
828
|
+
this.set({
|
|
829
|
+
...(isOvertaken(this, openSeq) ? {} : { [names.model]: model, [names.modelViewAt]: new Date() }),
|
|
830
|
+
...(ownsFlag ? { [names.modelLoading]: false } : {}),
|
|
831
|
+
});
|
|
832
|
+
},
|
|
833
|
+
});
|
|
851
834
|
},
|
|
852
835
|
[names.setModel]: function (this: SetGet, ...fullOrLightModels: Full[]) {
|
|
853
836
|
const currentState = this.get() as { [key: string]: any };
|
|
@@ -874,7 +857,7 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
|
|
|
874
857
|
[names.modelViewAt]: new Date(0),
|
|
875
858
|
[names.modelForm]: immerify(modelRef, defaultModel),
|
|
876
859
|
[names.modelModal]: null,
|
|
877
|
-
[names.modelDraft]: clearDraft(currentState[names.modelDraft] as DraftState | null),
|
|
860
|
+
[names.modelDraft]: clearDraft(this, currentState[names.modelDraft] as DraftState | null),
|
|
878
861
|
});
|
|
879
862
|
return model ?? null;
|
|
880
863
|
},
|
|
@@ -902,10 +885,10 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
|
|
|
902
885
|
[names.discardModelFormDraft]: function (this: SetGet) {
|
|
903
886
|
const draft = (this.get() as { [key: string]: any })[names.modelDraft] as DraftState | null;
|
|
904
887
|
if (!draft) return;
|
|
905
|
-
|
|
888
|
+
forgetDraft(this, draft.key);
|
|
906
889
|
|
|
907
890
|
this.set({
|
|
908
|
-
...(draft.appliedAt &&
|
|
891
|
+
...(draft.appliedAt && formBase() ? { [names.modelForm]: formBase() } : {}),
|
|
909
892
|
[names.modelDraft]: { ...draft, pending: null, appliedAt: null },
|
|
910
893
|
});
|
|
911
894
|
},
|