streamline things
This commit is contained in:
parent
04518f28cf
commit
f4290ca8fa
8
dist/index.d.ts
vendored
8
dist/index.d.ts
vendored
@ -5,19 +5,13 @@ import 'typings-global';
|
|||||||
export declare type TLoader = 'npm' | 'systemjs';
|
export declare type TLoader = 'npm' | 'systemjs';
|
||||||
export declare class LazyModule<T> {
|
export declare class LazyModule<T> {
|
||||||
name: string;
|
name: string;
|
||||||
nameIsPath: boolean;
|
|
||||||
cwd: string;
|
cwd: string;
|
||||||
whenLoaded: Promise<T>;
|
whenLoaded: Promise<T>;
|
||||||
loader: TLoader;
|
private nameIsPath;
|
||||||
private whenLoadedDeferred;
|
private whenLoadedDeferred;
|
||||||
constructor(nameArg: string, cwdArg: string);
|
constructor(nameArg: string, cwdArg: string);
|
||||||
setLoader(loaderArg: TLoader): void;
|
|
||||||
/**
|
/**
|
||||||
* loads the module
|
* loads the module
|
||||||
*/
|
*/
|
||||||
load(): Promise<T>;
|
load(): Promise<T>;
|
||||||
/**
|
|
||||||
* loads additional lazy modules specified as arguments and returns them in the promise for easy use
|
|
||||||
*/
|
|
||||||
loadAlso(...args: LazyModule<any>[]): void;
|
|
||||||
}
|
}
|
||||||
|
40
dist/index.js
vendored
40
dist/index.js
vendored
@ -1,4 +1,12 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||||
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||||||
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||||
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||||
|
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
||||||
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||||
|
});
|
||||||
|
};
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
require("typings-global");
|
require("typings-global");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
@ -19,7 +27,6 @@ class Smartsystem {
|
|||||||
let smartsystem = new Smartsystem();
|
let smartsystem = new Smartsystem();
|
||||||
class LazyModule {
|
class LazyModule {
|
||||||
constructor(nameArg, cwdArg) {
|
constructor(nameArg, cwdArg) {
|
||||||
this.loader = 'npm';
|
|
||||||
if (!cwdArg) {
|
if (!cwdArg) {
|
||||||
throw new Error('You must specify a directory to resolve from!');
|
throw new Error('You must specify a directory to resolve from!');
|
||||||
}
|
}
|
||||||
@ -33,37 +40,16 @@ class LazyModule {
|
|||||||
this.whenLoadedDeferred = smartq.defer();
|
this.whenLoadedDeferred = smartq.defer();
|
||||||
this.whenLoaded = this.whenLoadedDeferred.promise;
|
this.whenLoaded = this.whenLoadedDeferred.promise;
|
||||||
}
|
}
|
||||||
setLoader(loaderArg) {
|
|
||||||
this.loader = loaderArg;
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* loads the module
|
* loads the module
|
||||||
*/
|
*/
|
||||||
load() {
|
load() {
|
||||||
let done = smartq.defer();
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
let loadedModule;
|
let loadedModule = require(this.name);
|
||||||
if (this.loader === 'npm') {
|
|
||||||
loadedModule = require(this.name);
|
|
||||||
done.resolve(loadedModule);
|
|
||||||
}
|
|
||||||
else if (this.loader === 'systemjs') {
|
|
||||||
let systemjs = require('systemjs');
|
|
||||||
systemjs.import(this.name).then((m) => {
|
|
||||||
loadedModule = m;
|
|
||||||
this.whenLoadedDeferred.resolve(loadedModule);
|
this.whenLoadedDeferred.resolve(loadedModule);
|
||||||
done.resolve(loadedModule);
|
return loadedModule;
|
||||||
}).catch(err => { console.log(err); });
|
});
|
||||||
}
|
|
||||||
else {
|
|
||||||
throw Error('loader not supported');
|
|
||||||
}
|
|
||||||
return done.promise;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* loads additional lazy modules specified as arguments and returns them in the promise for easy use
|
|
||||||
*/
|
|
||||||
loadAlso(...args) {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.LazyModule = LazyModule;
|
exports.LazyModule = LazyModule;
|
||||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLDBCQUF1QjtBQUV2Qiw2QkFBNEI7QUFDNUIsaUNBQWdDO0FBQ2hDLDZCQUErQjtBQUUvQjtJQUFBO1FBQ0UsZ0JBQVcsR0FBRyxJQUFJLGVBQVMsRUFBbUIsQ0FBQTtJQVFoRCxDQUFDO0lBTkM7O09BRUc7SUFDSCxhQUFhLENBQUUsYUFBOEI7UUFDM0MsSUFBSSxDQUFDLFdBQVcsQ0FBQyxHQUFHLENBQUMsYUFBYSxDQUFDLENBQUE7SUFDckMsQ0FBQztDQUNGO0FBRUQsa0NBQWtDO0FBQ2xDLElBQUksV0FBVyxHQUFHLElBQUksV0FBVyxFQUFFLENBQUE7QUFRbkM7SUFPRSxZQUFZLE9BQWUsRUFBRSxNQUFjO1FBRjNDLFdBQU0sR0FBWSxLQUFLLENBQUE7UUFHckIsRUFBRSxDQUFDLENBQUMsQ0FBQyxNQUFNLENBQUMsQ0FBQyxDQUFDO1lBQ1osTUFBTSxJQUFJLEtBQUssQ0FBQywrQ0FBK0MsQ0FBQyxDQUFBO1FBQ2xFLENBQUM7UUFDRCxJQUFJLENBQUMsSUFBSSxHQUFHLE9BQU8sQ0FBQTtRQUNuQixJQUFJLENBQUMsR0FBRyxHQUFHLE1BQU0sQ0FBQTtRQUNqQixXQUFXLENBQUMsYUFBYSxDQUFDLElBQUksQ0FBQyxDQUFBLENBQUMscUNBQXFDO1FBQ3JFLElBQUksQ0FBQyxVQUFVLEdBQUcsTUFBTSxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUMsSUFB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BQUMsWUFBWSxDQUFBO1FBQ3JCLENBQUM7S0FBQTtDQUNGO0FBN0JELGdDQTZCQyJ9
|
10
package.json
10
package.json
@ -23,12 +23,12 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/smartsystem#README",
|
"homepage": "https://gitlab.com/pushrocks/smartsystem#README",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"lik": "^1.0.30",
|
"lik": "^1.0.38",
|
||||||
"smartq": "^1.1.1",
|
"smartq": "^1.1.6",
|
||||||
"systemjs": "^0.20.12",
|
"systemjs": "^0.20.17",
|
||||||
"typings-global": "^1.0.19"
|
"typings-global": "^1.0.20"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"tapbundle": "^1.0.12"
|
"tapbundle": "^1.1.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
11
test/test.ts
11
test/test.ts
@ -1,10 +1,15 @@
|
|||||||
import { tap, expect } from 'tapbundle'
|
import { tap, expect } from 'tapbundle'
|
||||||
import * as smartsystem from '../dist/index'
|
import * as smartsystem from '../ts/index'
|
||||||
import * as _moduleExample from './assets/moduleExample'
|
import * as _moduleExample from './assets/moduleExample'
|
||||||
|
|
||||||
tap.test('should load a module lazily', async (tools) => {
|
let lazyModuleExample: smartsystem.LazyModule<typeof _moduleExample>
|
||||||
let lazyModuleExample = new smartsystem.LazyModule<typeof _moduleExample>('./assets/moduleExample.js', __dirname)
|
|
||||||
|
|
||||||
|
tap.test('should create a lazy module instance', async (tools) => {
|
||||||
|
lazyModuleExample = new smartsystem.LazyModule<typeof _moduleExample>('./assets/moduleExample.js', __dirname)
|
||||||
|
expect(lazyModuleExample).to.be.instanceof(smartsystem.LazyModule)
|
||||||
|
})
|
||||||
|
|
||||||
|
tap.test('should load the module', async () => {
|
||||||
await lazyModuleExample.load().then(async m => {
|
await lazyModuleExample.load().then(async m => {
|
||||||
console.log(m.exportedTestBoolean)
|
console.log(m.exportedTestBoolean)
|
||||||
})
|
})
|
||||||
|
33
ts/index.ts
33
ts/index.ts
@ -26,10 +26,9 @@ export type TLoader = 'npm' | 'systemjs'
|
|||||||
|
|
||||||
export class LazyModule<T> {
|
export class LazyModule<T> {
|
||||||
name: string
|
name: string
|
||||||
nameIsPath: boolean
|
|
||||||
cwd: string
|
cwd: string
|
||||||
whenLoaded: Promise<T>
|
whenLoaded: Promise<T>
|
||||||
loader: TLoader = 'npm'
|
private nameIsPath: boolean
|
||||||
private whenLoadedDeferred: smartq.Deferred<T>
|
private whenLoadedDeferred: smartq.Deferred<T>
|
||||||
constructor (nameArg: string, cwdArg: string) {
|
constructor (nameArg: string, cwdArg: string) {
|
||||||
if (!cwdArg) {
|
if (!cwdArg) {
|
||||||
@ -46,36 +45,12 @@ export class LazyModule<T> {
|
|||||||
this.whenLoaded = this.whenLoadedDeferred.promise
|
this.whenLoaded = this.whenLoadedDeferred.promise
|
||||||
}
|
}
|
||||||
|
|
||||||
setLoader (loaderArg: TLoader) {
|
|
||||||
this.loader = loaderArg
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* loads the module
|
* loads the module
|
||||||
*/
|
*/
|
||||||
load (): Promise<T> {
|
async load (): Promise<T> {
|
||||||
let done = smartq.defer<T>()
|
let loadedModule: T = require(this.name)
|
||||||
let loadedModule: T
|
|
||||||
if (this.loader === 'npm') {
|
|
||||||
loadedModule = require(this.name)
|
|
||||||
done.resolve(loadedModule)
|
|
||||||
} else if (this.loader === 'systemjs') {
|
|
||||||
let systemjs = require('systemjs')
|
|
||||||
systemjs.import(this.name).then((m) => {
|
|
||||||
loadedModule = m
|
|
||||||
this.whenLoadedDeferred.resolve(loadedModule)
|
this.whenLoadedDeferred.resolve(loadedModule)
|
||||||
done.resolve(loadedModule)
|
return loadedModule
|
||||||
}).catch(err => { console.log(err) })
|
|
||||||
} else {
|
|
||||||
throw Error('loader not supported')
|
|
||||||
}
|
|
||||||
return done.promise
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* loads additional lazy modules specified as arguments and returns them in the promise for easy use
|
|
||||||
*/
|
|
||||||
loadAlso (...args: LazyModule<any>[]) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user