-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add event bus + a few events emitted from TaskGraph
- Loading branch information
Showing
10 changed files
with
389 additions
and
151 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (C) 2018 Garden Technologies, Inc. <info@garden.io> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import { EventEmitter2 } from "eventemitter2" | ||
import { TaskResult } from "./task-graph" | ||
import { ModuleVersion } from "./vcs/base" | ||
|
||
/** | ||
* This simple class serves as the central event bus for a Garden instance. Its function | ||
* is mainly to consolidate all events for the instance, to ensure type-safety. | ||
* | ||
* See below for the event interfaces. | ||
*/ | ||
export class EventBus extends EventEmitter2 { | ||
constructor() { | ||
super({ | ||
wildcard: false, | ||
newListener: false, | ||
maxListeners: 100, // we may need to adjust this | ||
}) | ||
} | ||
|
||
emit<T extends EventName>(name: T, payload: Events[T]) { | ||
return super.emit(name, payload) | ||
} | ||
|
||
on<T extends EventName>(name: T, listener: (payload: Events[T]) => void) { | ||
return super.on(name, listener) | ||
} | ||
|
||
onAny(listener: <T extends EventName>(name: T, payload: Events[T]) => void) { | ||
return super.onAny(<any>listener) | ||
} | ||
|
||
once<T extends EventName>(name: T, listener: (payload: Events[T]) => void) { | ||
return super.once(name, listener) | ||
} | ||
|
||
// TODO: wrap more methods to make them type-safe | ||
} | ||
|
||
/** | ||
* The supported events and their interfaces. | ||
*/ | ||
export interface Events { | ||
_test: string, | ||
taskPending: { | ||
addedAt: Date, | ||
key: string, | ||
version: ModuleVersion, | ||
}, | ||
taskComplete: TaskResult, | ||
taskError: TaskResult, | ||
} | ||
|
||
export type EventName = keyof Events |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright (C) 2018 Garden Technologies, Inc. <info@garden.io> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import { EventBus } from "../../src/events" | ||
import { expect } from "chai" | ||
|
||
describe("EventBus", () => { | ||
let events: EventBus | ||
|
||
beforeEach(() => { | ||
events = new EventBus() | ||
}) | ||
|
||
it("should send+receive events", (done) => { | ||
events.on("_test", (payload) => { | ||
expect(payload).to.equal("foo") | ||
done() | ||
}) | ||
events.emit("_test", "foo") | ||
}) | ||
|
||
describe("onAny", () => { | ||
it("should add listener for any supported event", (done) => { | ||
events.onAny((name, payload) => { | ||
expect(name).to.equal("_test") | ||
expect(payload).to.equal("foo") | ||
done() | ||
}) | ||
events.emit("_test", "foo") | ||
}) | ||
}) | ||
|
||
describe("once", () => { | ||
it("should add a listener that only gets called once", (done) => { | ||
events.once("_test", (payload) => { | ||
expect(payload).to.equal("foo") | ||
done() | ||
}) | ||
events.emit("_test", "foo") | ||
events.emit("_test", "bar") | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.