@arrai-innovations/reactive-helpers 20.0.1 → 20.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,15 +10,16 @@ import { readonly } from "vue";
10
10
 
11
11
  /**
12
12
  * @typedef {object} PaginateInfo
13
- * @property {number} totalRecords - The total records.
14
- * @property {number} totalPages - The total pages.
15
- * @property {number} perPage - The per page.
13
+ * @property {number} [totalRecords] - The total records.
14
+ * @property {number} [totalPages] - The total pages.
15
+ * @property {number} [perPage] - The per page.
16
+ * @property {number} [page] - The page you are giving us results for.
16
17
  */
17
18
 
18
19
  /**
19
20
  * @typedef {(
20
21
  * newObjects:import('../use/listInstance.js').ListObject,
21
- * paginationInfo: PaginateInfo|undefined
22
+ * paginationInfo?: PaginateInfo,
22
23
  * )=>void} PageCallback
23
24
  */
24
25
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrai-innovations/reactive-helpers",
3
- "version": "20.0.1",
3
+ "version": "20.1.0",
4
4
  "description": "VueJS 3 utility composition functions to help manipulate objects and lists.",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -13,17 +13,21 @@ export type PaginateInfo = {
13
13
  /**
14
14
  * - The total records.
15
15
  */
16
- totalRecords: number;
16
+ totalRecords?: number;
17
17
  /**
18
18
  * - The total pages.
19
19
  */
20
- totalPages: number;
20
+ totalPages?: number;
21
21
  /**
22
22
  * - The per page.
23
23
  */
24
- perPage: number;
24
+ perPage?: number;
25
+ /**
26
+ * - The page you are giving us results for.
27
+ */
28
+ page?: number;
25
29
  };
26
- export type PageCallback = (newObjects: import("../use/listInstance.js").ListObject, paginationInfo: PaginateInfo | undefined) => void;
30
+ export type PageCallback = (newObjects: import("../use/listInstance.js").ListObject, paginationInfo?: PaginateInfo) => void;
27
31
  export type ListArgs = {
28
32
  /**
29
33
  * - The arguments to be passed to the crud handlers.
@@ -1 +1 @@
1
- {"version":3,"file":"listCrud.d.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,UAAU,GAAC,MAAM,EAC7E,MAAM,EAAE,QAAQ,GAAC,QAAQ,GAAC,QAAQ,KACnC,IAAI;;;;;YAKI,MAAM;;;;WACN,MAAM;;;;YACN,MAAM;;;;+BACN,yBAAyB;;;;;iBACzB,QAAQ,CAAC,OAAO,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;;;;;;YAMpC,MAAM;;;;SACN,MAAM,EAAE;;;;WACR,MAAM;;;;YACN,MAAM;;;;;iBACN,QAAQ,CAAC,OAAO,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;;gCAMvC,QAAQ,KACN,OAAO,gCAAgC,EAAE,uBAAuB,CAAC,IAAI,CAAC;sCAKxE,cAAc,KACZ,OAAO,gCAAgC,EAAE,uBAAuB,CAAC,IAAI,CAAC;yCAKxE,iBAAiB,KACf,OAAO,gCAAgC,EAAE,kBAAkB,CAAC,IAAI,CAAC;yCAKnE,iBAAiB,KACf,OAAO,gCAAgC,EAAE,uBAAuB,CAAC,MAAM,GAAC,MAAM,GAAC,IAAI,CAAC;;;;;WAKnF,UAAU;;;;iBACV,gBAAgB;;;;oBAChB,mBAAmB;;;;gBACnB,mBAAmB;;;;;;UAKnB,MAAM;;;;;;aAKN,MAAM"}
@@ -1,21 +1,3 @@
1
- /**
2
- * @module use/paginatedListInstance.js
3
- *
4
- */
5
- /**
6
- * @typedef {object} PagedListListanceOptions
7
- * @property {boolean} keepOldPages - Whether to keep old pages.
8
- */
9
- /**
10
- * @typedef {object} PagedListInstanceState
11
- * @property {number} totalRecords - The total records.
12
- * @property {number} totalPages - The total pages.
13
- * @property {number} perPage - The per page.
14
- */
15
- /**
16
- * @typedef {object} PagedListInstance
17
- * @property {import('./listInstance.js').ListInstanceState & PagedListInstanceState} state - The state.
18
- */
19
1
  /**
20
2
  *
21
3
  * @param {PagedListListanceOptions & import('./listInstance.js').ListInstanceOptions} options - The options.
@@ -28,7 +10,7 @@ export type PagedListListanceOptions = {
28
10
  */
29
11
  keepOldPages: boolean;
30
12
  };
