Skip to content

Commit 87b96b6

Browse files
zigmovegarringdal
authored andcommitted
Add an optional callback to runAsync() to be notified when a check finished. (#30)
* Add an optional callback to runAsync() to be notified when a check finished. Provide an option to raise notifications (e.g. node-notifier) after checking. * added /dist * Added a check befor accessing the msg property * new /dist * Refactoring, cleanup, removed pushResultWithWorker() and added as option to printResultWithWorker + fixed tslint issues + added to readme * new /dist * Fix duplicated output * new /dist
1 parent 33a172e commit 87b96b6

File tree

12 files changed

+57
-25
lines changed

12 files changed

+57
-25
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ var testAsync = TypeHelper({
4545
})
4646

4747
testAsync.runAsync();
48+
/* or with optional callback
49+
testAsync.runAsync((errors: number) => {
50+
// errors > 0 => notify
51+
});
52+
*/
53+
4854

4955

5056
// Watch folder and use worker (uses internal watcher)

dist/commonjs/checker.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/commonjs/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ export declare class TypeHelperClass {
66
private monitor;
77
private watchTimeout;
88
private isWorkerInspectPreformed;
9+
private workerCallback?;
910
constructor(options: ITypeCheckerOptions);
10-
runAsync(): void;
11+
runAsync(callback?: (errors: number) => void): void;
1112
runSync(): number;
1213
runPromise(): Promise<number>;
1314
runWatch(pathToWatch: string): void;

dist/commonjs/index.js

Lines changed: 12 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/commonjs/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/commonjs/interfaces.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface IInternalTypeCheckerOptions extends ITypeCheckerOptions {
2929
export interface IWorkerOptions {
3030
type: WorkerCommand;
3131
options?: IInternalTypeCheckerOptions;
32+
hasCallback?: boolean;
3233
}
3334
export declare enum WorkerCommand {
3435
inspectCode = 0,

dist/commonjs/interfaces.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/commonjs/worker.js

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/commonjs/worker.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class TypeHelperClass {
1616
private monitor: any;
1717
private watchTimeout: NodeJS.Timer;
1818
private isWorkerInspectPreformed: boolean;
19-
19+
private workerCallback?: (errors: number) => void;
2020

2121
constructor(options: ITypeCheckerOptions) {
2222
this.checker = new Checker();
@@ -59,11 +59,14 @@ export class TypeHelperClass {
5959
* Runs in own thread/works and quits
6060
*
6161
*/
62-
public runAsync(): void {
62+
public runAsync(callback?: (errors: number) => void): void {
6363

6464
// set options, add if it need to quit and run type
6565
let options: IInternalTypeCheckerOptions = Object.assign(this.options, { quit: true, type: TypecheckerRunType.async });
6666

67+
// set the worker callback
68+
this.workerCallback = callback;
69+
6770
// create thread
6871
this.createThread();
6972

@@ -237,15 +240,12 @@ export class TypeHelperClass {
237240
if (this.isWorkerInspectPreformed) {
238241

239242
// all well, lets preform printout
240-
this.worker.send({ type: WorkerCommand.printResult });
243+
this.worker.send({ type: WorkerCommand.printResult, hasCallback: this.workerCallback != null });
241244
} else {
242245
this.writeText('You can not run pront before you have inspected code first');
243246
}
244247
}
245248

246-
247-
248-
249249
/**
250250
* Creates thread/worker
251251
*
@@ -256,18 +256,22 @@ export class TypeHelperClass {
256256
this.worker = child.fork(path.join(__dirname, 'worker.js'), []);
257257

258258
// listen for worker messages
259-
this.worker.on('message', (err: any) => {
259+
this.worker.on('message', (msg: any) => {
260260

261-
if (err === 'error') {
261+
if (msg === 'error') {
262262

263263
// if error then exit
264264
this.writeText('error typechecker');
265265
process.exit(1);
266266
} else {
267267

268-
// if not error, then just kill worker
269-
this.writeText('killing worker');
270-
this.killWorker();
268+
if (this.workerCallback && msg && typeof msg === 'object' && msg.type === 'result') {
269+
this.workerCallback(msg.result);
270+
} else {
271+
// if not error, then just kill worker
272+
this.writeText('killing worker');
273+
this.killWorker();
274+
}
271275
}
272276
});
273277
}

0 commit comments

Comments
 (0)