now passing required loading time through promise

This commit is contained in:
Philipp Kunz 2016-09-15 14:59:22 +02:00
parent 140bfb4f5e
commit ac92f355d4
8 changed files with 47 additions and 37 deletions

View File

@ -10,7 +10,9 @@ early.start("myModuleName");
/*
do your loading stuff
*/
early.stop(); // stop "early" when your module is ready
early.stop().then((loadingTime:number) => { // loadingTime in milliseconds
}); // stop "early" when your module is ready
```
early runs in its own process: The loader animation runs smoothly even when your event queue of your currently loading module is working heavily.

2
dist/index.d.ts vendored
View File

@ -2,4 +2,4 @@
import 'typings-global';
import q = require('q');
export declare let start: (moduleNameArg?: string, loaderLengthArg?: string) => void;
export declare let stop: () => q.Promise<void>;
export declare let stop: () => q.Promise<number>;

13
dist/index.js vendored
View File

@ -35,18 +35,19 @@ exports.start = function (moduleNameArg = '', loaderLengthArg = '10') {
exports.stop = () => {
let done = q.defer();
let endTime = Date.now();
let executionTime = ((endTime - startTime) / 1000).toString();
let earlyExecutionTime = (endTime - startTime);
let earlyExecutionTimeString = (earlyExecutionTime / 1000).toString();
if (doAnimation) {
earlyChild.kill('SIGINT');
earlyChild.on('close', function () {
console.log(`loaded ${chalk.green(moduleName)} in ${executionTime} seconds!`);
done.resolve();
console.log(`loaded ${chalk.green(moduleName)} in ${earlyExecutionTimeString} seconds!`);
done.resolve(earlyExecutionTime);
});
}
else {
console.log(`... finished loading ${moduleName} in ${executionTime}`);
done.resolve();
console.log(`... finished loading ${moduleName} in ${earlyExecutionTimeString}`);
done.resolve(earlyExecutionTime);
}
return done.promise;
};
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsUUFBTyxnQkFDUCxDQUFDLENBRHNCO0FBQ3ZCLE1BQU8sSUFBSSxXQUFXLE1BQU0sQ0FBQyxDQUFBO0FBQzdCLE1BQU8sS0F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SUFBSSxDQUFDLEdBQUcsRUFBRSxDQUFBO0lBQ3hCLElBQUksa0JBQWtCLEdBQVcsQ0FBQyxPQUFPLEdBQUcsU0FBUyxDQUFDLENBQUE7SUFDdEQsSUFBSSx3QkFBd0IsR0FBVyxDQUFDLGtCQUFrQixHQUFHLElBQUksQ0FBQyxDQUFDLFFBQVEsRUFBRSxDQUFBO0lBQzdFLEVBQUUsQ0FBQyxDQUFDLFdBQVcsQ0FBQyxDQUFDLENBQUM7UUFDZCxVQUFVLENBQUMsSUFBSSxDQUFDLFFBQVEsQ0FBQyxDQUFBO1FBQ3pCLFVBQVUsQ0FBQyxFQUFFLENBQUMsT0FBTyxFQUFFO1lBQ25CLE9BQU8sQ0FBQyxHQUFHLENBQUMsVUFBVSxLQUFLLENBQUMsS0FBSyxDQUFDLFVBQVUsQ0FBQyxPQUFPLHdCQUF3QixXQUFXLENBQUMsQ0FBQTtZQUN4RixJQUFJLENBQUMsT0FBTyxDQUFDLGtCQUFrQixDQUFDLENBQUE7UUFDcEMsQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDO0lBQUMsSUFBSSxDQUFDLENBQUM7UUFDSixPQUFPLENBQUMsR0FBRyxDQUFDLHdCQUF3QixVQUFVLE9BQU8sd0JBQXdCLEVBQUUsQ0FBQyxDQUFBO1FBQ2hGLElBQUksQ0FBQyxPQUFPLENBQUMsa0JBQWtCLENBQUMsQ0FBQTtJQUNwQyxDQUFDO0lBQ0QsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUE7QUFDdkIsQ0FBQyxDQUFBIn0=

View File

@ -74,7 +74,9 @@ early.start(<span class="hljs-string">"myModuleName"</span>);
<span class="hljs-comment">/*
do your loading stuff
*/</span>
early.stop(); <span class="hljs-comment">// stop "early" when your module is ready</span>
early.stop().then((loadingTime:<span class="hljs-built_in">number</span>) =&gt; { <span class="hljs-comment">// loadingTime in milliseconds</span>
}); <span class="hljs-comment">// stop "early" when your module is ready</span>
</code></pre>
<p>early runs in its own process: The loader animation runs smoothly even when your event queue of your currently loading module is working heavily.</p>
</div>

2
test/test.d.ts vendored
View File

@ -1 +1 @@
import "typings-test";
import 'typings-test';

View File

@ -1,15 +1,17 @@
"use strict";
require("typings-test");
const early = require("../dist/index");
describe("early", function () {
it(".start()", function (done) {
require('typings-test');
const early = require('../dist/index');
describe('early', function () {
it('.start()', function (done) {
this.timeout(10000);
early.start("early");
early.start('early');
setTimeout(done, 5000);
});
it(".stop()", function (done) {
it('.stop()', function (done) {
early.stop()
.then(done);
.then(() => {
done();
});
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsTUFBTyxLQUFLLFdBQVcsZUFBZSxDQUFDLENBQUM7QUFFeEMsUUFBUSxDQUFDLE9BQU8sRUFBQztJQUNiLEVBQUUsQ0FBQyxVQUFVLEVBQUMsVUFBUyxJQUFJO1FBQ3ZCLElBQUksQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDLENBQUM7UUFDcEIsS0FBSyxDQUFDLEtBQUssQ0FBQyxPQUFPLENBQUMsQ0FBQztRQUNyQixVQUFVLENBQUMsSUFBSSxFQUFDLElBQUksQ0FBQyxDQUFDO0lBQzFCLENBQUMsQ0FBQyxDQUFDO0lBQ0gsRUFBRSxDQUFDLFNBQVMsRUFBQyxVQUFTLElBQUk7UUFDdEIsS0FBSyxDQUFDLElBQUksRUFBRTthQUNQLElBQUksQ0FBQyxJQUFJLENBQUMsQ0FBQztJQUNwQixDQUFDLENBQUMsQ0FBQTtBQUNOLENBQUMsQ0FBQyxDQUFBIn0=
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FDUCxDQUFDLENBRG9CO0FBQ3JCLE1BQU8sS0FBSyxXQUFXLGVBQWUsQ0FBQyxDQUFBO0FBRXZDLFFBQVEsQ0FBQyxPQUFPLEVBQUM7SUFDYixFQUFFLENBQUMsVUFBVSxFQUFDLFVBQVMsSUFBSTtRQUN2QixJQUFJLENBQUMsT0FBTyxDQUFDLEtBQUssQ0FBQyxDQUFBO1FBQ25CLEtBQUssQ0FBQyxLQUFLLENBQUMsT0FBTyxDQUFDLENBQUE7UUFDcEIsVUFBVSxDQUFDLElBQUksRUFBQyxJQUFJLENBQUMsQ0FBQTtJQUN6QixDQUFDLENBQUMsQ0FBQTtJQUNGLEVBQUUsQ0FBQyxTQUFTLEVBQUMsVUFBUyxJQUFJO1FBQ3RCLEtBQUssQ0FBQyxJQUFJLEVBQUU7YUFDUCxJQUFJLENBQUM7WUFDRixJQUFJLEVBQUUsQ0FBQTtRQUNWLENBQUMsQ0FBQyxDQUFBO0lBQ1YsQ0FBQyxDQUFDLENBQUE7QUFDTixDQUFDLENBQUMsQ0FBQSJ9

View File

@ -1,14 +1,16 @@
import "typings-test";
import early = require("../dist/index");
import 'typings-test'
import early = require('../dist/index')
describe("early",function(){
it(".start()",function(done){
this.timeout(10000);
early.start("early");
setTimeout(done,5000);
});
it(".stop()",function(done){
describe('early',function(){
it('.start()',function(done){
this.timeout(10000)
early.start('early')
setTimeout(done,5000)
})
it('.stop()',function(done){
early.stop()
.then(done);
.then(() => {
done()
})
})
})

View File

@ -36,19 +36,20 @@ export let start = function (moduleNameArg: string = '', loaderLengthArg: string
}
}
export let stop = (): q.Promise<void> => {
let done = q.defer<void>()
export let stop = (): q.Promise<number> => {
let done = q.defer<number>()
let endTime = Date.now()
let executionTime: string = ((endTime - startTime) / 1000).toString()
let earlyExecutionTime: number = (endTime - startTime)
let earlyExecutionTimeString: string = (earlyExecutionTime / 1000).toString()
if (doAnimation) {
earlyChild.kill('SIGINT')
earlyChild.on('close', function () {
console.log(`loaded ${chalk.green(moduleName)} in ${executionTime} seconds!`)
done.resolve()
console.log(`loaded ${chalk.green(moduleName)} in ${earlyExecutionTimeString} seconds!`)
done.resolve(earlyExecutionTime)
})
} else {
console.log(`... finished loading ${moduleName} in ${executionTime}`)
done.resolve()
console.log(`... finished loading ${moduleName} in ${earlyExecutionTimeString}`)
done.resolve(earlyExecutionTime)
}
return done.promise
}