@arrai-innovations/reactive-helpers 1.0.1 → 1.0.3

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.
@@ -1,9 +1,8 @@
1
1
  version: 2.1
2
2
  orbs:
3
- eslint: arrai/eslint@7.3.0
3
+ eslint: arrai/eslint@7.4.0
4
4
  prettier: arrai/prettier@5.1.1
5
- utils: arrai/utils@1.9.0
6
- npm: arrai/npm@2.3.2
5
+ npm: arrai/npm@2.4.1
7
6
  github: arrai/github@1.1.0
8
7
  workflows:
9
8
  test:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrai-innovations/reactive-helpers",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "VueJS 3 utility composition functions to help manipulate objects and lists.",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -421,4 +421,53 @@ describe("use/listInstance.spec.js", function () {
421
421
  expect(fakeId).toBeTruthy();
422
422
  });
423
423
  });
424
+ it("computes objectsInOrder and maintains state", () => {
425
+ const crudListResolvedPage3 = [
426
+ {
427
+ id: 3,
428
+ __str__: "qwer",
429
+ name: "qwer",
430
+ },
431
+ {
432
+ id: 8,
433
+ __str__: "asfd",
434
+ name: "asdf",
435
+ },
436
+ {
437
+ id: 2,
438
+ __str__: "zxcv",
439
+ name: "zxcv",
440
+ },
441
+ ];
442
+ const addObject = {
443
+ id: 4,
444
+ __str__: "yuio",
445
+ name: "yiuo",
446
+ };
447
+ const listInstance = useListInstance({
448
+ defaultListArgs: { user: 1 },
449
+ defaultRetrieveArgs: { fields: fields },
450
+ });
451
+ let crudListResolve;
452
+ const crudListPromise = new Promise((resolve) => {
453
+ crudListResolve = resolve;
454
+ });
455
+ let passedPageCallback;
456
+ globalList.mockImplementation(({ pageCallback }) => {
457
+ passedPageCallback = pageCallback;
458
+ return crudListPromise;
459
+ });
460
+
461
+ listInstance.list();
462
+
463
+ passedPageCallback(crudListResolvedPage3);
464
+ crudListResolve();
465
+ expect(listInstance.state.objectsInOrder).toEqual(crudListResolvedPage3);
466
+ listInstance.addListObject(addObject);
467
+ crudListResolvedPage3.push(addObject);
468
+ expect(listInstance.state.objectsInOrder).toEqual(crudListResolvedPage3);
469
+ listInstance.deleteListObject(8);
470
+ crudListResolvedPage3.splice(1, 1);
471
+ expect(listInstance.state.objectsInOrder).toEqual(crudListResolvedPage3);
472
+ });
424
473
  });
package/use/index.js CHANGED
@@ -5,4 +5,5 @@ export * from "./listSort.js";
5
5
  export * from "./listSubscription.js";
6
6
  export * from "./objectInstance.js";
7
7
  export * from "./objectSubscription.js";
8
+ export * from "./paginatedListInstance.js";
8
9
  export * from "./search.js";
@@ -1,6 +1,6 @@
1
- import { isEmpty, keyBy } from "lodash";
2
- import { effectScope, reactive, unref } from "vue";
3
- import { addOrUpdateReactiveObject, assignReactiveObject } from "../utils/assignReactiveObject";
1
+ import { isEmpty } from "lodash";
2
+ import { computed, effectScope, reactive, unref } from "vue";
3
+ import { assignReactiveObject } from "../utils/assignReactiveObject";
4
4
  import inspect from "browser-util-inspect";
5
5
 
6
6
  export class ListError extends Error {
@@ -48,6 +48,16 @@ export function useListInstance({ crudArgs, defaultListArgs = {}, defaultRetriev
48
48
  assignReactiveObject(state.listInstanceCrud.args, crudArgs);
49
49
  }
50
50
 
51
+ const defaultPageCallback = (newObjects) => {
52
+ newObjects.forEach((newObject) => {
53
+ if (newObject.id in state.objects) {
54
+ updateListObject(newObject);
55
+ } else {
56
+ addListObject(newObject);
57
+ }
58
+ });
59
+ };
60
+
51
61
  async function list({ listArgs, retrieveArgs } = {}) {
52
62
  if (state.loading) {
53
63
  throw new ListError("already loading.");
@@ -66,9 +76,7 @@ export function useListInstance({ crudArgs, defaultListArgs = {}, defaultRetriev
66
76
  crudArgs: state.listInstanceCrud.args,
67
77
  retrieveArgs,
68
78
  listArgs,
69
- pageCallback: (newObjects) => {
70
- addOrUpdateReactiveObject(state.objects, keyBy(newObjects, "id"));
71
- },
79
+ pageCallback: returnedObject.pageCallback,
72
80
  })
73
81
  .then(() => {
74
82
  return Promise.resolve(true);
@@ -119,6 +127,13 @@ export function useListInstance({ crudArgs, defaultListArgs = {}, defaultRetriev
119
127
  delete state.objects[objectId];
120
128
  }
121
129
 
130
+ function clearList() {
131
+ state.addedOrder.splice(0);
132
+ for (const item in state.objects) {
133
+ delete state.objects[item];
134
+ }
135
+ }
136
+
122
137
  function getFakeId() {
123
138
  let fakeId;
124
139
  do {
@@ -129,16 +144,21 @@ export function useListInstance({ crudArgs, defaultListArgs = {}, defaultRetriev
129
144
 
130
145
  const es = effectScope();
131
146
 
132
- // we could have effects? let's keep the interface to keep our options open to add without major changes.
133
- es.run(() => {});
147
+ es.run(() => {
148
+ state.objectsInOrder = computed(() => state.addedOrder.map((id) => state.objects[id]));
149
+ });
134
150
 
135
- return {
151
+ const returnedObject = {
136
152
  state,
137
153
  list,
138
154
  addListObject,
139
155
  updateListObject,
140
156
  deleteListObject,
157
+ clearList,
141
158
  effectScope: es,
142
159
  getFakeId,
160
+ defaultPageCallback,
161
+ pageCallback: defaultPageCallback,
143
162
  };
163
+ return returnedObject;
144
164
  }
@@ -0,0 +1,19 @@
1
+ import { useListInstance } from "./listInstance";
2
+
3
+ export function usePagedListInstance({ crudArgs, defaultListArgs = {}, defaultRetrieveArgs = {} }) {
4
+ const listInstance = useListInstance({ crudArgs, defaultListArgs, defaultRetrieveArgs });
5
+
6
+ listInstance.state.count = 0;
7
+
8
+ listInstance.pageCallback = (newObjects, count) => {
9
+ // display one page at a time, clear the list
10
+ listInstance.clearList();
11
+
12
+ listInstance.defaultPageCallback(newObjects);
13
+ if (count !== undefined) {
14
+ listInstance.state.count = count;
15
+ }
16
+ };
17
+
18
+ return listInstance;
19
+ }