@agile-ts/core
Advanced tools
Comparing version 0.0.11 to 0.0.12
# Change Log | ||
## 0.0.12 | ||
### Patch Changes | ||
- 9071dd6: reduced bundle size and fixed copy method | ||
## 0.0.11 | ||
@@ -4,0 +10,0 @@ |
@@ -6,7 +6,2 @@ "use strict"; | ||
class Agile { | ||
/** | ||
* @public | ||
* Agile - Global state and logic framework for reactive Typescript & Javascript applications | ||
* @param config - Config | ||
*/ | ||
constructor(config = {}) { | ||
@@ -34,41 +29,13 @@ config = internal_1.defineConfig(config, { | ||
}); | ||
// Assign customized config to Logger | ||
Agile.logger = new internal_1.Logger(config.logConfig); | ||
// Logging | ||
Agile.logger.success('Created new AgileInstance ', this, Agile.logger); | ||
// Create global instance of Agile | ||
if (!internal_1.globalBind('__agile__', this)) | ||
Agile.logger.warn('Be careful with multiple Agile Instances in one Application!'); | ||
} | ||
//========================================================================================================= | ||
// Storage | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Storage - Handy Interface for storing Items permanently | ||
* @param config - Config | ||
*/ | ||
createStorage(config) { | ||
return new internal_1.Storage(config); | ||
} | ||
//========================================================================================================= | ||
// State | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* State - Class that holds one Value and causes rerender on subscribed Components | ||
* @param initialValue - Initial Value of the State | ||
* @param config - Config | ||
*/ | ||
createState(initialValue, config = {}) { | ||
return new internal_1.State(this, initialValue, config); | ||
} | ||
//========================================================================================================= | ||
// Collection | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Collection - Class that holds a List of Objects with key and causes rerender on subscribed Components | ||
* @param config - Config | ||
*/ | ||
createCollection(config) { | ||
@@ -92,21 +59,5 @@ return new internal_1.Collection(this, config); | ||
} | ||
//========================================================================================================= | ||
// Event | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Event - Class that holds a List of Functions which can be triggered at the same time | ||
* @param config - Config | ||
*/ | ||
createEvent(config) { | ||
return new internal_1.Event(this, config); | ||
} | ||
//========================================================================================================= | ||
// Integrate | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Integrates framework into Agile | ||
* @param integration - Integration that gets registered/integrated | ||
*/ | ||
integrate(integration) { | ||
@@ -116,11 +67,2 @@ this.integrations.integrate(integration); | ||
} | ||
//========================================================================================================= | ||
// Register Storage | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Registers new Storage as Agile Storage | ||
* @param storage - new Storage | ||
* @param config - Config | ||
*/ | ||
registerStorage(storage, config = {}) { | ||
@@ -130,19 +72,5 @@ this.storages.register(storage, config); | ||
} | ||
//========================================================================================================= | ||
// Has Integration | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Checks if Agile has any registered Integration | ||
*/ | ||
hasIntegration() { | ||
return this.integrations.hasIntegration(); | ||
} | ||
//========================================================================================================= | ||
// Has Storage | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Checks if Agile has any registered Storage | ||
*/ | ||
hasStorage() { | ||
@@ -153,4 +81,3 @@ return this.storages.hasStorage(); | ||
exports.Agile = Agile; | ||
Agile.initialIntegrations = []; // External added initial Integrations | ||
// Static Logger with default config -> will be overwritten by config of last created Agile Instance | ||
Agile.initialIntegrations = []; | ||
Agile.logger = new internal_1.Logger({ | ||
@@ -157,0 +84,0 @@ prefix: 'Agile', |
@@ -15,8 +15,2 @@ "use strict"; | ||
class CollectionPersistent extends internal_1.Persistent { | ||
/** | ||
* @internal | ||
* Collection Persist Manager - Handles permanent storing of Collection Value | ||
* @param collection - Collection that gets stored | ||
* @param config - Config | ||
*/ | ||
constructor(collection, config = {}) { | ||
@@ -35,14 +29,5 @@ super(collection.agileInstance(), { | ||
}); | ||
// Load/Store persisted Value/s for the first Time | ||
if (this.ready && config.instantiate) | ||
this.initialLoading(); | ||
} | ||
//========================================================================================================= | ||
// Set Key | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Updates Key/Name of Persistent | ||
* @param value - New Key/Name of Persistent | ||
*/ | ||
setKey(value) { | ||
@@ -52,3 +37,2 @@ return __awaiter(this, void 0, void 0, function* () { | ||
const wasReady = this.ready; | ||
// Assign Key | ||
if (value === this._key) | ||
@@ -58,3 +42,2 @@ return; | ||
const isValid = this.validatePersistent(); | ||
// Try to Initial Load Value if persistent wasn't ready | ||
if (!wasReady) { | ||
@@ -65,5 +48,3 @@ if (isValid) | ||
} | ||
// Remove value at old Key | ||
yield this.removePersistedValue(oldKey); | ||
// Assign Value to new Key | ||
if (isValid) | ||
@@ -73,9 +54,2 @@ yield this.persistValue(value); | ||
} | ||
//========================================================================================================= | ||
// Initial Loading | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Loads/Saves Storage Value for the first Time | ||
*/ | ||
initialLoading() { | ||
@@ -91,11 +65,2 @@ const _super = Object.create(null, { | ||
} | ||
//========================================================================================================= | ||
// Load Persisted Value | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Loads Collection from Storage | ||
* @param key - Prefix Key of Persisted Instances (default PersistentKey) | ||
* @return Success? | ||
*/ | ||
loadPersistedValue(key) { | ||
@@ -106,7 +71,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
const _key = key || this._key; | ||
// Check if Collection is Persisted | ||
const isPersisted = yield this.agileInstance().storages.get(_key, this.defaultStorageKey); | ||
if (!isPersisted) | ||
return false; | ||
// Loads Values into Collection | ||
const loadValuesIntoCollection = () => __awaiter(this, void 0, void 0, function* () { | ||
@@ -117,3 +80,2 @@ var _a, _b; | ||
return false; | ||
// Persist Default Group and load its Value manually to be 100% sure it got loaded | ||
defaultGroup.persist({ | ||
@@ -127,10 +89,7 @@ instantiate: false, | ||
} | ||
// Load Items into Collection | ||
for (const itemKey of defaultGroup._value) { | ||
const itemStorageKey = CollectionPersistent.getItemStorageKey(itemKey, _key); | ||
// Get Storage Value | ||
const storageValue = yield this.agileInstance().storages.get(itemStorageKey, this.defaultStorageKey); | ||
if (!storageValue) | ||
continue; | ||
// Collect found Storage Value | ||
this.collection().collect(storageValue); | ||
@@ -141,3 +100,2 @@ } | ||
const success = yield loadValuesIntoCollection(); | ||
// Persist Collection, so that the Storage Value updates dynamically if the Collection updates | ||
if (success) | ||
@@ -148,11 +106,2 @@ yield this.persistValue(_key); | ||
} | ||
//========================================================================================================= | ||
// Persist Value | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Sets everything up so that the Collection gets saved in the Storage | ||
* @param key - Prefix Key of Persisted Instances (default PersistentKey) | ||
* @return Success? | ||
*/ | ||
persistValue(key) { | ||
@@ -166,10 +115,6 @@ return __awaiter(this, void 0, void 0, function* () { | ||
return false; | ||
// Set Collection to Persisted (in Storage) | ||
this.agileInstance().storages.set(_key, true, this.storageKeys); | ||
// Persist default Group | ||
if (!defaultGroup.isPersisted) | ||
defaultGroup.persist({ followCollectionPersistKeyPattern: true }); | ||
// Add sideEffect to default Group which adds and removes Items from the Storage depending on the Group Value | ||
defaultGroup.addSideEffect(CollectionPersistent.defaultGroupSideEffectKey, () => this.rebuildStorageSideEffect(defaultGroup, _key), { weight: 0 }); | ||
// Persist Collection Items | ||
for (const itemKey of defaultGroup._value) { | ||
@@ -184,11 +129,2 @@ const item = this.collection().getItem(itemKey); | ||
} | ||
//========================================================================================================= | ||
// Remove Persisted Value | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Removes Collection from the Storage | ||
* @param key - Prefix Key of Persisted Instances (default PersistentKey) | ||
* @return Success? | ||
*/ | ||
removePersistedValue(key) { | ||
@@ -203,9 +139,5 @@ var _a, _b; | ||
return false; | ||
// Set Collection to not Persisted | ||
this.agileInstance().storages.remove(_key, this.storageKeys); | ||
// Remove default Group from Storage | ||
(_a = defaultGroup.persistent) === null || _a === void 0 ? void 0 : _a.removePersistedValue(); | ||
// Remove Rebuild Storage sideEffect from default Group | ||
defaultGroup.removeSideEffect(CollectionPersistent.defaultGroupSideEffectKey); | ||
// Remove Collection Items from Storage | ||
for (const itemKey of defaultGroup._value) { | ||
@@ -219,13 +151,4 @@ const item = this.collection().getItem(itemKey); | ||
} | ||
//========================================================================================================= | ||
// Format Key | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Formats Storage Key | ||
* @param key - Key that gets formatted | ||
*/ | ||
formatKey(key) { | ||
const collection = this.collection(); | ||
// Get key from Collection | ||
if (!key && collection._key) | ||
@@ -235,3 +158,2 @@ return collection._key; | ||
return; | ||
// Set Storage Key to Collection Key if Collection has no key | ||
if (!collection._key) | ||
@@ -241,11 +163,2 @@ collection._key = key; | ||
} | ||
//========================================================================================================= | ||
// Rebuild Storage SideEffect | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Rebuilds Storage depending on Group | ||
* @param group - Group | ||
* @param key - Prefix Key of Persisted Instances (default PersistentKey) | ||
*/ | ||
rebuildStorageSideEffect(group, key) { | ||
@@ -255,3 +168,2 @@ var _a; | ||
const _key = key || ((_a = collection.persistent) === null || _a === void 0 ? void 0 : _a._key); | ||
// Return if only a ItemKey got updated | ||
if (group.previousStateValue.length === group._value.length) | ||
@@ -261,3 +173,2 @@ return; | ||
const removedKeys = group.previousStateValue.filter((key) => !group._value.includes(key)); | ||
// Persist Added Keys | ||
addedKeys.forEach((itemKey) => { | ||
@@ -274,3 +185,2 @@ var _a; | ||
}); | ||
// Unpersist removed Keys | ||
removedKeys.forEach((itemKey) => { | ||
@@ -286,11 +196,2 @@ var _a; | ||
} | ||
//========================================================================================================= | ||
// Get Item Storage Key | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Build Item StorageKey with Collection Persist Pattern | ||
* @param itemKey - Key of Item | ||
* @param collectionKey - Key of Collection | ||
*/ | ||
static getItemStorageKey(itemKey, collectionKey) { | ||
@@ -307,11 +208,2 @@ if (!itemKey || !collectionKey) | ||
} | ||
//========================================================================================================= | ||
// Get Group Storage Key | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Build Group StorageKey with Collection Persist Pattern | ||
* @param groupKey - Key of Group | ||
* @param collectionKey - Key of Collection | ||
*/ | ||
static getGroupStorageKey(groupKey, collectionKey) { | ||
@@ -318,0 +210,0 @@ if (!groupKey || !collectionKey) |
@@ -6,24 +6,11 @@ "use strict"; | ||
class Group extends internal_1.State { | ||
/** | ||
* @public | ||
* Group - Holds Items of Collection | ||
* @param collection - Collection to that the Group belongs | ||
* @param initialItems - Initial Key of Items in this Group | ||
* @param config - Config | ||
*/ | ||
constructor(collection, initialItems, config = {}) { | ||
super(collection.agileInstance(), initialItems || [], config); | ||
this._output = []; // Output of Group | ||
this._items = []; // Items of Group | ||
this.notFoundItemKeys = []; // Contains all keys of Group that can't be found in Collection | ||
this._output = []; | ||
this._items = []; | ||
this.notFoundItemKeys = []; | ||
this.collection = () => collection; | ||
// Add rebuild to sideEffects to rebuild Group on Value Change | ||
this.addSideEffect(Group.rebuildGroupSideEffectKey, () => this.rebuild()); | ||
// Initial Rebuild | ||
this.rebuild(); | ||
} | ||
/** | ||
* @public | ||
* Get Item Values of Group | ||
*/ | ||
get output() { | ||
@@ -33,13 +20,5 @@ internal_1.ComputedTracker.tracked(this.observer); | ||
} | ||
/** | ||
* @public | ||
* Set Item Values of Group | ||
*/ | ||
set output(value) { | ||
this._output = value; | ||
} | ||
/** | ||
* @public | ||
* Get Items of Group | ||
*/ | ||
get items() { | ||
@@ -49,39 +28,11 @@ internal_1.ComputedTracker.tracked(this.observer); | ||
} | ||
/** | ||
* @public | ||
* Set Items of Group | ||
*/ | ||
set items(value) { | ||
this._items = value.map((item) => () => item); | ||
} | ||
//========================================================================================================= | ||
// Has | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Checks if Group contains ItemKey | ||
* @param itemKey - ItemKey that gets checked | ||
*/ | ||
has(itemKey) { | ||
return this.value.findIndex((key) => key === itemKey) !== -1; | ||
} | ||
//========================================================================================================= | ||
// Size | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Get size of Group (-> How many Items it contains) | ||
*/ | ||
get size() { | ||
return this.value.length; | ||
} | ||
//========================================================================================================= | ||
// Remove | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Removes ItemKey/s from Group | ||
* @param itemKeys - ItemKey/s that get removed from Group | ||
* @param config - Config | ||
*/ | ||
remove(itemKeys, config = {}) { | ||
@@ -95,5 +46,3 @@ const _itemKeys = internal_1.normalizeArray(itemKeys); | ||
}); | ||
// Remove ItemKeys from Group | ||
_itemKeys.forEach((itemKey) => { | ||
// Check if itemKey exists in Group | ||
if (!newGroupValue.includes(itemKey)) { | ||
@@ -105,12 +54,8 @@ internal_1.Agile.logger.error(`Couldn't find ItemKey '${itemKey}' in Group '${this._key}'!`); | ||
} | ||
// Check if ItemKey exists in Collection | ||
if (!this.collection().getItem(itemKey)) | ||
notExistingItemKeysInCollection.push(itemKey); | ||
// Remove ItemKey from Group | ||
newGroupValue = newGroupValue.filter((key) => key !== itemKey); | ||
}); | ||
// Return if passed ItemKeys doesn't exist | ||
if (notExistingItemKeys.length >= _itemKeys.length) | ||
return this; | ||
// If all removed ItemKeys doesn't exist in Collection -> no rerender necessary since output doesn't change | ||
if (notExistingItemKeysInCollection.length >= _itemKeys.length) | ||
@@ -121,11 +66,2 @@ config.background = true; | ||
} | ||
//========================================================================================================= | ||
// Add | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Adds ItemKey/s to Group | ||
* @param itemKeys - ItemKey/s that get added to the Group | ||
* @param config - Config | ||
*/ | ||
add(itemKeys, config = {}) { | ||
@@ -141,9 +77,6 @@ const _itemKeys = internal_1.normalizeArray(itemKeys); | ||
}); | ||
// Add ItemKeys to Group | ||
_itemKeys.forEach((itemKey) => { | ||
const existsInGroup = newGroupValue.includes(itemKey); | ||
// Check if ItemKey exists in Collection | ||
if (!this.collection().getItem(itemKey)) | ||
notExistingItemKeysInCollection.push(itemKey); | ||
// Remove ItemKey from Group if it should get overwritten and already exists | ||
if (existsInGroup) { | ||
@@ -158,9 +91,6 @@ if (config.overwrite) { | ||
} | ||
// Add new ItemKey to Group | ||
newGroupValue[config.method || 'push'](itemKey); | ||
}); | ||
// Return if passed ItemKeys already exist | ||
if (existingItemKeys.length >= _itemKeys.length) | ||
return this; | ||
// If all added ItemKeys doesn't exist in Collection or already exist -> no rerender necessary since output doesn't change | ||
if (notExistingItemKeysInCollection.concat(existingItemKeys).length >= | ||
@@ -172,12 +102,2 @@ _itemKeys.length) | ||
} | ||
//========================================================================================================= | ||
// Replace | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Replaces oldItemKey with newItemKey | ||
* @param oldItemKey - Old ItemKey | ||
* @param newItemKey - New ItemKey | ||
* @param config - Config | ||
*/ | ||
replace(oldItemKey, newItemKey, config = {}) { | ||
@@ -214,13 +134,5 @@ const newGroupValue = internal_1.copy(this._value); | ||
} | ||
//========================================================================================================= | ||
// Rebuild | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Rebuilds Output and Items of Group | ||
*/ | ||
rebuild() { | ||
const notFoundItemKeys = []; // Item Keys that couldn't be found in Collection | ||
const notFoundItemKeys = []; | ||
const groupItems = []; | ||
// Create groupItems by finding Item at ItemKey in Collection | ||
this._value.forEach((itemKey) => { | ||
@@ -233,7 +145,5 @@ const item = this.collection().getItem(itemKey); | ||
}); | ||
// Create groupOutput out of groupItems | ||
const groupOutput = groupItems.map((item) => { | ||
return item.getPublicValue(); | ||
}); | ||
// Logging | ||
if (notFoundItemKeys.length > 0) { | ||
@@ -240,0 +150,0 @@ internal_1.Agile.logger.warn(`Couldn't find some Items in Collection '${this.collection()._key}' (${this._key})`, notFoundItemKeys); |
@@ -6,12 +6,6 @@ "use strict"; | ||
class Collection { | ||
/** | ||
* @public | ||
* Collection - Class that holds a List of Objects with key and causes rerender on subscribed Components | ||
* @param agileInstance - An instance of Agile | ||
* @param config - Config | ||
*/ | ||
constructor(agileInstance, config = {}) { | ||
this.size = 0; // Amount of Items stored in Collection | ||
this.data = {}; // Collection Data | ||
this.isPersisted = false; // If Collection can be stored in Agile Storage (-> successfully integrated persistent) | ||
this.size = 0; | ||
this.data = {}; | ||
this.isPersisted = false; | ||
this.groups = {}; | ||
@@ -21,3 +15,2 @@ this.selectors = {}; | ||
this.agileInstance = () => agileInstance; | ||
// Set temp Config for creating proper Placeholder Items (of Selector) | ||
this.config = { | ||
@@ -27,3 +20,2 @@ defaultGroupKey: 'default', | ||
}; | ||
// Assign Properties | ||
let _config = typeof config === 'function' ? config(this) : config; | ||
@@ -48,30 +40,12 @@ _config = internal_1.defineConfig(_config, { | ||
} | ||
/** | ||
* @public | ||
* Set Key/Name of Collection | ||
*/ | ||
set key(value) { | ||
this.setKey(value); | ||
} | ||
/** | ||
* @public | ||
* Get Key/Name of Collection | ||
*/ | ||
get key() { | ||
return this._key; | ||
} | ||
//========================================================================================================= | ||
// Set Key | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Set Key/Name of Collection | ||
* @param value - New Key/Name of Collection | ||
*/ | ||
setKey(value) { | ||
var _a, _b; | ||
const oldKey = this._key; | ||
// Update State Key | ||
this._key = value; | ||
// Update Key in Persistent (only if oldKey equal to persistentKey -> otherwise the PersistentKey got formatted and will be set where other) | ||
if (value && ((_a = this.persistent) === null || _a === void 0 ? void 0 : _a._key) === oldKey) | ||
@@ -81,11 +55,2 @@ (_b = this.persistent) === null || _b === void 0 ? void 0 : _b.setKey(value); | ||
} | ||
//========================================================================================================= | ||
// Group | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Group - Holds Items of this Collection | ||
* @param initialItems - Initial ItemKeys of Group | ||
* @param config - Config | ||
*/ | ||
Group(initialItems, config) { | ||
@@ -101,11 +66,2 @@ if (this.isInstantiated) { | ||
} | ||
//========================================================================================================= | ||
// Selector | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Selector - Represents an Item of this Collection | ||
* @param initialKey - Key of Item that the Selector represents | ||
* @param config - Config | ||
*/ | ||
Selector(initialKey, config) { | ||
@@ -121,9 +77,2 @@ if (this.isInstantiated) { | ||
} | ||
//========================================================================================================= | ||
// Init Groups | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Instantiates Groups | ||
*/ | ||
initGroups(groups) { | ||
@@ -133,3 +82,2 @@ if (!groups) | ||
let groupsObject = {}; | ||
// If groups is Array of GroupNames transform it to Group Object | ||
if (Array.isArray(groups)) { | ||
@@ -144,7 +92,5 @@ groups.forEach((groupKey) => { | ||
groupsObject = groups; | ||
// Add default Group | ||
groupsObject[this.config.defaultGroupKey] = new internal_1.Group(this, [], { | ||
key: this.config.defaultGroupKey, | ||
}); | ||
// Set Key/Name of Group to property Name | ||
for (const key in groupsObject) | ||
@@ -155,9 +101,2 @@ if (!groupsObject[key]._key) | ||
} | ||
//========================================================================================================= | ||
// Init Selectors | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Instantiates Selectors | ||
*/ | ||
initSelectors(selectors) { | ||
@@ -167,3 +106,2 @@ if (!selectors) | ||
let selectorsObject = {}; | ||
// If selectors is Array of SelectorNames transform it to Selector Object | ||
if (Array.isArray(selectors)) { | ||
@@ -178,3 +116,2 @@ selectors.forEach((selectorKey) => { | ||
selectorsObject = selectors; | ||
// Set Key/Name of Selector to property Name | ||
for (const key in selectorsObject) | ||
@@ -185,12 +122,2 @@ if (!selectorsObject[key]._key) | ||
} | ||
//========================================================================================================= | ||
// Collect | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Collect Item/s | ||
* @param data - Data that gets added to Collection | ||
* @param groupKeys - Add collected Item/s to certain Groups | ||
* @param config - Config | ||
*/ | ||
collect(data, groupKeys, config = {}) { | ||
@@ -207,10 +134,7 @@ const _data = internal_1.normalizeArray(data); | ||
}); | ||
// Add default GroupKey, because Items get always added to default Group | ||
if (!_groupKeys.includes(defaultGroupKey)) | ||
_groupKeys.push(defaultGroupKey); | ||
// Create not existing Groups | ||
_groupKeys.forEach((key) => !this.groups[key] && this.createGroup(key)); | ||
_data.forEach((data, index) => { | ||
const itemKey = data[primaryKey]; | ||
// Add Item to Collection | ||
const success = this.setData(data, { | ||
@@ -222,3 +146,2 @@ patch: config.patch, | ||
return this; | ||
// Add ItemKey to provided Groups | ||
_groupKeys.forEach((groupKey) => { | ||
@@ -238,12 +161,2 @@ var _a; | ||
} | ||
//========================================================================================================= | ||
// Update | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Updates Item at provided Key | ||
* @param itemKey - ItemKey of Item that gets updated | ||
* @param changes - Changes that will be merged into the Item (flatMerge) | ||
* @param config - Config | ||
*/ | ||
update(itemKey, changes, config = {}) { | ||
@@ -253,3 +166,3 @@ const item = this.getItem(itemKey, { notExisting: true }); | ||
config = internal_1.defineConfig(config, { | ||
addNewProperties: false, | ||
addNewProperties: true, | ||
background: false, | ||
@@ -268,6 +181,4 @@ }); | ||
const updateItemKey = oldItemKey !== newItemKey; | ||
// Delete primaryKey from 'changes' because if it has changed, it gets properly updated in 'updateItemKey' (below) | ||
if (changes[primaryKey]) | ||
delete changes[primaryKey]; | ||
// Update ItemKey | ||
if (updateItemKey) | ||
@@ -277,3 +188,2 @@ this.updateItemKey(oldItemKey, newItemKey, { | ||
}); | ||
// Apply changes to Item | ||
item.patch(changes, { | ||
@@ -285,11 +195,2 @@ background: config.background, | ||
} | ||
//========================================================================================================= | ||
// Create Group | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Creates new Group that can hold Items of Collection | ||
* @param groupKey - Name/Key of Group | ||
* @param initialItems - Initial ItemKeys of Group | ||
*/ | ||
createGroup(groupKey, initialItems = []) { | ||
@@ -300,3 +201,2 @@ let group = this.getGroup(groupKey, { notExisting: true }); | ||
} | ||
// Check if Group already exists | ||
if (group) { | ||
@@ -310,3 +210,2 @@ if (!group.isPlaceholder) { | ||
} | ||
// Create Group | ||
group = new internal_1.Group(this, initialItems, { key: groupKey }); | ||
@@ -316,23 +215,5 @@ this.groups[groupKey] = group; | ||
} | ||
//========================================================================================================= | ||
// Has Group | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Check if Group exists in Collection | ||
* @param groupKey - Key/Name of Group | ||
* @param config - Config | ||
*/ | ||
hasGroup(groupKey, config = {}) { | ||
return !!this.getGroup(groupKey, config); | ||
} | ||
//========================================================================================================= | ||
// Get Group | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Get Group by Key/Name | ||
* @param groupKey - Key/Name of Group | ||
* @param config - Config | ||
*/ | ||
getGroup(groupKey, config = {}) { | ||
@@ -342,5 +223,3 @@ config = internal_1.defineConfig(config, { | ||
}); | ||
// Get Group | ||
const group = groupKey ? this.groups[groupKey] : undefined; | ||
// Check if Group exists | ||
if (!group || (!config.notExisting && group.isPlaceholder)) | ||
@@ -351,13 +230,4 @@ return undefined; | ||
} | ||
//========================================================================================================= | ||
// Get Group With Reference | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Get Group by Key/Name or a Reference to it if it doesn't exist yet | ||
* @param groupKey - Name/Key of Group | ||
*/ | ||
getGroupWithReference(groupKey) { | ||
let group = this.getGroup(groupKey, { notExisting: true }); | ||
// Create dummy Group to hold reference | ||
if (!group) { | ||
@@ -373,10 +243,2 @@ group = new internal_1.Group(this, [], { | ||
} | ||
//========================================================================================================= | ||
// Remove Group | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Removes Group by Key/Name | ||
* @param groupKey - Name/Key of Group | ||
*/ | ||
removeGroup(groupKey) { | ||
@@ -390,11 +252,2 @@ if (!this.groups[groupKey]) { | ||
} | ||
//========================================================================================================= | ||
// Create Selector | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Creates new Selector that represents an Item of the Collection | ||
* @param selectorKey - Name/Key of Selector | ||
* @param itemKey - Key of Item which the Selector represents | ||
*/ | ||
createSelector(selectorKey, itemKey) { | ||
@@ -405,3 +258,2 @@ let selector = this.getSelector(selectorKey, { notExisting: true }); | ||
} | ||
// Check if Selector already exists | ||
if (selector) { | ||
@@ -415,3 +267,2 @@ if (!selector.isPlaceholder) { | ||
} | ||
// Create Selector | ||
selector = new internal_1.Selector(this, itemKey, { | ||
@@ -423,23 +274,5 @@ key: selectorKey, | ||
} | ||
//========================================================================================================= | ||
// Has Selector | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Check if Selector exists in Collection | ||
* @param selectorKey - Key/Name of Selector | ||
* @param config - Config | ||
*/ | ||
hasSelector(selectorKey, config = {}) { | ||
return !!this.getSelector(selectorKey, config); | ||
} | ||
//========================================================================================================= | ||
// Get Selector | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Get Selector by Key/Name | ||
* @param selectorKey - Key/Name of Selector | ||
* @param config - Config | ||
*/ | ||
getSelector(selectorKey, config = {}) { | ||
@@ -449,5 +282,3 @@ config = internal_1.defineConfig(config, { | ||
}); | ||
// Get Selector | ||
const selector = selectorKey ? this.selectors[selectorKey] : undefined; | ||
// Check if Selector exists | ||
if (!selector || (!config.notExisting && selector.isPlaceholder)) | ||
@@ -458,13 +289,4 @@ return undefined; | ||
} | ||
//========================================================================================================= | ||
// Get Selector With Reference | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Get Selector by Key/Name or a Reference to it if it doesn't exist yet | ||
* @param selectorKey - Name/Key of Selector | ||
*/ | ||
getSelectorWithReference(selectorKey) { | ||
let selector = this.getSelector(selectorKey, { notExisting: true }); | ||
// Create dummy Selector to hold reference | ||
if (!selector) { | ||
@@ -480,10 +302,2 @@ selector = new internal_1.Selector(this, 'unknown', { | ||
} | ||
//========================================================================================================= | ||
// Remove Selector | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Removes Selector by Key/Name | ||
* @param selectorKey - Name/Key of Selector | ||
*/ | ||
removeSelector(selectorKey) { | ||
@@ -495,27 +309,9 @@ var _a; | ||
} | ||
(_a = this.selectors[selectorKey]) === null || _a === void 0 ? void 0 : _a.unselect(); // Unselects current selected Item | ||
(_a = this.selectors[selectorKey]) === null || _a === void 0 ? void 0 : _a.unselect(); | ||
delete this.selectors[selectorKey]; | ||
return this; | ||
} | ||
//========================================================================================================= | ||
// Has Item | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Check if Item exists in Collection | ||
* @param itemKey - Key/Name of Item | ||
* @param config - Config | ||
*/ | ||
hasItem(itemKey, config = {}) { | ||
return !!this.getItem(itemKey, config); | ||
} | ||
//========================================================================================================= | ||
// Get Item by Id | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Get Item by Key/Name | ||
* @param itemKey - ItemKey of Item | ||
* @param config - Config | ||
*/ | ||
getItem(itemKey, config = {}) { | ||
@@ -525,5 +321,3 @@ config = internal_1.defineConfig(config, { | ||
}); | ||
// Get Item | ||
const item = itemKey ? this.data[itemKey] : undefined; | ||
// Check if Item exists | ||
if (!item || (!config.notExisting && !item.exists)) | ||
@@ -534,10 +328,4 @@ return undefined; | ||
} | ||
/** | ||
* @public | ||
* Get Item by Key/Name or a Reference to it if it doesn't exist yet | ||
* @param itemKey - Key/Name of Item | ||
*/ | ||
getItemWithReference(itemKey) { | ||
let item = this.getItem(itemKey, { notExisting: true }); | ||
// Create dummy Item to hold reference | ||
if (!item) { | ||
@@ -555,11 +343,2 @@ item = new internal_1.Item(this, { | ||
} | ||
//========================================================================================================= | ||
// Get Value by Id | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Get Value of Item by Key/Name | ||
* @param itemKey - ItemKey of Item that holds the Value | ||
* @param config - Config | ||
*/ | ||
getItemValue(itemKey, config = {}) { | ||
@@ -571,10 +350,2 @@ const item = this.getItem(itemKey, config); | ||
} | ||
//========================================================================================================= | ||
// Get All Items | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Get all Items of Collection | ||
* @param config - Config | ||
*/ | ||
getAllItems(config = {}) { | ||
@@ -584,3 +355,2 @@ config = internal_1.defineConfig(config, { | ||
}); | ||
// Get Items | ||
const items = []; | ||
@@ -595,10 +365,2 @@ for (const key in this.data) { | ||
} | ||
//========================================================================================================= | ||
// Get All Item Values | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Get all Values of Items in a Collection | ||
* @param config - Config | ||
*/ | ||
getAllItemValues(config = {}) { | ||
@@ -625,3 +387,2 @@ const items = this.getAllItems(config); | ||
internal_1.Agile.logger.warn(`By persisting the Collection '${this._key}' twice you overwrite the old Persistent Instance!`); | ||
// Create persistent -> Persist Value | ||
this.persistent = new internal_1.CollectionPersistent(this, { | ||
@@ -634,15 +395,5 @@ instantiate: _config.instantiate, | ||
} | ||
//========================================================================================================= | ||
// On Load | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Callback Function that gets called if the persisted Value gets loaded into the Collection for the first Time | ||
* Note: Only useful for persisted Collections! | ||
* @param callback - Callback Function | ||
*/ | ||
onLoad(callback) { | ||
if (this.persistent) { | ||
this.persistent.onLoad = callback; | ||
// If Collection is already 'isPersisted' the loading was successful -> callback can be called | ||
if (this.isPersisted) | ||
@@ -656,9 +407,2 @@ callback(true); | ||
} | ||
//========================================================================================================= | ||
// Get Group Count | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Get count of registered Groups in Collection | ||
*/ | ||
getGroupCount() { | ||
@@ -670,9 +414,2 @@ let size = 0; | ||
} | ||
//========================================================================================================= | ||
// Get Selector Count | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Get count of registered Selectors in Collection | ||
*/ | ||
getSelectorCount() { | ||
@@ -684,35 +421,14 @@ let size = 0; | ||
} | ||
//========================================================================================================= | ||
// Reset | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Resets this Collection | ||
*/ | ||
reset() { | ||
var _a, _b; | ||
// Reset Data | ||
this.data = {}; | ||
this.size = 0; | ||
// Reset Groups | ||
for (const key in this.groups) | ||
(_a = this.getGroup(key)) === null || _a === void 0 ? void 0 : _a.reset(); | ||
// Reset Selectors | ||
for (const key in this.selectors) | ||
(_b = this.getSelector(key)) === null || _b === void 0 ? void 0 : _b.reset(); | ||
} | ||
//========================================================================================================= | ||
// Put | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Puts ItemKey/s into Group/s (GroupKey/s) | ||
* @param itemKeys - ItemKey/s that get added to provided Group/s | ||
* @param groupKeys - Group/s to which the ItemKey/s get added | ||
* @param config - Config | ||
*/ | ||
put(itemKeys, groupKeys, config = {}) { | ||
const _itemKeys = internal_1.normalizeArray(itemKeys); | ||
const _groupKeys = internal_1.normalizeArray(groupKeys); | ||
// Add ItemKeys to Groups | ||
_groupKeys.forEach((groupKey) => { | ||
@@ -723,12 +439,2 @@ var _a; | ||
} | ||
//========================================================================================================= | ||
// Update Item Key | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Updates Key/Name of Item in all Instances (Group, Selector, ..) | ||
* @param oldItemKey - Old ItemKey | ||
* @param newItemKey - New ItemKey | ||
* @param config - Config | ||
*/ | ||
updateItemKey(oldItemKey, newItemKey, config = {}) { | ||
@@ -742,3 +448,2 @@ var _a; | ||
return false; | ||
// Check if Item with newItemKey already exists | ||
if (this.hasItem(newItemKey)) { | ||
@@ -748,12 +453,8 @@ internal_1.Agile.logger.warn(`Couldn't update ItemKey from '${oldItemKey}' to '${newItemKey}' because an Item with the key/name '${newItemKey}' already exists!`); | ||
} | ||
// Remove Item from old ItemKey and add Item to new ItemKey | ||
delete this.data[oldItemKey]; | ||
this.data[newItemKey] = item; | ||
// Update Key/Name of Item | ||
item.setKey(newItemKey, { | ||
background: config.background, | ||
}); | ||
// Update persist Key of Item (Doesn't get updated by updating key of Item because PersistKey is special formatted) | ||
(_a = item.persistent) === null || _a === void 0 ? void 0 : _a.setKey(internal_1.CollectionPersistent.getItemStorageKey(newItemKey, this._key)); | ||
// Update ItemKey in Groups | ||
for (const groupKey in this.groups) { | ||
@@ -765,3 +466,2 @@ const group = this.getGroup(groupKey, { notExisting: true }); | ||
} | ||
// Update ItemKey in Selectors | ||
for (const selectorKey in this.selectors) { | ||
@@ -771,3 +471,2 @@ const selector = this.getSelector(selectorKey, { notExisting: true }); | ||
continue; | ||
// Reselect Item in existing Selector which has selected the newItemKey | ||
if (selector.hasSelected(newItemKey)) { | ||
@@ -779,3 +478,2 @@ selector.select(newItemKey, { | ||
} | ||
// Select newItemKey in existing Selector which has selected the oldItemKey | ||
if (selector.hasSelected(oldItemKey)) | ||
@@ -788,10 +486,2 @@ selector.select(newItemKey, { | ||
} | ||
//========================================================================================================= | ||
// Get GroupKeys That Have ItemKey | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Gets GroupKeys that contain the passed ItemKey | ||
* @param itemKey - ItemKey | ||
*/ | ||
getGroupKeysThatHaveItemKey(itemKey) { | ||
@@ -806,10 +496,2 @@ const groupKeys = []; | ||
} | ||
//========================================================================================================= | ||
// Remove | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Remove Items from Collection | ||
* @param itemKeys - ItemKey/s that get removed | ||
*/ | ||
remove(itemKeys) { | ||
@@ -821,11 +503,2 @@ return { | ||
} | ||
//========================================================================================================= | ||
// Remove From Groups | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Removes Item/s from Group/s | ||
* @param itemKeys - ItemKey/s that get removed from Group/s | ||
* @param groupKeys - GroupKey/s of Group/s form which the ItemKey/s will be removed | ||
*/ | ||
removeFromGroups(itemKeys, groupKeys) { | ||
@@ -836,3 +509,2 @@ const _itemKeys = internal_1.normalizeArray(itemKeys); | ||
let removedFromGroupsCount = 0; | ||
// Remove ItemKey from Groups | ||
_groupKeys.forEach((groupKey) => { | ||
@@ -845,3 +517,2 @@ const group = this.getGroup(groupKey, { notExisting: true }); | ||
}); | ||
// If Item got removed from every Groups the Item was in, remove it completely | ||
if (removedFromGroupsCount >= | ||
@@ -852,10 +523,2 @@ this.getGroupKeysThatHaveItemKey(itemKey).length) | ||
} | ||
//========================================================================================================= | ||
// Remove Items | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Removes Item completely from Collection | ||
* @param itemKeys - ItemKey/s of Item/s | ||
*/ | ||
removeItems(itemKeys) { | ||
@@ -868,3 +531,2 @@ const _itemKeys = internal_1.normalizeArray(itemKeys); | ||
return; | ||
// Remove Item from Groups | ||
for (const groupKey in this.groups) { | ||
@@ -875,7 +537,4 @@ const group = this.getGroup(groupKey, { notExisting: true }); | ||
} | ||
// Remove Item from Storage | ||
(_a = item.persistent) === null || _a === void 0 ? void 0 : _a.removePersistedValue(); | ||
// Remove Item from Collection | ||
delete this.data[itemKey]; | ||
// Reselect Item in Selectors (to create new dummyItem that holds reference) | ||
for (const selectorKey in this.selectors) { | ||
@@ -889,13 +548,4 @@ const selector = this.getSelector(selectorKey, { notExisting: true }); | ||
} | ||
//========================================================================================================= | ||
// Set Data | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Updates existing or creates Item from provided Data | ||
* @param data - Data | ||
* @param config - Config | ||
*/ | ||
setData(data, config = {}) { | ||
const _data = internal_1.copy(data); // Transformed Data to any because of unknown Object (DataType) | ||
const _data = internal_1.copy(data); | ||
const primaryKey = this.config.primaryKey; | ||
@@ -918,3 +568,2 @@ config = internal_1.defineConfig(config, { | ||
const createItem = !item; | ||
// Create or update Item | ||
if (!createItem && config.patch) | ||
@@ -925,6 +574,4 @@ item === null || item === void 0 ? void 0 : item.patch(_data, { background: config.background }); | ||
if (createItem) { | ||
// Create and assign Item to Collection | ||
item = new internal_1.Item(this, _data); | ||
this.data[itemKey] = item; | ||
// Rebuild Groups That include ItemKey after assigning Item to Collection (otherwise it can't find Item) | ||
this.rebuildGroupsThatIncludeItemKey(itemKey, { | ||
@@ -934,3 +581,2 @@ background: config.background, | ||
} | ||
// Increase size of Collection | ||
if (createItem || wasPlaceholder) | ||
@@ -940,11 +586,2 @@ this.size++; | ||
} | ||
//========================================================================================================= | ||
// Rebuild Groups That Includes Item Key | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Rebuilds Groups that include the provided ItemKey | ||
* @itemKey - Item Key | ||
* @config - Config | ||
*/ | ||
rebuildGroupsThatIncludeItemKey(itemKey, config = {}) { | ||
@@ -958,7 +595,5 @@ config = internal_1.defineConfig(config, { | ||
}); | ||
// Rebuild Groups that include ItemKey | ||
for (const groupKey in this.groups) { | ||
const group = this.getGroup(groupKey); | ||
if (group === null || group === void 0 ? void 0 : group.has(itemKey)) { | ||
// group.rebuild(); Not necessary because a sideEffect of the Group is to rebuild it self | ||
group === null || group === void 0 ? void 0 : group.ingest({ | ||
@@ -965,0 +600,0 @@ background: config === null || config === void 0 ? void 0 : config.background, |
@@ -6,9 +6,2 @@ "use strict"; | ||
class Item extends internal_1.State { | ||
/** | ||
* @public | ||
* Item of Collection | ||
* @param collection - Collection to which the Item belongs | ||
* @param data - Data that the Item holds | ||
* @param config - Config | ||
*/ | ||
constructor(collection, data, config = {}) { | ||
@@ -19,16 +12,6 @@ super(collection.agileInstance(), data, { | ||
}); | ||
this.isSelected = false; // If Item is selected by a Selector | ||
this.isSelected = false; | ||
this.collection = () => collection; | ||
// Add rebuildGroupsThatIncludeItemKey to sideEffects to rebuild Groups that include this Item if it mutates | ||
this.addRebuildGroupThatIncludeItemKeySideEffect(this._key || 'unknown'); | ||
} | ||
//========================================================================================================= | ||
// Set Key | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Updates Key/Name of State | ||
* @param value - New Key/Name of State | ||
* @param config - Config | ||
*/ | ||
setKey(value, config = {}) { | ||
@@ -48,7 +31,4 @@ super.setKey(value); | ||
return this; | ||
// Remove old rebuildGroupsThatIncludeItemKey sideEffect | ||
this.removeSideEffect(Item.updateGroupSideEffectKey); | ||
// Add rebuildGroupsThatIncludeItemKey to sideEffects to rebuild Groups that include this Item if it mutates | ||
this.addRebuildGroupThatIncludeItemKeySideEffect(value); | ||
// Update ItemKey in ItemValue (After updating the sideEffect because otherwise it calls the old sideEffect) | ||
this.patch({ [this.collection().config.primaryKey]: value }, { | ||
@@ -63,10 +43,2 @@ sideEffects: config.sideEffects, | ||
} | ||
//========================================================================================================= | ||
// Add Rebuild Group That Include ItemKey SideEffect | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Adds rebuildGroupThatIncludeItemKey to the Item sideEffects | ||
* @param itemKey - ItemKey at which the groups has to rebuild | ||
*/ | ||
addRebuildGroupThatIncludeItemKeySideEffect(itemKey) { | ||
@@ -73,0 +45,0 @@ this.addSideEffect(Item.updateGroupSideEffectKey, (instance, config) => instance.collection().rebuildGroupsThatIncludeItemKey(itemKey, config), { weight: 100 }); |
@@ -6,9 +6,2 @@ "use strict"; | ||
class Selector extends internal_1.State { | ||
/** | ||
* @public | ||
* Represents Item of Collection | ||
* @param collection - Collection that contains the Item | ||
* @param itemKey - ItemKey of Item that the Selector represents | ||
* @param config - Config | ||
*/ | ||
constructor(collection, itemKey, config = {}) { | ||
@@ -24,33 +17,15 @@ super(collection.agileInstance(), undefined, config); | ||
this.isPlaceholder = true; | ||
// Initial Select | ||
if (!config.isPlaceholder) | ||
this.select(itemKey, { overwrite: true }); | ||
} | ||
/** | ||
* @public | ||
* Set ItemKey that the Selector represents | ||
*/ | ||
set itemKey(value) { | ||
this.select(value); | ||
} | ||
/** | ||
* @public | ||
* Get ItemKey that the Selector represents | ||
*/ | ||
get itemKey() { | ||
return this._itemKey; | ||
} | ||
//========================================================================================================= | ||
// Select | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Select new ItemKey | ||
* @param itemKey - New ItemKey | ||
* @param config - Config | ||
*/ | ||
select(itemKey, config = {}) { | ||
const oldItem = this.collection().getItem(this._itemKey, { | ||
notExisting: true, | ||
}); // Because this.item might be outdated | ||
}); | ||
const newItem = this.collection().getItemWithReference(itemKey); | ||
@@ -71,3 +46,2 @@ config = internal_1.defineConfig(config, { | ||
} | ||
// Unselect old Item | ||
this.unselect({ background: true }); | ||
@@ -77,5 +51,3 @@ this._itemKey = itemKey; | ||
newItem.isSelected = true; | ||
// Add SideEffect to newItem, that rebuild this Selector depending on the current Item Value | ||
newItem.addSideEffect(Selector.rebuildSelectorSideEffectKey, (instance, config) => this.rebuildSelector(config), { weight: 100 }); | ||
// Add sideEffect to Selector, that updates the Item Value if this Value got updated | ||
this.addSideEffect(Selector.rebuildItemSideEffectKey, (instance, config) => { | ||
@@ -91,20 +63,9 @@ var _a, _b; | ||
}, { weight: 90 }); | ||
// Rebuild Selector for instantiating new 'selected' ItemKey properly | ||
this.rebuildSelector(config); | ||
return this; | ||
} | ||
//========================================================================================================= | ||
// Unselect | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Unselects current selected Item | ||
* @param config - Config | ||
*/ | ||
unselect(config = {}) { | ||
// Because this.item might be outdated | ||
const item = this.collection().getItem(this._itemKey, { | ||
notExisting: true, | ||
}); | ||
// Unselect Item | ||
if (item) { | ||
@@ -117,3 +78,2 @@ item.isSelected = false; | ||
} | ||
// Reset and rebuild Selector | ||
this.item = undefined; | ||
@@ -125,9 +85,2 @@ this._itemKey = Selector.dummyItemKey; | ||
} | ||
//========================================================================================================= | ||
// Has Selected | ||
//========================================================================================================= | ||
/** | ||
* Checks if Selector has selected passed ItemKey | ||
* @param itemKey | ||
*/ | ||
hasSelected(itemKey) { | ||
@@ -139,12 +92,3 @@ const isSelected = this._itemKey === itemKey; | ||
} | ||
//========================================================================================================= | ||
// Rebuild Selector | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Rebuilds Selector | ||
* @param config - Config | ||
*/ | ||
rebuildSelector(config = {}) { | ||
// Set Selector Value to undefined if Item doesn't exist | ||
if (!this.item || this.item.isPlaceholder) { | ||
@@ -154,3 +98,2 @@ this.set(undefined, config); | ||
} | ||
// Set Selector Value to updated Item Value | ||
this.set(this.item._value, config); | ||
@@ -157,0 +100,0 @@ } |
@@ -5,20 +5,5 @@ "use strict"; | ||
class ComputedTracker { | ||
//========================================================================================================= | ||
// Track | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Starts tracking Observers | ||
*/ | ||
static track() { | ||
this.isTracking = true; | ||
} | ||
//========================================================================================================= | ||
// Tracked | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Adds passed Observer to tracked Observers, if ComputedTracker is currently tracking | ||
* @param observer - Observer | ||
*/ | ||
static tracked(observer) { | ||
@@ -28,12 +13,4 @@ if (this.isTracking) | ||
} | ||
//========================================================================================================= | ||
// Get Tracked Observers | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Returns tracked Observers and stops tracking anymore Observers | ||
*/ | ||
static getTrackedObservers() { | ||
const trackedObservers = Array.from(this.trackedObservers); | ||
// Reset tracking | ||
this.isTracking = false; | ||
@@ -40,0 +17,0 @@ this.trackedObservers = new Set(); |
@@ -6,9 +6,2 @@ "use strict"; | ||
class Computed extends internal_1.State { | ||
/** | ||
* @public | ||
* Computed - Function that recomputes its value if a dependency changes | ||
* @param agileInstance - An instance of Agile | ||
* @param computeFunction - Function for computing value | ||
* @param config - Config | ||
*/ | ||
constructor(agileInstance, computeFunction, config = {}) { | ||
@@ -19,4 +12,4 @@ super(agileInstance, computeFunction(), { | ||
}); | ||
this.deps = []; // All Dependencies of Computed (hardCoded and autoDetected) | ||
this.hardCodedDeps = []; // HardCoded Dependencies of Computed | ||
this.deps = []; | ||
this.hardCodedDeps = []; | ||
config = internal_1.defineConfig(config, { | ||
@@ -27,16 +20,6 @@ computedDeps: [], | ||
this.computeFunction = computeFunction; | ||
// Format hardCodedDeps | ||
this.hardCodedDeps = this.formatDeps(config.computedDeps); | ||
this.deps = this.hardCodedDeps; | ||
// Recompute for setting initial value and adding missing dependencies | ||
this.recompute(); | ||
} | ||
//========================================================================================================= | ||
// Recompute | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Recomputes Value of Computed | ||
* @param config - Config | ||
*/ | ||
recompute(config = {}) { | ||
@@ -52,12 +35,2 @@ config = internal_1.defineConfig(config, { | ||
} | ||
//========================================================================================================= | ||
// Updates Compute Function | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Applies new compute Function to Computed | ||
* @param computeFunction - New Function for computing value | ||
* @param deps - Hard coded dependencies of Computed Function | ||
* @param config - Config | ||
*/ | ||
updateComputeFunction(computeFunction, deps = [], config = {}) { | ||
@@ -72,3 +45,2 @@ config = internal_1.defineConfig(config, { | ||
}); | ||
// Update deps | ||
const newDeps = this.formatDeps(deps); | ||
@@ -80,5 +52,3 @@ if (config.overwriteDeps) | ||
this.deps = this.hardCodedDeps; | ||
// Update computeFunction | ||
this.computeFunction = computeFunction; | ||
// Recompute for setting initial Computed Function Value and adding missing Dependencies | ||
this.recompute({ | ||
@@ -89,19 +59,9 @@ background: config.background, | ||
} | ||
//========================================================================================================= | ||
// Compute Values | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Computes Value and adds missing Dependencies to Computed | ||
*/ | ||
computeValue() { | ||
// Auto track Observers the computeFunction might depend on | ||
internal_1.ComputedTracker.track(); | ||
const computedValue = this.computeFunction(); | ||
const foundDeps = internal_1.ComputedTracker.getTrackedObservers(); | ||
// Handle foundDeps and hardCodedDeps | ||
const newDeps = []; | ||
this.hardCodedDeps.concat(foundDeps).forEach((observer) => { | ||
newDeps.push(observer); | ||
// Make this Observer depending on foundDep Observer | ||
observer.depend(this.observer); | ||
@@ -112,10 +72,2 @@ }); | ||
} | ||
//========================================================================================================= | ||
// Format Deps | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Gets Observer out of passed Instances | ||
* @param instances - Instances that hold an Observer | ||
*/ | ||
formatDeps(instances) { | ||
@@ -135,5 +87,2 @@ const finalInstances = []; | ||
} | ||
//========================================================================================================= | ||
// Overwriting some functions which aren't allowed to use in Computed | ||
//========================================================================================================= | ||
patch() { | ||
@@ -140,0 +89,0 @@ internal_1.Agile.logger.error("You can't use patch method on ComputedState!"); |
@@ -5,8 +5,2 @@ "use strict"; | ||
class EventJob { | ||
/** | ||
* @public | ||
* Event Job - Holds Payload and gets executed/performed by the Event | ||
* @param payload - Payload that is represented by this Job | ||
* @param keys - Keys of EventCallbacks that get executed with the passed payload | ||
*/ | ||
constructor(payload, keys) { | ||
@@ -13,0 +7,0 @@ this.payload = payload; |
@@ -6,8 +6,2 @@ "use strict"; | ||
class EventObserver extends internal_1.Observer { | ||
/** | ||
* @internal | ||
* Event Observer - Handles Event dependencies and ingests Event triggers into the Runtime | ||
* @param event - Event | ||
* @param config - Config | ||
*/ | ||
constructor(event, config = {}) { | ||
@@ -21,14 +15,5 @@ super(event.agileInstance(), { | ||
} | ||
//========================================================================================================= | ||
// Perform | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Performs Job from Runtime | ||
* @param job - Job that gets performed | ||
*/ | ||
perform(job) { | ||
// Noting to perform | ||
} | ||
} | ||
exports.EventObserver = EventObserver; |
@@ -7,13 +7,7 @@ "use strict"; | ||
class Event { | ||
/** | ||
* @public | ||
* Event - Class that holds a List of Functions which can be triggered at the same time | ||
* @param agileInstance - An instance of Agile | ||
* @param config - Config | ||
*/ | ||
constructor(agileInstance, config = {}) { | ||
this.uses = 0; | ||
this.callbacks = {}; // All 'subscribed' callback function | ||
this.callbacks = {}; | ||
this.enabled = true; | ||
this.queue = []; // Queue of delayed Events | ||
this.queue = []; | ||
this.agileInstance = () => agileInstance; | ||
@@ -42,24 +36,8 @@ config = internal_1.defineConfig(config, { | ||
} | ||
/** | ||
* @public | ||
* Set Key/Name of Event | ||
*/ | ||
set key(value) { | ||
this.setKey(value); | ||
} | ||
/** | ||
* @public | ||
* Get Key/Name of Event | ||
*/ | ||
get key() { | ||
return this._key; | ||
} | ||
//========================================================================================================= | ||
// Set Key | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Set Key/Name of Event | ||
* @param value - New Key/Name of Event | ||
*/ | ||
setKey(value) { | ||
@@ -82,3 +60,2 @@ this._key = value; | ||
} | ||
// Check if Callback is a Function | ||
if (!internal_1.isFunction(_callback)) { | ||
@@ -88,3 +65,2 @@ internal_1.Agile.logger.error('A Event Callback Function has to be typeof Function!'); | ||
} | ||
// Check if Callback Function already exists | ||
if (this.callbacks[key]) { | ||
@@ -97,11 +73,2 @@ internal_1.Agile.logger.error(`Event Callback Function with the key/name '${key}' already exists!`); | ||
} | ||
//========================================================================================================= | ||
// Trigger | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Triggers Events | ||
* @param payload - Payload that gets passed into the Callback Functions | ||
* @param keys - Keys of Callback Functions that get triggered (Note: if not passed all registered Events will be triggered) | ||
*/ | ||
trigger(payload, keys) { | ||
@@ -116,9 +83,2 @@ if (!this.enabled) | ||
} | ||
//========================================================================================================= | ||
// Disable | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Disables Event | ||
*/ | ||
disable() { | ||
@@ -128,9 +88,2 @@ this.enabled = false; | ||
} | ||
//========================================================================================================= | ||
// Enable | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Enables Event | ||
*/ | ||
enable() { | ||
@@ -140,9 +93,2 @@ this.enabled = true; | ||
} | ||
//========================================================================================================= | ||
// Reset | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Resets Event | ||
*/ | ||
reset() { | ||
@@ -157,10 +103,2 @@ this.enabled = this.initialConfig.enabled; | ||
} | ||
//========================================================================================================= | ||
// Remove Callback | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Removes Callback Function at given Key | ||
* @param key - Key of Callback Function that gets removed | ||
*/ | ||
removeCallback(key) { | ||
@@ -170,13 +108,3 @@ delete this.callbacks[key]; | ||
} | ||
//========================================================================================================= | ||
// Normal Trigger | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Triggers normal Event | ||
* @param payload - Payload that gets passed into the Callback Functions | ||
* @param keys - Keys of Callback Functions that get triggered (Note: if not passed all registered Events will be triggered) | ||
*/ | ||
normalTrigger(payload, keys) { | ||
// Call wished Callback Functions | ||
if (!keys) { | ||
@@ -192,23 +120,10 @@ for (const key in this.callbacks) | ||
} | ||
// Cause rerender | ||
if (this.config.rerender) | ||
this.observer.ingest(); | ||
this.uses++; | ||
// Disable Event if maxUses got reached | ||
if (this.config.maxUses && this.uses >= this.config.maxUses) | ||
this.disable(); | ||
} | ||
//========================================================================================================= | ||
// Delayed Trigger | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Triggers async Event (Events with a delay) | ||
* @param payload - Payload that gets passed into the Callback Functions | ||
* @param delay - Delay until Events get triggered | ||
* @param keys - Keys of Callback Functions that get triggered (Note: if not passed all registered Events will be triggered) | ||
*/ | ||
delayedTrigger(payload, delay, keys) { | ||
const eventJob = new internal_1.EventJob(payload, keys); | ||
// Execute Event no matter if another event is currently active | ||
if (this.config.overlap) { | ||
@@ -220,3 +135,2 @@ setTimeout(() => { | ||
} | ||
// Check if a Event(Timeout) is currently active if so add EventJob to queue | ||
if (this.currentTimeout !== undefined) { | ||
@@ -227,3 +141,2 @@ if (payload) | ||
} | ||
// Executes EventJob and calls itself again if queue isn't empty to execute the next EventJob | ||
const looper = (eventJob) => { | ||
@@ -230,0 +143,0 @@ this.currentTimeout = setTimeout(() => { |
@@ -15,24 +15,9 @@ "use strict"; | ||
class Integrations { | ||
/** | ||
* @internal | ||
* Integrations - Manages Integrations of Agile | ||
* @param agileInstance - An Instance of Agile | ||
*/ | ||
constructor(agileInstance) { | ||
this.integrations = new Set(); // All registered Integrations | ||
this.integrations = new Set(); | ||
this.agileInstance = () => agileInstance; | ||
// Integrate initial Integrations which are static and got set external | ||
internal_1.Agile.initialIntegrations.forEach((integration) => this.integrate(integration)); | ||
} | ||
//========================================================================================================= | ||
// Integrate | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Integrates Framework(Integration) into Agile | ||
* @param integration - Integration/Framework that gets integrated | ||
*/ | ||
integrate(integration) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// Check if Integration is valid | ||
if (!integration._key) { | ||
@@ -42,3 +27,2 @@ internal_1.Agile.logger.error('Failed to integrate framework! Invalid Integration!', integration._key); | ||
} | ||
// Bind Framework to Agile | ||
if (integration.methods.bind) | ||
@@ -48,6 +32,4 @@ integration.ready = yield integration.methods.bind(this.agileInstance()); | ||
integration.ready = true; | ||
// Integrate Framework | ||
this.integrations.add(integration); | ||
integration.integrated = true; | ||
// Logging | ||
internal_1.Agile.logger.success(`Integrated '${integration._key}' into AgileTs`); | ||
@@ -57,12 +39,2 @@ return true; | ||
} | ||
//========================================================================================================= | ||
// Update | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Updates registered and ready Integrations | ||
* -> calls 'updateMethod' in all registered and ready Integrations | ||
* @param componentInstance - Component that gets updated | ||
* @param updatedData - Properties that differ from the last Value | ||
*/ | ||
update(componentInstance, updatedData) { | ||
@@ -78,9 +50,2 @@ this.integrations.forEach((integration) => { | ||
} | ||
//========================================================================================================= | ||
// Has Integration | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Check if at least one Integration got registered | ||
*/ | ||
hasIntegration() { | ||
@@ -87,0 +52,0 @@ return this.integrations.size > 0; |
@@ -5,7 +5,2 @@ "use strict"; | ||
class Integration { | ||
/** | ||
* @public | ||
* Integration - Represents a Framework/Integration of Agile | ||
* @param config - Config | ||
*/ | ||
constructor(config) { | ||
@@ -21,13 +16,5 @@ this.ready = false; | ||
} | ||
/** | ||
* @public | ||
* Set Value of Integration | ||
*/ | ||
set key(key) { | ||
this._key = key; | ||
} | ||
/** | ||
* @public | ||
* Get Value of Integration | ||
*/ | ||
get key() { | ||
@@ -34,0 +21,0 @@ return this._key; |
"use strict"; | ||
// This file exposes Agile functions and types to the outside world | ||
// It also serves as a cyclic dependency workaround | ||
// https://medium.com/visual-development/how-to-fix-nasty-circular-dependency-issues-once-and-for-all-in-javascript-typescript-a04c987cf0de. | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
@@ -16,10 +13,5 @@ if (k2 === undefined) k2 = k; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// !! All internal Agile modules must be imported from here!! | ||
// Logger | ||
__exportStar(require("./logger"), exports); | ||
// Utils | ||
__exportStar(require("./utils"), exports); | ||
// Agile | ||
__exportStar(require("./agile"), exports); | ||
// Runtime | ||
__exportStar(require("./runtime"), exports); | ||
@@ -32,7 +24,5 @@ __exportStar(require("./runtime/observer"), exports); | ||
__exportStar(require("./runtime/subscription/sub.controller"), exports); | ||
// Storage | ||
__exportStar(require("./storages"), exports); | ||
__exportStar(require("./storages/storage"), exports); | ||
__exportStar(require("./storages/persistent"), exports); | ||
// State | ||
__exportStar(require("./state"), exports); | ||
@@ -42,6 +32,4 @@ __exportStar(require("./state/state.observer"), exports); | ||
__exportStar(require("./state/state.runtime.job"), exports); | ||
// Computed | ||
__exportStar(require("./computed"), exports); | ||
__exportStar(require("./computed/computed.tracker"), exports); | ||
// Collection | ||
__exportStar(require("./collection"), exports); | ||
@@ -52,8 +40,6 @@ __exportStar(require("./collection/group"), exports); | ||
__exportStar(require("./collection/collection.persistent"), exports); | ||
// Event | ||
__exportStar(require("./event"), exports); | ||
__exportStar(require("./event/event.job"), exports); | ||
__exportStar(require("./event/event.observer"), exports); | ||
// Integrations | ||
__exportStar(require("./integrations"), exports); | ||
__exportStar(require("./integrations/integration"), exports); |
@@ -6,9 +6,5 @@ "use strict"; | ||
class Logger { | ||
/** | ||
* @public | ||
* Logger - Handy Class for handling console.logs | ||
*/ | ||
constructor(config = {}) { | ||
this.allowedTags = []; | ||
this.loggerCategories = {}; // Holds all registered Logger Categories | ||
this.loggerCategories = {}; | ||
this.watchers = {}; | ||
@@ -34,6 +30,2 @@ let _config = typeof config === 'function' ? config(this) : config; | ||
} | ||
/** | ||
* @public | ||
* Adds Conditions to Logs | ||
*/ | ||
get if() { | ||
@@ -44,6 +36,2 @@ return { | ||
} | ||
/** | ||
* @public | ||
* Default Levels of Logger | ||
*/ | ||
static get level() { | ||
@@ -61,9 +49,2 @@ return { | ||
} | ||
//========================================================================================================= | ||
// Add Default Logger Categories | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Adds Default Logger Categories | ||
*/ | ||
addDefaultLoggerCategories() { | ||
@@ -112,10 +93,2 @@ this.createLoggerCategory({ | ||
} | ||
//========================================================================================================= | ||
// Tag | ||
//========================================================================================================= | ||
/** | ||
* @private | ||
* Only executes following 'command' if all given tags are included in allowedTags | ||
* @param tags - Tags | ||
*/ | ||
tag(tags) { | ||
@@ -136,24 +109,16 @@ if (internal_1.includesArray(this.allowedTags, tags)) { | ||
log: () => { | ||
/* do nothing */ | ||
}, | ||
debug: () => { | ||
/* do nothing */ | ||
}, | ||
info: () => { | ||
/* do nothing */ | ||
}, | ||
success: () => { | ||
/* do nothing */ | ||
}, | ||
warn: () => { | ||
/* do nothing */ | ||
}, | ||
error: () => { | ||
/* do nothing */ | ||
}, | ||
trace: () => { | ||
/* do nothing */ | ||
}, | ||
table: () => { | ||
/* do nothing */ | ||
}, | ||
@@ -189,18 +154,6 @@ }; | ||
} | ||
//========================================================================================================= | ||
// Invoke Console | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Logs data in Console | ||
* @param data - Data | ||
* @param loggerCategoryKey - Key/Name of Logger Category | ||
* @param consoleLogType - console[consoleLogProperty] | ||
*/ | ||
invokeConsole(data, loggerCategoryKey, consoleLogType) { | ||
const loggerCategory = this.getLoggerCategory(loggerCategoryKey); | ||
// Check if Logger Category is allowed | ||
if (!this.isActive || loggerCategory.level < this.config.level) | ||
return; | ||
// Build Prefix of Log | ||
const buildPrefix = () => { | ||
@@ -218,3 +171,2 @@ let prefix = ''; | ||
}; | ||
// Add built Prefix | ||
if (typeof data[0] === 'string') | ||
@@ -224,3 +176,2 @@ data[0] = buildPrefix().concat(' ').concat(data[0]); | ||
data.unshift(buildPrefix()); | ||
// Call Watcher Callbacks | ||
for (const key in this.watchers) { | ||
@@ -232,6 +183,5 @@ const watcher = this.watchers[key]; | ||
} | ||
// Init Custom Style | ||
if (this.config.canUseCustomStyles && loggerCategory.customStyle) { | ||
const newLogs = []; | ||
let hasStyledString = false; // NOTE: Only one style can be used for one String block! | ||
let hasStyledString = false; | ||
for (const log of data) { | ||
@@ -249,3 +199,2 @@ if (!hasStyledString && typeof log === 'string') { | ||
} | ||
// Handle Console Table Log | ||
if (consoleLogType === 'table') { | ||
@@ -258,13 +207,4 @@ if (typeof data[0] === 'string') { | ||
} | ||
// Normal Log | ||
console[consoleLogType](...data); | ||
} | ||
//========================================================================================================= | ||
// Create Logger Category | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Creates new Logger Category | ||
* @param loggerCategory - Logger Category | ||
*/ | ||
createLoggerCategory(loggerCategory) { | ||
@@ -277,10 +217,2 @@ loggerCategory = internal_1.defineConfig(loggerCategory, { | ||
} | ||
//========================================================================================================= | ||
// Get Logger Category | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Get Logger Category | ||
* @param key - Key/Name of Logger Category | ||
*/ | ||
getLoggerCategory(key) { | ||
@@ -304,3 +236,2 @@ return this.loggerCategories[key]; | ||
}); | ||
// Check if Callback is a Function | ||
if (!internal_1.isFunction(_config.callback)) { | ||
@@ -310,3 +241,2 @@ console.error('Agile: A Watcher Callback Function has to be an function!'); | ||
} | ||
// Check if Callback Function already exists | ||
if (this.watchers[key]) { | ||
@@ -319,10 +249,2 @@ console.error(`Agile: Watcher Callback Function with the key/name ${key} already exists!`); | ||
} | ||
//========================================================================================================= | ||
// Remove Watcher | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Removes Watcher at given Key | ||
* @param key - Key of Watcher that gets removed | ||
*/ | ||
removeWatcher(key) { | ||
@@ -332,11 +254,2 @@ delete this.watchers[key]; | ||
} | ||
//========================================================================================================= | ||
// Set Level | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Assigns new Level to Logger | ||
* NOTE: Default Levels can be found in 'Logger.level.x' | ||
* @param level - Level | ||
*/ | ||
setLevel(level) { | ||
@@ -343,0 +256,0 @@ this.config.level = level; |
@@ -6,24 +6,9 @@ "use strict"; | ||
class Runtime { | ||
/** | ||
* @internal | ||
* Runtime - Performs ingested Observers | ||
* @param agileInstance - An instance of Agile | ||
*/ | ||
constructor(agileInstance) { | ||
// Queue system | ||
this.currentJob = null; | ||
this.jobQueue = []; | ||
this.notReadyJobsToRerender = new Set(); // Jobs that got performed but aren't ready to get rerendered (wait for mount) | ||
this.jobsToRerender = []; // Jobs that are performed and will be rendered | ||
this.notReadyJobsToRerender = new Set(); | ||
this.jobsToRerender = []; | ||
this.agileInstance = () => agileInstance; | ||
} | ||
//========================================================================================================= | ||
// Ingest | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Ingests Job into Runtime that gets performed | ||
* @param job - Job | ||
* @param config - Config | ||
*/ | ||
ingest(job, config = {}) { | ||
@@ -34,5 +19,3 @@ config = internal_1.defineConfig(config, { | ||
this.jobQueue.push(job); | ||
// Logging | ||
internal_1.Agile.logger.if.tag(['runtime']).info(`Created Job '${job._key}'`, job); | ||
// Perform Job | ||
if (config.perform) { | ||
@@ -44,16 +27,6 @@ const performJob = this.jobQueue.shift(); | ||
} | ||
//========================================================================================================= | ||
// Perform | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Performs Job and adds it to the rerender queue if necessary | ||
* @param job - Job that gets performed | ||
*/ | ||
perform(job) { | ||
this.currentJob = job; | ||
// Perform Job | ||
job.observer.perform(job); | ||
job.performed = true; | ||
// Ingest Dependents of Observer into Runtime | ||
job.observer.dependents.forEach((observer) => observer.ingest({ perform: false })); | ||
@@ -63,5 +36,3 @@ if (job.rerender) | ||
this.currentJob = null; | ||
// Logging | ||
internal_1.Agile.logger.if.tag(['runtime']).info(`Completed Job '${job._key}'`, job); | ||
// Perform Jobs as long as Jobs are left in queue, if no job left update/rerender Subscribers of jobsToRerender | ||
if (this.jobQueue.length > 0) { | ||
@@ -74,3 +45,2 @@ const performJob = this.jobQueue.shift(); | ||
if (this.jobsToRerender.length > 0) { | ||
// https://stackoverflow.com/questions/9083594/call-settimeout-without-delay | ||
setTimeout(() => { | ||
@@ -82,9 +52,2 @@ this.updateSubscribers(); | ||
} | ||
//========================================================================================================= | ||
// Update Subscribers | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Updates/Rerenders all Subscribed Components of the Job (Observer) | ||
*/ | ||
updateSubscribers() { | ||
@@ -99,11 +62,6 @@ if (!this.agileInstance().hasIntegration()) { | ||
return false; | ||
// Subscriptions that has to be updated/rerendered | ||
// 'Set' to combine several SubscriptionContainers that are equals into one (optimizes rerender) | ||
// Better would be to optimize the rerender based on the Component, because a Component can have multiple SubscriptionContainers | ||
const subscriptionsToUpdate = new Set(); | ||
// Build final jobsToRerender and reset jobsToRerender Instances | ||
const jobsToRerender = this.jobsToRerender.concat(Array.from(this.notReadyJobsToRerender)); | ||
this.notReadyJobsToRerender = new Set(); | ||
this.jobsToRerender = []; | ||
// Check if Job Subscriptions are ready and add them to subscriptionsToUpdate | ||
jobsToRerender.forEach((job) => { | ||
@@ -113,7 +71,5 @@ job.subscriptionContainersToUpdate.forEach((subscriptionContainer) => { | ||
this.notReadyJobsToRerender.add(job); | ||
// Logging | ||
internal_1.Agile.logger.warn("SubscriptionContainer/Component isn't ready to rerender!", subscriptionContainer); | ||
return; | ||
} | ||
// Handle Object based Subscription | ||
if (subscriptionContainer.isObjectBased) | ||
@@ -126,10 +82,7 @@ this.handleObjectBasedSubscription(subscriptionContainer, job); | ||
subscriptionsToUpdate.forEach((subscriptionContainer) => { | ||
// Call 'callback function' if Callback based Subscription | ||
if (subscriptionContainer instanceof internal_1.CallbackSubscriptionContainer) | ||
subscriptionContainer.callback(); | ||
// Call 'update method' if Component based Subscription | ||
if (subscriptionContainer instanceof internal_1.ComponentSubscriptionContainer) | ||
this.agileInstance().integrations.update(subscriptionContainer.component, this.getObjectBasedProps(subscriptionContainer)); | ||
}); | ||
// Logging | ||
internal_1.Agile.logger.if | ||
@@ -140,17 +93,6 @@ .tag(['runtime']) | ||
} | ||
//========================================================================================================= | ||
// Handle Object Based Subscription | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Finds key of Observer (Job) in subsObject and adds it to 'changedObjectKeys' | ||
* @param subscriptionContainer - Object based SubscriptionContainer | ||
* @param job - Job that holds the searched Observer | ||
*/ | ||
handleObjectBasedSubscription(subscriptionContainer, job) { | ||
let foundKey = null; | ||
// Check if SubscriptionContainer is Object based | ||
if (!subscriptionContainer.isObjectBased) | ||
return; | ||
// Find Key of Job Observer in SubscriptionContainer | ||
for (const key in subscriptionContainer.subsObject) | ||
@@ -162,14 +104,5 @@ if (subscriptionContainer.subsObject[key] === job.observer) | ||
} | ||
//========================================================================================================= | ||
// Get Object Based Props | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Builds Object out of changedObjectKeys with Observer Value | ||
* @param subscriptionContainer - Object based SubscriptionContainer | ||
*/ | ||
getObjectBasedProps(subscriptionContainer) { | ||
var _a; | ||
const props = {}; | ||
// Map trough observerKeysToUpdate and build object out of Observer value | ||
if (subscriptionContainer.subsObject) | ||
@@ -176,0 +109,0 @@ for (const updatedKey of subscriptionContainer.observerKeysToUpdate) |
@@ -6,13 +6,6 @@ "use strict"; | ||
class Observer { | ||
/** | ||
* @internal | ||
* Observer - Handles subscriptions and dependencies of an Agile Class and is like an instance to the Runtime | ||
* Note: No stand alone class!! | ||
* @param agileInstance - An instance of Agile | ||
* @param config - Config | ||
*/ | ||
constructor(agileInstance, config = {}) { | ||
var _a, _b; | ||
this.dependents = new Set(); // Observers that depend on this Observer | ||
this.subs = new Set(); // SubscriptionContainers (Components) that this Observer has subscribed | ||
this.dependents = new Set(); | ||
this.subs = new Set(); | ||
config = internal_1.defineConfig(config, { | ||
@@ -28,24 +21,8 @@ dependents: [], | ||
} | ||
/** | ||
* @internal | ||
* Set Key/Name of Observer | ||
*/ | ||
set key(value) { | ||
this._key = value; | ||
} | ||
/** | ||
* @internal | ||
* Get Key/Name of Observer | ||
*/ | ||
get key() { | ||
return this._key; | ||
} | ||
//========================================================================================================= | ||
// Ingest | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Ingests Observer into Runtime | ||
* @param config - Configuration | ||
*/ | ||
ingest(config = {}) { | ||
@@ -61,3 +38,2 @@ config = internal_1.defineConfig(config, { | ||
}); | ||
// Create Job | ||
const job = new internal_1.RuntimeJob(this, { | ||
@@ -73,21 +49,5 @@ force: config.force, | ||
} | ||
//========================================================================================================= | ||
// Perform | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Performs Job of Runtime | ||
* @param job - Job that gets performed | ||
*/ | ||
perform(job) { | ||
internal_1.Agile.logger.warn("Perform function isn't Set in Observer! Be aware that Observer is no stand alone class!"); | ||
} | ||
//========================================================================================================= | ||
// Depend | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Adds Dependent to Observer which gets ingested into the Runtime whenever this Observer mutates | ||
* @param observer - Observer that will depend on this Observer | ||
*/ | ||
depend(observer) { | ||
@@ -97,25 +57,8 @@ if (!this.dependents.has(observer)) | ||
} | ||
//========================================================================================================= | ||
// Subscribe | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Adds Subscription to Observer | ||
* @param subscriptionContainer - SubscriptionContainer(Component) that gets subscribed by this Observer | ||
*/ | ||
subscribe(subscriptionContainer) { | ||
if (!this.subs.has(subscriptionContainer)) { | ||
this.subs.add(subscriptionContainer); | ||
// Add this to subscriptionContainer to keep track of the Observers the subscriptionContainer hold | ||
subscriptionContainer.subs.add(this); | ||
} | ||
} | ||
//========================================================================================================= | ||
// Unsubscribe | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Removes Subscription from Observer | ||
* @param subscriptionContainer - SubscriptionContainer(Component) that gets unsubscribed by this Observer | ||
*/ | ||
unsubscribe(subscriptionContainer) { | ||
@@ -122,0 +65,0 @@ if (this.subs.has(subscriptionContainer)) { |
@@ -6,11 +6,5 @@ "use strict"; | ||
class RuntimeJob { | ||
/** | ||
* @internal | ||
* Job - Represents Observer that gets performed by the Runtime | ||
* @param observer - Observer | ||
* @param config - Config | ||
*/ | ||
constructor(observer, config = {}) { | ||
this.performed = false; // If Job has been performed by Runtime | ||
this.subscriptionContainersToUpdate = new Set(); // SubscriptionContainer that have to be updated/rerendered | ||
this.performed = false; | ||
this.subscriptionContainersToUpdate = new Set(); | ||
config = internal_1.defineConfig(config, { | ||
@@ -17,0 +11,0 @@ background: false, |
@@ -6,9 +6,2 @@ "use strict"; | ||
class CallbackSubscriptionContainer extends internal_1.SubscriptionContainer { | ||
/** | ||
* @internal | ||
* CallbackSubscriptionContainer - Subscription Container for Callback based Subscriptions | ||
* @param callback - Callback Function that causes rerender on Component that is subscribed by Agile | ||
* @param subs - Initial Subscriptions | ||
* @param key - Key/Name of Callback Subscription Container | ||
*/ | ||
constructor(callback, subs = [], key) { | ||
@@ -15,0 +8,0 @@ super(subs, key); |
@@ -6,9 +6,2 @@ "use strict"; | ||
class ComponentSubscriptionContainer extends internal_1.SubscriptionContainer { | ||
/** | ||
* @internal | ||
* ComponentSubscriptionContainer - SubscriptionContainer for Component based Subscription | ||
* @param component - Component that is subscribed by Agile | ||
* @param subs - Initial Subscriptions | ||
* @param key - Key/Name of Component Subscription Container | ||
*/ | ||
constructor(component, subs = [], key) { | ||
@@ -15,0 +8,0 @@ super(subs, key); |
@@ -6,14 +6,6 @@ "use strict"; | ||
class SubscriptionContainer { | ||
/** | ||
* @internal | ||
* SubscriptionContainer - Represents Component/(Way to rerender Component) that is subscribed by Observer/s (Agile) | ||
* -> Used to cause rerender on Component | ||
* @param subs - Initial Subscriptions | ||
* @param key - Key/Name of Subscription Container | ||
*/ | ||
constructor(subs = [], key) { | ||
this.ready = false; | ||
// For Object based Subscription | ||
this.isObjectBased = false; | ||
this.observerKeysToUpdate = []; // Holds temporary keys of Observers that got updated (Note: keys based on 'subsObject') | ||
this.observerKeysToUpdate = []; | ||
this.subs = new Set(subs); | ||
@@ -20,0 +12,0 @@ this.key = key || internal_1.generateId(); |
@@ -6,35 +6,16 @@ "use strict"; | ||
class SubController { | ||
/** | ||
* @internal | ||
* SubController - Handles subscriptions to Components | ||
* @param agileInstance - An instance of Agile | ||
*/ | ||
constructor(agileInstance) { | ||
this.componentSubs = new Set(); // Holds all registered Component based Subscriptions | ||
this.callbackSubs = new Set(); // Holds all registered Callback based Subscriptions | ||
this.mountedComponents = new Set(); // Holds all mounted Components (only if agileInstance.config.mount = true) | ||
this.componentSubs = new Set(); | ||
this.callbackSubs = new Set(); | ||
this.mountedComponents = new Set(); | ||
this.agileInstance = () => agileInstance; | ||
} | ||
//========================================================================================================= | ||
// Subscribe with Subs Object | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Subscribe with Object shaped Subscriptions | ||
* @param integrationInstance - Callback Function or Component | ||
* @param subs - Initial Subscription Object | ||
* @param key - Key/Name of SubscriptionContainer | ||
*/ | ||
subscribeWithSubsObject(integrationInstance, subs = {}, key) { | ||
const props = {}; | ||
// Create subsArray | ||
const subsArray = []; | ||
for (const key in subs) | ||
subsArray.push(subs[key]); | ||
// Register Subscription -> decide weather subscriptionInstance is callback or component based | ||
const subscriptionContainer = this.registerSubscription(integrationInstance, subsArray, key); | ||
// Set SubscriptionContainer to Object based | ||
subscriptionContainer.isObjectBased = true; | ||
subscriptionContainer.subsObject = subs; | ||
// Register subs and build props object | ||
for (const key in subs) { | ||
@@ -51,32 +32,10 @@ const observer = subs[key]; | ||
} | ||
//========================================================================================================= | ||
// Subscribe with Subs Array | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Subscribe with Array shaped Subscriptions | ||
* @param integrationInstance - Callback Function or Component | ||
* @param subs - Initial Subscription Array | ||
* @param key - Key/Name of SubscriptionContainer | ||
*/ | ||
subscribeWithSubsArray(integrationInstance, subs = [], key) { | ||
// Register Subscription -> decide weather subscriptionInstance is callback or component based | ||
const subscriptionContainer = this.registerSubscription(integrationInstance, subs, key); | ||
// Register subs | ||
subs.forEach((observer) => observer.subscribe(subscriptionContainer)); | ||
return subscriptionContainer; | ||
} | ||
//========================================================================================================= | ||
// Unsubscribe | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Unsubscribes SubscriptionContainer(Component) | ||
* @param subscriptionInstance - SubscriptionContainer or Component that holds an SubscriptionContainer | ||
*/ | ||
unsubscribe(subscriptionInstance) { | ||
// Helper function to remove SubscriptionContainer from Observer | ||
const unsub = (subscriptionContainer) => { | ||
subscriptionContainer.ready = false; | ||
// Remove SubscriptionContainers from Observer | ||
subscriptionContainer.subs.forEach((observer) => { | ||
@@ -86,7 +45,5 @@ observer.unsubscribe(subscriptionContainer); | ||
}; | ||
// Unsubscribe callback based Subscription | ||
if (subscriptionInstance instanceof internal_1.CallbackSubscriptionContainer) { | ||
unsub(subscriptionInstance); | ||
this.callbackSubs.delete(subscriptionInstance); | ||
// Logging | ||
internal_1.Agile.logger.if | ||
@@ -97,7 +54,5 @@ .tag(['runtime', 'subscription']) | ||
} | ||
// Unsubscribe component based Subscription | ||
if (subscriptionInstance instanceof internal_1.ComponentSubscriptionContainer) { | ||
unsub(subscriptionInstance); | ||
this.componentSubs.delete(subscriptionInstance); | ||
// Logging | ||
internal_1.Agile.logger.if | ||
@@ -108,7 +63,5 @@ .tag(['runtime', 'subscription']) | ||
} | ||
// Unsubscribe component based Subscription with subscriptionInstance that holds a componentSubscriptionContainer | ||
if (subscriptionInstance.componentSubscriptionContainer) { | ||
unsub(subscriptionInstance.componentSubscriptionContainer); | ||
this.componentSubs.delete(subscriptionInstance.componentSubscriptionContainer); | ||
// Logging | ||
internal_1.Agile.logger.if | ||
@@ -119,3 +72,2 @@ .tag(['runtime', 'subscription']) | ||
} | ||
// Unsubscribe component based Subscription with subscriptionInstance that holds componentSubscriptionContainers | ||
if (subscriptionInstance.componentSubscriptionContainers && | ||
@@ -126,3 +78,2 @@ Array.isArray(subscriptionInstance.componentSubscriptionContainers)) { | ||
this.componentSubs.delete(subContainer); | ||
// Logging | ||
internal_1.Agile.logger.if | ||
@@ -134,3 +85,2 @@ .tag(['runtime', 'subscription']) | ||
} | ||
// Logging | ||
internal_1.Agile.logger.if | ||
@@ -141,12 +91,2 @@ .tag(['runtime', 'subscription']) | ||
} | ||
//========================================================================================================= | ||
// Register Subscription | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Registers SubscriptionContainer and decides weather integrationInstance is a callback or component based Subscription | ||
* @param integrationInstance - Callback Function or Component | ||
* @param subs - Initial Subscriptions | ||
* @param key - Key/Name of SubscriptionContainer | ||
*/ | ||
registerSubscription(integrationInstance, subs = [], key) { | ||
@@ -157,18 +97,5 @@ if (internal_1.isFunction(integrationInstance)) | ||
} | ||
//========================================================================================================= | ||
// Register Component Subscription | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Registers Component based Subscription and applies SubscriptionContainer to Component. | ||
* If an instance called 'subscriptionContainers' exists in Component it will push the new SubscriptionContainer to this Array, | ||
* otherwise it creates a new Instance called 'subscriptionContainer' which holds the new SubscriptionContainer | ||
* @param componentInstance - Component that got subscribed by Observer/s | ||
* @param subs - Initial Subscriptions | ||
* @param key - Key/Name of SubscriptionContainer | ||
*/ | ||
registerComponentSubscription(componentInstance, subs = [], key) { | ||
const componentSubscriptionContainer = new internal_1.ComponentSubscriptionContainer(componentInstance, subs, key); | ||
this.componentSubs.add(componentSubscriptionContainer); | ||
// Set to ready if not waiting for component to mount | ||
if (this.agileInstance().config.waitForMount) { | ||
@@ -180,3 +107,2 @@ if (this.mountedComponents.has(componentInstance)) | ||
componentSubscriptionContainer.ready = true; | ||
// Add subscriptionContainer to Component, to have an instance of it there (necessary to unsubscribe SubscriptionContainer later) | ||
if (componentInstance.componentSubscriptionContainers && | ||
@@ -187,3 +113,2 @@ Array.isArray(componentInstance.componentSubscriptionContainers)) | ||
componentInstance.componentSubscriptionContainer = componentSubscriptionContainer; | ||
// Logging | ||
internal_1.Agile.logger.if | ||
@@ -194,12 +119,2 @@ .tag(['runtime', 'subscription']) | ||
} | ||
//========================================================================================================= | ||
// Register Callback Subscription | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Registers Callback based Subscription | ||
* @param callbackFunction - Callback Function that causes rerender on Component which got subscribed by Observer/s | ||
* @param subs - Initial Subscriptions | ||
* @param key - Key/Name of SubscriptionContainer | ||
*/ | ||
registerCallbackSubscription(callbackFunction, subs = [], key) { | ||
@@ -209,3 +124,2 @@ const callbackSubscriptionContainer = new internal_1.CallbackSubscriptionContainer(callbackFunction, subs, key); | ||
callbackSubscriptionContainer.ready = true; | ||
// Logging | ||
internal_1.Agile.logger.if | ||
@@ -216,10 +130,2 @@ .tag(['runtime', 'subscription']) | ||
} | ||
//========================================================================================================= | ||
// Mount | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Mounts Component based SubscriptionContainer | ||
* @param componentInstance - SubscriptionContainer(Component) that gets mounted | ||
*/ | ||
mount(componentInstance) { | ||
@@ -230,10 +136,2 @@ if (componentInstance.componentSubscriptionContainer) | ||
} | ||
//========================================================================================================= | ||
// Unmount | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Unmounts Component based SubscriptionContainer | ||
* @param componentInstance - SubscriptionContainer(Component) that gets unmounted | ||
*/ | ||
unmount(componentInstance) { | ||
@@ -240,0 +138,0 @@ if (componentInstance.componentSubscriptionContainer) |
@@ -6,14 +6,7 @@ "use strict"; | ||
class State { | ||
/** | ||
* @public | ||
* State - Class that holds one Value and causes rerender on subscribed Components | ||
* @param agileInstance - An instance of Agile | ||
* @param initialValue - Initial Value of State | ||
* @param config - Config | ||
*/ | ||
constructor(agileInstance, initialValue, config = {}) { | ||
this.isSet = false; // If value is not the same as initialValue | ||
this.isSet = false; | ||
this.isPlaceholder = false; | ||
this.sideEffects = {}; // SideEffects of State (will be executed in Runtime) | ||
this.isPersisted = false; // If State can be stored in Agile Storage (-> successfully integrated persistent) | ||
this.sideEffects = {}; | ||
this.isPersisted = false; | ||
this.watchers = {}; | ||
@@ -35,17 +28,8 @@ config = internal_1.defineConfig(config, { | ||
this.isPlaceholder = true; | ||
// Initial Set | ||
if (!config.isPlaceholder) | ||
this.set(initialValue, { overwrite: true }); | ||
} | ||
/** | ||
* @public | ||
* Set Value of State | ||
*/ | ||
set value(value) { | ||
this.set(value); | ||
} | ||
/** | ||
* @public | ||
* Get Value of State | ||
*/ | ||
get value() { | ||
@@ -55,32 +39,13 @@ internal_1.ComputedTracker.tracked(this.observer); | ||
} | ||
/** | ||
* @public | ||
* Set Key/Name of State | ||
*/ | ||
set key(value) { | ||
this.setKey(value); | ||
} | ||
/** | ||
* @public | ||
* Get Key/Name of State | ||
*/ | ||
get key() { | ||
return this._key; | ||
} | ||
//========================================================================================================= | ||
// Set Key | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Updates Key/Name of State | ||
* @param value - New Key/Name of State | ||
*/ | ||
setKey(value) { | ||
var _a, _b; | ||
const oldKey = this._key; | ||
// Update State Key | ||
this._key = value; | ||
// Update Key in Observer | ||
this.observer._key = value; | ||
// Update Key in Persistent (only if oldKey equal to persistentKey -> otherwise the PersistentKey got formatted and will be set where other) | ||
if (value && ((_a = this.persistent) === null || _a === void 0 ? void 0 : _a._key) === oldKey) | ||
@@ -90,11 +55,2 @@ (_b = this.persistent) === null || _b === void 0 ? void 0 : _b.setKey(value); | ||
} | ||
//========================================================================================================= | ||
// Set | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Updates Value of State | ||
* @param value - new State Value | ||
* @param config - Config | ||
*/ | ||
set(value, config = {}) { | ||
@@ -112,3 +68,2 @@ config = internal_1.defineConfig(config, { | ||
}); | ||
// Check value has correct Type (js) | ||
if (!this.hasCorrectType(value)) { | ||
@@ -122,14 +77,5 @@ const message = `Incorrect type (${typeof value}) was provided.`; | ||
} | ||
// Ingest new value into Runtime | ||
this.observer.ingestValue(value, config); | ||
return this; | ||
} | ||
//========================================================================================================= | ||
// Ingest | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Ingests nextStateValue, computedValue into Runtime | ||
* @param config - Config | ||
*/ | ||
ingest(config = {}) { | ||
@@ -139,14 +85,4 @@ this.observer.ingest(config); | ||
} | ||
//========================================================================================================= | ||
// Type | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Assign primitive type to State Value | ||
* Note: This function is mainly thought for JS users | ||
* @param type - wished Type ('String', 'Boolean', 'Array', 'Object', 'Number') | ||
*/ | ||
type(type) { | ||
const supportedTypes = ['String', 'Boolean', 'Array', 'Object', 'Number']; | ||
// Check if type is a supported Type | ||
if (!supportedTypes.includes(type.name)) { | ||
@@ -159,10 +95,2 @@ internal_1.Agile.logger.warn(`'${type}' is not supported! Supported types: String, Boolean, Array, Object, Number`); | ||
} | ||
//========================================================================================================= | ||
// Undo | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Undoes latest State Value change | ||
* @param config - Config | ||
*/ | ||
undo(config = {}) { | ||
@@ -172,10 +100,2 @@ this.set(this.previousStateValue, config); | ||
} | ||
//========================================================================================================= | ||
// Reset | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Resets State to its initial Value | ||
* @param config - Config | ||
*/ | ||
reset(config = {}) { | ||
@@ -185,12 +105,2 @@ this.set(this.initialStateValue, config); | ||
} | ||
//========================================================================================================= | ||
// Patch | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Patches Object with changes into State Value | ||
* Note: Only useful if State is an Object | ||
* @param targetWithChanges - Object that holds changes which get patched into State Value | ||
* @param config - Config | ||
*/ | ||
patch(targetWithChanges, config = {}) { | ||
@@ -216,5 +126,3 @@ config = internal_1.defineConfig(config, { | ||
} | ||
// Merge targetWithChanges into nextStateValue | ||
this.nextStateValue = internal_1.flatMerge(internal_1.copy(this.nextStateValue), targetWithChanges, { addNewProperties: config.addNewProperties }); | ||
// Ingest updated nextStateValue into Runtime | ||
this.ingest({ | ||
@@ -241,3 +149,2 @@ background: config.background, | ||
} | ||
// Check if Callback is valid Function | ||
if (!internal_1.isFunction(_callback)) { | ||
@@ -247,3 +154,2 @@ internal_1.Agile.logger.error('A Watcher Callback Function has to be typeof Function!'); | ||
} | ||
// Check if watcherKey is already occupied | ||
if (this.watchers[key]) { | ||
@@ -256,10 +162,2 @@ internal_1.Agile.logger.error(`Watcher Callback Function with the key/name '${key}' already exists!`); | ||
} | ||
//========================================================================================================= | ||
// Remove Watcher | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Removes Watcher at given Key | ||
* @param key - Key of Watcher that gets removed | ||
*/ | ||
removeWatcher(key) { | ||
@@ -269,7 +167,2 @@ delete this.watchers[key]; | ||
} | ||
/** | ||
* @public | ||
* Creates a Watcher that gets once called when the State Value changes for the first time and than destroys itself | ||
* @param callback - Callback Function that gets called if the State Value changes | ||
*/ | ||
onInaugurated(callback) { | ||
@@ -283,10 +176,2 @@ const watcherKey = 'InauguratedWatcherKey'; | ||
} | ||
//========================================================================================================= | ||
// Has Watcher | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Checks if watcher at given Key exists | ||
* @param key - Key/Name of Watcher | ||
*/ | ||
hasWatcher(key) { | ||
@@ -312,3 +197,2 @@ return !!this.watchers[key]; | ||
internal_1.Agile.logger.warn(`By persisting the State '${this._key}' twice you overwrite the old Persistent Instance!`); | ||
// Create persistent -> Persist Value | ||
this.persistent = new internal_1.StatePersistent(this, { | ||
@@ -321,15 +205,5 @@ instantiate: _config.instantiate, | ||
} | ||
//========================================================================================================= | ||
// On Load | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Callback Function that gets called if the persisted Value gets loaded into the State for the first Time | ||
* Note: Only useful for persisted States! | ||
* @param callback - Callback Function | ||
*/ | ||
onLoad(callback) { | ||
if (this.persistent) { | ||
this.persistent.onLoad = callback; | ||
// If State is already 'isPersisted' the loading was successful -> callback can be called | ||
if (this.isPersisted) | ||
@@ -343,52 +217,14 @@ callback(true); | ||
} | ||
//========================================================================================================= | ||
// Copy | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Creates fresh copy of State Value (-> No reference to State Value) | ||
*/ | ||
copy() { | ||
return internal_1.copy(this.value); | ||
} | ||
//========================================================================================================= | ||
// Exists | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Checks if State exists | ||
*/ | ||
get exists() { | ||
return !this.isPlaceholder; | ||
} | ||
//========================================================================================================= | ||
// Is | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Equivalent to === | ||
* @param value - Value that gets checked if its equals to the State Value | ||
*/ | ||
is(value) { | ||
return internal_1.equal(value, this.value); | ||
} | ||
//========================================================================================================= | ||
// Is Not | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Equivalent to !== | ||
* @param value - Value that gets checked if its not equals to the State Value | ||
*/ | ||
isNot(value) { | ||
return internal_1.notEqual(value, this.value); | ||
} | ||
//========================================================================================================= | ||
// Invert | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Inverts State Value | ||
* Note: Only useful with boolean based States | ||
*/ | ||
invert() { | ||
@@ -403,10 +239,2 @@ if (typeof this._value === 'boolean') { | ||
} | ||
//========================================================================================================= | ||
// Compute | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Function that recomputes State Value if it changes | ||
* @param method - Computed Function | ||
*/ | ||
compute(method) { | ||
@@ -420,12 +248,2 @@ if (!internal_1.isFunction(method)) { | ||
} | ||
//========================================================================================================= | ||
// Add SideEffect | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Adds SideEffect to State | ||
* @param key - Key/Name of SideEffect | ||
* @param callback - Callback Function that gets called on every State Value change | ||
* @param config - Config | ||
*/ | ||
addSideEffect(key, callback, config = {}) { | ||
@@ -445,10 +263,2 @@ config = internal_1.defineConfig(config, { | ||
} | ||
//========================================================================================================= | ||
// Remove SideEffect | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Removes SideEffect at given Key | ||
* @param key - Key of the SideEffect that gets removed | ||
*/ | ||
removeSideEffect(key) { | ||
@@ -458,22 +268,5 @@ delete this.sideEffects[key]; | ||
} | ||
//========================================================================================================= | ||
// Has SideEffect | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Checks if sideEffect at given Key exists | ||
* @param key - Key of SideEffect | ||
*/ | ||
hasSideEffect(key) { | ||
return !!this.sideEffects[key]; | ||
} | ||
//========================================================================================================= | ||
// Is Correct Type | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Checks if Value has correct valueType (js) | ||
* Note: If no valueType set, it returns true | ||
* @param value - Value that gets checked for its correct Type | ||
*/ | ||
hasCorrectType(value) { | ||
@@ -485,11 +278,3 @@ if (!this.valueType) | ||
} | ||
//========================================================================================================= | ||
// Get Public Value | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Returns public Value of State | ||
*/ | ||
getPublicValue() { | ||
// If State Value is used internal and output represents the real state value (for instance in Group) | ||
if (this['output'] !== undefined) | ||
@@ -499,9 +284,2 @@ return this['output']; | ||
} | ||
//========================================================================================================= | ||
// Get Persistable Value | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Returns Value that gets written into the Agile Storage | ||
*/ | ||
getPersistableValue() { | ||
@@ -508,0 +286,0 @@ return this._value; |
@@ -6,8 +6,2 @@ "use strict"; | ||
class StateObserver extends internal_1.Observer { | ||
/** | ||
* @internal | ||
* State Observer - Handles State changes, dependencies (-> Interface to Runtime) | ||
* @param state - State | ||
* @param config - Config | ||
*/ | ||
constructor(state, config = {}) { | ||
@@ -18,10 +12,2 @@ super(state.agileInstance(), Object.assign(Object.assign({}, config), { value: state._value })); | ||
} | ||
//========================================================================================================= | ||
// Ingest | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Ingests nextStateValue or computedValue into Runtime and applies it to the State | ||
* @param config - Config | ||
*/ | ||
ingest(config = {}) { | ||
@@ -36,11 +22,2 @@ const state = this.state(); | ||
} | ||
//========================================================================================================= | ||
// Ingest Value | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Ingests new State Value into Runtime and applies it to the State | ||
* @param newStateValue - New Value of the State | ||
* @param config - Config | ||
*/ | ||
ingestValue(newStateValue, config = {}) { | ||
@@ -59,3 +36,2 @@ const state = this.state(); | ||
}); | ||
// Force overwriting State because if assigning Value to State, the State shouldn't be a placeholder anymore | ||
if (state.isPlaceholder) { | ||
@@ -65,10 +41,7 @@ config.force = true; | ||
} | ||
// Assign next State Value and compute it if necessary | ||
this.nextStateValue = state.computeMethod | ||
? internal_1.copy(state.computeMethod(newStateValue)) | ||
: internal_1.copy(newStateValue); | ||
// Check if State Value and new/next Value are equals | ||
if (internal_1.equal(state._value, this.nextStateValue) && !config.force) | ||
return; | ||
// Create Job | ||
const job = new internal_1.StateRuntimeJob(this, { | ||
@@ -86,17 +59,7 @@ storage: config.storage, | ||
} | ||
//========================================================================================================= | ||
// Perform | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Performs Job that holds this Observer | ||
* @param job - Job | ||
*/ | ||
perform(job) { | ||
const state = job.observer.state(); | ||
// Assign new State Values | ||
state.previousStateValue = internal_1.copy(state._value); | ||
state._value = internal_1.copy(job.observer.nextStateValue); | ||
state.nextStateValue = internal_1.copy(job.observer.nextStateValue); | ||
// Overwrite old State Values | ||
if (job.config.overwrite) { | ||
@@ -109,23 +72,10 @@ state.initialStateValue = internal_1.copy(state._value); | ||
this.sideEffects(job); | ||
// Assign Public Value to Observer after sideEffects like 'rebuildGroup', | ||
// because sometimes (for instance in Group) the publicValue is not the value(nextStateValue) | ||
// and the observer value is at some point the publicValue because the end user uses it | ||
job.observer.value = internal_1.copy(state.getPublicValue()); | ||
} | ||
//========================================================================================================= | ||
// Side Effect | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* SideEffects of Job | ||
* @param job - Job | ||
*/ | ||
sideEffects(job) { | ||
var _a, _b, _c; | ||
const state = job.observer.state(); | ||
// Call Watchers Functions | ||
for (const watcherKey in state.watchers) | ||
if (internal_1.isFunction(state.watchers[watcherKey])) | ||
state.watchers[watcherKey](state.getPublicValue(), watcherKey); | ||
// Call SideEffect Functions | ||
if ((_b = (_a = job.config) === null || _a === void 0 ? void 0 : _a.sideEffects) === null || _b === void 0 ? void 0 : _b.enabled) { | ||
@@ -132,0 +82,0 @@ const sideEffectArray = internal_1.createArrayFromObject(state.sideEffects); |
@@ -15,8 +15,2 @@ "use strict"; | ||
class StatePersistent extends internal_1.Persistent { | ||
/** | ||
* @internal | ||
* State Persist Manager - Handles permanent storing of State Value | ||
* @param state - State that gets stored | ||
* @param config - Config | ||
*/ | ||
constructor(state, config = {}) { | ||
@@ -35,14 +29,5 @@ super(state.agileInstance(), { | ||
}); | ||
// Load/Store persisted Value for the first Time | ||
if (this.ready && config.instantiate) | ||
this.initialLoading(); | ||
} | ||
//========================================================================================================= | ||
// Set Key | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Updates Key/Name of Persistent | ||
* @param value - New Key/Name of Persistent | ||
*/ | ||
setKey(value) { | ||
@@ -52,3 +37,2 @@ return __awaiter(this, void 0, void 0, function* () { | ||
const wasReady = this.ready; | ||
// Assign Key | ||
if (value === this._key) | ||
@@ -58,3 +42,2 @@ return; | ||
const isValid = this.validatePersistent(); | ||
// Try to Initial Load Value if persistent wasn't ready and return | ||
if (!wasReady) { | ||
@@ -65,5 +48,3 @@ if (isValid) | ||
} | ||
// Remove value at old Key | ||
yield this.removePersistedValue(oldKey); | ||
// Assign Value to new Key | ||
if (isValid) | ||
@@ -73,9 +54,2 @@ yield this.persistValue(value); | ||
} | ||
//========================================================================================================= | ||
// Initial Loading | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Loads/Saves Storage Value for the first Time | ||
*/ | ||
initialLoading() { | ||
@@ -91,10 +65,2 @@ const _super = Object.create(null, { | ||
} | ||
//========================================================================================================= | ||
// Load Persisted Value | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Loads State Value from the Storage | ||
* @return Success? | ||
*/ | ||
loadPersistedValue(key) { | ||
@@ -105,9 +71,6 @@ return __awaiter(this, void 0, void 0, function* () { | ||
const _key = key || this._key; | ||
// Load Value from default Storage | ||
const loadedValue = yield this.agileInstance().storages.get(_key, this.defaultStorageKey); | ||
if (!loadedValue) | ||
return false; | ||
// Assign loaded Value to State | ||
this.state().set(loadedValue, { storage: false }); | ||
// Persist State, so that the Storage Value updates dynamically if the State updates | ||
yield this.persistValue(_key); | ||
@@ -117,10 +80,2 @@ return true; | ||
} | ||
//========================================================================================================= | ||
// Persist Value | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Sets everything up so that the State gets saved in the Storage on every Value change | ||
* @return Success? | ||
*/ | ||
persistValue(key) { | ||
@@ -131,7 +86,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
const _key = key || this._key; | ||
// Add SideEffect to State, that updates the saved State Value depending on the current State Value | ||
this.state().addSideEffect(StatePersistent.storeValueSideEffectKey, (instance, config) => { | ||
this.rebuildStorageSideEffect(this.state(), _key, config); | ||
}, { weight: 0 }); | ||
// Rebuild Storage for saving State Value in the Storage | ||
this.rebuildStorageSideEffect(this.state(), _key); | ||
@@ -142,10 +95,2 @@ this.isPersisted = true; | ||
} | ||
//========================================================================================================= | ||
// Remove Persisted Value | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Removes State Value form the Storage | ||
* @return Success? | ||
*/ | ||
removePersistedValue(key) { | ||
@@ -156,5 +101,3 @@ return __awaiter(this, void 0, void 0, function* () { | ||
const _key = key || this._key; | ||
// Remove SideEffect | ||
this.state().removeSideEffect(StatePersistent.storeValueSideEffectKey); | ||
// Remove Value from Storage | ||
this.agileInstance().storages.remove(_key, this.storageKeys); | ||
@@ -165,13 +108,4 @@ this.isPersisted = false; | ||
} | ||
//========================================================================================================= | ||
// Format Key | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Formats Storage Key | ||
* @param key - Key that gets formatted | ||
*/ | ||
formatKey(key) { | ||
const state = this.state(); | ||
// Get key from State | ||
if (!key && state._key) | ||
@@ -181,3 +115,2 @@ return state._key; | ||
return; | ||
// Set State Key to Storage Key if State has no key | ||
if (!state._key) | ||
@@ -187,12 +120,2 @@ state._key = key; | ||
} | ||
//========================================================================================================= | ||
// Rebuild Storage SideEffect | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Rebuilds Storage depending on the State Value (Saves current State Value into the Storage) | ||
* @param state - State that holds the new Value | ||
* @param key - Key/Name of Persistent | ||
* @param config - Config | ||
*/ | ||
rebuildStorageSideEffect(state, key, config = {}) { | ||
@@ -199,0 +122,0 @@ if (config.storage !== undefined && !config.storage) |
@@ -6,10 +6,4 @@ "use strict"; | ||
class Storages { | ||
/** | ||
* @internal | ||
* Storages - Manages Storages of Agile | ||
* @param agileInstance - An Instance of Agile | ||
* @param config - Config | ||
*/ | ||
constructor(agileInstance, config = {}) { | ||
this.storages = {}; // All registered Storages | ||
this.storages = {}; | ||
this.persistentInstances = new Set(); | ||
@@ -23,11 +17,3 @@ this.agileInstance = () => agileInstance; | ||
} | ||
//========================================================================================================= | ||
// Instantiate Local Storage | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Instantiates Local Storage | ||
*/ | ||
instantiateLocalStorage() { | ||
// Check if Local Storage is Available | ||
if (!Storages.localStorageAvailable()) { | ||
@@ -37,3 +23,2 @@ internal_1.Agile.logger.warn('Local Storage is here not available, to use Storage functionalities like persist please provide a custom Storage!'); | ||
} | ||
// Create and register Local Storage | ||
const _localStorage = new internal_1.Storage({ | ||
@@ -50,14 +35,4 @@ key: 'localStorage', | ||
} | ||
//========================================================================================================= | ||
// Register | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Register new Storage as Agile Storage | ||
* @param storage - new Storage | ||
* @param config - Config | ||
*/ | ||
register(storage, config = {}) { | ||
const hasRegisteredAnyStorage = internal_1.notEqual(this.storages, {}); | ||
// Check if Storage already exists | ||
if (Object.prototype.hasOwnProperty.call(this.storages, storage.key)) { | ||
@@ -67,3 +42,2 @@ internal_1.Agile.logger.error(`Storage with the key/name '${storage.key}' already exists`); | ||
} | ||
// Set first added Storage as default Storage | ||
if (!hasRegisteredAnyStorage && config.default === false) { | ||
@@ -74,3 +48,2 @@ internal_1.Agile.logger.warn('Be aware that Agile has to assign the first added Storage as default Storage!'); | ||
config.default = true; | ||
// Register Storage | ||
this.storages[storage.key] = storage; | ||
@@ -80,3 +53,2 @@ if (config.default) | ||
this.persistentInstances.forEach((persistent) => { | ||
// If Persistent isn't ready and has no default StorageKey.. reassignStorageKeys and try to load it | ||
if (!persistent.defaultStorageKey) { | ||
@@ -89,3 +61,2 @@ persistent.assignStorageKeys(); | ||
} | ||
// Add Value of Persistent to newly registered Storage | ||
if (persistent.storageKeys.includes(storage.key)) | ||
@@ -96,13 +67,4 @@ persistent.persistValue(); | ||
} | ||
//========================================================================================================= | ||
// Get Storage | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Get Storage at Key/Name | ||
* @param key - Key/Name of Storage | ||
*/ | ||
getStorage(key) { | ||
const storage = this.storages[key]; | ||
// Check if Storage exists | ||
if (!storage) { | ||
@@ -112,3 +74,2 @@ internal_1.Agile.logger.error(`Storage with the key/name '${key}' doesn't exist`); | ||
} | ||
// Check if Storage is ready | ||
if (!storage.ready) { | ||
@@ -120,11 +81,2 @@ internal_1.Agile.logger.error(`Storage with the key/name '${key}' isn't ready`); | ||
} | ||
//========================================================================================================= | ||
// Get | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Gets value at provided Key | ||
* @param key - Key of Storage property | ||
* @param storageKey - Key/Name of Storage from which the Item is fetched (if not provided default Storage will be used) | ||
*/ | ||
get(key, storageKey) { | ||
@@ -136,3 +88,2 @@ var _a; | ||
} | ||
// Call get Method in specific Storage | ||
if (storageKey) { | ||
@@ -143,15 +94,4 @@ const storage = this.getStorage(storageKey); | ||
} | ||
// Call get Method in default Storage | ||
return ((_a = this.defaultStorage) === null || _a === void 0 ? void 0 : _a.get(key)) || Promise.resolve(undefined); | ||
} | ||
//========================================================================================================= | ||
// Set | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Saves/Updates value at provided Key | ||
* @param key - Key of Storage property | ||
* @param value - new Value that gets set at provided Key | ||
* @param storageKeys - Key/Name of Storages where the Value gets set (if not provided default Storage will be used) | ||
*/ | ||
set(key, value, storageKeys) { | ||
@@ -163,3 +103,2 @@ var _a, _b; | ||
} | ||
// Call set Method in specific Storages | ||
if (storageKeys) { | ||
@@ -170,14 +109,4 @@ for (const storageKey of storageKeys) | ||
} | ||
// Call set Method in default Storage | ||
(_b = this.defaultStorage) === null || _b === void 0 ? void 0 : _b.set(key, value); | ||
} | ||
//========================================================================================================= | ||
// Remove | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Removes value at provided Key | ||
* @param key - Key of Storage property | ||
* @param storageKeys - Key/Name of Storages where the Value gets removed (if not provided default Storage will be used) | ||
*/ | ||
remove(key, storageKeys) { | ||
@@ -189,3 +118,2 @@ var _a, _b; | ||
} | ||
// Call remove Method in specific Storages | ||
if (storageKeys) { | ||
@@ -196,22 +124,7 @@ for (const storageKey of storageKeys) | ||
} | ||
// Call remove Method in default Storage | ||
(_b = this.defaultStorage) === null || _b === void 0 ? void 0 : _b.remove(key); | ||
} | ||
//========================================================================================================= | ||
// Has Storage | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Check if at least one Storage got registered | ||
*/ | ||
hasStorage() { | ||
return internal_1.notEqual(this.storages, {}); | ||
} | ||
//========================================================================================================= | ||
// Local Storage Available | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Checks if localStorage is available in this Environment | ||
*/ | ||
static localStorageAvailable() { | ||
@@ -218,0 +131,0 @@ try { |
@@ -15,13 +15,5 @@ "use strict"; | ||
class Persistent { | ||
/** | ||
* @internal | ||
* Persistent - Handles storing of Agile Instances | ||
* Note: No stand alone class!! | ||
* @param agileInstance - An instance of Agile | ||
* @param config - Config | ||
*/ | ||
constructor(agileInstance, config = {}) { | ||
this.ready = false; | ||
this.isPersisted = false; // If Value is stored in Agile Storage | ||
// StorageKeys of Storages in that the Persisted Value gets saved | ||
this.isPersisted = false; | ||
this.storageKeys = []; | ||
@@ -41,37 +33,11 @@ this.agileInstance = () => agileInstance; | ||
} | ||
/** | ||
* @internal | ||
* Set Key/Name of Persistent | ||
*/ | ||
set key(value) { | ||
this.setKey(value); | ||
} | ||
/** | ||
* @internal | ||
* Get Key/Name of Persistent | ||
*/ | ||
get key() { | ||
return this._key; | ||
} | ||
//========================================================================================================= | ||
// Set Key | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Sets Key/Name of Persistent | ||
* @param value - New Key/Name of Persistent | ||
*/ | ||
setKey(value) { | ||
this._key = value; | ||
} | ||
//========================================================================================================= | ||
// Instantiate Persistent | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Instantiates this Class | ||
* Note: Had to outsource it from the constructor because some extending classes | ||
* have to define some stuff before being able to instantiate the parent (this) | ||
* @param config - Config | ||
*/ | ||
instantiatePersistent(config = {}) { | ||
@@ -82,12 +48,4 @@ this._key = this.formatKey(config.key) || Persistent.placeHolderKey; | ||
} | ||
//========================================================================================================= | ||
// Validate Persistent | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Validates Persistent and updates its 'ready' property | ||
*/ | ||
validatePersistent() { | ||
let isValid = true; | ||
// Validate Key | ||
if (this._key === Persistent.placeHolderKey) { | ||
@@ -97,3 +55,2 @@ internal_1.Agile.logger.error('No valid persist Key found! Please provide a Key or assign one to the parent instance.'); | ||
} | ||
// Validate StorageKeys | ||
if (!this.defaultStorageKey || this.storageKeys.length <= 0) { | ||
@@ -106,13 +63,4 @@ internal_1.Agile.logger.error('No persist Storage Key found! Please provide at least one Storage Key.'); | ||
} | ||
//========================================================================================================= | ||
// Assign StorageKeys | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Assign new StorageKeys to Persistent and overwrite the old ones | ||
* @param storageKeys - New Storage Keys | ||
*/ | ||
assignStorageKeys(storageKeys = []) { | ||
const storages = this.agileInstance().storages; | ||
// Set default Agile Storage to defaultStorage if no storageKey provided | ||
if (storageKeys.length <= 0) { | ||
@@ -130,9 +78,2 @@ this.storageKeys = []; | ||
} | ||
//========================================================================================================= | ||
// Initial Loading | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Loads/Saves Storage Value for the first Time | ||
*/ | ||
initialLoading() { | ||
@@ -147,10 +88,2 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
//========================================================================================================= | ||
// Load Value | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Loads Value from Storage | ||
* @return Success? | ||
*/ | ||
loadPersistedValue() { | ||
@@ -162,10 +95,2 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
//========================================================================================================= | ||
// Update Value | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Saves/Updates Value in Storage | ||
* @return Success? | ||
*/ | ||
persistValue() { | ||
@@ -177,10 +102,2 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
//========================================================================================================= | ||
// Remove Value | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Removes Value form Storage | ||
* @return Success? | ||
*/ | ||
removePersistedValue() { | ||
@@ -192,10 +109,2 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
//========================================================================================================= | ||
// Format Key | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Validates Storage Key | ||
* @param key - Key that gets validated | ||
*/ | ||
formatKey(key) { | ||
@@ -202,0 +111,0 @@ return key; |
@@ -6,7 +6,2 @@ "use strict"; | ||
class Storage { | ||
/** | ||
* @public | ||
* Storage - Interface for storing Items permanently | ||
* @param config - Config | ||
*/ | ||
constructor(config) { | ||
@@ -27,3 +22,2 @@ this.ready = false; | ||
return; | ||
// Check if Storage is async | ||
if (internal_1.isAsyncFunction(this.methods.get) || | ||
@@ -34,9 +28,2 @@ internal_1.isAsyncFunction(this.methods.set) || | ||
} | ||
//========================================================================================================= | ||
// Validate | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Validates Storage Methods | ||
*/ | ||
validate() { | ||
@@ -58,11 +45,2 @@ var _a, _b, _c; | ||
} | ||
//========================================================================================================= | ||
// Normal Get | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Gets value at provided Key (normal) | ||
* Note: Only use this if you are 100% sure this Storage doesn't work async | ||
* @param key - Key of Storage property | ||
*/ | ||
normalGet(key) { | ||
@@ -73,3 +51,2 @@ if (!this.ready || !this.methods.get) | ||
internal_1.Agile.logger.warn("Be aware that 'normalGet' returns a Promise with a stringified Value if using it in an async Storage!"); | ||
// Get Value | ||
const res = this.methods.get(this.getStorageKey(key)); | ||
@@ -80,17 +57,7 @@ if (internal_1.isJsonString(res)) | ||
} | ||
//========================================================================================================= | ||
// Async Get | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Gets value at provided Key (async) | ||
* @param key - Key of Storage property | ||
*/ | ||
get(key) { | ||
if (!this.ready || !this.methods.get) | ||
return Promise.resolve(undefined); | ||
// Get Value in 'dummy' promise if get method isn't async | ||
if (!internal_1.isAsyncFunction(this.methods.get)) | ||
return Promise.resolve(this.normalGet(key)); | ||
// Get Value (async) | ||
return new Promise((resolve, reject) => { | ||
@@ -105,11 +72,2 @@ var _a; | ||
} | ||
//========================================================================================================= | ||
// Set | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Saves/Updates value at provided Key | ||
* @param key - Key of Storage property | ||
* @param value - new Value that gets set | ||
*/ | ||
set(key, value) { | ||
@@ -120,10 +78,2 @@ if (!this.ready || !this.methods.set) | ||
} | ||
//========================================================================================================= | ||
// Remove | ||
//========================================================================================================= | ||
/** | ||
* @public | ||
* Removes value at provided Key | ||
* @param key - Key of Storage property | ||
*/ | ||
remove(key) { | ||
@@ -134,10 +84,2 @@ if (!this.ready || !this.methods.remove) | ||
} | ||
//========================================================================================================= | ||
// Get Storage Key | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Creates Storage Key from provided key | ||
* @param key - Key that gets converted into a Storage Key | ||
*/ | ||
getStorageKey(key) { | ||
@@ -144,0 +86,0 @@ return this.config.prefix |
@@ -5,15 +5,8 @@ "use strict"; | ||
const internal_1 = require("./internal"); | ||
//========================================================================================================= | ||
// Copy | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Creates a fresh copy of an Array/Object | ||
* https://www.samanthaming.com/tidbits/70-3-ways-to-clone-objects/ | ||
* @param value - Array/Object that gets copied | ||
*/ | ||
function copy(value) { | ||
// Extra checking '!value' because 'typeof null === object' | ||
if (!value || typeof value !== 'object') | ||
if (!value) | ||
return value; | ||
const valConstructorName = Object.getPrototypeOf(value).constructor.name; | ||
if (!['object', 'array'].includes(valConstructorName.toLowerCase())) | ||
return value; | ||
let temp; | ||
@@ -28,11 +21,2 @@ const newObject = Array.isArray(value) ? [] : {}; | ||
exports.copy = copy; | ||
//========================================================================================================= | ||
// Is Valid Object | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Checks if an value is a valid Object | ||
* https://stackoverflow.com/questions/12996871/why-does-typeof-array-with-objects-return-object-and-not-array | ||
* @param value - Value that is tested for its correctness | ||
*/ | ||
function isValidObject(value) { | ||
@@ -56,11 +40,2 @@ function isHTMLElement(obj) { | ||
exports.isValidObject = isValidObject; | ||
//========================================================================================================= | ||
// Includes Array | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Check if array1 contains all elements of array2 | ||
* @param array1 - Array 1 | ||
* @param array2 - Array 2 | ||
*/ | ||
function includesArray(array1, array2) { | ||
@@ -70,11 +45,2 @@ return array2.every((element) => array1.includes(element)); | ||
exports.includesArray = includesArray; | ||
//========================================================================================================= | ||
// Normalize Array | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Transforms Item/s to an Item Array | ||
* @param items - Item/s that gets transformed to an Array | ||
* @param config - Config | ||
*/ | ||
function normalizeArray(items, config = {}) { | ||
@@ -89,14 +55,4 @@ config = defineConfig(config, { | ||
exports.normalizeArray = normalizeArray; | ||
//========================================================================================================= | ||
// Get Instance | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Tries to get an Instance of Agile from provided Instance | ||
* If no agileInstance found it returns the global bound Agile Instance | ||
* @param instance - Instance that might hold an Agile Instance | ||
*/ | ||
function getAgileInstance(instance) { | ||
try { | ||
// Try to get agileInstance from passed Instance | ||
if (instance) { | ||
@@ -109,3 +65,2 @@ const _agileInstance = isFunction(instance['agileInstance']) | ||
} | ||
// Return global bound agileInstance | ||
return globalThis['__agile__']; | ||
@@ -119,10 +74,2 @@ } | ||
exports.getAgileInstance = getAgileInstance; | ||
//========================================================================================================= | ||
// Is Function | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Checks if value is a function | ||
* @param value - Value that gets tested if its a function | ||
*/ | ||
function isFunction(value) { | ||
@@ -132,10 +79,2 @@ return typeof value === 'function'; | ||
exports.isFunction = isFunction; | ||
//========================================================================================================= | ||
// Is Async Function | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Checks if value is an async function | ||
* @param value - Value that gets tested if its an async function | ||
*/ | ||
function isAsyncFunction(value) { | ||
@@ -148,17 +87,8 @@ const valueString = value.toString(); | ||
exports.isAsyncFunction = isAsyncFunction; | ||
//========================================================================================================= | ||
// Is Valid Url | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Checks the correctness of an url | ||
* Resource: https://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-a-url | ||
* @param url - Url that gets tested for its correctness | ||
*/ | ||
function isValidUrl(url) { | ||
const pattern = new RegExp('^(https?:\\/\\/)?' + // protocol | ||
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name | ||
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address | ||
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path | ||
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string | ||
const pattern = new RegExp('^(https?:\\/\\/)?' + | ||
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + | ||
'((\\d{1,3}\\.){3}\\d{1,3}))' + | ||
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + | ||
'(\\?[;&a-z\\d%_.~+=-]*)?' + | ||
'(\\#[-a-z\\d_]*)?$', 'i'); | ||
@@ -168,10 +98,2 @@ return pattern.test(url); | ||
exports.isValidUrl = isValidUrl; | ||
//========================================================================================================= | ||
// Is Json String | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Checks if value is valid JsonString | ||
* @param value - Value that gets checked | ||
*/ | ||
function isJsonString(value) { | ||
@@ -189,12 +111,2 @@ if (typeof value !== 'string') | ||
exports.isJsonString = isJsonString; | ||
//========================================================================================================= | ||
// Define Config | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Merges default values/properties into config object | ||
* @param config - Config object that receives default values | ||
* @param defaults - Default values object that gets merged into config object | ||
* @param overwriteUndefinedProperties - If undefined Properties in config gets overwritten by the default value | ||
*/ | ||
function defineConfig(config, defaults, overwriteUndefinedProperties) { | ||
@@ -213,10 +125,2 @@ if (overwriteUndefinedProperties === undefined) | ||
exports.defineConfig = defineConfig; | ||
/** | ||
* @internal | ||
* Merges items into object, be aware that the merge will only happen at the top level of the object. | ||
* Initially it adds new properties of the changes object into the source object. | ||
* @param source - Source object | ||
* @param changes - Changes that get merged into the source object | ||
* @param config - Config | ||
*/ | ||
function flatMerge(source, changes, config = {}) { | ||
@@ -226,7 +130,5 @@ config = defineConfig(config, { | ||
}); | ||
// Copy Source to avoid References | ||
const _source = copy(source); | ||
if (!_source) | ||
return _source; | ||
// Merge Changes Object into Source Object | ||
const keys = Object.keys(changes); | ||
@@ -241,11 +143,2 @@ keys.forEach((property) => { | ||
exports.flatMerge = flatMerge; | ||
//========================================================================================================= | ||
// Equals | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Check if two values are equal | ||
* @param value1 - First Value | ||
* @param value2 - Second Value | ||
*/ | ||
function equal(value1, value2) { | ||
@@ -255,11 +148,2 @@ return value1 === value2 || JSON.stringify(value1) === JSON.stringify(value2); | ||
exports.equal = equal; | ||
//========================================================================================================= | ||
// Not Equals | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Checks if two values aren't equal | ||
* @param value1 - First Value | ||
* @param value2 - Second Value | ||
*/ | ||
function notEqual(value1, value2) { | ||
@@ -269,10 +153,2 @@ return !equal(value1, value2); | ||
exports.notEqual = notEqual; | ||
//========================================================================================================= | ||
// Generate Id | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Generates random Id | ||
* @param length - Length of generated Id | ||
*/ | ||
function generateId(length) { | ||
@@ -290,10 +166,2 @@ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
exports.generateId = generateId; | ||
//========================================================================================================= | ||
// Create Array From Object | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Transforms Object to Array | ||
* @param object - Object that gets transformed | ||
*/ | ||
function createArrayFromObject(object) { | ||
@@ -310,15 +178,5 @@ const array = []; | ||
exports.createArrayFromObject = createArrayFromObject; | ||
//========================================================================================================= | ||
// Clone | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Clones a Class | ||
* @param instance - Instance of Class you want to clone | ||
*/ | ||
function clone(instance) { | ||
// Clone Class | ||
const objectCopy = Object.create(Object.getPrototypeOf(instance)); | ||
const objectClone = Object.assign(objectCopy, instance); | ||
// Copy Properties of Class to remove flat references | ||
for (const key in objectClone) | ||
@@ -329,14 +187,2 @@ objectClone[key] = copy(objectClone[key]); | ||
exports.clone = clone; | ||
//========================================================================================================= | ||
// Global Bind | ||
//========================================================================================================= | ||
/** | ||
* @internal | ||
* Binds passed Instance globally at passed Key | ||
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis | ||
* https://blog.logrocket.com/what-is-globalthis-why-use-it/ | ||
* @param key - Key/Name of Instance | ||
* @param instance - Instance | ||
* @param overwrite - If already existing instance at passed Key gets overwritten | ||
*/ | ||
function globalBind(key, instance, overwrite = false) { | ||
@@ -343,0 +189,0 @@ try { |
{ | ||
"name": "@agile-ts/core", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"author": "BennoDev", | ||
@@ -20,6 +20,8 @@ "license": "MIT", | ||
"build": "tsc", | ||
"prepare": "tsc && tsc -p ./tsconfig.production.json", | ||
"dev-publish": "yalc publish", | ||
"dev-push": "yalc push", | ||
"watch": "tsc-watch --onSuccess \"yarn run dev-push\"", | ||
"release": "node ./scripts/prepublish.js && yarn run build" | ||
"release": "node ./scripts/prepublish.js && yarn run prepare", | ||
"preview": "npm pack" | ||
}, | ||
@@ -26,0 +28,0 @@ "publishConfig": { |
@@ -12,3 +12,3 @@ <img src="https://raw.githubusercontent.com/agile-ts/agile/master/static/header_background.png" alt="AgileTs"> | ||
<a href="https://npm.im/@agile-ts/core"> | ||
<img src="https://img.shields.io/bundlephobia/min/@agile-ts/core.svg?label=minified%20size&style=flat&colorA=293140&colorB=4a4872" alt="npm minified size"/> | ||
<img src="https://img.shields.io/bundlephobia/min/@agile-ts/core.svg?label=bundle%20size&style=flat&colorA=293140&colorB=4a4872" alt="npm minified size"/> | ||
</a> | ||
@@ -27,2 +27,5 @@ <a href="https://npm.im/@agile-ts/core"> | ||
</a> | ||
<a href="https://coveralls.io/github/agile-ts/agile?branch=master"> | ||
<img src="https://coveralls.io/repos/github/agile-ts/agile/badge.svg?branch=master" alt="Coverage Badge"/> | ||
</a> | ||
</p> | ||
@@ -33,2 +36,5 @@ | ||
</a> | ||
<a href="https://discord.gg/T9GzreAwPH"> | ||
<img src="https://img.shields.io/discord/795291052897992724.svg?label=&logo=discord&logoColor=ffffff&color=7389D8&labelColor=6A7EC2" alt="Join Discord"/> | ||
</a> | ||
</p> | ||
@@ -35,0 +41,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
181
210328
5482