Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/collection/collection.persistent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class CollectionPersistent<

// If successfully loaded Item value, assign Item to Collection
if (loadedPersistedValueIntoItem)
this.collection().assignItem(dummyItem); // TODO SECOND GROUP REBUILD (by calling rebuildGroupThatInclude() in the assignItem() method)
this.collection().assignItem(dummyItem, { overwrite: false }); // TODO SECOND GROUP REBUILD (by calling rebuildGroupThatInclude() in the assignItem() method)
}
}
}
Expand Down
30 changes: 14 additions & 16 deletions packages/core/src/collection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export class Collection<DataType extends Object = DefaultItem> {
* @param config - Configuration object
*/
public Selector(
initialKey: ItemKey,
initialKey: ItemKey | null,
config: SelectorConfigInterface = {}
): Selector<DataType> {
if (this.isInstantiated) {
Expand Down Expand Up @@ -489,7 +489,7 @@ export class Collection<DataType extends Object = DefaultItem> {
* @param config - Configuration object
*/
public getGroup(
groupKey: GroupKey | undefined,
groupKey: GroupKey | undefined | null,
config: HasConfigInterface = {}
): Group<DataType> | undefined {
config = defineConfig(config, {
Expand Down Expand Up @@ -589,7 +589,7 @@ export class Collection<DataType extends Object = DefaultItem> {
*/
public createSelector(
selectorKey: SelectorKey,
itemKey: ItemKey
itemKey: ItemKey | null
): Selector<DataType> {
let selector = this.getSelector(selectorKey, { notExisting: true });
if (!this.isInstantiated) LogCodeManager.log('1B:02:04');
Expand Down Expand Up @@ -662,7 +662,7 @@ export class Collection<DataType extends Object = DefaultItem> {
* @param config - Configuration object
*/
public getSelector(
selectorKey: SelectorKey | undefined,
selectorKey: SelectorKey | undefined | null,
config: HasConfigInterface = {}
): Selector<DataType> | undefined {
config = defineConfig(config, {
Expand Down Expand Up @@ -699,14 +699,10 @@ export class Collection<DataType extends Object = DefaultItem> {

// Create dummy Selector to hold reference
if (selector == null) {
selector = new Selector<DataType>(
this,
Selector.unknownItemPlaceholderKey,
{
key: selectorKey,
isPlaceholder: true,
}
);
selector = new Selector<DataType>(this, null, {
key: selectorKey,
isPlaceholder: true,
});
this.selectors[selectorKey] = selector;
}

Expand Down Expand Up @@ -773,7 +769,7 @@ export class Collection<DataType extends Object = DefaultItem> {
* @param config - Configuration object
*/
public getItem(
itemKey: ItemKey | undefined,
itemKey: ItemKey | undefined | null,
config: HasConfigInterface = {}
): Item<DataType> | undefined {
config = defineConfig(config, {
Expand Down Expand Up @@ -1425,9 +1421,11 @@ export class Collection<DataType extends Object = DefaultItem> {
}

// Check if Item already exists
if (this.getItem(itemKey) != null) {
if (!config.overwrite) return true;
else increaseCollectionSize = false;
if (this.getItem(itemKey, { notExisting: true }) != null) {
if (!config.overwrite) {
this.assignData(item._value);
return true;
} else increaseCollectionSize = false;
}

// Assign/add Item to Collection
Expand Down
56 changes: 31 additions & 25 deletions packages/core/src/collection/selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ import {
StateRuntimeJobConfigInterface,
} from '../internal';

export class Selector<DataType extends Object = DefaultItem> extends State<
DataType | undefined
> {
export class Selector<
DataType extends Object = DefaultItem
> extends State<DataType | null> {
// Collection the Selector belongs to
public collection: () => Collection<DataType>;

static unknownItemPlaceholderKey = '__UNKNOWN__ITEM__KEY__';
static rebuildSelectorSideEffectKey = 'rebuildSelector';
static rebuildItemSideEffectKey = 'rebuildItem';

// Item the Selector represents
public _item: Item<DataType> | undefined;
public _item: Item<DataType> | null;
// Key/Name identifier of the Item the Selector represents
public _itemKey: ItemKey;
public _itemKey: ItemKey | null;

/**
* A Selector represents an Item from a Collection in the long term.
Expand All @@ -39,23 +38,21 @@ export class Selector<DataType extends Object = DefaultItem> extends State<
*/
constructor(
collection: Collection<DataType>,
itemKey: ItemKey,
itemKey: ItemKey | null,
config: SelectorConfigInterface = {}
) {
config = defineConfig(config, {
isPlaceholder: false,
});
super(collection.agileInstance(), undefined, config);
super(collection.agileInstance(), null, config);
this.collection = () => collection;
this._item = undefined;
this._itemKey = !config.isPlaceholder
? itemKey
: Selector.unknownItemPlaceholderKey;
this._item = null;
this._itemKey = !config.isPlaceholder && itemKey != null ? itemKey : null;
this._key = config?.key;
this.isPlaceholder = true; // Because hasn't selected any Item yet

// Initial select of the Item
if (!config.isPlaceholder) this.select(itemKey, { overwrite: true });
if (this._itemKey != null) this.select(itemKey, { overwrite: true });
}

/**
Expand All @@ -65,7 +62,7 @@ export class Selector<DataType extends Object = DefaultItem> extends State<
*
* @public
*/
public get itemKey(): ItemKey {
public get itemKey(): ItemKey | null {
return this._itemKey;
}

Expand All @@ -78,7 +75,7 @@ export class Selector<DataType extends Object = DefaultItem> extends State<
* @public
* @param value - New key/name identifier of the Item to be represented by the Selector.
*/
public set itemKey(value: ItemKey) {
public set itemKey(value: ItemKey | null) {
this.select(value);
}

Expand All @@ -89,7 +86,7 @@ export class Selector<DataType extends Object = DefaultItem> extends State<
*
* @public
*/
public get item(): Item<DataType> | undefined {
public get item(): Item<DataType> | null {
return this._item;
}

Expand All @@ -102,7 +99,7 @@ export class Selector<DataType extends Object = DefaultItem> extends State<
* @public
* @param value - New Item to be represented by the Selector.
*/
public set item(value: Item<DataType> | undefined) {
public set item(value: Item<DataType> | null) {
if (value?._key) this.select(value._key);
}

Expand All @@ -116,7 +113,7 @@ export class Selector<DataType extends Object = DefaultItem> extends State<
* @param config - Configuration object
*/
public select(
itemKey: ItemKey,
itemKey: ItemKey | null,
config: StateRuntimeJobConfigInterface = {}
): this {
config = defineConfig(config, {
Expand All @@ -140,7 +137,9 @@ export class Selector<DataType extends Object = DefaultItem> extends State<
return this;

// Unselect old Item
this.unselect({ background: true });
this.unselect({ background: itemKey != null });

if (itemKey == null) return this;

// Retrieve new Item from Collection
const newItem = this.collection().getItemWithReference(itemKey);
Expand Down Expand Up @@ -224,16 +223,20 @@ export class Selector<DataType extends Object = DefaultItem> extends State<
});

// Unselect Item
if (item) {
if (item != null) {
item.selectedBy.delete(this._key as any);
item.removeSideEffect(Selector.rebuildSelectorSideEffectKey);
item.removeSideEffect(Selector.rebuildItemSideEffectKey);
if (item.isPlaceholder) delete this.collection().data[this._itemKey];
if (
item.isPlaceholder &&
this._itemKey != null
)
delete this.collection().data[this._itemKey];
}

// Reset Selector
this._item = undefined;
this._itemKey = Selector.unknownItemPlaceholderKey;
this._item = null;
this._itemKey = null;
this.rebuildSelector(config);
this.isPlaceholder = true;

Expand All @@ -250,7 +253,10 @@ export class Selector<DataType extends Object = DefaultItem> extends State<
* @param itemKey - Key/Name identifier of the Item.
* @param correctlySelected - Whether the Item has to be selected correctly.
*/
public hasSelected(itemKey: ItemKey, correctlySelected = true): boolean {
public hasSelected(
itemKey: ItemKey | null,
correctlySelected = true
): boolean {
if (correctlySelected) {
return (
this._itemKey === itemKey &&
Expand All @@ -273,7 +279,7 @@ export class Selector<DataType extends Object = DefaultItem> extends State<
public rebuildSelector(config: StateRuntimeJobConfigInterface = {}): this {
// Assign 'undefined' to the Selector value if no Item is set
if (this._item == null || this._item.isPlaceholder) {
this.set(undefined, config);
this.set(null, config);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,9 @@ describe('CollectionPersistent Tests', () => {
followCollectionPersistKeyPattern: false,
}
);
expect(dummyCollection.assignItem).toHaveBeenCalledWith(
placeholderItem1
);
expect(
dummyCollection.assignItem
).toHaveBeenCalledWith(placeholderItem1, { overwrite: false });
expect(dummyCollection.assignItem).not.toHaveBeenCalledWith(
placeholderItem2
); // Because Item persistent isn't ready
Expand Down Expand Up @@ -507,9 +507,9 @@ describe('CollectionPersistent Tests', () => {
followCollectionPersistKeyPattern: false,
}
);
expect(dummyCollection.assignItem).toHaveBeenCalledWith(
placeholderItem1
);
expect(
dummyCollection.assignItem
).toHaveBeenCalledWith(placeholderItem1, { overwrite: false });
expect(dummyCollection.assignItem).not.toHaveBeenCalledWith(
placeholderItem3
); // Because Item 3 is already present in the Collection
Expand Down
10 changes: 10 additions & 0 deletions packages/core/tests/unit/collection/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,10 @@ describe('Collection Tests', () => {

expect(response).toBeInstanceOf(Selector);
expect(response.isPlaceholder).toBeTruthy();
expect(response._item).toBeNull();
expect(response._itemKey).toBeNull();
expect(response._key).toBe('notExistingSelector');

expect(collection.selectors['notExistingSelector']).toBe(response);
expect(ComputedTracker.tracked).toHaveBeenCalledWith(response.observer);
});
Expand Down Expand Up @@ -2832,6 +2835,7 @@ describe('Collection Tests', () => {
dummyItem1.patch = jest.fn();
toAddDummyItem2.patch = jest.fn();
collection.rebuildGroupsThatIncludeItemKey = jest.fn();
collection.assignData = jest.fn();
});

it('should assign valid Item to Collection (default config)', () => {
Expand All @@ -2847,6 +2851,7 @@ describe('Collection Tests', () => {
background: false,
}
);
expect(collection.assignData).not.toHaveBeenCalled();

expect(toAddDummyItem2.patch).not.toHaveBeenCalled();
expect(toAddDummyItem2._key).toBe('dummyItem2');
Expand All @@ -2870,6 +2875,7 @@ describe('Collection Tests', () => {
background: true,
}
);
expect(collection.assignData).not.toHaveBeenCalled();

expect(toAddDummyItem2.patch).not.toHaveBeenCalled();
expect(toAddDummyItem2._key).toBe('dummyItem2');
Expand All @@ -2895,6 +2901,7 @@ describe('Collection Tests', () => {
background: false,
}
);
expect(collection.assignData).not.toHaveBeenCalled();

expect(toAddDummyItem2.patch).toHaveBeenCalledWith(
{ id: 'randomDummyId' },
Expand Down Expand Up @@ -2923,6 +2930,7 @@ describe('Collection Tests', () => {
expect(
collection.rebuildGroupsThatIncludeItemKey
).not.toHaveBeenCalled();
expect(collection.assignData).not.toHaveBeenCalled();

expect(toAddDummyItem2.patch).not.toHaveBeenCalled();
expect(toAddDummyItem2._key).toBe('dummyItem2');
Expand All @@ -2944,6 +2952,7 @@ describe('Collection Tests', () => {
expect(
collection.rebuildGroupsThatIncludeItemKey
).not.toHaveBeenCalled();
expect(collection.assignData).toHaveBeenCalledWith(dummyItem1._value);

expect(dummyItem1.patch).not.toHaveBeenCalled();
expect(dummyItem1._key).toBe('dummyItem1');
Expand All @@ -2965,6 +2974,7 @@ describe('Collection Tests', () => {
background: false,
}
);
expect(collection.assignData).not.toHaveBeenCalled();

expect(dummyItem1.patch).not.toHaveBeenCalled();
expect(dummyItem1._key).toBe('dummyItem1');
Expand Down
Loading