akanjs 3.0.0-alpha.86 → 3.0.0-alpha.88

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akanjs",
3
- "version": "3.0.0-alpha.86",
3
+ "version": "3.0.0-alpha.88",
4
4
  "sourceType": "module",
5
5
  "type": "module",
6
6
  "publishConfig": {
package/store/action.ts CHANGED
@@ -516,12 +516,11 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
516
516
  queryArgsOfModel: `queryArgsOf${className}`,
517
517
  sortOfModel: `sortOf${className}`,
518
518
  };
519
- const staleAtOfOtherSlices = (createdSliceName: string) => {
519
+
520
+ const staleAtOfSlices = () => {
520
521
  const staleAt = new Date();
521
522
  return Object.fromEntries(
522
- slices
523
- .filter(({ sliceName }) => sliceName !== createdSliceName)
524
- .map(({ sliceName }) => [sliceName.replace(names.model, names.modelStaleAt), staleAt]),
523
+ slices.map(({ sliceName }) => [sliceName.replace(names.model, names.modelStaleAt), staleAt]),
525
524
  );
526
525
  };
527
526
  const baseAction = {
@@ -562,7 +561,7 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
562
561
  [namesOfSlice.modelInsight]: newModelInsight,
563
562
  [names.modelViewAt]: new Date(),
564
563
  [names.modelModal]: modal ?? null,
565
- ...staleAtOfOtherSlices(sliceName),
564
+ ...staleAtOfSlices(),
566
565
  ...(typeof path === "string" && path ? { [path]: model } : {}),
567
566
  });
568
567
  await onSuccess?.(model);
@@ -593,6 +592,7 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
593
592
  : {}),
594
593
  [names.modelForm]: immerify(modelRef, defaultModel),
595
594
  [names.modelModal]: modal ?? null,
595
+ ...staleAtOfSlices(),
596
596
  ...(typeof path === "string" && path ? { [path]: updatedModel } : {}),
597
597
  });
598
598
  const updatedLightModel = new cnst.light().set(updatedModel) as unknown as Light;
@@ -645,7 +645,7 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
645
645
  [namesOfSlice.modelInsight]: newModelInsight,
646
646
  [names.modelViewAt]: new Date(),
647
647
  [names.modelModal]: modal ?? null,
648
- ...staleAtOfOtherSlices(sliceName),
648
+ ...staleAtOfSlices(),
649
649
  ...(typeof path === "string" && path ? { [path]: model } : {}),
650
650
  });
651
651
  await onSuccess?.(model);
@@ -669,6 +669,7 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
669
669
  ? { [names.model]: updatedModel, [names.modelLoading]: false, [names.modelViewAt]: new Date() }
670
670
  : {}),
671
671
  [names.modelModal]: modal ?? null,
672
+ ...staleAtOfSlices(),
672
673
  ...(typeof path === "string" && path ? { [path]: updatedModel } : {}),
673
674
  });
674
675
  const updatedLightModel = new cnst.light().set(updatedModel) as unknown as Light;
@@ -729,6 +730,7 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
729
730
  });
730
731
  }
731
732
  });
733
+ this.set(staleAtOfSlices());
732
734
  },
733
735
  [names.checkModelSubmitable]: function (this: SetGet, disabled?: boolean) {
734
736
  const currentState = this.get() as { [key: string]: any };
@@ -806,6 +808,7 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
806
808
  this.set({
807
809
  [names.model]: id === model?.id ? updatedModel : model,
808
810
  [names.modelLoading]: false,
811
+ ...staleAtOfSlices(),
809
812
  });
810
813
  const updatedLightModel = new cnst.light().set(updatedModel) as unknown as Light;
811
814
  slices.forEach(({ sliceName }) => {
@@ -863,6 +866,7 @@ export const makeActions = (refName: string, slice: { [key: string]: SerializedS
863
866
  });
864
867
  this.set({ [namesOfSlice.modelList]: modelList.save() });
865
868
  });
869
+ this.set(staleAtOfSlices());
866
870
  },
