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.
Files changed (130) hide show
  1. package/ARCHITECTURE.md +3 -3
  2. package/CHANGELOG.md +105 -1
  3. package/client/csrTypes.ts +13 -1
  4. package/client/frameConfig.ts +4 -2
  5. package/client/route/RouteHead.ts +11 -2
  6. package/client/router.ts +8 -4
  7. package/client/rscNavigation.ts +2 -3
  8. package/client/rscRoutePatch.ts +42 -0
  9. package/common/akanGraph.ts +1 -1
  10. package/common/types.ts +5 -0
  11. package/dictionary/base.dictionary.ts +1 -0
  12. package/fetch/client/fetchClient.ts +11 -16
  13. package/fetch/client/fetchHandle.ts +11 -1
  14. package/fetch/client/inflightQueries.ts +17 -0
  15. package/fetch/index.ts +1 -0
  16. package/fetch/staleReads.ts +27 -0
  17. package/fetch/streamedRead.ts +23 -0
  18. package/package.json +1 -1
  19. package/server/akanServer.ts +1 -1
  20. package/server/artifact/builderRpc.ts +22 -7
  21. package/server/artifact/ipcTypes.ts +9 -3
  22. package/server/devtools/README.md +4 -3
  23. package/server/devtools/devtoolsRouter.ts +9 -5
  24. package/server/devtools/studio/studioRoute.ts +18 -11
  25. package/server/head.tsx +4 -19
  26. package/server/routeElementComposer.tsx +86 -3
  27. package/server/routeState.ts +5 -1
  28. package/server/routeTreeBuilder.ts +1 -0
  29. package/server/rscClient.tsx +118 -21
  30. package/server/rscClientChunks.ts +62 -0
  31. package/server/rscClientFetch.ts +6 -1
  32. package/server/rscHeadPatch.ts +17 -56
  33. package/server/rscParkedRedirect.ts +23 -0
  34. package/server/rscPatchPlanner.ts +220 -0
  35. package/server/rscPrefetchCache.ts +39 -17
  36. package/server/rscResponse.ts +78 -0
  37. package/server/rscSegmentOutlet.tsx +24 -5
  38. package/server/rscSegmentOutletReference.ts +10 -12
  39. package/server/rscWorker.tsx +109 -154
  40. package/server/rscWorkerCache.ts +56 -0
  41. package/server/rscWorkerHost.ts +6 -8
  42. package/server/ssrTypes.ts +9 -0
  43. package/server/webRouter.ts +12 -89
  44. package/store/action.ts +282 -299
  45. package/store/actionErrorKey.ts +9 -0
  46. package/store/draftFlush.ts +5 -0
  47. package/store/draftStore.ts +2 -0
  48. package/store/formDrafts.ts +115 -0
  49. package/store/modelListPatch.ts +53 -0
  50. package/store/modelWriteGuard.ts +91 -0
  51. package/store/rootStore.ts +2 -0
  52. package/store/storeInstance.ts +9 -10
  53. package/store/uploadPoll.ts +52 -0
  54. package/store/withSelector.ts +6 -4
  55. package/types/client/csrTypes.d.ts +13 -1
  56. package/types/client/route/RouteHead.d.ts +5 -1
  57. package/types/client/router.d.ts +3 -2
  58. package/types/client/rscNavigation.d.ts +2 -2
  59. package/types/client/rscRoutePatch.d.ts +22 -0
  60. package/types/common/akanGraph.d.ts +1 -1
  61. package/types/common/types.d.ts +5 -0
  62. package/types/dictionary/base.dictionary.d.ts +1 -1
  63. package/types/dictionary/dictionary.d.ts +8 -8
  64. package/types/fetch/client/fetchHandle.d.ts +1 -0
  65. package/types/fetch/client/inflightQueries.d.ts +2 -0
  66. package/types/fetch/index.d.ts +1 -0
  67. package/types/fetch/staleReads.d.ts +8 -0
  68. package/types/fetch/streamedRead.d.ts +8 -0
  69. package/types/server/artifact/builderRpc.d.ts +8 -0
  70. package/types/server/artifact/ipcTypes.d.ts +8 -0
  71. package/types/server/devtools/devtoolsRouter.d.ts +4 -1
  72. package/types/server/devtools/studio/studioRoute.d.ts +2 -1
  73. package/types/server/head.d.ts +2 -2
  74. package/types/server/logging/rotatingLogWriter.d.ts +1 -1
  75. package/types/server/routeElementComposer.d.ts +24 -1
  76. package/types/server/routeState.d.ts +2 -0
  77. package/types/server/rscClient.d.ts +1 -0
  78. package/types/server/rscClientChunks.d.ts +10 -0
  79. package/types/server/rscHeadPatch.d.ts +5 -4
  80. package/types/server/rscParkedRedirect.d.ts +12 -0
  81. package/types/server/rscPatchPlanner.d.ts +49 -0
  82. package/types/server/rscPrefetchCache.d.ts +2 -2
  83. package/types/server/rscResponse.d.ts +9 -0
  84. package/types/server/rscSegmentOutlet.d.ts +5 -0
  85. package/types/server/rscSegmentOutletReference.d.ts +2 -1
  86. package/types/server/rscWorkerCache.d.ts +33 -1
  87. package/types/server/rscWorkerHost.d.ts +2 -6
  88. package/types/server/ssrTypes.d.ts +8 -0
  89. package/types/server/webRouter.d.ts +1 -7
  90. package/types/store/action.d.ts +10 -6
  91. package/types/store/actionErrorKey.d.ts +1 -0
  92. package/types/store/baseSt.d.ts +1 -295
  93. package/types/store/draftFlush.d.ts +4 -0
  94. package/types/store/formDrafts.d.ts +21 -0
  95. package/types/store/modelListPatch.d.ts +34 -0
  96. package/types/store/modelWriteGuard.d.ts +45 -0
  97. package/types/store/rootStore.d.ts +6 -0
  98. package/types/store/uploadPoll.d.ts +16 -0
  99. package/types/store/withSelector.d.ts +1 -1
  100. package/types/ui/Link/inputModality.d.ts +5 -0
  101. package/types/ui/Link/viewportPrefetch.d.ts +4 -0
  102. package/types/ui/Load/Edit_Client.d.ts +2 -0
  103. package/types/ui/Load/Pagination.d.ts +4 -1
  104. package/types/ui/Load/Stream.d.ts +7 -1
  105. package/types/ui/Load/StreamBoundary.d.ts +20 -0
  106. package/types/ui/Load/Units.d.ts +2 -0
  107. package/types/ui/Load/View.d.ts +2 -0
  108. package/types/webkit/nativeCore.d.ts +1 -0
  109. package/types/webkit/nativePurchase.d.ts +2 -1
  110. package/types/webkit/useFrameRuntime.d.ts +1 -0
  111. package/types/webkit/useGeoLocation.d.ts +1 -1
  112. package/types/webkit/usePurchase.d.ts +1 -1
  113. package/types/webkit/useTransitionStore.d.ts +1 -0
  114. package/ui/Button.tsx +1 -0
  115. package/ui/Link/inputModality.ts +21 -0
  116. package/ui/Link/prefetchIntent.ts +7 -25
  117. package/ui/Link/viewportPrefetch.ts +30 -0
  118. package/ui/Load/Edit_Client.tsx +7 -1
  119. package/ui/Load/Pagination.tsx +10 -2
  120. package/ui/Load/Stream.tsx +24 -4
  121. package/ui/Load/StreamBoundary.tsx +70 -0
  122. package/ui/Load/Units.tsx +3 -0
  123. package/ui/Load/View.tsx +3 -0
  124. package/ui/System/Client.tsx +28 -7
  125. package/webkit/nativeCore.ts +1 -0
  126. package/webkit/nativePurchase.ts +6 -3
  127. package/webkit/useCamera.tsx +22 -21
  128. package/webkit/useGeoLocation.tsx +4 -1
  129. package/webkit/usePurchase.tsx +4 -2
  130. 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 { DraftStore } from "./draftStore";
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
- const UPLOAD_POLL_INTERVAL_MS = 3000;
70
- const UPLOAD_POLL_ATTEMPTS = 40;
71
-
72
- export interface CreateOption<Full extends { id: string }> {
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?: FetchPolicy & { modal?: string | null },
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}`]: (model: Full | string, options?: EditOption & FetchPolicy) => Promise<void>;
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?: FetchPolicy,
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 } & FetchPolicy,
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 UploadState = { [key: string]: { [field: string]: string[] } };
322
+ type FormState = { [key: string]: unknown };
315
323
  let uploadSeq = 0;
316
- const releaseUpload = (state: UploadState, field: string, ticket?: string) => {
317
- const pending = state[names.modelFormUploading][field];
318
- if (!pending || (ticket && !pending.includes(ticket))) return;
319
- const { [field]: tickets = [], ...rest } = state[names.modelFormUploading];
320
- const left = ticket ? tickets.filter((pending) => pending !== ticket) : [];
321
- state[names.modelFormUploading] = left.length ? { ...rest, [field]: left } : rest;
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((state: { [key: string]: any }) => {
326
- pathSet(state[names.modelForm], path, value);
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.set((state: { [key: string]: any }) => {
337
-
338
- if (isFileField && !field.isArray) releaseUpload(state as UploadState, namesOfField.field);
339
- const setValue =
340
- value === null
341
- ? null
342
- : field.isClass
343
- ? immerify<object>(field.modelRef, value as object)
344
- : (value as object);
345
- (state[names.modelForm] as { [key: string]: any })[namesOfField.field] = setValue;
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((state: { [key: string]: any }) => {
366
- (state[names.modelForm] as { [key: string]: any })[namesOfField.field] = [
367
- ...(form[namesOfField.field] as object[]).slice(0, idx),
368
- ...(Array.isArray(setValue) ? setValue : [setValue]),
369
- ...(form[namesOfField.field] as object[]).slice(idx),
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((state: { [key: string]: any }) => {
376
- (state[names.modelForm] as { [key: string]: any })[namesOfField.field] =
377
- typeof idx === "number"
378
- ? form[namesOfField.field].filter((_, i) => i !== idx)
379
- : form[namesOfField.field].filter((_, i) => !idx.includes(i));
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.set((state: UploadState) => {
410
- const tickets = state[names.modelFormUploading][namesOfField.field] ?? [];
411
- state[names.modelFormUploading] = {
412
- ...state[names.modelFormUploading],
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 held = (state: { [key: string]: any }) =>
417
- !!(state as UploadState)[names.modelFormUploading][namesOfField.field]?.includes(ticket) &&
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((state: UploadState) => releaseUpload(state, namesOfField.field, ticket));
442
+ this.set({ [names.modelFormUploading]: releasedIn(this.get() as StateRecord) });
424
443
  throw error;
425
444
  }
426
- let isWritten = false;
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((state: { [key: string]: any }) => {
429
- isWritten = held(state);
430
- releaseUpload(state as UploadState, namesOfField.field, ticket);
431
- if (!isWritten) return;
432
- const form = state[names.modelForm] as { [key: string]: ProtoLightFile[] | ProtoLightFile | null };
433
- if (field.isArray) {
434
- const current = (form[namesOfField.field] as ProtoLightFile[] | null) ?? [];
435
- const idx = Math.min(index ?? current.length, current.length);
436
- form[namesOfField.field] = [...current.slice(0, idx), ...files, ...current.slice(idx)];
437
- } else form[namesOfField.field] = files[0] ?? null;
438
- });
439
- if (!isWritten) return;
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
- files.forEach((file) => {
456
- let attemptsLeft = UPLOAD_POLL_ATTEMPTS;
457
- const intervalKey = setInterval(() => {
458
- void (async () => {
459
- if (!holdsFile(file.id)) {
460
- clearInterval(intervalKey);
461
- return;
462
- }
463
- attemptsLeft -= 1;
464
- try {
465
- const currentFile = await (
466
- (fetch as { [key: string]: any })[fileReadName] as (id: string) => Promise<ProtoLightFile>
467
- )(file.id);
468
- if (!holdsFile(currentFile.id)) {
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
- function armDraft(this: SetGet, draftScope: string | undefined, form: object, baseUpdatedAt: string | null) {
544
- openFormBase = draftScope ? form : null;
545
- if (!draftScope) return null;
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
- this.set({ [names.modelLoading]: true });
644
- const model = await servedFetch<Full>(names.createModel)(modelInput, { onError });
645
- this.set({
646
- [names.modelForm]: immerify(modelRef, defaultModel),
647
- [names.modelDraft]: clearDraft(currentState[names.modelDraft] as DraftState | null),
648
- [names.model]: model,
649
- [names.modelLoading]: false,
650
- [names.modelViewAt]: new Date(),
651
- [names.modelModal]: modal ?? null,
652
- ...staleAtOfSlices(),
653
- ...(typeof path === "string" && path ? { [path]: model } : {}),
654
- ...createdListPatchOf(sliceName, model, this.get()),
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
- if (model?.id === modelForm.id) this.set({ [names.modelLoading]: modelForm.id });
670
- const updatedModel = await servedFetch<Full>(names.updateModel)(modelForm.id, modelInput, { onError });
671
- this.set({
672
- ...(model?.id === updatedModel.id
673
- ? { [names.model]: updatedModel, [names.modelLoading]: false, [names.modelViewAt]: new Date() }
674
- : {}),
675
- [names.modelForm]: immerify(modelRef, defaultModel),
676
- [names.modelDraft]: clearDraft(currentState[names.modelDraft] as DraftState | null),
677
- [names.modelModal]: modal ?? null,
678
- ...staleAtOfSlices(),
679
- ...(typeof path === "string" && path ? { [path]: updatedModel } : {}),
680
- ...updatedListPatchOf(this.get(), updatedModel),
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
- this.set({ [names.modelLoading]: true });
692
- const model = await servedFetch<Full>(names.createModel)(modelInput, { onError });
693
- this.set({
694
- [names.model]: model,
695
- [names.modelLoading]: false,
696
- [names.modelViewAt]: new Date(),
697
- [names.modelModal]: modal ?? null,
698
- ...staleAtOfSlices(),
699
- ...(typeof path === "string" && path ? { [path]: model } : {}),
700
- ...createdListPatchOf(sliceName, model, this.get()),
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
- if (model?.id === id) this.set({ [names.modelLoading]: id });
716
- const updatedModel = await servedFetch<Full>(names.updateModel)(id, modelInput, {
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
- this.set({
720
- ...(model?.id === updatedModel.id
721
- ? { [names.model]: updatedModel, [names.modelLoading]: false, [names.modelViewAt]: new Date() }
722
- : {}),
723
- [names.modelModal]: modal ?? null,
724
- ...staleAtOfSlices(),
725
- ...(typeof path === "string" && path ? { [path]: updatedModel } : {}),
726
- ...updatedListPatchOf(this.get(), updatedModel),
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 (this: SetGet, id: string, options?: FetchPolicy & { modal?: string | null }) {
731
- const { modal, ...fetchPolicyOptions } = options ?? {};
732
- const model = await servedFetch<Full & { removedAt: Dayjs | null }>(names.removeModel)(id, fetchPolicyOptions);
733
- const currentState = this.get() as { [key: string]: unknown };
734
- if (!model.removedAt) {
735
- const listPatch = updatedListPatchOf(currentState, model);
736
- this.set({
737
- ...listPatch,
738
- ...(modal !== undefined && Object.keys(listPatch).length ? { [names.modelModal]: modal } : {}),
739
- ...staleAtOfSlices(),
740
- });
741
- return;
742
- }
743
- const removedPatch: { [key: string]: unknown } = {};
744
- for (const { keys } of slices) {
745
- const namesOfSlice = keys.state;
746
- const modelList = currentState[namesOfSlice.modelList] as DataList<Light>;
747
- const modelSelection = currentState[namesOfSlice.modelSelection] as DataList<Light>;
748
- const modelInsight = currentState[namesOfSlice.modelInsight] as Insight & BaseInsight;
749
- if (currentState[namesOfSlice.modelListLoading] || !modelList.has(model.id)) continue;
750
- removedPatch[namesOfSlice.modelList] = new DataList(modelList).delete(id);
751
- removedPatch[namesOfSlice.modelInsight] = new cnst.insight().set({
752
- ...modelInsight,
753
- count: modelInsight.count - 1,
754
- });
755
- if (modelSelection.has(model.id))
756
- removedPatch[namesOfSlice.modelSelection] = new DataList(modelSelection).delete(id);
757
- if (modal !== undefined) removedPatch[names.modelModal] = modal;
758
- }
759
- this.set({ ...removedPatch, ...staleAtOfSlices() });
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 & FetchPolicy = {},
762
+ { modal, onError, draftScope }: EditOption & ReportingFetchPolicy = {},
809
763
  ) {
810
764
  const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
811
- this.set({ [names.modelFormLoading]: id, [names.modelModal]: modal ?? "edit" });
812
- const model = await servedFetch<Full>(names.model)(id, { onError });
813
- const modelForm = immerify(modelRef, deepObjectify<Input>(model as unknown as Input) as object) as Input;
814
- this.set({
815
- [names.model]: model,
816
- [names.modelFormLoading]: false,
817
- [names.modelViewAt]: new Date(),
818
- [names.modelForm]: modelForm,
819
- [names.modelDraft]: armDraft.call(this, draftScope, modelForm, updatedAtOf(model)),
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?: FetchPolicy,
795
+ options?: ReportingFetchPolicy,
829
796
  ) {
797
+ const { onError, ...fetchPolicyOptions } = options ?? {};
830
798
  const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
831
- const currentState = this.get() as { [key: string]: any };
832
- const model = currentState[names.model] as Full | null;
833
- if (id === model?.id) this.set({ modelLoading: id });
834
- const updatedModel = await servedFetch<Full>(names.mergeModel)(modelOrId, data, options);
835
- this.set({
836
- [names.model]: id === model?.id ? updatedModel : model,
837
- [names.modelLoading]: false,
838
- ...staleAtOfSlices(),
839
- ...updatedListPatchOf(this.get(), updatedModel),
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 } & FetchPolicy = {},
817
+ { modal, onError }: { modal?: string | null } & ReportingFetchPolicy = {},
846
818
  ) {
847
819
  const id = typeof modelOrId === "string" ? modelOrId : modelOrId.id;
848
- this.set({ [names.modelModal]: modal ?? "view", [names.modelLoading]: id });
849
- const model = await servedFetch<Full>(names.model)(id, { onError });
850
- this.set({ [names.model]: model, [names.modelViewAt]: new Date(), [names.modelLoading]: false });
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
- void DraftStore.remove(draft.key);
888
+ forgetDraft(this, draft.key);
906
889
 
907
890
  this.set({
908
- ...(draft.appliedAt && openFormBase ? { [names.modelForm]: openFormBase } : {}),
891
+ ...(draft.appliedAt && formBase() ? { [names.modelForm]: formBase() } : {}),
909
892
  [names.modelDraft]: { ...draft, pending: null, appliedAt: null },
910
893
  });
911
894
  },