-
Notifications
You must be signed in to change notification settings - Fork 34
[ALTERNATE] Bug 1689547 - Create plugin / events infrastructure and define afterPingCollection event
#95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[ALTERNATE] Bug 1689547 - Create plugin / events infrastructure and define afterPingCollection event
#95
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0dbba26
Fix identation in logPings function
2e5881e
Fix testReset and testInitialize function signatures
2886705
Create plugin and event infra and define afterPingCollection event
d2f7348
Trigger the afterPingCollection event at the expected time
6c1a7fe
Fix lints
b26eb66
Remove incorrect import
bd959af
Fix more lints
da6e745
instrument -> listen to
acc52cc
Add tests for core events
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,78 @@ | ||
| /* 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 Plugin from "../../plugins"; | ||
|
|
||
| import { PingPayload } from "../pings/database"; | ||
| import { JSONObject } from "../utils"; | ||
|
|
||
| export class CoreEvent< | ||
| // An array of arguments that the event will provide as context to the plugin action. | ||
| Context extends unknown[] = unknown[], | ||
| // The expected type of the action result. To be returned by the plugin. | ||
| Result extends unknown = unknown | ||
| > { | ||
| // The plugin to be triggered eveytime this even occurs. | ||
| private plugin?: Plugin<CoreEvent<Context, Result>>; | ||
|
|
||
| constructor(readonly name: string) {} | ||
|
|
||
| /** | ||
| * Registers a plugin that listens to this event. | ||
| * | ||
| * @param plugin The plugin to register. | ||
| */ | ||
| registerPlugin(plugin: Plugin<CoreEvent<Context, Result>>): void { | ||
| if (this.plugin) { | ||
| console.error( | ||
| `Attempted to register plugin '${plugin.name}', which listens to the event '${plugin.event}'.`, | ||
| `That event is already watched by plugin '${this.plugin.name}'`, | ||
| `Plugin '${plugin.name}' will be ignored.` | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| this.plugin = plugin; | ||
| } | ||
|
|
||
| /** | ||
| * Deregisters the currently registered plugin. | ||
| * | ||
| * If no plugin is currently registered this is a no-op. | ||
| */ | ||
| deregisterPlugin(): void { | ||
| this.plugin = undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Triggers this event. | ||
| * | ||
| * Will execute the action of the registered plugin, if there is any. | ||
| * | ||
| * @param args The arguments to be passed as context to the registered plugin. | ||
| * | ||
| * @returns The result from the plugin execution. | ||
| */ | ||
| trigger(...args: Context): Result | void { | ||
| if (this.plugin) { | ||
| return this.plugin.action(...args); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Glean internal events. | ||
| */ | ||
| const CoreEvents: { | ||
| afterPingCollection: CoreEvent<[PingPayload], Promise<JSONObject>>, | ||
| [unused: string]: CoreEvent | ||
| } = { | ||
| // Event that is triggered immediatelly after a ping is collect and before it is recorded. | ||
| // | ||
| // - Context: The `PingPayload` of the recently collected ping. | ||
| // - Result: The modified payload as a JSON object. | ||
| afterPingCollection: new CoreEvent<[PingPayload], Promise<JSONObject>>("afterPingCollection") | ||
| }; | ||
|
|
||
| export default CoreEvents; | ||
This file contains hidden or 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,38 @@ | ||
| /* 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 CoreEvents, { CoreEvent } from "./index"; | ||
| import Plugin from "../../plugins"; | ||
|
|
||
| /** | ||
| * Registers a plugin to the desired Glean event. | ||
| * | ||
| * If the plugin is attempting to listen to an unknown event it will be ignored. | ||
| * | ||
| * @param plugin The plugin to register. | ||
| */ | ||
| export function registerPluginToEvent<E extends CoreEvent>(plugin: Plugin<E>): void { | ||
| const eventName = plugin.event; | ||
| if (eventName in CoreEvents) { | ||
| const event = CoreEvents[eventName]; | ||
| event.registerPlugin(plugin); | ||
| return; | ||
| } | ||
|
|
||
| console.error( | ||
| `Attempted to register plugin '${plugin.name}', which listens to the event '${plugin.event}'.`, | ||
| "That is not a valid Glean event. Ignoring" | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * **Test-only API** | ||
| * | ||
| * Deregister plugins registered to all Glean events. | ||
| */ | ||
| export function testResetEvents(): void { | ||
| for (const event in CoreEvents) { | ||
| CoreEvents[event].deregisterPlugin(); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,39 @@ | ||
| /* 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 { CoreEvent } from "../core/events"; | ||
|
|
||
| // Helper type that extracts the type of the Context from a generic CoreEvent. | ||
| export type EventContext<Context> = Context extends CoreEvent<infer InnerContext, unknown> | ||
| ? InnerContext | ||
| : undefined[]; | ||
|
|
||
| // Helper type that extracts the type of the Result from a generic CoreEvent. | ||
| export type EventResult<Result> = Result extends CoreEvent<unknown[], infer InnerResult> | ||
| ? InnerResult | ||
| : void; | ||
|
|
||
| /** | ||
| * Plugins can listen to events that happen during Glean's lifecycle. | ||
| * | ||
| * Every Glean plugin must extend this class. | ||
| */ | ||
| abstract class Plugin<E extends CoreEvent = CoreEvent> { | ||
| /** | ||
| * Instantiates the Glean plugin. | ||
| * | ||
| * @param event The name of the even this plugin listens to. | ||
| * @param name The name of this plugin. | ||
| */ | ||
| constructor(readonly event: string, readonly name: string) {} | ||
|
|
||
| /** | ||
| * An action that will be triggered everytime the listened to event occurs. | ||
| * | ||
| * @param args The arguments that are expected to be passed by this event. | ||
| */ | ||
| abstract action(...args: EventContext<E>): EventResult<E>; | ||
| } | ||
|
|
||
| export default Plugin; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.