|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { Event, EventEmitter } from 'vscode'; |
| 5 | +import { areSameEnvironment, PythonEnvInfo, PythonEnvKind } from '../base/info'; |
| 6 | +import { |
| 7 | + ILocator, IPythonEnvsIterator, PythonEnvUpdatedEvent, QueryForEvent, |
| 8 | +} from '../base/locator'; |
| 9 | +import { PythonEnvsChangedEvent } from '../base/watcher'; |
| 10 | + |
| 11 | +type IteratorState = { |
| 12 | + /** |
| 13 | + * Set to true when all information from incoming iterator has been received |
| 14 | + */ |
| 15 | + done: boolean; |
| 16 | + /** |
| 17 | + * Carries the number of pending background calls ongoing at the moment |
| 18 | + */ |
| 19 | + pending: number; |
| 20 | +}; |
| 21 | + |
| 22 | +export class PythonEnvsReducer implements ILocator { |
| 23 | + public get onChanged(): Event<PythonEnvsChangedEvent> { |
| 24 | + return this.pythonEnvsManager.onChanged; |
| 25 | + } |
| 26 | + |
| 27 | + constructor(private readonly pythonEnvsManager: ILocator) {} |
| 28 | + |
| 29 | + public resolveEnv(env: string | PythonEnvInfo): Promise<PythonEnvInfo | undefined> { |
| 30 | + return this.pythonEnvsManager.resolveEnv(env); |
| 31 | + } |
| 32 | + |
| 33 | + public iterEnvs(query?: QueryForEvent<PythonEnvsChangedEvent>): IPythonEnvsIterator { |
| 34 | + const didUpdate = new EventEmitter<PythonEnvUpdatedEvent | null>(); |
| 35 | + const iterator: IPythonEnvsIterator = this.iterEnvsIterator(didUpdate, query); |
| 36 | + iterator.onUpdated = didUpdate.event; |
| 37 | + return iterator; |
| 38 | + } |
| 39 | + |
| 40 | + private async* iterEnvsIterator( |
| 41 | + didUpdate: EventEmitter<PythonEnvUpdatedEvent | null>, |
| 42 | + query?: QueryForEvent<PythonEnvsChangedEvent>, |
| 43 | + ): AsyncIterator<PythonEnvInfo, void> { |
| 44 | + const state = { |
| 45 | + done: false, |
| 46 | + pending: 0, |
| 47 | + }; |
| 48 | + const seen: PythonEnvInfo[] = []; |
| 49 | + const iterator = this.pythonEnvsManager.iterEnvs(query); |
| 50 | + |
| 51 | + if (iterator.onUpdated !== undefined) { |
| 52 | + iterator.onUpdated((event) => { |
| 53 | + if (event === null) { |
| 54 | + state.done = true; |
| 55 | + checkIfFinishedAndNotify(state, didUpdate); |
| 56 | + } else { |
| 57 | + const old = seen.find((s) => areSameEnvironment(s, event.old)); |
| 58 | + if (old !== undefined) { |
| 59 | + state.pending += 1; |
| 60 | + resolveDifferencesInBackground(old, event.new, state, didUpdate).ignoreErrors(); |
| 61 | + } |
| 62 | + } |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + let result = await iterator.next(); |
| 67 | + while (!result.done) { |
| 68 | + const currEnv = result.value; |
| 69 | + const old = seen.find((s) => areSameEnvironment(s, currEnv)); |
| 70 | + if (old !== undefined) { |
| 71 | + state.pending += 1; |
| 72 | + resolveDifferencesInBackground(old, currEnv, state, didUpdate).ignoreErrors(); |
| 73 | + } else { |
| 74 | + yield currEnv; |
| 75 | + seen.push(currEnv); |
| 76 | + } |
| 77 | + // eslint-disable-next-line no-await-in-loop |
| 78 | + result = await iterator.next(); |
| 79 | + } |
| 80 | + if (iterator.onUpdated === undefined) { |
| 81 | + state.done = true; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +async function resolveDifferencesInBackground( |
| 87 | + oldEnv: PythonEnvInfo, |
| 88 | + newEnv: PythonEnvInfo, |
| 89 | + state: IteratorState, |
| 90 | + didUpdate: EventEmitter<PythonEnvUpdatedEvent | null>, |
| 91 | +) { |
| 92 | + const merged = mergeEnvironments(oldEnv, newEnv); |
| 93 | + didUpdate.fire({ old: oldEnv, new: merged }); |
| 94 | + state.pending -= 1; |
| 95 | + checkIfFinishedAndNotify(state, didUpdate); |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * When all info from incoming iterator has been received and all background calls finishes, notify that we're done |
| 100 | + * @param state Carries the current state of progress |
| 101 | + * @param didUpdate Used to notify when finished |
| 102 | + */ |
| 103 | +function checkIfFinishedAndNotify( |
| 104 | + state: IteratorState, |
| 105 | + didUpdate: EventEmitter<PythonEnvUpdatedEvent | null>, |
| 106 | +) { |
| 107 | + if (state.done && state.pending === 0) { |
| 108 | + didUpdate.fire(null); |
| 109 | + didUpdate.dispose(); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +export function mergeEnvironments(environment: PythonEnvInfo, other: PythonEnvInfo): PythonEnvInfo { |
| 114 | + // Preserve type information. |
| 115 | + // Possible we identified environment as unknown, but a later provider has identified env type. |
| 116 | + if (environment.kind === PythonEnvKind.Unknown && other.kind && other.kind !== PythonEnvKind.Unknown) { |
| 117 | + environment.kind = other.kind; |
| 118 | + } |
| 119 | + const props: (keyof PythonEnvInfo)[] = [ |
| 120 | + 'version', 'kind', 'executable', 'name', 'arch', 'distro', 'defaultDisplayName', 'searchLocation', |
| 121 | + ]; |
| 122 | + props.forEach((prop) => { |
| 123 | + if (!environment[prop] && other[prop]) { |
| 124 | + // tslint:disable: no-any |
| 125 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 126 | + (environment as any)[prop] = other[prop]; |
| 127 | + } |
| 128 | + }); |
| 129 | + return environment; |
| 130 | +} |
0 commit comments