Skip to content

Commit

Permalink
Cache JestHook emitters (#8746)
Browse files Browse the repository at this point in the history
  • Loading branch information
Connormiha authored and SimenB committed Aug 3, 2019
1 parent 2cd7576 commit cff67fd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -49,6 +49,8 @@

### Performance

- `[jest-watcher]` Minor optimization for JestHook ([#8746](https://github.com/facebook/jest/pull/8746)

## 24.8.0

### Features
Expand Down
27 changes: 17 additions & 10 deletions packages/jest-watcher/src/JestHooks.ts
Expand Up @@ -25,20 +25,17 @@ class JestHooks {
shouldRunTestSuite: Array<ShouldRunTestSuite>;
};

private _subscriber: JestHookSubscriber;
private _emitter: JestHookEmitter;

constructor() {
this._listeners = {
onFileChange: [],
onTestRunComplete: [],
shouldRunTestSuite: [],
};
}

isUsed(hook: AvailableHooks) {
return this._listeners[hook] && this._listeners[hook].length;
}

getSubscriber(): JestHookSubscriber {
return {
this._subscriber = {
onFileChange: fn => {
this._listeners.onFileChange.push(fn);
},
Expand All @@ -49,10 +46,8 @@ class JestHooks {
this._listeners.shouldRunTestSuite.push(fn);
},
};
}

getEmitter(): JestHookEmitter {
return {
this._emitter = {
onFileChange: fs =>
this._listeners.onFileChange.forEach(listener => listener(fs)),
onTestRunComplete: results =>
Expand All @@ -70,6 +65,18 @@ class JestHooks {
},
};
}

isUsed(hook: AvailableHooks) {
return this._listeners[hook] && this._listeners[hook].length;
}

getSubscriber(): Readonly<JestHookSubscriber> {
return this._subscriber;
}

getEmitter(): Readonly<JestHookEmitter> {
return this._emitter;
}
}

export default JestHooks;

0 comments on commit cff67fd

Please sign in to comment.