BREAKING CHANGE(objectmap): switch to async behaviours

This commit is contained in:
Philipp Kunz 2021-09-21 16:34:12 +02:00
parent 15b8fe406a
commit f402e55ff3
3 changed files with 23 additions and 8 deletions

View File

@ -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 () => { tap.test('lik.Objectmap.find() -> should correctly find an object', async () => {
let myObject = { propOne: 'helloThere', propTwo: 'helloAnyway' }; let myObject = { propOne: 'helloThere', propTwo: 'helloAnyway' };
testObjectmap.add(myObject); testObjectmap.add(myObject);
let referenceObject = testObjectmap.find((itemArg) => { let referenceObject = await testObjectmap.find(async (itemArg) => {
return itemArg.propOne === 'helloThere'; return itemArg.propOne === 'helloThere';
}); });
// tslint:disable-next-line:no-unused-expression // tslint:disable-next-line:no-unused-expression

View File

@ -76,7 +76,7 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
* @param objectArg removes an interest from the InterestMap * @param objectArg removes an interest from the InterestMap
*/ */
public removeInterest(interestArg: Interest<DTInterestId, DTInterestFullfillment>) { public removeInterest(interestArg: Interest<DTInterestId, DTInterestFullfillment>) {
const interestToRemove = this.interestObjectMap.findOneAndRemove((interestArg2) => { const interestToRemove = this.interestObjectMap.findOneAndRemoveSync((interestArg2) => {
return interestArg.comparisonString === interestArg2.comparisonString; return interestArg.comparisonString === interestArg2.comparisonString;
}); });
} }
@ -94,7 +94,7 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
* @param comparisonStringArg * @param comparisonStringArg
*/ */
public checkInterestByString(comparisonStringArg: string): boolean { public checkInterestByString(comparisonStringArg: string): boolean {
const foundInterest = this.interestObjectMap.find((interest) => { const foundInterest = this.interestObjectMap.findSync((interest) => {
return interest.comparisonString === comparisonStringArg; return interest.comparisonString === comparisonStringArg;
}); });
if (foundInterest) { if (foundInterest) {
@ -121,7 +121,7 @@ export class InterestMap<DTInterestId, DTInterestFullfillment> {
*/ */
public findInterest(objectArg: DTInterestId): Interest<DTInterestId, DTInterestFullfillment> { public findInterest(objectArg: DTInterestId): Interest<DTInterestId, DTInterestFullfillment> {
const comparableString = this.comparisonFunc(objectArg); const comparableString = this.comparisonFunc(objectArg);
const interest = this.interestObjectMap.find((interestArg) => { const interest = this.interestObjectMap.findSync((interestArg) => {
return interestArg.comparisonString === comparableString; return interestArg.comparisonString === comparableString;
}); });
return interest; // if an interest is found, the interest is returned, otherwise interest is null return interest; // if an interest is found, the interest is returned, otherwise interest is null

View File

@ -13,10 +13,14 @@ export interface IObjectmapForEachFunction<T> {
(itemArg: T): void; (itemArg: T): void;
} }
export interface IObjectmapFindFunction<T> { export interface IObjectmapFindFunctionSync<T> {
(itemArg: T): boolean; (itemArg: T): boolean;
} }
export interface IObjectmapFindFunction<T> {
(itemArg: T): Promise<boolean>;
}
export interface IObjectMapEventData<T> { export interface IObjectMapEventData<T> {
operation: 'add' | 'remove'; operation: 'add' | 'remove';
payload: T; payload: T;
@ -121,7 +125,11 @@ export class ObjectMap<T> {
/** /**
* find object * find object
*/ */
public find(findFunction: IObjectmapFindFunction<T>): T { public async find(findFunction: IObjectmapFindFunction<T>): Promise<T> {
return this.fastMap.find(findFunction);
}
public findSync(findFunction: IObjectmapFindFunctionSync<T>): T {
for (const keyArg of this.fastMap.getKeys()) { for (const keyArg of this.fastMap.getKeys()) {
if (findFunction(this.fastMap.getByKey(keyArg))) { if (findFunction(this.fastMap.getByKey(keyArg))) {
return this.getMappedUnique(keyArg); return this.getMappedUnique(keyArg);
@ -132,8 +140,15 @@ export class ObjectMap<T> {
/** /**
* finds a specific element and then removes it * finds a specific element and then removes it
*/ */
public findOneAndRemove(findFunction: IObjectmapFindFunction<T>): T { public async findOneAndRemove(findFunction: IObjectmapFindFunction<T>): Promise<T> {
const foundElement = this.find(findFunction); const foundElement = await this.find(findFunction);
if (foundElement) {
this.remove(foundElement);
}
return foundElement;
}
public findOneAndRemoveSync(findFunction: IObjectmapFindFunctionSync<T>): T {
const foundElement = this.findSync(findFunction);
if (foundElement) { if (foundElement) {
this.remove(foundElement); this.remove(foundElement);
} }