Skip to content

Commit

Permalink
Added run method
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Dec 5, 2024
1 parent 1cd4dde commit 6949de5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/models/TaskManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class TaskManager {

private waiting: Queue<{ resolve, reject, fx }> = new Queue();

protected run<TR>(fx: (... a: any[]) => Promise<TR>): Promise<TR> {
protected queueRun<TR>(fx: (... a: any[]) => Promise<TR>): Promise<TR> {

const pr = new Promise((resolve, reject) => {
this.waiting.enqueue({ resolve, reject, fx });
Expand Down
30 changes: 30 additions & 0 deletions src/services/BaseEntityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,36 @@ export default abstract class BaseEntityService extends HttpSession {
}) as Promise<T>;
}


public run<T extends IClrEntity, TA, TQ>(m: IModel<T, TQ, TA>, method: keyof TA, argEntity: IClrEntity, {
args = void 0 as any[],
cacheSeconds = 0,
cacheVersion = void 0 as any,
cancelToken = void 0 as CancelToken
} = {
}) {
// will send keys only...
const { $type, $key } = argEntity;
if (!$key) {
throw new Error(`Run requires encrypted $key`);
}
const usp = new URLSearchParams();
usp.append("key", $key);
if (args) {
usp.append("args", JSON.stringify(args));
}
if (cacheSeconds) {
usp.append("cache", cacheSeconds.toString());
}
if (cacheVersion) {
usp.append("cv", cacheVersion);
}
return this.getJson({
url: `${this.url}run/${$type}/${method as any}?${usp.toString()}`,
cancelToken,
}) as Promise<T>;
}

public save<T extends IClrEntity>(body: T, cloner?: (c: Cloner<T>) => Cloner<T>, trace?: boolean): Promise<T>;
public save<T extends IClrEntity>(body: T[], cloner?: (c: Cloner<T>) => Cloner<T>, trace?: boolean): Promise<T[]>;
public async save(body: any, cloner?: (c: Cloner<any>) => Cloner<any>, trace?: boolean): Promise<any> {
Expand Down
2 changes: 1 addition & 1 deletion src/services/HttpSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class HttpSession extends TaskManager {
protected resultConverter = (e) => e;

protected fetchJson<T>(options: IHttpRequest): Promise<T> {
return this.run(() => this.uncheckedFetchJson<T>(options));
return this.queueRun(() => this.uncheckedFetchJson<T>(options));
}

protected async uncheckedFetchJson<T>(options: IHttpRequest): Promise<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/task-manager/TaskManagerTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const sleep = (n) => new Promise((r) => setTimeout(r, n));
export class TestTaskManager extends TaskManager {

public sleep(fx: () => any) {
return this.run(async () => {
return this.queueRun(async () => {
fx();
await sleep(100);
})
Expand Down

0 comments on commit 6949de5

Please sign in to comment.