|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +// tslint:disable:max-classes-per-file |
| 5 | + |
| 6 | +import { Event, EventEmitter, Uri } from 'vscode'; |
| 7 | +import { PythonEnvKind } from './info'; |
| 8 | + |
| 9 | +/** |
| 10 | + * The most basic info for a Python environments event. |
| 11 | + * |
| 12 | + * @prop kind - the env kind, if any, affected by the event |
| 13 | + */ |
| 14 | +export type BasicPythonEnvsChangedEvent = { |
| 15 | + kind?: PythonEnvKind; |
| 16 | +}; |
| 17 | + |
| 18 | +/** |
| 19 | + * The full set of possible info for a Python environments event. |
| 20 | + * |
| 21 | + * @prop searchLocation - the location, if any, affected by the event |
| 22 | + */ |
| 23 | +export type PythonEnvsChangedEvent = BasicPythonEnvsChangedEvent & { |
| 24 | + searchLocation?: Uri; |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | + * A "watcher" for events related to changes to Python environemts. |
| 29 | + * |
| 30 | + * The watcher will notify listeners (callbacks registered through |
| 31 | + * `onChanged`) of events at undetermined times. The actual emitted |
| 32 | + * events, their source, and the timing is entirely up to the watcher |
| 33 | + * implementation. |
| 34 | + */ |
| 35 | +export interface IPythonEnvsWatcher<E extends BasicPythonEnvsChangedEvent = PythonEnvsChangedEvent> { |
| 36 | + /** |
| 37 | + * The hook for registering event listeners (callbacks). |
| 38 | + */ |
| 39 | + readonly onChanged: Event<E>; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * This provides the fundamental functionality of a watcher for any event type. |
| 44 | + * |
| 45 | + * Consumers register listeners (callbacks) using `onChanged`. Each |
| 46 | + * listener is invoked when `fire()` is called. |
| 47 | + * |
| 48 | + * Note that in most cases classes will not inherit from this classes, |
| 49 | + * but instead keep a private watcher property. The rule of thumb |
| 50 | + * is to follow whether or not consumers of *that* class should be able |
| 51 | + * to trigger events (via `fire()`). |
| 52 | + */ |
| 53 | +class WatcherBase<T> { |
| 54 | + /** |
| 55 | + * The hook for registering event listeners (callbacks). |
| 56 | + */ |
| 57 | + public readonly onChanged: Event<T>; |
| 58 | + private readonly didChange = new EventEmitter<T>(); |
| 59 | + |
| 60 | + constructor() { |
| 61 | + this.onChanged = this.didChange.event; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Send the event to all registered listeners. |
| 66 | + */ |
| 67 | + public fire(event: T) { |
| 68 | + this.didChange.fire(event); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * A watcher for the basic Python environments events. |
| 74 | + * |
| 75 | + * The same rule-of-thumb for subclassing `WatcherBase` applies here. |
| 76 | + */ |
| 77 | +export class BasicPythonEnvsWatcher extends WatcherBase<BasicPythonEnvsChangedEvent> { |
| 78 | + /** |
| 79 | + * Fire an event based on the given info. |
| 80 | + */ |
| 81 | + public trigger(kind?: PythonEnvKind) { |
| 82 | + this.fire({ kind }); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * A general-use watcher for Python environments events. |
| 88 | + * |
| 89 | + * The same rule-of-thumb for subclassing `WatcherBase` applies here. |
| 90 | + */ |
| 91 | +export class PythonEnvsWatcher extends WatcherBase<PythonEnvsChangedEvent> { |
| 92 | + /** |
| 93 | + * Fire an event based on the given info. |
| 94 | + */ |
| 95 | + public trigger(kind?: PythonEnvKind, searchLocation?: Uri) { |
| 96 | + this.fire({ kind, searchLocation }); |
| 97 | + } |
| 98 | +} |
0 commit comments