diff --git a/test/test.objectmap.both.ts b/test/test.objectmap.both.ts index 7fe862e..eab662e 100644 --- a/test/test.objectmap.both.ts +++ b/test/test.objectmap.both.ts @@ -52,7 +52,7 @@ tap.test('Objectmap.forEach -> should correctly run a function forEach map objec tap.test('lik.Objectmap.find() -> should correctly find an object', async () => { let myObject = { propOne: 'helloThere', propTwo: 'helloAnyway' }; testObjectmap.add(myObject); - let referenceObject = testObjectmap.find((itemArg) => { + let referenceObject = await testObjectmap.find(async (itemArg) => { return itemArg.propOne === 'helloThere'; }); // tslint:disable-next-line:no-unused-expression diff --git a/ts/lik.interestmap.ts b/ts/lik.interestmap.ts index 5c2db36..b534d76 100644 --- a/ts/lik.interestmap.ts +++ b/ts/lik.interestmap.ts @@ -76,7 +76,7 @@ export class InterestMap { * @param objectArg removes an interest from the InterestMap */ public removeInterest(interestArg: Interest) { - const interestToRemove = this.interestObjectMap.findOneAndRemove((interestArg2) => { + const interestToRemove = this.interestObjectMap.findOneAndRemoveSync((interestArg2) => { return interestArg.comparisonString === interestArg2.comparisonString; }); } @@ -94,7 +94,7 @@ export class InterestMap { * @param comparisonStringArg */ public checkInterestByString(comparisonStringArg: string): boolean { - const foundInterest = this.interestObjectMap.find((interest) => { + const foundInterest = this.interestObjectMap.findSync((interest) => { return interest.comparisonString === comparisonStringArg; }); if (foundInterest) { @@ -121,7 +121,7 @@ export class InterestMap { */ public findInterest(objectArg: DTInterestId): Interest { const comparableString = this.comparisonFunc(objectArg); - const interest = this.interestObjectMap.find((interestArg) => { + const interest = this.interestObjectMap.findSync((interestArg) => { return interestArg.comparisonString === comparableString; }); return interest; // if an interest is found, the interest is returned, otherwise interest is null diff --git a/ts/lik.objectmap.ts b/ts/lik.objectmap.ts index 1eda947..78a0cdc 100644 --- a/ts/lik.objectmap.ts +++ b/ts/lik.objectmap.ts @@ -13,10 +13,14 @@ export interface IObjectmapForEachFunction { (itemArg: T): void; } -export interface IObjectmapFindFunction { +export interface IObjectmapFindFunctionSync { (itemArg: T): boolean; } +export interface IObjectmapFindFunction { + (itemArg: T): Promise; +} + export interface IObjectMapEventData { operation: 'add' | 'remove'; payload: T; @@ -121,7 +125,11 @@ export class ObjectMap { /** * find object */ - public find(findFunction: IObjectmapFindFunction): T { + public async find(findFunction: IObjectmapFindFunction): Promise { + return this.fastMap.find(findFunction); + } + + public findSync(findFunction: IObjectmapFindFunctionSync): T { for (const keyArg of this.fastMap.getKeys()) { if (findFunction(this.fastMap.getByKey(keyArg))) { return this.getMappedUnique(keyArg); @@ -132,8 +140,15 @@ export class ObjectMap { /** * finds a specific element and then removes it */ - public findOneAndRemove(findFunction: IObjectmapFindFunction): T { - const foundElement = this.find(findFunction); + public async findOneAndRemove(findFunction: IObjectmapFindFunction): Promise { + const foundElement = await this.find(findFunction); + if (foundElement) { + this.remove(foundElement); + } + return foundElement; + } + public findOneAndRemoveSync(findFunction: IObjectmapFindFunctionSync): T { + const foundElement = this.findSync(findFunction); if (foundElement) { this.remove(foundElement); }