fix(core): update

This commit is contained in:
2023-01-18 17:45:19 +01:00
parent 47b511f1f0
commit 6f1e32284d
13 changed files with 143 additions and 86 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@pushrocks/lik',
version: '6.0.1',
version: '6.0.2',
description: 'light little helpers for node'
}

View File

@ -1,4 +1,4 @@
export * from './lik.asyncexecutionstack.js'
export * from './lik.asyncexecutionstack.js';
export * from './lik.fastmap.js';
export * from './lik.interestmap.js';
export * from './lik.interestmap.interest.js';

View File

@ -1,25 +1,82 @@
import * as plugins from './lik.plugins.js'
import * as plugins from './lik.plugins.js';
interface IExecutionSlot<T> {
executionDeferred: plugins.smartpromise.Deferred<T>;
funcToExecute: () => Promise<T>;
timeout?: number;
mode: 'exclusive' | 'nonexclusive';
}
/**
* allows for avoiding race condition
*/
export class AsyncExecutionStack {
public currentExecutions: Promise<any>[] = [];
public async getExclusiveExecutionSlot(funcArg: () => Promise<any>, timeoutArg: number) {
const executionDeferred = plugins.smartpromise.defer();
this.currentExecutions.push(executionDeferred.promise);
for (const promiseArg of this.currentExecutions) {
if (promiseArg !== executionDeferred.promise) {
await promiseArg;
} else {
if (timeoutArg) {
await Promise.race([funcArg(),plugins.smartdelay.delayFor(timeoutArg)])
private executionSlots: IExecutionSlot<any>[] = [];
public async getExclusiveExecutionSlot<T = any>(funcArg: () => Promise<T>, timeoutArg?: number) {
const executionDeferred = plugins.smartpromise.defer<T>();
const executionSlot: IExecutionSlot<T> = {
funcToExecute: funcArg,
executionDeferred,
timeout: timeoutArg,
mode: 'exclusive',
};
this.executionSlots.push(executionSlot);
this.processExecutionSlots();
return executionDeferred.promise;
}
public async getNonExclusiveExecutionSlot<T = any>(
funcArg: () => Promise<T>,
timeoutArg?: number
) {
const executionDeferred = plugins.smartpromise.defer<T>();
const executionSlot: IExecutionSlot<T> = {
funcToExecute: funcArg,
executionDeferred,
timeout: timeoutArg,
mode: 'nonexclusive',
};
this.executionSlots.push(executionSlot);
this.processExecutionSlots();
return executionDeferred.promise;
}
private currentlyExecutingDeferred: plugins.smartpromise.Deferred<any>;
private async processExecutionSlots() {
if (this.currentlyExecutingDeferred) {
return;
}
this.currentlyExecutingDeferred = plugins.smartpromise.defer();
let nonExclusiveRunningSlots: IExecutionSlot<any>[] = [];
const checkNonExclusiveRunningSlots = async (cleanArg = false) => {
if (nonExclusiveRunningSlots.length > 100 || cleanArg) {
await Promise.all(nonExclusiveRunningSlots.map(nonExclusiveRunningSlotArg => nonExclusiveRunningSlotArg.executionDeferred.promise));
nonExclusiveRunningSlots = [];
}
};
while (this.executionSlots.length > 0) {
const nextExecutionSlot = this.executionSlots.shift();
const runNextExecution = async () => {
if (nextExecutionSlot.timeout) {
const result = await Promise.race([
nextExecutionSlot.funcToExecute(),
plugins.smartdelay.delayFor(nextExecutionSlot.timeout),
]);
nextExecutionSlot.executionDeferred.resolve(result);
} else {
await funcArg();
nextExecutionSlot.executionDeferred.resolve(await nextExecutionSlot.funcToExecute());
}
executionDeferred.resolve();
this.currentExecutions.shift();
};
if (nextExecutionSlot.mode === 'exclusive') {
await checkNonExclusiveRunningSlots(true);
await runNextExecution();
} else {
nonExclusiveRunningSlots.push(nextExecutionSlot);
await checkNonExclusiveRunningSlots(false);
runNextExecution();
}
}
};
}
this.currentlyExecutingDeferred.resolve();
this.currentlyExecutingDeferred = null;
}
}

View File

@ -28,9 +28,8 @@ export class Interest<DTInterestId, DTInterestFullfillment> {
return this.comparisonFunc(this.originalInterest);
}
private interestDeferred: plugins.smartpromise.Deferred<
DTInterestFullfillment
> = new plugins.smartpromise.Deferred();
private interestDeferred: plugins.smartpromise.Deferred<DTInterestFullfillment> =
new plugins.smartpromise.Deferred();
public interestFullfilled = this.interestDeferred.promise;
/**

View File

@ -8,7 +8,6 @@ Subssequent interests will be mapped to the same interest
which is then is only fullfilled once.
=========== */
import * as plugins from './lik.plugins.js';
import { ObjectMap } from './lik.objectmap.js';
import { Interest } from './lik.interestmap.interest.js';

View File

@ -85,7 +85,7 @@ export class ObjectMap<T> {
this.addMappedUnique(uniqueKey, objectArg);
this.eventSubject.next({
operation: 'add',
payload: objectArg
payload: objectArg,
});
return uniqueKey;
}
@ -140,7 +140,7 @@ export class ObjectMap<T> {
/**
* finds a specific element and then removes it
*/
public async findOneAndRemove(findFunction: IObjectmapFindFunction<T>): Promise<T> {
public async findOneAndRemove(findFunction: IObjectmapFindFunction<T>): Promise<T> {
const foundElement = await this.find(findFunction);
if (foundElement) {
this.remove(foundElement);
@ -176,7 +176,7 @@ export class ObjectMap<T> {
const removedItem = this.fastMap.removeFromMap(keyToUse);
this.eventSubject.next({
operation: 'remove',
payload: removedItem
payload: removedItem,
});
return removedItem;
}
@ -209,7 +209,7 @@ export class ObjectMap<T> {
const removedObject = this.fastMap.removeFromMap(keyArg);
this.eventSubject.next({
operation: 'remove',
payload: removedObject
payload: removedObject,
});
return removedObject;
}