Skip to content

Commit

Permalink
watcher interface simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jun 23, 2018
1 parent 95f83a0 commit f2508d8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/Graph.ts
Expand Up @@ -2,6 +2,7 @@ import * as acorn from 'acorn';
import injectDynamicImportPlugin from 'acorn-dynamic-import/lib/inject';
import injectImportMeta from 'acorn-import-meta/inject';
import { Program } from 'estree';
import { EventEmitter } from 'events';
import GlobalScope from './ast/scopes/GlobalScope';
import { EntityPathTracker } from './ast/utils/EntityPathTracker';
import GlobalVariable from './ast/variables/GlobalVariable';
Expand All @@ -22,8 +23,7 @@ import {
RollupWarning,
SourceDescription,
TreeshakingOptions,
WarningHandler,
Watcher
WarningHandler
} from './rollup/types';
import { Asset, createAssetPluginHooks, finaliseAsset } from './utils/assetHooks';
import { load, makeOnwarn, resolveId } from './utils/defaults';
Expand Down Expand Up @@ -68,7 +68,7 @@ export default class Graph {
// deprecated
treeshake: boolean;

constructor(options: InputOptions, watcher?: Watcher) {
constructor(options: InputOptions, watcher?: EventEmitter) {
this.curChunkIndex = 0;
this.reassignmentTracker = new EntityPathTracker();
this.cachedModules = new Map();
Expand Down
6 changes: 3 additions & 3 deletions src/rollup/index.ts
@@ -1,3 +1,4 @@
import { EventEmitter } from 'events';
import { optimizeChunks } from '../chunk-optimization';
import Graph from '../Graph';
import { createAddons } from '../utils/addons';
Expand All @@ -12,7 +13,6 @@ import mergeOptions, { GenericConfigObject } from '../utils/mergeOptions';
import { basename, dirname, resolve } from '../utils/path';
import { SOURCEMAPPING_URL } from '../utils/sourceMappingURL';
import { getTimings, initialiseTimers, timeEnd, timeStart } from '../utils/timers';
import { Watcher } from '../watch';
import {
InputOptions,
OutputAsset,
Expand Down Expand Up @@ -144,8 +144,8 @@ function getInputOptions(rawInputOptions: GenericConfigObject): any {
return inputOptions;
}

let curWatcher: Watcher;
export function setWatcher(watcher: Watcher) {
let curWatcher: EventEmitter;
export function setWatcher(watcher: EventEmitter) {
curWatcher = watcher;
}

Expand Down
10 changes: 6 additions & 4 deletions src/rollup/types.d.ts
Expand Up @@ -66,7 +66,7 @@ export interface ModuleJSON {
}

export interface PluginContext {
watcher: Watcher;
watcher: EventEmitter;
resolveId: ResolveIdHook;
isExternal: IsExternal;
parse: (input: string, options: any) => ESTree.Program;
Expand Down Expand Up @@ -351,8 +351,6 @@ export interface RollupOptions extends InputOptions {

export function rollup(options: RollupOptions): Promise<RollupBuild>;

export interface Watcher extends EventEmitter {}

// chokidar watch options
export interface WatchOptions {
persistent?: boolean;
Expand Down Expand Up @@ -389,4 +387,8 @@ export interface RollupWatchOptions extends InputOptions {
watch?: WatcherOptions;
}

export function watch(configs: RollupWatchOptions[]): Watcher;
export interface RollupWatcher extends EventEmitter {
close(): void;
}

export function watch(configs: RollupWatchOptions[]): RollupWatcher;
28 changes: 19 additions & 9 deletions src/watch/index.ts
Expand Up @@ -6,10 +6,9 @@ import rollup, { setWatcher } from '../rollup/index';
import {
InputOptions,
ModuleJSON,
OutputChunk,
OutputOptions,
RollupBuild,
RollupSingleFileBuild,
RollupWatcher,
RollupWatchOptions
} from '../rollup/types';
import ensureArray from '../utils/ensureArray';
Expand All @@ -20,27 +19,38 @@ import { addTask, deleteTask } from './fileWatchers';

const DELAY = 200;

export class Watcher extends EventEmitter {
export class Watcher {
emitter: RollupWatcher;
private buildTimeout: NodeJS.Timer;
private running: boolean;
private rerun: boolean = false;
private tasks: Task[];
private succeeded: boolean = false;

constructor(configs: RollupWatchOptions[]) {
super();
this.emitter = new class extends EventEmitter implements RollupWatcher {
close: () => void;
constructor(close: () => void) {
super();
this.close = close;
}
}(this.close.bind(this));
this.tasks = ensureArray(configs).map(config => new Task(this, config));
this.running = true;
process.nextTick(() => this.run());
}

emit(event: string, value: any) {
this.emitter.emit(event, value);
}

close() {
if (this.buildTimeout) clearTimeout(this.buildTimeout);
this.tasks.forEach(task => {
task.close();
});

this.removeAllListeners();
this.emitter.removeAllListeners();
}

invalidate() {
Expand Down Expand Up @@ -194,7 +204,7 @@ export class Task {
});
}

setWatcher(this.watcher);
setWatcher(this.watcher.emitter);
return rollup(options)
.then(result => {
if (this.closed) return;
Expand All @@ -218,11 +228,11 @@ export class Task {

return Promise.all(
this.outputs.map(output => {
return <Promise<OutputChunk | Record<string, OutputChunk>>>result.write(output);
return result.write(output);
})
).then(() => result);
})
.then((result: RollupSingleFileBuild | RollupBuild) => {
.then((result: RollupBuild) => {
this.watcher.emit('event', {
code: 'BUNDLE_END',
input: this.inputOptions.input,
Expand Down Expand Up @@ -266,5 +276,5 @@ export class Task {
}

export default function watch(configs: RollupWatchOptions[]) {
return new Watcher(configs);
return new Watcher(configs).emitter;
}

0 comments on commit f2508d8

Please sign in to comment.