From a7273447d758f170917c58b80503d308c0381c93 Mon Sep 17 00:00:00 2001 From: Don Jayamanne Date: Tue, 31 Oct 2023 00:53:03 +1100 Subject: [PATCH] Remove rxjs from widgetManager (#14615) --- package-lock.json | 11 +++++++++++ package.json | 1 + .../webview-side/ipywidgets/kernel/index.ts | 7 +++++-- .../webview-side/ipywidgets/kernel/manager.ts | 16 +++++++++------- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1f3ed6bc590..faacd9b4ebc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "hasInstallScript": true, "license": "MIT", "dependencies": { + "@c4312/evt": "^0.1.1", "@enonic/fnv-plus": "^1.3.0", "@fluentui/react": "^7.160.1", "@jupyter-widgets/base": "4.0.0", @@ -813,6 +814,11 @@ "node": ">=6.9.0" } }, + "node_modules/@c4312/evt": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/@c4312/evt/-/evt-0.1.1.tgz", + "integrity": "sha512-8EW+r3o2mothQKzNuBUGm8MTysihexfBCp1wvw3O6bgA2q7YHNlT104+LOFH1HS/PRy8owzkvZRLbPx4modJoQ==" + }, "node_modules/@csstools/cascade-layer-name-parser": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/@csstools/cascade-layer-name-parser/-/cascade-layer-name-parser-1.0.2.tgz", @@ -24420,6 +24426,11 @@ "to-fast-properties": "^2.0.0" } }, + "@c4312/evt": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/@c4312/evt/-/evt-0.1.1.tgz", + "integrity": "sha512-8EW+r3o2mothQKzNuBUGm8MTysihexfBCp1wvw3O6bgA2q7YHNlT104+LOFH1HS/PRy8owzkvZRLbPx4modJoQ==" + }, "@csstools/cascade-layer-name-parser": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/@csstools/cascade-layer-name-parser/-/cascade-layer-name-parser-1.0.2.tgz", diff --git a/package.json b/package.json index b867458943b..c8389046f48 100644 --- a/package.json +++ b/package.json @@ -2119,6 +2119,7 @@ "webpack-analyze:web": "npx webpack --json --config build/webpack/webpack.extension.web.config.js > out/stats-web.json && yarn webpack-bundle-analyzer out/stats-web.json" }, "dependencies": { + "@c4312/evt": "^0.1.1", "@enonic/fnv-plus": "^1.3.0", "@fluentui/react": "^7.160.1", "@jupyter-widgets/base": "4.0.0", diff --git a/src/webviews/webview-side/ipywidgets/kernel/index.ts b/src/webviews/webview-side/ipywidgets/kernel/index.ts index bb3a182377b..4389da2ec63 100644 --- a/src/webviews/webview-side/ipywidgets/kernel/index.ts +++ b/src/webviews/webview-side/ipywidgets/kernel/index.ts @@ -223,7 +223,8 @@ let widgetManagerPromise: Promise | undefined; async function getWidgetManager(): Promise { if (!widgetManagerPromise) { function reInitializeWidgetManager(resolve?: (value: WidgetManager) => void) { - WidgetManager.instance.subscribe((wm) => { + function initializeInstance() { + const wm = WidgetManager.instance; if (wm) { const oldDispose = wm.dispose.bind(wm); wm.dispose = () => { @@ -237,7 +238,9 @@ async function getWidgetManager(): Promise { } widgetManagerPromise = Promise.resolve(wm); } - }); + } + initializeInstance(); + WidgetManager.onDidChangeInstance(initializeInstance); } // eslint-disable-next-line , @typescript-eslint/no-explicit-any widgetManagerPromise = new Promise((resolve) => reInitializeWidgetManager(resolve as any)); diff --git a/src/webviews/webview-side/ipywidgets/kernel/manager.ts b/src/webviews/webview-side/ipywidgets/kernel/manager.ts index ffa1d632201..5ee6d48509b 100644 --- a/src/webviews/webview-side/ipywidgets/kernel/manager.ts +++ b/src/webviews/webview-side/ipywidgets/kernel/manager.ts @@ -7,8 +7,7 @@ import type { Kernel, KernelMessage } from '@jupyterlab/services'; import type * as nbformat from '@jupyterlab/nbformat'; import { Widget } from '@lumino/widgets'; import fastDeepEqual from 'fast-deep-equal'; -import { Observable } from 'rxjs/Observable'; -import { ReplaySubject } from 'rxjs/ReplaySubject'; +import { EventEmitter, Event } from '@c4312/evt'; import { logMessage, setLogger } from '../../react-common/logger'; import { IMessageHandler, PostOffice } from '../../react-common/postOffice'; import { create as createKernel } from './kernel'; @@ -29,10 +28,11 @@ import { noop } from '../../../../platform/common/utils/misc'; /* eslint-disable @typescript-eslint/no-explicit-any */ export class WidgetManager implements IIPyWidgetManager, IMessageHandler { - public static get instance(): Observable { - return WidgetManager._instance; + public static get onDidChangeInstance(): Event { + return WidgetManager._onDidChangeInstance.event; } - private static _instance = new ReplaySubject(); + private static _onDidChangeInstance = new EventEmitter(); + public static instance: WidgetManager | undefined; private manager?: IJupyterLabWidgetManager; private proxyKernel?: Kernel.IKernelConnection; private options?: KernelSocketOptions; @@ -105,7 +105,8 @@ export class WidgetManager implements IIPyWidgetManager, IMessageHandler { this.manager = undefined; this.proxyKernel?.dispose(); // NOSONAR this.proxyKernel = undefined; - WidgetManager._instance.next(undefined); + WidgetManager.instance = undefined; + WidgetManager._onDidChangeInstance.fire(undefined); } else if (!this.proxyKernel) { logMessage(`Received some pending message ${message}`); this.pendingMessages.push({ message, payload }); @@ -237,7 +238,8 @@ export class WidgetManager implements IIPyWidgetManager, IMessageHandler { this.manager.onUnhandledIOPubMessage.connect(this.handleUnhandledIOPubMessage.bind(this)); // Tell the observable about our new manager - WidgetManager._instance.next(this); + WidgetManager.instance = this; + WidgetManager._onDidChangeInstance.fire(this); } catch (ex) { // eslint-disable-next-line no-console console.error('Failed to initialize WidgetManager', ex);