31
- export type PagedListInstanceState = {
13
+ export type PaginatedRawState = {
32
14
  /**
33
15
  * - The total records.
34
16
  */
@@ -41,11 +23,37 @@ export type PagedListInstanceState = {
41
23
  * - The per page.
42
24
  */
43
25
  perPage: number;
26
+ /**
27
+ * - The page to ids map.
28
+ */
29
+ pageToIds: Map<number, string[]>;
30
+ };
31
+ export type PaginatedState = import("vue").UnwrapNestedRefs<PaginatedRawState>;
32
+ export type PagedListInstanceStateExtension = {
33
+ /**
34
+ * - The total records.
35
+ */
36
+ totalRecords: import("vue").Ref<number>;
37
+ /**
38
+ * - The total pages.
39
+ */
40
+ totalPages: import("vue").Ref<number>;
41
+ /**
42
+ * - The per page.
43
+ */
44
+ perPage: import("vue").Ref<number>;
45
+ /**
46
+ * - The page to ids map.
47
+ */
48
+ pageToIds: import("vue").Ref<Map<number, string[]>>;
44
49
  };
45
- export type PagedListInstance = {
50
+ export type PagedListInstanceState = typeof reactive;
51
+ export type PagedListRawInstance = {
46
52
  /**
47
53
  * - The state.
48
54
  */
49
- state: import("./listInstance.js").ListInstanceState & PagedListInstanceState;
55
+ state: PagedListInstanceState;
50
56
  };
57
+ export type PagedListInstance = import("./listInstance.js").ListInstance & PagedListRawInstance;
58
+ import { reactive } from "vue";
51
59
  //# sourceMappingURL=paginatedListInstance.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"paginatedListInstance.d.ts","sourceRoot":"","sources":["../../use/paginatedListInstance.js"],"names":[],"mappings":"AAEA;;;GAGG;AAEH;;;GAGG;AAEH;;;;;GAKG;AAEH;;;GAGG;AAEH;;;;GAIG;AACH,+EAHW,wBAAwB,GAAG,OAAO,mBAAmB,EAAE,mBAAmB,GACxE,iBAAiB,CA+C7B;;;;;kBAjEa,OAAO;;;;;;kBAKP,MAAM;;;;gBACN,MAAM;;;;aACN,MAAM;;;;;;WAKN,OAAO,mBAAmB,EAAE,iBAAiB,GAAG,sBAAsB"}
1
+ {"version":3,"file":"paginatedListInstance.d.ts","sourceRoot":"","sources":["../../use/paginatedListInstance.js"],"names":[],"mappings":"AA2DA;;;;GAIG;AACH,+EAHW,wBAAwB,GAAG,OAAO,mBAAmB,EAAE,mBAAmB,GACxE,iBAAiB,CA6D7B;;;;;kBAjHa,OAAO;;;;;;kBAKP,MAAM;;;;gBACN,MAAM;;;;aACN,MAAM;;;;eACN,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;;6BAItB,OAAO,KAAK,EAAE,gBAAgB,CAAC,iBAAiB,CAAC;;;;;kBAKhD,OAAO,KAAK,EAAE,GAAG,CAAC,MAAM,CAAC;;;;gBACzB,OAAO,KAAK,EAAE,GAAG,CAAC,MAAM,CAAC;;;;aACzB,OAAO,KAAK,EAAE,GAAG,CAAC,MAAM,CAAC;;;;eACzB,OAAO,KAAK,EAAE,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;;;;;;;WAYxC,sBAAsB;;gCAIvB,OAAO,mBAAmB,EAAE,YAAY,GAAG,oBAAoB;yBA7C3C,KAAK"}
@@ -1,4 +1,5 @@
1
1
  import { useListInstance } from "./listInstance.js";
2
+ import { reactive, toRefs } from "vue";
2
3
 
3
4
  /**
4
5
  * @module use/paginatedListInstance.js
@@ -11,65 +12,113 @@ import { useListInstance } from "./listInstance.js";
11
12
  */
12
13
 
13
14
  /**
14
- * @typedef {object} PagedListInstanceState
15
+ * @typedef {object} PaginatedRawState
15
16
  * @property {number} totalRecords - The total records.
16
17
  * @property {number} totalPages - The total pages.
17
18
  * @property {number} perPage - The per page.
19
+ * @property {Map<number, string[]>} pageToIds - The page to ids map.
18
20
  */
19
21
 
20
22
  /**
21
- * @typedef {object} PagedListInstance
22
- * @property {import('./listInstance.js').ListInstanceState & PagedListInstanceState} state - The state.
23
+ * @typedef {import('vue').UnwrapNestedRefs<PaginatedRawState>} PaginatedState
23
24
  */
24
25
 
26
+ /**
27
+ * @typedef {object} PagedListInstanceStateExtension
28
+ * @property {import('vue').Ref<number>} totalRecords - The total records.
29
+ * @property {import('vue').Ref<number>} totalPages - The total pages.
30
+ * @property {import('vue').Ref<number>} perPage - The per page.
31
+ * @property {import('vue').Ref<Map<number, string[]>>} pageToIds - The page to ids map.
32
+ */
33
+
34
+ /**
35
+ * @typedef {import('vue').reactive<(
36
+ * import('./listInstance.js').ListInstanceRawState &
37
+ * PagedListInstanceStateExtension
38
+ * )>} PagedListInstanceState
39
+ */
40
+
41
+ /**
42
+ * @typedef {object} PagedListRawInstance
43
+ * @property {PagedListInstanceState} state - The state.
44
+ */
45
+
46
+ /**
47
+ * @typedef {import('./listInstance.js').ListInstance & PagedListRawInstance} PagedListInstance
48
+ */
49
+
50
+ /**
51
+ * @param {import('./listInstance.js').ListInstance} listInstance - The list instance.
52
+ * @param {PaginatedState} paginatedState - The paginated state.
53
+ * @returns {PagedListInstance} - The paged list instance.
54
+ */
55
+ function makePagedListInstance(listInstance, paginatedState) {
56
+ Object.assign(listInstance.state, toRefs(paginatedState));
57
+ return /** @type {PagedListInstance} */ (listInstance);
58
+ }
59
+
25
60
  /**
26
61
  *
27
62
  * @param {PagedListListanceOptions & import('./listInstance.js').ListInstanceOptions} options - The options.
28
63
  * @returns {PagedListInstance} - The paged list instance.
29
64
  */
30
65
  export function usePagedListInstance({ keepOldPages, ...useListInstanceArgs }) {
31
- const listInstance = /** @type {PagedListInstance & import('./listInstance.js').ListInstance} */ (
32
- /** @type {unknown} */ (useListInstance({ keepOldPages, ...useListInstanceArgs }))
33
- );
66
+ /** @type {PaginatedState} */
67
+ const paginatedState = reactive({
68
+ totalRecords: 0,
69
+ totalPages: 0,
70
+ perPage: 0,
71
+ pageToIds: new Map(),
72
+ });
34
73
 
35
- listInstance.state.totalRecords = 0;
36
- listInstance.state.totalPages = 0;
37
- listInstance.state.perPage = 0;
74
+ const paginatedListInstance = makePagedListInstance(
75
+ useListInstance({ keepOldPages, ...useListInstanceArgs }),
76
+ paginatedState
77
+ );
38
78
 
39
- const superClearList = listInstance.clearList;
40
- listInstance.clearList = () => {
79
+ const superClearList = paginatedListInstance.clearList;
80
+ paginatedListInstance.clearList = () => {
41
81
  superClearList();
42
- listInstance.state.totalRecords = 0;
43
- listInstance.state.totalPages = 0;
44
- listInstance.state.perPage = 0;
82
+ paginatedState.totalRecords = 0;
83
+ paginatedState.totalPages = 0;
84
+ paginatedState.perPage = 0;
45
85
  };
46
86
  if (keepOldPages === undefined) {
47
87
  throw new Error("keepOldPages is required");
48
88
  }
49
89
 
50
- listInstance.pageCallback = (newObjects, { totalRecords, totalPages, perPage }) => {
90
+ paginatedListInstance.pageCallback = (
91
+ /** @type {import('./listInstance.js').ListObject[]} */ newObjects,
92
+ /** @type {import('../config/listCrud.js').PaginateInfo} */ { page, totalRecords, totalPages, perPage }
93
+ ) => {
51
94
  // with keepOldPages, you are responsible for clearing the list as needed
52
95
  if (!keepOldPages) {
53
96
  // display one page at a time, clear the list
54
- listInstance.clearList();
97
+ paginatedListInstance.clearList();
55
98
  }
56
99
  newObjects.forEach((newObject) => {
57
- if (newObject[listInstance.state.pkKey] in listInstance.state.objects) {
58
- listInstance.updateListObject(newObject);
100
+ if (newObject[paginatedListInstance.state.pkKey] in paginatedListInstance.state.objects) {
101
+ paginatedListInstance.updateListObject(newObject);
59
102
  } else {
60
- listInstance.addListObject(newObject);
103
+ paginatedListInstance.addListObject(newObject);
61
104
  }
62
105
  });
106
+ if (keepOldPages && page !== undefined) {
107
+ const pageIds = newObjects.map((obj) => obj[paginatedListInstance.state.pkKey] + "");
108
+ // webstorm being crazy
109
+ // noinspection JSUnresolvedReference
110
+ paginatedState.pageToIds.set(page, pageIds);
111
+ }
63
112
  if (totalRecords !== undefined) {
64
- listInstance.state.totalRecords = totalRecords;
113
+ paginatedState.totalRecords = totalRecords;
65
114
  }
66
115
  if (totalPages !== undefined) {
67
- listInstance.state.totalPages = totalPages;
116
+ paginatedState.totalPages = totalPages;
68
117
  }
69
118
  if (perPage !== undefined) {
70
- listInstance.state.perPage = perPage;
119
+ paginatedState.perPage = perPage;
71
120
  }
72
121
  };
73
122
 
74
- return listInstance;
123
+ return paginatedListInstance;
75
124
  }