Skip to content

Commit

Permalink
feat(deasync): make the deasync module an optional dependency since n…
Browse files Browse the repository at this point in the history
…ot all environments support it
  • Loading branch information
wessberg committed Oct 14, 2019
1 parent 6e73f5a commit 814e2f8
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -62,14 +62,14 @@
"@types/node": "12.7.4",
"@types/object-path": "0.11.0",
"chalk": "2.4.2",
"deasync": "0.1.15",
"object-path": "0.11.4",
"tslib": "1.10.0",
"typescript": "3.6.2"
},
"optionalDependencies": {
"@types/jsdom": "12.2.4",
"jsdom": "15.1.1"
"jsdom": "15.1.1",
"deasync": "0.1.15"
},
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -24,6 +24,7 @@ export {MaxOpDurationExceededError} from "./interpreter/error/policy-error/max-o
export {NetworkError} from "./interpreter/error/policy-error/network-error/network-error";
export {NonDeterministicError} from "./interpreter/error/policy-error/non-deterministic-error/non-deterministic-error";
export {ProcessError} from "./interpreter/error/policy-error/process-error/process-error";
export {AsyncNotSupportedError} from "./interpreter/error/async-not-supported-error/async-not-supported-error";

// Reporting
export {BindingReportCallback, IReportingOptions, ReportingOptions} from "./interpreter/reporting/i-reporting-options";
@@ -0,0 +1,13 @@
import {EvaluationError} from "../evaluation-error/evaluation-error";
import {IAsyncNotSupportedErrorOptions} from "./i-async-not-supported-error-options";
import {createEmptyStatement} from "typescript";

/**
* An Error that can be thrown when an async operation is attempted but can't be computed
*/
export class AsyncNotSupportedError extends EvaluationError {

constructor ({message = `It is not possible to evaluate asynchronously: Optional dependency 'deasync' must be installed.'`}: IAsyncNotSupportedErrorOptions) {
super({message, node: createEmptyStatement()});
}
}
@@ -0,0 +1,4 @@
import {IEvaluationErrorOptions} from "../evaluation-error/i-evaluation-error-options";

export interface IAsyncNotSupportedErrorOptions extends Omit<IEvaluationErrorOptions, "node"> {
}
16 changes: 15 additions & 1 deletion src/interpreter/util/await/sync-await.ts
@@ -1,11 +1,25 @@
import {loopWhile} from "deasync";
import {AsyncNotSupportedError} from "../../error/async-not-supported-error/async-not-supported-error";

let loopWhile: (typeof import("deasync").loopWhile)|undefined;

try {
// tslint:disable-next-line:no-require-imports no-var-requires
const importedModule = require("deasync") as typeof import("deasync");
loopWhile = importedModule.loopWhile;
} catch {
// This is OK
}

/**
* A synchronous way to await a result
* @param {Promise<T>} promise
* @return {T}
*/
export function syncAwait<T> (promise: Promise<T>): T {
if (loopWhile === undefined) {
// Throw this error if for some reason the deasync module wasn't installed
throw new AsyncNotSupportedError({});
}
let done = false;
let rejected = false;
let resolveResult: unknown;
Expand Down

0 comments on commit 814e2f8

Please sign in to comment.