@arrai-innovations/reactive-helpers 1.0.1 → 1.0.2

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": "@arrai-innovations/reactive-helpers",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
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
  });
@@ -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 {
@@ -67,7 +67,13 @@ export function useListInstance({ crudArgs, defaultListArgs = {}, defaultRetriev
67
67
  retrieveArgs,
68
68
  listArgs,
69
69
  pageCallback: (newObjects) => {
70
- addOrUpdateReactiveObject(state.objects, keyBy(newObjects, "id"));
70
+ newObjects.forEach((newObject) => {
71
+ if (newObject.id in state.objects) {
72
+ updateListObject(newObject);
73
+ } else {
74
+ addListObject(newObject);
75
+ }
76
+ });
71
77
  },
72
78
  })
73
79
  .then(() => {
@@ -129,8 +135,9 @@ export function useListInstance({ crudArgs, defaultListArgs = {}, defaultRetriev
129
135
 
130
136
  const es = effectScope();
131
137
 
132
- // we could have effects? let's keep the interface to keep our options open to add without major changes.
133
- es.run(() => {});
138
+ es.run(() => {
139
+ state.objectsInOrder = computed(() => state.addedOrder.map((id) => state.objects[id]));
140
+ });
134
141
 
135
142
  return {
136
143
  state,