Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update jaasync and add timeout option to scan #25

Merged
merged 2 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@
"license": "MIT",
"dependencies": {
"core-decorators": "^0.20.0",
"jaasync": "^0.10.1"
"jaasync": "^0.11.0"
},
"devDependencies": {
"@types/jasmine": "^3.3.0",
"@types/node": "^10.17.3",
"jasmine": "^3.3.0",
"jasmine-spec-reporter": "^7.0.0",
"tslint": "^6.1.3",
"typedoc": "^0.23.0",
"typedoc": "0.23.0-beta.5",
"typescript": "^4.7.4"
}
}
2 changes: 1 addition & 1 deletion src/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class Scanner<TResult> implements CancelablePromise<TResult> {
return this._promise.finally(onfinally);
}

public cancel(reason?: string): boolean {
public cancel(reason?: string|Error): boolean {
if(this.settle()) {
this._promise.reject(reason);
return true;
Expand Down
23 changes: 19 additions & 4 deletions src/strongbus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import {autobind} from 'core-decorators';
import {CancelablePromise, cancelable} from 'jaasync';
import {CancelablePromise, cancelable, timeout} from 'jaasync';

import {Scanner} from './scanner';
import {StrongbusLogger} from './strongbusLogger';
Expand Down Expand Up @@ -267,32 +267,47 @@ export class Bus<TEventMap extends Events.EventMap = Events.EventMap> implements
* @param params.evaluator - an evaluation function that should check for a certain state
* and may resolve or reject the scan based on the state.
* @param params.trigger - event or events that should trigger evaluator
* @param {boolean} [params.pool=true] - attempt to pool scanners that can be resolved by the same evaluator and trigger; default is `true`
* @param {integer} [params.timeout] - cancel the scan after `params.timeout` milliseconds. Values `<= 0` are ignored. If configured, will disable pooling regardles of `params.pool`'s value
* @param {boolean} [params.eager=true] - should `params.evaluator` be called immediately; default is `true`.
* This eliminates the following anti-pattern:
* ```
* if(!someCondition) {
* await this.scan({evaluator: evaluateSomeCondition, trigger: ...});
* }
* ```
* @param {boolean} [params.pool=true] - attempt to pool scanners that can be resolved by the same evaluator and trigger; default is `true`
*/
public scan<TEvaluator extends Scanner.Evaluator<any, TEventMap>>(
params: {
evaluator: TEvaluator;
trigger: Events.Listenable<EventKeys<TEventMap>>;
eager?: boolean;
pool?: boolean;
timeout?: number;
}): CancelablePromise<TEvaluator extends Scanner.Evaluator<infer U, TEventMap> ? U : any> {

type TReturnType = TEvaluator extends Scanner.Evaluator<infer U, TEventMap> ? U : any;

if(params.pool === false) {
if(params.timeout && params.timeout > 0) {
const scanner = new Scanner<TReturnType>(params);
scanner.scan<TEventMap>(this, params.trigger);
// tslint:disable-next-line:prefer-object-spread
return Object.assign(
timeout(scanner, {ms: params.timeout, cancelUnderlyingPromiseOnTimeout: true}),
{
[INTERNAL_PROMISE]: scanner,
cancel: (err: any) => scanner.cancel(err)
}
);
} else if(params.pool === false) {
const scanner = new Scanner<TReturnType>(params);
scanner.scan<TEventMap>(this, params.trigger);
// tslint:disable-next-line:prefer-object-spread
return Object.assign(
cancelable(() => scanner),
scanner,
{[INTERNAL_PROMISE]: scanner}
);

}

/*
Expand Down
Loading