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
57 changes: 34 additions & 23 deletions packages/core/src/collection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class Collection<DataType = DefaultItem> {

// Set Key/Name of Group to property Name
for (let key in groupsObject)
if (!groupsObject[key]._key) groupsObject[key]._key = key;
if (!groupsObject[key]._key) groupsObject[key].setKey(key);

this.groups = groupsObject;
}
Expand Down Expand Up @@ -193,7 +193,7 @@ export class Collection<DataType = DefaultItem> {

// Set Key/Name of Selector to property Name
for (let key in selectorsObject)
if (!selectorsObject[key]._key) selectorsObject[key]._key = key;
if (!selectorsObject[key]._key) selectorsObject[key].setKey(key);

this.selectors = selectorsObject;
}
Expand Down Expand Up @@ -270,7 +270,7 @@ export class Collection<DataType = DefaultItem> {
changes: DefaultItem | DataType,
config: UpdateConfigInterface = {}
): Item<DataType> | undefined {
const item = this.getItem(itemKey);
const item = this.getItem(itemKey, { notExisting: true });
const primaryKey = this.config.primaryKey;
config = defineConfig(config, {
addNewProperties: false,
Expand Down Expand Up @@ -327,7 +327,7 @@ export class Collection<DataType = DefaultItem> {
groupKey: GroupKey,
initialItems?: Array<ItemKey>
): Group<DataType> {
let group = this.getGroup(groupKey);
let group = this.getGroup(groupKey, { notExisting: true });

// Check if Group already exists
if (group) {
Expand Down Expand Up @@ -359,7 +359,7 @@ export class Collection<DataType = DefaultItem> {
selectorKey: SelectorKey,
itemKey: ItemKey
): Selector<DataType> {
let selector = this.getSelector(selectorKey);
let selector = this.getSelector(selectorKey, { notExisting: true });

// Check if Selector already exists
if (selector) {
Expand Down Expand Up @@ -418,7 +418,7 @@ export class Collection<DataType = DefaultItem> {
* @param groupKey - Name/Key of Group
*/
public getGroupWithReference(groupKey: GroupKey): Group<DataType> {
let group = this.getGroup(groupKey);
let group = this.getGroup(groupKey, { notExisting: true });

// Create dummy Group to hold reference
if (!group) {
Expand Down Expand Up @@ -493,7 +493,7 @@ export class Collection<DataType = DefaultItem> {
public getSelectorWithReference(
selectorKey: SelectorKey
): Selector<DataType> {
let selector = this.getSelector(selectorKey);
let selector = this.getSelector(selectorKey, { notExisting: true });

// Create dummy Selector to hold reference
if (!selector) {
Expand Down Expand Up @@ -578,13 +578,20 @@ export class Collection<DataType = DefaultItem> {
* @param itemKey - Name/Key of Item
*/
public getItemWithReference(itemKey: ItemKey): Item<DataType> {
let item = this.getItem(itemKey);
let item = this.getItem(itemKey, { notExisting: true });

// Create Placeholder Item to hold reference
if (!item) {
const dummyItem = new Item<DataType>(this, "unknown" as any, {
isPlaceholder: true,
});
const dummyItem = new Item<DataType>(
this,
{
[this.config.primaryKey]: itemKey, // Setting ItemKey to assign key to Item
dummy: "item",
} as any,
{
isPlaceholder: true,
}
);
this.data[itemKey] = dummyItem;
return dummyItem;
}
Expand All @@ -600,9 +607,13 @@ export class Collection<DataType = DefaultItem> {
* @public
* Get Value of Item by Key/Name
* @param itemKey - ItemKey of Item that holds the Value
* @param config - Config
*/
public getItemValue(itemKey: ItemKey | undefined): DataType | undefined {
let item = this.getItem(itemKey);
public getItemValue(
itemKey: ItemKey | undefined,
config: GetItemConfigInterface = {}
): DataType | undefined {
let item = this.getItem(itemKey, config);
if (!item) return undefined;
return item.value;
}
Expand Down Expand Up @@ -749,9 +760,9 @@ export class Collection<DataType = DefaultItem> {
public updateItemKey(
oldItemKey: ItemKey,
newItemKey: ItemKey,
config?: UpdateItemKeyConfigInterface
config: UpdateItemKeyConfigInterface = {}
): void {
const item = this.getItem(oldItemKey);
const item = this.getItem(oldItemKey, { notExisting: true });
config = defineConfig(config, {
background: false,
});
Expand All @@ -772,7 +783,7 @@ export class Collection<DataType = DefaultItem> {

// Update Groups
for (let groupKey in this.groups) {
const group = this.getGroup(groupKey);
const group = this.getGroup(groupKey, { notExisting: true });
if (!group || group.isPlaceholder || !group.has(oldItemKey)) continue;

// Replace old ItemKey with new ItemKey
Expand All @@ -783,7 +794,7 @@ export class Collection<DataType = DefaultItem> {

// Update Selectors
for (let selectorKey in this.selectors) {
const selector = this.getSelector(selectorKey);
const selector = this.getSelector(selectorKey, { notExisting: true });
if (!selector || selector.itemKey !== oldItemKey) continue;

// Replace old selected ItemKey with new ItemKey
Expand Down Expand Up @@ -814,7 +825,7 @@ export class Collection<DataType = DefaultItem> {

// Remove ItemKey from Groups
_groupKeys.forEach((groupKey) => {
const group = this.getGroup(groupKey);
const group = this.getGroup(groupKey, { notExisting: true });
if (!group) return;
group.remove(itemKey);
removedFromGroupsCount++;
Expand All @@ -838,19 +849,19 @@ export class Collection<DataType = DefaultItem> {
const _itemKeys = normalizeArray<ItemKey>(itemKeys);

_itemKeys.forEach((itemKey) => {
const item = this.getItem(itemKey);
const item = this.getItem(itemKey, { notExisting: true });
if (!item) return;

// Remove Item from Groups
for (let groupKey in this.groups) {
const group = this.getGroup(groupKey);
const group = this.getGroup(groupKey, { notExisting: true });
if (group && group.has(itemKey)) group.remove(itemKey);
}

// Remove Selectors that represented this Item
for (let selectorKey in this.selectors) {
const selector = this.getSelector(selectorKey);
if (selector?.itemKey === itemKey) this.removeSelector(selectorKey);
const selector = this.getSelector(selectorKey, { notExisting: true });
if (selector?._itemKey === itemKey) this.removeSelector(selectorKey);
}

// Remove Item from Storage
Expand Down Expand Up @@ -896,7 +907,7 @@ export class Collection<DataType = DefaultItem> {
}

const itemKey = _data[primaryKey];
let item = this.getItem(itemKey);
let item = this.getItem(itemKey, { notExisting: true });
const wasPlaceholder = item?.isPlaceholder || false;
const createItem = !item;

Expand Down
14 changes: 6 additions & 8 deletions packages/core/src/collection/item.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
State,
Collection,
DefaultItem,
StateKey,
} from "../internal";
import { State, Collection, DefaultItem, StateKey } from "../internal";

export class Item<DataType = DefaultItem> extends State<DataType> {
static updateGroupSideEffectKey = "rebuildGroup";
Expand All @@ -21,10 +16,13 @@ export class Item<DataType = DefaultItem> extends State<DataType> {
data: DataType,
config: ItemConfigInterface = {}
) {
super(collection.agileInstance(), data, config);
super(collection.agileInstance(), data, {
isPlaceholder: config.isPlaceholder,
key: data[collection.config.primaryKey], // Set Key/Name of Item to primaryKey of Data
});
this.collection = () => collection;

// Set Key/Name of Item to primaryKey of Data
// Reassign Key to assign sideEffects
this.setKey(data[collection.config.primaryKey]);
}

Expand Down
49 changes: 8 additions & 41 deletions packages/core/src/collection/selector.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {
Agile,
Collection,
copy,
DefaultItem,
defineConfig,
Item,
ItemKey,
SetConfigInterface,
State,
StateRuntimeJobConfigInterface,
} from "../internal";

export class Selector<DataType = DefaultItem> extends State<
Expand Down Expand Up @@ -67,33 +66,25 @@ export class Selector<DataType = DefaultItem> extends State<
* @param itemKey - New ItemKey
* @param config - Config
*/
public select(itemKey: ItemKey, config: SelectConfigInterface = {}): this {
public select(
itemKey: ItemKey,
config: StateRuntimeJobConfigInterface = {}
): this {
const oldItem = this.item;
let newItem = this.collection().getItemWithReference(itemKey);
config = defineConfig(config, {
background: false,
sideEffects: true,
force: false,
overwrite: oldItem?.isPlaceholder || false,
storage: true,
});

if (this._itemKey === itemKey && !config.force) {
Agile.logger.warn(`Selector has already selected '${itemKey}'!`);
return this;
}

// Overwrite old Item Values with new Item Value
if (config.overwrite) {
this._value = copy(newItem._value);
this.nextStateValue = copy(newItem._value);
this.previousStateValue = copy(newItem._value);
this.initialStateValue = copy(newItem._value);
this.isSet = false;
this.isPlaceholder = false;

config.force = true;
}

// Remove old Item from Collection if it is an Placeholder
if (oldItem?.isPlaceholder) delete this.collection().data[this._itemKey];

Expand All @@ -109,11 +100,7 @@ export class Selector<DataType = DefaultItem> extends State<
);

// Rebuild Selector for instantiating new 'selected' ItemKey properly
this.rebuildSelector({
background: config.background,
sideEffects: config.sideEffects,
force: config.force,
});
this.rebuildSelector(config);

return this;
}
Expand All @@ -126,14 +113,7 @@ export class Selector<DataType = DefaultItem> extends State<
* Rebuilds Selector
* @param config - Config
*/
public rebuildSelector(config: SetConfigInterface = {}) {
config = defineConfig(config, {
sideEffects: true,
background: false,
force: false,
storage: true,
});

public rebuildSelector(config: StateRuntimeJobConfigInterface = {}) {
// Set Selector Value to undefined if Item doesn't exist
if (!this.item || this.item.isPlaceholder) {
this.set(undefined, config);
Expand All @@ -155,16 +135,3 @@ export interface SelectorConfigInterface {
key?: SelectorKey;
isPlaceholder?: boolean;
}

/**
* @param background - If selecting a new Item happens in the background (-> not causing any rerender)
* @param sideEffects - If Side Effects of Selector get executed
* @param force - Force to select ItemKey
* @param overwrite - If the Selector gets overwritten with the new selected Item (initialStateValue, ..)
*/
export interface SelectConfigInterface {
background?: boolean;
sideEffects?: boolean;
force?: boolean;
overwrite?: boolean;
}
37 changes: 33 additions & 4 deletions packages/core/src/event/event.observer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {
Observer,
Job,
RuntimeJob,
ObserverKey,
Event,
SubscriptionContainer,
IngestConfigInterface,
RuntimeJobConfigInterface,
defineConfig,
RuntimeJobKey,
} from "../internal";

export class EventObserver<PayloadType = any> extends Observer {
Expand Down Expand Up @@ -36,8 +39,25 @@ export class EventObserver<PayloadType = any> extends Observer {
* Ingests Event into Runtime and causes Rerender on Components that got subscribed by the Event (Observer)
* @param config - Config
*/
public trigger(config: IngestConfigInterface = {}): void {
this.agileInstance().runtime.ingest(this, config);
public trigger(config: EventIngestConfigInterface = {}): void {
config = defineConfig(config, {
perform: true,
background: false,
sideEffects: true,
force: false,
});

// Create Job
const job = new RuntimeJob(this, {
force: config.force,
sideEffects: config.sideEffects,
background: config.background,
key: config.key || this._key,
});

this.agileInstance().runtime.ingest(job, {
perform: config.perform,
});
}

//=========================================================================================================
Expand All @@ -48,7 +68,7 @@ export class EventObserver<PayloadType = any> extends Observer {
* Performs Job from Runtime
* @param job - Job that gets performed
*/
public perform(job: Job<this>) {
public perform(job: RuntimeJob<this>) {
// Noting to perform
}
}
Expand All @@ -63,3 +83,12 @@ export interface CreateEventObserverConfigInterface {
subs?: Array<SubscriptionContainer>;
key?: ObserverKey;
}

/**
* @param key - Key/Name of Job that gets created
*/
export interface EventIngestConfigInterface
extends RuntimeJobConfigInterface,
IngestConfigInterface {
key?: RuntimeJobKey;
}
3 changes: 2 additions & 1 deletion packages/core/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export * from "./agile";
// Runtime
export * from "./runtime";
export * from "./runtime/observer";
export * from "./runtime/job";
export * from "./runtime/runtime.job";
export * from "./runtime/subscription/container/SubscriptionContainer";
export * from "./runtime/subscription/container/CallbackSubscriptionContainer";
export * from "./runtime/subscription/container/ComponentSubscriptionContainer";
Expand All @@ -31,6 +31,7 @@ export * from "./storages/persistent";
export * from "./state";
export * from "./state/state.observer";
export * from "./state/state.persistent";
export * from "./state/state.runtime.job";

// Computed
export * from "./computed";
Expand Down
Loading