fix(core): update
This commit is contained in:
@ -2,8 +2,9 @@ import * as plugins from './lik.plugins';
|
||||
|
||||
// import modules
|
||||
|
||||
export * from './lik.interestmap';
|
||||
export * from './lik.limitedarray';
|
||||
export * from './lik.looptracker';
|
||||
export * from './lik.objectmap';
|
||||
export * from './lik.stringmap';
|
||||
export * from './lik.limitedarray';
|
||||
export * from './lik.tree';
|
||||
|
76
ts/lik.interestmap.interest.ts
Normal file
76
ts/lik.interestmap.interest.ts
Normal file
@ -0,0 +1,76 @@
|
||||
import * as plugins from './lik.plugins';
|
||||
|
||||
import { InterestMap, IInterestComparisonFunc} from './lik.interestmap';
|
||||
|
||||
export class Interest<DTInterestId, DTInterestFullfillment> {
|
||||
private interestMapRef: InterestMap<DTInterestId, DTInterestFullfillment>;
|
||||
public originalInterest: DTInterestId;
|
||||
public comparisonFunc: IInterestComparisonFunc<DTInterestId>;
|
||||
public destructionTimer = new plugins.smarttime.Timer(10000);
|
||||
public isFullfilled = false;
|
||||
|
||||
/**
|
||||
* a generic store to store objects in that are needed for fullfillment;
|
||||
*/
|
||||
public fullfillmentStore: any[] = [];
|
||||
|
||||
/**
|
||||
* quick access to a string that makes the interest comparable for checking for similar interests
|
||||
*/
|
||||
public get comparisonString() {
|
||||
return this.comparisonFunc(this.originalInterest);
|
||||
}
|
||||
|
||||
private interestDeferred: plugins.smartpromise.Deferred<DTInterestFullfillment> = new plugins.smartpromise.Deferred();
|
||||
public interestFullfilled = this.interestDeferred.promise;
|
||||
|
||||
/**
|
||||
* fullfill the interest
|
||||
*/
|
||||
public fullfillInterest(objectArg: DTInterestFullfillment) {
|
||||
this.isFullfilled = true;
|
||||
this.fullfillmentStore = [];
|
||||
this.interestDeferred.resolve(objectArg);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
constructor(
|
||||
interestMapArg: InterestMap<DTInterestId, DTInterestFullfillment>,
|
||||
interestArg: DTInterestId,
|
||||
comparisonFuncArg: IInterestComparisonFunc<DTInterestId>
|
||||
) {
|
||||
this.originalInterest = interestArg;
|
||||
this.comparisonFunc = comparisonFuncArg;
|
||||
this.interestMapRef = interestMapArg;
|
||||
this.destructionTimer.completed.then(() => {
|
||||
this.destroy();
|
||||
});
|
||||
}
|
||||
|
||||
// ===============================
|
||||
// LIFECYCLE MANAGEMENT
|
||||
// ===============================
|
||||
|
||||
/**
|
||||
* self destructs the interest
|
||||
*/
|
||||
public destroy() {
|
||||
this.interestMapRef.removeInterest(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* notifies the interest that the interest in it has been lost
|
||||
*/
|
||||
public markLost() {
|
||||
this.destructionTimer.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* notifies the interest that the interest in it has been restored
|
||||
*/
|
||||
public renew() {
|
||||
this.destructionTimer.reset();
|
||||
}
|
||||
}
|
117
ts/lik.interestmap.ts
Normal file
117
ts/lik.interestmap.ts
Normal file
@ -0,0 +1,117 @@
|
||||
import * as plugins from './lik.plugins';
|
||||
import { Objectmap } from './lik.objectmap';
|
||||
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
import { Interest } from './lik.interestmap.interest';
|
||||
|
||||
export type IInterestComparisonFunc<T> = (objectArg: T) => string;
|
||||
|
||||
|
||||
|
||||
export class InterestMap<DTInterestId, DTInterestFullfillment> {
|
||||
/**
|
||||
* stores interests that are currently fullfilled by the cache
|
||||
*/
|
||||
private interestObjectMap = new Objectmap<Interest<DTInterestId, DTInterestFullfillment>>();
|
||||
|
||||
/**
|
||||
* a function to compare interests
|
||||
*/
|
||||
private comparisonFunc: IInterestComparisonFunc<DTInterestId>;
|
||||
|
||||
constructor(comparisonFuncArg: IInterestComparisonFunc<DTInterestId>) {
|
||||
this.comparisonFunc = comparisonFuncArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* adds an interest to the InterestMap
|
||||
* @param objectArg
|
||||
*/
|
||||
public async addInterest(
|
||||
objectArg: DTInterestId
|
||||
): Promise<Interest<DTInterestId, DTInterestFullfillment>> {
|
||||
const comparisonString = this.comparisonFunc(objectArg);
|
||||
let returnInterest: Interest<DTInterestId, DTInterestFullfillment>;
|
||||
const newInterest = new Interest<DTInterestId, DTInterestFullfillment>(
|
||||
this,
|
||||
objectArg,
|
||||
this.comparisonFunc
|
||||
);
|
||||
let interestExists = false;
|
||||
await this.interestObjectMap.forEach(interestArg => {
|
||||
if (!interestExists && interestArg.comparisonString === newInterest.comparisonString) {
|
||||
console.log('info', `interest already exists for ${newInterest.comparisonString}`);
|
||||
interestExists = true;
|
||||
returnInterest = interestArg;
|
||||
returnInterest.renew();
|
||||
}
|
||||
});
|
||||
if (!returnInterest) {
|
||||
returnInterest = newInterest;
|
||||
this.interestObjectMap.add(returnInterest);
|
||||
}
|
||||
this.interestObservable.push(returnInterest);
|
||||
return returnInterest;
|
||||
}
|
||||
|
||||
// tslint:disable-next-line:member-ordering
|
||||
public interestObservable = new plugins.smartrx.ObservableIntake<Interest<DTInterestId, any>>();
|
||||
|
||||
/**
|
||||
* removes an interest from the interest map
|
||||
* @param objectArg removes an interest from the InterestMap
|
||||
*/
|
||||
public removeInterest(interestArg: Interest<DTInterestId, DTInterestFullfillment>) {
|
||||
const interestToRemove = this.interestObjectMap.findOneAndRemove(interestArg2 => {
|
||||
return interestArg.comparisonString === interestArg2.comparisonString;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* check interest
|
||||
*/
|
||||
public checkInterest(objectArg: DTInterestId): boolean {
|
||||
const comparisonString = this.comparisonFunc(objectArg);
|
||||
return this.checkInterestByString(comparisonString);
|
||||
}
|
||||
|
||||
/**
|
||||
* checks an interest
|
||||
* @param comparisonStringArg
|
||||
*/
|
||||
public checkInterestByString(comparisonStringArg: string): boolean {
|
||||
const foundInterest = this.interestObjectMap.find(interest => {
|
||||
return interest.comparisonString === comparisonStringArg;
|
||||
});
|
||||
if (foundInterest) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* inform lost interest
|
||||
* @param interestId
|
||||
*/
|
||||
public informLostInterest (interestId: DTInterestId) {
|
||||
const wantedInterest = this.findInterest(interestId);
|
||||
if (wantedInterest) {
|
||||
wantedInterest.markLost();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* finds an interest
|
||||
* @param objectArg
|
||||
*/
|
||||
public findInterest(objectArg: DTInterestId): Interest<DTInterestId, DTInterestFullfillment> {
|
||||
const comparableString = this.comparisonFunc(objectArg);
|
||||
const interest = this.interestObjectMap.find(interest => {
|
||||
return interest.comparisonString === comparableString;
|
||||
});
|
||||
return interest; // if an interest is found, the interest is returned, otherwise interest is null
|
||||
}
|
||||
}
|
@ -26,7 +26,7 @@ export class Objectmap<T> {
|
||||
* returns false if the object is already in the map
|
||||
* returns true if the object was added successfully
|
||||
*/
|
||||
add(objectArg: T): boolean {
|
||||
public add(objectArg: T): boolean {
|
||||
if (this.checkForObject(objectArg)) {
|
||||
// the object is already in the objectmap
|
||||
return false;
|
||||
@ -40,7 +40,7 @@ export class Objectmap<T> {
|
||||
/**
|
||||
* like .add but adds an whole array of objects
|
||||
*/
|
||||
addArray(objectArrayArg: T[]) {
|
||||
public addArray(objectArrayArg: T[]) {
|
||||
for (let item of objectArrayArg) {
|
||||
this.add(item);
|
||||
}
|
||||
@ -49,15 +49,15 @@ export class Objectmap<T> {
|
||||
/**
|
||||
* check if object is in Objectmap
|
||||
*/
|
||||
checkForObject(objectArg: T) {
|
||||
public checkForObject(objectArg: T) {
|
||||
return this.objectArray.indexOf(objectArg) !== -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* find object
|
||||
*/
|
||||
find(findFunction: IObjectmapFindFunction<T>) {
|
||||
let resultArray = this.objectArray.filter(findFunction);
|
||||
public find(findFunction: IObjectmapFindFunction<T>) {
|
||||
const resultArray = this.objectArray.filter(findFunction);
|
||||
if (resultArray.length > 0) {
|
||||
return resultArray[0];
|
||||
} else {
|
||||
@ -68,8 +68,8 @@ export class Objectmap<T> {
|
||||
/**
|
||||
* finds a specific element and then removes it
|
||||
*/
|
||||
findOneAndRemove(findFunction: IObjectmapFindFunction<T>): T {
|
||||
let foundElement = this.find(findFunction);
|
||||
public findOneAndRemove(findFunction: IObjectmapFindFunction<T>): T {
|
||||
const foundElement = this.find(findFunction);
|
||||
if (foundElement) {
|
||||
this.remove(foundElement);
|
||||
}
|
||||
@ -79,7 +79,7 @@ export class Objectmap<T> {
|
||||
/**
|
||||
* run function for each item in Objectmap
|
||||
*/
|
||||
async forEach(functionArg: IObjectmapForEachFunction<T>) {
|
||||
public async forEach(functionArg: IObjectmapForEachFunction<T>) {
|
||||
for (let object of this.objectArray) {
|
||||
await functionArg(object);
|
||||
}
|
||||
@ -88,21 +88,25 @@ export class Objectmap<T> {
|
||||
/**
|
||||
* gets an object in the Observablemap and removes it, so it can't be retrieved again
|
||||
*/
|
||||
getOneAndRemove(): T {
|
||||
public getOneAndRemove(): T {
|
||||
return this.objectArray.shift();
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a cloned array of all the objects currently in the Objectmap
|
||||
*/
|
||||
getArray() {
|
||||
return plugins.lodash.cloneDeep(this.objectArray);
|
||||
public getArray() {
|
||||
const returnArray: any[] = [];
|
||||
for (const objectItem of this.objectArray) {
|
||||
returnArray.push(objectItem);
|
||||
}
|
||||
return returnArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if Objectmap ist empty
|
||||
*/
|
||||
isEmpty(): boolean {
|
||||
public isEmpty(): boolean {
|
||||
if (this.objectArray.length === 0) {
|
||||
return true;
|
||||
} else {
|
||||
@ -113,7 +117,7 @@ export class Objectmap<T> {
|
||||
/**
|
||||
* remove object from Objectmap
|
||||
*/
|
||||
remove(objectArg: T) {
|
||||
public remove(objectArg: T) {
|
||||
let replacementArray = [];
|
||||
for (let item of this.objectArray) {
|
||||
if (item !== objectArg) {
|
||||
@ -126,7 +130,7 @@ export class Objectmap<T> {
|
||||
/**
|
||||
* wipe Objectmap
|
||||
*/
|
||||
wipe() {
|
||||
public wipe() {
|
||||
this.objectArray = [];
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,30 @@
|
||||
// ==============
|
||||
// native
|
||||
// ==============
|
||||
|
||||
import * as events from 'events';
|
||||
import * as lodash from 'lodash';
|
||||
import * as minimatch from 'minimatch';
|
||||
|
||||
export { events };
|
||||
|
||||
// ==============
|
||||
// @pushrocks
|
||||
// ==============
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrx from '@pushrocks/smartrx';
|
||||
import * as smarttime from '@pushrocks/smarttime';
|
||||
|
||||
export {
|
||||
smartdelay,
|
||||
smartpromise,
|
||||
smartrx,
|
||||
smarttime
|
||||
};
|
||||
|
||||
// ==============
|
||||
// third party
|
||||
// ==============
|
||||
import * as minimatch from 'minimatch';
|
||||
const symbolTree = require('symbol-tree');
|
||||
|
||||
export { events, lodash, minimatch, smartpromise, symbolTree };
|
||||
export { minimatch, symbolTree };
|
||||
|
@ -52,14 +52,14 @@ export class Stringmap {
|
||||
/**
|
||||
* check if string is in Stringmap
|
||||
*/
|
||||
checkString(stringArg: string): boolean {
|
||||
public checkString(stringArg: string): boolean {
|
||||
return this._stringArray.indexOf(stringArg) !== -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* checks stringPresence with minimatch
|
||||
*/
|
||||
checkMinimatch(miniMatchStringArg: string): boolean {
|
||||
public checkMinimatch(miniMatchStringArg: string): boolean {
|
||||
let foundMatch: boolean = false;
|
||||
for (let stringItem of this._stringArray) {
|
||||
if (plugins.minimatch(stringItem, miniMatchStringArg)) {
|
||||
@ -72,15 +72,19 @@ export class Stringmap {
|
||||
/**
|
||||
* checks if the Stringmap is empty
|
||||
*/
|
||||
checkIsEmpty() {
|
||||
public checkIsEmpty() {
|
||||
return this._stringArray.length === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets a cloned copy of the current string Array
|
||||
*/
|
||||
getStringArray() {
|
||||
return plugins.lodash.cloneDeep(this._stringArray);
|
||||
public getStringArray() {
|
||||
const returnArray: string[] = [];
|
||||
for (const stringItem of this._stringArray) {
|
||||
returnArray.push(stringItem);
|
||||
}
|
||||
return returnArray;
|
||||
}
|
||||
|
||||
// trigger registering
|
||||
@ -88,7 +92,7 @@ export class Stringmap {
|
||||
/**
|
||||
* register a new trigger
|
||||
*/
|
||||
registerUntilTrue(functionArg: ITriggerFunction, doFunctionArg) {
|
||||
public registerUntilTrue(functionArg: ITriggerFunction, doFunctionArg) {
|
||||
this._triggerUntilTrueFunctionArray.push(() => {
|
||||
let result = functionArg();
|
||||
if (result === true) {
|
||||
|
Reference in New Issue
Block a user