From 814e2f857132b9ff356a15be0c41217eb5c27f64 Mon Sep 17 00:00:00 2001 From: wessberg Date: Mon, 14 Oct 2019 10:58:37 +0200 Subject: [PATCH] feat(deasync): make the deasync module an optional dependency since not all environments support it --- package.json | 4 ++-- src/index.ts | 1 + .../async-not-supported-error.ts | 13 +++++++++++++ .../i-async-not-supported-error-options.ts | 4 ++++ src/interpreter/util/await/sync-await.ts | 16 +++++++++++++++- 5 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/interpreter/error/async-not-supported-error/async-not-supported-error.ts create mode 100644 src/interpreter/error/async-not-supported-error/i-async-not-supported-error-options.ts diff --git a/package.json b/package.json index da44ee5..4be479b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 31e38b0..1967b27 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; \ No newline at end of file diff --git a/src/interpreter/error/async-not-supported-error/async-not-supported-error.ts b/src/interpreter/error/async-not-supported-error/async-not-supported-error.ts new file mode 100644 index 0000000..a249876 --- /dev/null +++ b/src/interpreter/error/async-not-supported-error/async-not-supported-error.ts @@ -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()}); + } +} \ No newline at end of file diff --git a/src/interpreter/error/async-not-supported-error/i-async-not-supported-error-options.ts b/src/interpreter/error/async-not-supported-error/i-async-not-supported-error-options.ts new file mode 100644 index 0000000..b87b035 --- /dev/null +++ b/src/interpreter/error/async-not-supported-error/i-async-not-supported-error-options.ts @@ -0,0 +1,4 @@ +import {IEvaluationErrorOptions} from "../evaluation-error/i-evaluation-error-options"; + +export interface IAsyncNotSupportedErrorOptions extends Omit { +} \ No newline at end of file diff --git a/src/interpreter/util/await/sync-await.ts b/src/interpreter/util/await/sync-await.ts index a515f3a..a0793ce 100644 --- a/src/interpreter/util/await/sync-await.ts +++ b/src/interpreter/util/await/sync-await.ts @@ -1,4 +1,14 @@ -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 @@ -6,6 +16,10 @@ import {loopWhile} from "deasync"; * @return {T} */ export function syncAwait (promise: Promise): 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;