# HG changeset patch # User cin # Date 2019-05-28 10:29:30 # Node ID 600f0201c4baaf375eab5f5c5e656409c2afd537 # Parent f664825db16ab877396ce5d3fb8bf1413b30af8c Minor fixes and improvements - added `isCancellable` type predicate function to `safe` module - `isString, isNumber, isInteger, isPrimitive` are now type predicates diff --git a/history.md b/history.md --- a/history.md +++ b/history.md @@ -1,6 +1,14 @@ HISTORY ======= +1.2.16 +------ + +Minor fixes and improvements + +- added `isCancellable` type predicate function to `safe` module +- `isString, isNumber, isInteger, isPrimitive` are now type predicates + 1.2.0 ----- diff --git a/package-lock.json b/package-lock.json --- a/package-lock.json +++ b/package-lock.json @@ -90,7 +90,7 @@ }, "duplexer": { "version": "0.1.1", - "resolved": "http://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz", + "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz", "integrity": "sha1-rOb/gIwc5mtX0ev5eXessCM0z8E=", "dev": true }, @@ -144,7 +144,7 @@ "dependencies": { "tape": { "version": "2.3.3", - "resolved": "http://registry.npmjs.org/tape/-/tape-2.3.3.tgz", + "resolved": "https://registry.npmjs.org/tape/-/tape-2.3.3.tgz", "integrity": "sha1-Lnzgox3wn41oUWZKcYQuDKUFevc=", "dev": true, "requires": { @@ -277,7 +277,7 @@ }, "minimist": { "version": "0.0.5", - "resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.5.tgz", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.5.tgz", "integrity": "sha1-16oye87PUY+RBqxrjwA/o7zqhWY=", "dev": true }, @@ -316,7 +316,7 @@ }, "readable-stream": { "version": "1.1.14", - "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", "dev": true, "requires": { @@ -369,7 +369,7 @@ }, "string_decoder": { "version": "0.10.31", - "resolved": "http://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=", "dev": true }, @@ -432,7 +432,7 @@ }, "through2": { "version": "0.2.3", - "resolved": "http://registry.npmjs.org/through2/-/through2-0.2.3.tgz", + "resolved": "https://registry.npmjs.org/through2/-/through2-0.2.3.tgz", "integrity": "sha1-6zKE2k6jEbbMis42U3SKUqvyWj8=", "dev": true, "requires": { diff --git a/src/amd/ts/di/ResolverHelper.ts b/src/amd/ts/di/ResolverHelper.ts --- a/src/amd/ts/di/ResolverHelper.ts +++ b/src/amd/ts/di/ResolverHelper.ts @@ -1,6 +1,6 @@ import { Uuid } from "../Uuid"; import { argumentNotEmptyString, getGlobal, isNullOrEmptyString } from "../safe"; -import { TraceSource, DebugLevel } from "../log/TraceSource"; +import { TraceSource } from "../log/TraceSource"; import m = require("module"); const sandboxId = Uuid(); @@ -11,38 +11,13 @@ const globalRequire = getGlobal().requir const trace = TraceSource.get(m.id); -export async function createContextRequire(moduleName: string): Promise { - argumentNotEmptyString(moduleName, "moduleName"); - - const parts = moduleName.split("/"); - if (parts[0] === ".") - throw new Error("An absolute module path is required"); - - if (parts.length > 1) - parts.splice(-1, 1, Uuid()); - else - parts.push(Uuid()); - - const shim = parts.join("/"); - - trace.debug(`define shim ${shim}`); - - return new Promise(cb => { - define(shim, ["require"], r => { - trace.debug("shim resolved"); - return r; - }); - require([shim], cb); - }); -} - class ModuleResolver { _base: string; _require: Require; constructor(req: Require, base?: string) { this._base = base; - this._require = req || globalRequire; + this._require = req; } resolve(moduleName: string) { @@ -58,17 +33,9 @@ class ModuleResolver { } } -export async function makeResolver(moduleName: string, contextRequire: Require) { - trace.debug( - "makeResolver moduleName={0}, contextRequire={1}", - moduleName || "", - contextRequire ? typeof (contextRequire) : "" - ); +export function makeResolver(moduleName: string, contextRequire: Require) { + const base = moduleName && moduleName.split("/").slice(0, -1).join("/"); - const nestedRequire = isNullOrEmptyString(moduleName) ? null : await createContextRequire(moduleName); - - // const base = moduleName && moduleName.split("/").slice(0, -1).join("/"); - - const resolver = new ModuleResolver(nestedRequire, null); + const resolver = new ModuleResolver(contextRequire, base); return (id: string) => resolver.resolve(id); } diff --git a/src/main/ts/di/Configuration.ts b/src/main/ts/di/Configuration.ts --- a/src/main/ts/di/Configuration.ts +++ b/src/main/ts/di/Configuration.ts @@ -178,7 +178,7 @@ export class Configuration { trace.debug("<{0}", name); } - _visit(data, name: string): Promise { + async _visit(data, name: string) { if (isPrimitive(data) || isDescriptor(data)) return data; diff --git a/src/main/ts/safe.ts b/src/main/ts/safe.ts --- a/src/main/ts/safe.ts +++ b/src/main/ts/safe.ts @@ -1,3 +1,5 @@ +import { ICancellable } from "./interfaces"; + let _nextOid = 0; const _oid = typeof Symbol === "function" ? Symbol("__implab__oid__") : @@ -40,20 +42,20 @@ export function isNull(arg) { return (arg === null || arg === undefined); } -export function isPrimitive(arg) { +export function isPrimitive(arg): arg is string | number | boolean | undefined | null { return (arg === null || arg === undefined || typeof (arg) === "string" || typeof (arg) === "number" || typeof (arg) === "boolean"); } -export function isInteger(arg) { +export function isInteger(arg): arg is number { return parseInt(arg, 10) === arg; } -export function isNumber(arg) { +export function isNumber(arg): arg is number { return parseFloat(arg) === arg; } -export function isString(val) { +export function isString(val): val is string { return typeof (val) === "string" || val instanceof String; } @@ -61,6 +63,10 @@ export function isPromise(val): val is P return val && typeof val.then === "function"; } +export function isCancellable(val): val is ICancellable { + return val && typeof val.cancel === "function"; +} + export function isNullOrEmptyString(str) { if (str === null || str === undefined || ((typeof (str) === "string" || str instanceof String) && str.length === 0))