867
871
  [names.resetModel]: function (this: SetGet, model?: Full) {
868
872
  const currentState = this.get() as { [key: string]: any };
package/ui/Load/Units.tsx CHANGED
@@ -65,7 +65,7 @@ function Render<RefName extends string, Light extends { id: string }>({
65
65
  pagination,
66
66
  staleTime,
67
67
  }: RenderProps<RefName, Light>) {
68
- const loaded = useRef(false);
68
+ const loadedQueryArgs = useRef<object[] | null>(null);
69
69
  const storeUse = st.use as { [key: string]: () => unknown };
70
70
  const storeDo = st.do as unknown as { [key: string]: (...args: any[]) => Promise<void> };
71
71
  const storeGet = st.get as unknown as <T>() => { [key: string]: T };
@@ -114,23 +114,26 @@ function Render<RefName extends string, Light extends { id: string }>({
114
114
  const initModelObjInsight = (init as DynamicRecord)[names.modelObjInsight] as BaseInsight | null;
115
115
  const initLimitOfModel = (init as DynamicRecord)[names.limitOfModel] as number;
116
116
  const initPageOfModel = (init as DynamicRecord)[names.pageOfModel] as number;
117
+ const initSignature = JSON.stringify(initQueryArgs);
117
118
 
118
119
  const useCache =
119
120
  !modelListLoading &&
120
121
  isQueryEqual(storeGet<object[]>()[namesOfSlice.queryArgsOfModel], initQueryArgs) &&
121
122
  storeGet<Date>()[namesOfSlice.modelInitAt].getTime() >= initModelInitAt.getTime();
122
- if (useCache) loaded.current = true;
123
+ if (useCache) loadedQueryArgs.current = initQueryArgs;
124
+
125
+ const loaded = !!loadedQueryArgs.current && isQueryEqual(loadedQueryArgs.current, initQueryArgs);
123
126
 
124
127
  const modelInitList = useMemo<DataList<Light>>(() => {
125
- if (loaded.current) return modelList;
128
+ if (loaded) return modelList;
126
129
  const initModelObjList = (init as DynamicRecord)[names.modelObjList] as Light[];
127
130
  return new DataList<Light>(
128
131
  withSharedInstances(() => initModelObjList.map((model) => new cnst.light().set(model) as unknown as Light)),
129
132
  );
130
- }, []);
133
+ }, [initSignature]);
131
134
 
132
135
  useEffect(() => {
133
- if (loaded.current) return;
136
+ if (loaded) return;
134
137
  const modelObjInsight = (init as DynamicRecord)[names.modelObjInsight] as BaseInsight | null;
135
138
 
136
139
  const insight = new cnst.insight().set(
@@ -153,8 +156,8 @@ function Render<RefName extends string, Light extends { id: string }>({
153
156
  [namesOfSlice.queryArgsOfModel]: initQueryArgsOfModel,
154
157
  [namesOfSlice.sortOfModel]: initSortOfModel,
155
158
  });
156
- loaded.current = true;
157
- }, []);
159
+ loadedQueryArgs.current = initQueryArgs;
160
+ }, [initSignature]);
158
161
 
159
162
  useEffect(() => {
160
163
  const modelStaleAt = storeGet<Date>()[namesOfSlice.modelStaleAt];
@@ -162,15 +165,15 @@ function Render<RefName extends string, Light extends { id: string }>({
162
165
  if (storeGet<Date>()[namesOfSlice.modelInitAt].getTime() >= staleThreshold) return;
163
166
  if (storeGet<boolean>()[namesOfSlice.modelListLoading]) return;
164
167
  void storeDo[namesOfSlice.refreshModel]({ invalidate: true });
165
- }, []);
168
+ }, [initSignature]);
166
169
 
167
170
  const modelInsight = storeUse[namesOfSlice.modelInsight]() as BaseInsight;
168
171
  const limitOfModel = storeUse[namesOfSlice.limitOfModel]() as number;
169
172
  const pageOfModel = storeUse[namesOfSlice.pageOfModel]() as number;
170
- const insight = loaded.current ? modelInsight : initModelObjInsight;
171
- const limit = loaded.current ? limitOfModel : initLimitOfModel;
172
- const page = loaded.current ? pageOfModel : initPageOfModel;
173
- const total = insight?.count ?? (loaded.current ? modelList : modelInitList).length;
173
+ const insight = loaded ? modelInsight : initModelObjInsight;
174
+ const limit = loaded ? limitOfModel : initLimitOfModel;
175
+ const page = loaded ? pageOfModel : initPageOfModel;
176
+ const total = insight?.count ?? (loaded ? modelList : modelInitList).length;
174
177
  const moreProps = {
175
178
  total,
176
179
  currentPage: page,
@@ -197,7 +200,7 @@ function Render<RefName extends string, Light extends { id: string }>({
197
200
  onSelect: (page) => moreProps.onPageSelect(page, { scrollToTop: false }),
198
201
  });
199
202
 
200
- const modelDataList = !loaded.current ? modelInitList.filter(filter).sort(sort) : modelList.filter(filter).sort(sort);
203
+ const modelDataList = !loaded ? modelInitList.filter(filter).sort(sort) : modelList.filter(filter).sort(sort);
201
204
  const scopePath = useScreenScope({
202
205
  id: sliceName,
203
206
  kind: refName,
@@ -207,7 +210,7 @@ function Render<RefName extends string, Light extends { id: string }>({
207
210
  return { id: item.id, ...(label ? { label } : {}) };
208
211
  }),
209
212
  });
210
- const showLoading = loaded.current && modelListLoading;
213
+ const showLoading = loaded && modelListLoading;
211
214
  if (renderList)
212
215
  return (
213
216
  <>
@@ -342,7 +345,7 @@ interface MoreWrapperProps {
342
345
  moreProps: MoreProps;
343
346
  }
344
347
  const MoreWrapper = ({ children, pagination, moreProps }: MoreWrapperProps) => {
345
- return pagination ? <More {...moreProps}>{children}</More> : <>{children}</>;
348
+ return pagination ? <More {...moreProps}>{children}</More> : children;
346
349
  };
347
350
 
348
351
  interface ContainerWrapperProps {
package/ui/Load/View.tsx CHANGED
@@ -1,5 +1,6 @@
1
1
  "use client";
2
2
  import { cn } from "akanjs/client";
3
+ import { capitalize } from "akanjs/common";
3
4
  import { ConstantRegistry, labelOf } from "akanjs/constant";
4
5
  import type { ClientView, ServerView } from "akanjs/fetch";
5
6
  import { st } from "akanjs/store";
@@ -40,6 +41,7 @@ function Render<T extends string, Full extends { id: string }>({
40
41
  }: RenderProps<T, Full>) {
41
42
  const loadedId = useRef<string | null>(null);
42
43
  const storeUse = st.use as { [key: string]: () => unknown };
44
+ const storeDo = st.do as unknown as { [key: string]: (...args: any[]) => Promise<void> };
43
45
  const storeGet = st.get as unknown as <T>() => { [key: string]: T };
44
46
  const { refName } = view;
45
47
  const model = storeUse[refName]() as Full | null;
@@ -72,6 +74,14 @@ function Render<T extends string, Full extends { id: string }>({
72
74
  loadedId.current = modelObj.id;
73
75
  }, [modelViewAt, modelObj.id]);
74
76
 
77
+ useEffect(() => {
78
+
79
+ const modelStaleAt = storeGet<Date | undefined>()[`${refName}StaleAt`];
80
+ if (!modelStaleAt || storeGet<Date>()[`${refName}ViewAt`].getTime() >= modelStaleAt.getTime()) return;
81
+ if (storeGet<string | boolean>()[`${refName}Loading`]) return;
82
+ void storeDo[`view${capitalize(refName)}`](modelObj.id);
83
+ }, [modelViewAt, modelObj.id]);
84
+
75
85
  const renderModel = loadedId.current === modelObj.id ? model : modelInit;
76
86
  const scopePath = useScreenScope({
77
87
  id: `${refName}-view`,