|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import CoreEvents from "../../../dist/webext/types/core/events"; |
| 6 | +import Plugin from "../../plugins"; |
| 7 | + |
| 8 | +import { PingPayload } from "../pings/database"; |
| 9 | +import { JSONObject } from "../utils"; |
| 10 | + |
| 11 | +export class CoreEvent< |
| 12 | + // An array of arguments that the event will provide as context to the plugin action. |
| 13 | + Context extends unknown[] = unknown[], |
| 14 | + // The expected type of the action result. To be returned by the plugin. |
| 15 | + Result extends unknown = unknown |
| 16 | + > { |
| 17 | + // The plugin to be triggered eveytime this even occurs. |
| 18 | + private plugin?: Plugin<CoreEvent<Context, Result>>; |
| 19 | + |
| 20 | + constructor(readonly name: string) {} |
| 21 | + |
| 22 | + /** |
| 23 | + * Registers a plugin that listens to this event. |
| 24 | + * |
| 25 | + * @param plugin The plugin to register. |
| 26 | + */ |
| 27 | + registerPlugin(plugin: Plugin<CoreEvent<Context, Result>>): void { |
| 28 | + if (this.plugin) { |
| 29 | + console.error( |
| 30 | + `Attempted to register plugin '${plugin.name}', which listens to the event '${plugin.event}'.`, |
| 31 | + `That event is already watched by plugin '${this.plugin.name}'`, |
| 32 | + `Plugin '${plugin.name}' will be ignored.` |
| 33 | + ); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + this.plugin = plugin; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Deregisters the currently registered plugin. |
| 42 | + * |
| 43 | + * If no plugin is currently registered this is a no-op. |
| 44 | + */ |
| 45 | + deregisterPlugin(): void { |
| 46 | + this.plugin = undefined; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Triggers this event. |
| 51 | + * |
| 52 | + * Will execute the action of the registered plugin, if there is any. |
| 53 | + * |
| 54 | + * @param args The arguments to be passed as context to the registered plugin. |
| 55 | + * |
| 56 | + * @returns The result from the plugin execution. |
| 57 | + */ |
| 58 | + trigger(...args: Context): Result | void { |
| 59 | + if (this.plugin) { |
| 60 | + return this.plugin.action(...args); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Glean internal events. |
| 67 | + */ |
| 68 | +const CoreEvents: { |
| 69 | + afterPingCollection: CoreEvent<[PingPayload], Promise<JSONObject>>, |
| 70 | + [unused: string]: CoreEvent |
| 71 | +} = { |
| 72 | + // Event that is triggered immediatelly after a ping is collect and before it is recorded. |
| 73 | + // |
| 74 | + // - Context: The `PingPayload` of the recently collected ping. |
| 75 | + // - Result: The modified payload as a JSON object. |
| 76 | + afterPingCollection: new CoreEvent<[PingPayload], Promise<JSONObject>>("afterPingCollection") |
| 77 | +}; |
| 78 | + |
| 79 | +export default CoreEvents; |
0 commit comments