Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit ad56d39

Browse files
committed
fixed typo
1 parent af798b7 commit ad56d39

File tree

5 files changed

+169
-76
lines changed

5 files changed

+169
-76
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createCollection } from '@agile-ts/core';
1+
import { createCollection, globalBind } from '@agile-ts/core';
22
import { assignSharedAgileLoggerConfig, Logger } from '@agile-ts/logger';
33

44
assignSharedAgileLoggerConfig({ level: Logger.level.DEBUG });
@@ -7,3 +7,5 @@ assignSharedAgileLoggerConfig({ level: Logger.level.DEBUG });
77
export const TODOS = createCollection({
88
initialData: [{ id: 1, name: 'Clean Bathroom' }],
99
}).persist('todos'); // persist does store the Collection in the Local Storage
10+
11+
globalBind('__core__', { TODOS });

packages/core/src/collection/collection.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,9 @@ export class Collection<
12181218
* @public
12191219
* @param itemKeys - Item/s with identifier/s to be removed.
12201220
*/
1221-
public remove(itemKeys: ItemKey | Array<ItemKey>): {
1221+
public remove(
1222+
itemKeys: ItemKey | Array<ItemKey>
1223+
): {
12221224
fromGroups: (groups: Array<ItemKey> | ItemKey) => Collection<DataType>;
12231225
everywhere: (config?: RemoveItemsConfigInterface) => Collection<DataType>;
12241226
} {
@@ -1467,16 +1469,43 @@ export class Collection<
14671469
for (const groupKey of Object.keys(this.groups)) {
14681470
const group = this.getGroup(groupKey);
14691471
if (group != null && group.has(itemKey)) {
1470-
group.rebuild(
1471-
[
1472-
{
1473-
key: itemKey,
1474-
index: group.nextStateValue.findIndex((ik) => itemKey === ik),
1475-
method: TrackedChangeMethod.UPDATE,
1476-
},
1477-
],
1478-
config
1479-
);
1472+
const index = group._preciseItemKeys.findIndex((ik) => itemKey === ik);
1473+
1474+
// Update Group output at index
1475+
if (index !== -1) {
1476+
group.rebuild(
1477+
[
1478+
{
1479+
key: itemKey,
1480+
index: index,
1481+
method: TrackedChangeMethod.UPDATE,
1482+
},
1483+
],
1484+
config
1485+
);
1486+
}
1487+
// Add Item to the Group output if it isn't yet represented there to be updated
1488+
else {
1489+
const indexOfBeforeItemKey =
1490+
group.nextStateValue.findIndex((ik) => itemKey === ik) - 1;
1491+
1492+
group.rebuild(
1493+
[
1494+
{
1495+
key: itemKey,
1496+
index:
1497+
indexOfBeforeItemKey >= 0
1498+
? group._preciseItemKeys.findIndex(
1499+
(ik) =>
1500+
group.nextStateValue[indexOfBeforeItemKey] === ik
1501+
) + 1
1502+
: 0,
1503+
method: TrackedChangeMethod.ADD,
1504+
},
1505+
],
1506+
config
1507+
);
1508+
}
14801509
}
14811510
}
14821511
}

packages/core/src/collection/group/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,9 @@ export class Group<
415415

416416
switch (change.method) {
417417
case TrackedChangeMethod.ADD:
418-
this._preciseItemKeys.splice(change.index, 0, change.key);
419418
// this._value.splice(change.index, 0, change.key); // Already updated in 'add' method
420419
if (item != null) {
420+
this._preciseItemKeys.splice(change.index, 0, change.key);
421421
this.nextGroupOutput.splice(change.index, 0, copy(item._value));
422422
} else {
423423
notFoundItemKeys.push(change.key);
@@ -431,8 +431,8 @@ export class Group<
431431
}
432432
break;
433433
case TrackedChangeMethod.REMOVE:
434-
this._preciseItemKeys.splice(change.index, 1);
435434
// this._value.splice(change.index, 1); // Already updated in 'remove' method
435+
this._preciseItemKeys.splice(change.index, 1);
436436
this.nextGroupOutput.splice(change.index, 1);
437437
break;
438438
default:

packages/core/tests/unit/collection/collection.test.ts

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ describe('Collection Tests', () => {
265265
key: 'group1Key',
266266
});
267267

268-
expect(collection.createGroup).toHaveBeenCalledWith(
269-
'group1Key',
270-
[1, 2]
271-
);
268+
expect(collection.createGroup).toHaveBeenCalledWith('group1Key', [
269+
1,
270+
2,
271+
]);
272272
LogMock.hasLoggedCode('1B:02:00');
273273

274274
expect(response).toBeInstanceOf(Group);
@@ -3020,10 +3020,24 @@ describe('Collection Tests', () => {
30203020
let dummyGroup1: Group<ItemInterface>;
30213021
let dummyGroup2: Group<ItemInterface>;
30223022

3023+
let dummyItem1: Item<ItemInterface>;
3024+
let dummyItem2: Item<ItemInterface>;
3025+
30233026
beforeEach(() => {
3024-
dummyGroup1 = new Group(collection, ['dummyItem1', 'dummyItem2'], {
3025-
key: 'dummyGroup1',
3026-
});
3027+
dummyItem1 = new Item(collection, { id: 'dummyItem1', name: 'Jeff' });
3028+
dummyItem2 = new Item(collection, { id: 'dummyItem2', name: 'Jeff' });
3029+
collection.data = {
3030+
dummyItem1: dummyItem1,
3031+
dummyItem2: dummyItem2,
3032+
};
3033+
3034+
dummyGroup1 = new Group(
3035+
collection,
3036+
['dummyItem1', 'missingInCollectionItemKey', 'dummyItem2'],
3037+
{
3038+
key: 'dummyGroup1',
3039+
}
3040+
);
30273041
dummyGroup2 = new Group(collection, ['dummyItem2'], {
30283042
key: 'dummyGroup2',
30293043
});
@@ -3036,7 +3050,7 @@ describe('Collection Tests', () => {
30363050
dummyGroup2.rebuild = jest.fn();
30373051
});
30383052

3039-
it('should rebuild each Group that includes the specified itemKey (default config)', () => {
3053+
it('should update the Item in each Group (output) that includes the specified itemKey (default config)', () => {
30403054
collection.rebuildGroupsThatIncludeItemKey('dummyItem1');
30413055

30423056
// Group 1
@@ -3055,7 +3069,7 @@ describe('Collection Tests', () => {
30553069
expect(dummyGroup2.rebuild).not.toHaveBeenCalled();
30563070
});
30573071

3058-
it('should rebuild each Group that includes the specified itemKey (specific config)', () => {
3072+
it('should update the Item in each Group (output) that includes the specified itemKey (specific config)', () => {
30593073
collection.rebuildGroupsThatIncludeItemKey('dummyItem2', {
30603074
key: 'frank',
30613075
background: true,
@@ -3094,6 +3108,31 @@ describe('Collection Tests', () => {
30943108
}
30953109
);
30963110
});
3111+
3112+
it(
3113+
'should update the Item in each Group (output) that includes the specified itemKey ' +
3114+
"although the Item doesn't exist in the Group output yet",
3115+
() => {
3116+
collection.rebuildGroupsThatIncludeItemKey(
3117+
'missingInCollectionItemKey'
3118+
);
3119+
3120+
// Group 1
3121+
expect(dummyGroup1.rebuild).toHaveBeenCalledWith(
3122+
[
3123+
{
3124+
key: 'missingInCollectionItemKey',
3125+
index: 1,
3126+
method: TrackedChangeMethod.ADD,
3127+
},
3128+
],
3129+
{}
3130+
);
3131+
3132+
// Group 2
3133+
expect(dummyGroup2.rebuild).not.toHaveBeenCalled();
3134+
}
3135+
);
30973136
});
30983137
});
30993138
});

packages/core/tests/unit/collection/group/group.test.ts

Lines changed: 76 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -698,38 +698,35 @@ describe('Group Tests', () => {
698698

699699
it(
700700
'should soft rebuild the Group if trackedChanges were specified ' +
701-
'and set notExistingItemKeys to the not found Item Keys (default config)',
701+
'and set notExistingItemKeys to the not found itemKeys (ADD)',
702702
() => {
703-
group.nextGroupOutput = [
704-
{ id: 'dummyItem1Key', name: 'jeff' },
705-
{ id: 'dummyItem2Key', name: 'frank' },
706-
];
707-
group._preciseItemKeys = ['dummyItem1Key', 'dummyItem2Key'];
703+
group.nextGroupOutput = [{ id: 'dummyItem1Key', name: 'jeff' }];
704+
group._preciseItemKeys = ['dummyItem1Key'];
708705

709-
group.rebuild([
710-
{ index: 2, method: TrackedChangeMethod.ADD, key: 'dummyItem3Key' },
711-
{
712-
index: 1,
713-
method: TrackedChangeMethod.REMOVE,
714-
key: 'dummyItem2Key',
715-
},
716-
{
717-
index: 4,
718-
method: TrackedChangeMethod.UPDATE,
719-
key: 'missingInCollectionItemKey',
720-
},
721-
{
722-
index: 0,
723-
method: TrackedChangeMethod.UPDATE,
724-
key: 'dummyItem1Key',
725-
},
726-
]);
706+
group.rebuild(
707+
[
708+
{
709+
index: 1,
710+
method: TrackedChangeMethod.ADD,
711+
key: 'dummyItem3Key',
712+
},
713+
{
714+
index: 2,
715+
method: TrackedChangeMethod.ADD,
716+
key: 'missingInCollectionItemKey',
717+
},
718+
],
719+
{ key: 'test', background: true }
720+
);
727721

728722
expect(group.notFoundItemKeys).toStrictEqual([
729723
'missingInCollectionItemKey',
730724
]);
731725
expect(group.observers['output'].ingestOutput).not.toHaveBeenCalled();
732-
expect(group.observers['output'].ingest).toHaveBeenCalled();
726+
expect(group.observers['output'].ingest).toHaveBeenCalledWith({
727+
key: 'test',
728+
background: true,
729+
});
733730
expect(group.nextGroupOutput).toStrictEqual([
734731
{ id: 'dummyItem1Key', name: 'jeff' },
735732
{ id: 'dummyItem3Key', name: 'hans' },
@@ -747,59 +744,83 @@ describe('Group Tests', () => {
747744
}
748745
);
749746

747+
it('should soft rebuild the Group if trackedChanges were specified (REMOVE)', () => {
748+
group.nextGroupOutput = [
749+
{ id: 'dummyItem1Key', name: 'jeff' },
750+
{ id: 'dummyItem2Key', name: 'frank' },
751+
{ id: 'dummyItem3Key', name: 'hans' },
752+
];
753+
group._preciseItemKeys = [
754+
'dummyItem1Key',
755+
'dummyItem2Key',
756+
'dummyItem3Key',
757+
];
758+
759+
group.rebuild(
760+
[
761+
{
762+
index: 1,
763+
method: TrackedChangeMethod.REMOVE,
764+
key: 'dummyItem2Key',
765+
},
766+
],
767+
{ key: 'test', background: true }
768+
);
769+
770+
expect(group.notFoundItemKeys).toStrictEqual([]);
771+
expect(group.observers['output'].ingestOutput).not.toHaveBeenCalled();
772+
expect(group.observers['output'].ingest).toHaveBeenCalledWith({
773+
key: 'test',
774+
background: true,
775+
});
776+
expect(group.nextGroupOutput).toStrictEqual([
777+
{ id: 'dummyItem1Key', name: 'jeff' },
778+
{ id: 'dummyItem3Key', name: 'hans' },
779+
]);
780+
expect(group._preciseItemKeys).toStrictEqual([
781+
'dummyItem1Key',
782+
'dummyItem3Key',
783+
]);
784+
785+
LogMock.hasNotLogged('warn');
786+
});
787+
750788
it(
751789
'should soft rebuild the Group if trackedChanges were specified ' +
752-
'and set notExistingItemKeys to the not found Item Keys (specific config)',
790+
'and set notExistingItemKeys to the not found itemKeys (UPDATE)',
753791
() => {
754-
group.nextGroupOutput = [
755-
{ id: 'dummyItem1Key', name: 'jeff' },
756-
{ id: 'dummyItem2Key', name: 'frank' },
757-
];
758-
group._preciseItemKeys = ['dummyItem1Key', 'dummyItem2Key'];
792+
dummyItem1._value = { id: 'dummyItem1Key', name: 'frank' };
793+
group.nextGroupOutput = [{ id: 'dummyItem1Key', name: 'jeff' }];
794+
group._preciseItemKeys = ['dummyItem1Key'];
759795

760796
group.rebuild(
761797
[
762798
{
763-
index: 2,
764-
method: TrackedChangeMethod.ADD,
765-
key: 'dummyItem3Key',
799+
index: 0,
800+
method: TrackedChangeMethod.UPDATE,
801+
key: 'dummyItem1Key',
766802
},
767803
{
768804
index: 1,
769-
method: TrackedChangeMethod.REMOVE,
770-
key: 'dummyItem2Key',
771-
},
772-
{
773-
index: 4,
774805
method: TrackedChangeMethod.UPDATE,
775806
key: 'missingInCollectionItemKey',
776807
},
777-
{
778-
index: 0,
779-
method: TrackedChangeMethod.UPDATE,
780-
key: 'dummyItem1Key',
781-
},
782808
],
783-
{ key: 'frank', force: true, background: true }
809+
{ key: 'test', background: true }
784810
);
785811

786812
expect(group.notFoundItemKeys).toStrictEqual([
787813
'missingInCollectionItemKey',
788814
]);
789815
expect(group.observers['output'].ingestOutput).not.toHaveBeenCalled();
790816
expect(group.observers['output'].ingest).toHaveBeenCalledWith({
791-
key: 'frank',
792-
force: true,
817+
key: 'test',
793818
background: true,
794819
});
795820
expect(group.nextGroupOutput).toStrictEqual([
796-
{ id: 'dummyItem1Key', name: 'jeff' },
797-
{ id: 'dummyItem3Key', name: 'hans' },
798-
]);
799-
expect(group._preciseItemKeys).toStrictEqual([
800-
'dummyItem1Key',
801-
'dummyItem3Key',
821+
{ id: 'dummyItem1Key', name: 'frank' },
802822
]);
823+
expect(group._preciseItemKeys).toStrictEqual(['dummyItem1Key']);
803824

804825
LogMock.hasLoggedCode(
805826
'1C:02:00',
@@ -819,6 +840,8 @@ describe('Group Tests', () => {
819840

820841
expect(group.notFoundItemKeys).toStrictEqual([]);
821842
expect(group.observers['output'].ingestOutput).not.toHaveBeenCalled();
843+
expect(group.observers['output'].ingest).not.toHaveBeenCalled();
844+
822845
LogMock.hasNotLogged('warn');
823846
}
824847
);

0 commit comments

Comments
 (0)