@arrai-innovations/reactive-helpers 11.0.2 → 11.0.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrai-innovations/reactive-helpers",
3
- "version": "11.0.2",
3
+ "version": "11.0.4",
4
4
  "description": "VueJS 3 utility composition functions to help manipulate objects and lists.",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -3,7 +3,7 @@ import { nextTick, reactive, ref, unref } from "vue";
3
3
  import { deepUnref } from "vue-deepunref";
4
4
 
5
5
  describe("use/listSearch", () => {
6
- let useListInstance, useListSearch, useListSearches, useListSort, useListRelated, useListCalculated;
6
+ let useListInstance, useListSearch, useListSearches, useListSort, useListRelated, useListCalculated, useListFilter;
7
7
  beforeEach(async () => {
8
8
  const listInstanceModule = await import("../../../use/listInstance");
9
9
  useListInstance = listInstanceModule.useListInstance;
@@ -16,6 +16,8 @@ describe("use/listSearch", () => {
16
16
  useListRelated = listRelatedModule.useListRelated;
17
17
  const listCalculatedModule = await import("../../../use/listCalculated");
18
18
  useListCalculated = listCalculatedModule.useListCalculated;
19
+ const listFilterModule = await import("../../../use/listFilter");
20
+ useListFilter = listFilterModule.useListFilter;
19
21
  });
20
22
  it("should match by search term", async () => {
21
23
  const textSearchValue = ref("one");
@@ -502,4 +504,76 @@ describe("use/listSearch", () => {
502
504
  prop: "running",
503
505
  });
504
506
  });
507
+ it("should update when parentState is filtered in pass through mode.", async () => {
508
+ const listInstance = useListInstance({
509
+ props: {},
510
+ });
511
+ const allowedFilter = ref((obj) => !obj.filtered);
512
+ const listFilter = useListFilter({
513
+ parentState: listInstance.state,
514
+ allowedFilter,
515
+ });
516
+ const textSearchValue = ref("");
517
+ const listSearch = useListSearch({
518
+ parentState: listFilter.state,
519
+ props: reactive({
520
+ textSearchRules: ["name"],
521
+ textSearchValue,
522
+ }),
523
+ throttle: 20,
524
+ });
525
+ await doAwaitNot({
526
+ obj: listSearch.state,
527
+ prop: "running",
528
+ });
529
+ expect(listSearch.state.objects).toEqual({});
530
+ expect(listSearch.state.order).toEqual([]);
531
+ expect(listSearch.state.objectsInOrder).toEqual([]);
532
+ listInstance.addListObject({
533
+ id: 1,
534
+ filtered: false,
535
+ });
536
+ listInstance.addListObject({
537
+ id: 2,
538
+ filtered: true,
539
+ });
540
+ listInstance.addListObject({
541
+ id: 3,
542
+ filtered: false,
543
+ });
544
+ await doAwaitNot({
545
+ obj: listSearch.state,
546
+ prop: "running",
547
+ });
548
+ expect(listSearch.state.objects).toEqual({
549
+ 1: {
550
+ id: 1,
551
+ filtered: false,
552
+ },
553
+ 3: {
554
+ id: 3,
555
+ filtered: false,
556
+ },
557
+ });
558
+ expect(listSearch.state.order).toEqual(["1", "3"]);
559
+ expect(listSearch.state.objectsInOrder.map((e) => e.id)).toEqual([1, 3]);
560
+ listInstance.state.objects[2].filtered = false;
561
+ listInstance.state.objects[1].filtered = true;
562
+ await doAwaitNot({
563
+ obj: listSearch.state,
564
+ prop: "running",
565
+ });
566
+ expect(listSearch.state.objects).toEqual({
567
+ 2: {
568
+ id: 2,
569
+ filtered: false,
570
+ },
571
+ 3: {
572
+ id: 3,
573
+ filtered: false,
574
+ },
575
+ });
576
+ expect(listSearch.state.order).toEqual(["2", "3"]);
577
+ expect(listSearch.state.objectsInOrder.map((e) => e.id)).toEqual([2, 3]);
578
+ });
505
579
  });
@@ -1,5 +1,5 @@
1
- import { doAwaitTimeout } from "../../../utils/index.js";
2
- import { reactive } from "vue";
1
+ import { doAwaitTimeout, doAwaitNot } from "../../../utils/index.js";
2
+ import { reactive, ref } from "vue";
3
3
  import { deepUnref } from "vue-deepunref";
4
4
 
5
5
  describe("use/useListSort", () => {
@@ -10,6 +10,7 @@ describe("use/useListSort", () => {
10
10
  useListInstances,
11
11
  useListRelated,
12
12
  useListCalculated,
13
+ useListFilter,
13
14
  useListSort,
14
15
  useListSorts,
15
16
  setListSortDefaultOptions,
@@ -46,6 +47,7 @@ describe("use/useListSort", () => {
46
47
  useListInstances = imported.useListInstances;
47
48
  useListRelated = imported.useListRelated;
48
49
  useListCalculated = imported.useListCalculated;
50
+ useListFilter = imported.useListFilter;
49
51
  orderByRules = [
50
52
  { key: "organization", desc: true, localeCompare: false },
51
53
  { key: "lexical_name", desc: false, localeCompare: true },
@@ -339,4 +341,56 @@ describe("use/useListSort", () => {
339
341
  expect(listSort.state.objectsInOrder).toEqual(testOrder4.map((id) => listInstance.state.objects[id]));
340
342
  });
341
343
  });
344
+ it("pass through correctly when parentState changes their order", async () => {
345
+ const listInstance = useListInstance({
346
+ props: reactive({}),
347
+ });
348
+ const allowedFilter = ref((obj) => obj.name !== "two");
349
+ const listFilter = useListFilter({
350
+ parentState: listInstance.state,
351
+ allowedFilter,
352
+ });
353
+ const orderByRules = ref([]);
354
+ const listSort = useListSort({
355
+ parentState: listFilter.state,
356
+ orderByRules,
357
+ });
358
+ await doAwaitNot({
359
+ obj: listFilter.state,
360
+ prop: "running",
361
+ });
362
+ await waitForListSort(listSort);
363
+ expect(listFilter.state.order).toEqual([]);
364
+ expect(listFilter.state.objectsInOrder.map((obj) => obj.id)).toEqual([]);
365
+ expect(listSort.state.order).toEqual([]);
366
+ expect(listSort.state.objectsInOrder.map((obj) => obj.id)).toEqual([]);
367
+ listInstance.addListObject({ id: 1, name: "one" });
368
+ listInstance.addListObject({ id: 2, name: "two" });
369
+ listInstance.addListObject({ id: 3, name: "three" });
370
+ await doAwaitNot({
371
+ obj: listFilter.state,
372
+ prop: "running",
373
+ });
374
+ await waitForListSort(listSort);
375
+ expect(listFilter.state.order).toEqual(["1", "3"]);
376
+ expect(listFilter.state.objectsInOrder.map((obj) => obj.id)).toEqual([1, 3]);
377
+ expect(listSort.state.order).toEqual(["1", "3"]);
378
+ expect(listSort.state.objectsInOrder.map((obj) => obj.id)).toEqual([1, 3]);
379
+ listInstance.updateListObject({ id: 2, name: "twotwo" });
380
+ await doAwaitNot({
381
+ obj: listFilter.state,
382
+ prop: "running",
383
+ });
384
+ await waitForListSort(listSort);
385
+ expect(listSort.state.order).toEqual(["1", "2", "3"]);
386
+ expect(listSort.state.objectsInOrder.map((obj) => obj.id)).toEqual([1, 2, 3]);
387
+ listInstance.updateListObject({ id: 2, name: "two" });
388
+ await doAwaitNot({
389
+ obj: listFilter.state,
390
+ prop: "running",
391
+ });
392
+ await waitForListSort(listSort);
393
+ expect(listSort.state.order).toEqual(["1", "3"]);
394
+ expect(listSort.state.objectsInOrder.map((obj) => obj.id)).toEqual([1, 3]);
395
+ });
342
396
  });
package/use/listSearch.js CHANGED
@@ -274,7 +274,7 @@ export function useListSearch({ parentState, props, throttle = 500, showAllWhenE
274
274
  state.order = parentState.order.filter((id) => !!state.objects[id]);
275
275
  assignReactiveObject(
276
276
  state.objectsInOrderRefs,
277
- parentState.order.filter((id) => !!state.objects[id]).map((id) => toRef(state.objects, id))
277
+ state.order.map((id) => toRef(state.objects, id))
278
278
  );
279
279
  };
280
280
 
@@ -336,7 +336,7 @@ export function useListSearch({ parentState, props, throttle = 500, showAllWhenE
336
336
  }
337
337
  );
338
338
 
339
- watch(toRef(parentState, "order"), updateOrder, {
339
+ watch([() => Object.keys(state.objects), toRef(parentState, "order")], updateOrder, {
340
340
  immediate: true,
341
341
  deep: true,
342
342
  });
package/use/listSort.js CHANGED
@@ -18,7 +18,6 @@ import isUndefined from "lodash-es/isUndefined.js";
18
18
  import throttle from "lodash-es/throttle.js";
19
19
  import zip from "lodash-es/zip.js";
20
20
  import { effectScope, reactive, toRef, unref, watch, computed } from "vue";
21
- import { deepUnref } from "vue-deepunref";
22
21
 
23
22
  const collator = new Intl.Collator(undefined, { numeric: true });
24
23
 
@@ -132,14 +131,17 @@ export function useListSort({ parentState, orderByRules, sortThrottleWait = defa
132
131
 
133
132
  function sortCriteriaWatch() {
134
133
  try {
135
- if (!state.orderByRules || !state.orderByRules.filter(identity).length) {
134
+ if (!state.orderByRules?.length || !state.orderByRules.filter(identity).length) {
136
135
  if (!isEmpty(state.sortCriteria)) {
137
136
  for (const removedKey of Object.keys(state.sortCriteria)) {
138
137
  removeSortCriteria(removedKey);
139
138
  }
140
139
  }
141
- assignReactiveObject(state.order, deepUnref(parentState.order));
142
- assignReactiveObject(state.objectsInOrder, deepUnref(parentState.objectsInOrder));
140
+ state.order = [...parentState.order];
141
+ assignReactiveObject(
142
+ state.objectsInOrderRefs,
143
+ state.order.map((e) => toRef(parentState.objects, e))
144
+ );
143
145
  return;
144
146
  }
145
147
  const { removedKeys, addedKeys } = keyDiff(
@@ -152,9 +154,9 @@ export function useListSort({ parentState, orderByRules, sortThrottleWait = defa
152
154
 
153
155
  es.run(() => {
154
156
  for (const addedKey of addedKeys) {
155
- const object = toRef(() => parentState.objects[addedKey]);
156
- const relatedObj = toRef(() => parentState.relatedObjects?.[addedKey]);
157
- const calculatedObj = toRef(() => parentState.calculatedObjects?.[addedKey]);
157
+ const object = toRef(parentState.objects, addedKey);
158
+ const relatedObj = toRef(parentState.relatedObjects, addedKey);
159
+ const calculatedObj = toRef(parentState.calculatedObjects, addedKey);
158
160
  addSortCriteria(object, relatedObj, calculatedObj, addedKey);
159
161
  }
160
162
  });
@@ -169,17 +171,15 @@ export function useListSort({ parentState, orderByRules, sortThrottleWait = defa
169
171
 
170
172
  function sortWatch() {
171
173
  try {
172
- if (!state.orderByRules || !state.orderByRules.length) {
174
+ if (!state.orderByRules?.length) {
173
175
  state.order = [...parentState.order];
174
176
  assignReactiveObject(
175
177
  state.objectsInOrderRefs,
176
178
  state.order.map((e) => toRef(parentState.objects, e))
177
179
  );
178
- 3;
179
180
  return;
180
181
  }
181
-
182
- let idList = Object.keys(parentState.objects);
182
+ let idList = [...parentState.order];
183
183
  idList.sort((xKey, yKey) => {
184
184
  const xCriteria = state.sortCriteria[xKey];
185
185
  const yCriteria = state.sortCriteria[yKey];
@@ -238,9 +238,7 @@ export function useListSort({ parentState, orderByRules, sortThrottleWait = defa
238
238
  watch([toRef(state, "orderByDesc"), toRef(state, "sortCriteria")], throttledSortWatch, {
239
239
  deep: true,
240
240
  });
241
- // we do not need two immediate watches to the same function.
242
- watch(() => Object.keys(parentState.objects), sortCriteriaWatch);
243
- watch(toRef(state, "orderByRules"), sortCriteriaWatch, {
241
+ watch([toRef(state, "orderByRules"), toRef(parentState, "order")], sortCriteriaWatch, {
244
242
  deep: true,
245
243
  immediate: true,
246
244
  });