Skip to content

Commit 3e137f2

Browse files
authored
Don't call acquireVsCodeApi more than once (#14543)
* Use globalThis * Make this.vscodeApi accessible and pass it through
1 parent 5fd5759 commit 3e137f2

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

src/datascience-ui/interactive-common/redux/store.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ function getDebugState(vms: ICellViewModel[]): DebugState {
293293
return firstNonDesign ? firstNonDesign.runningByLine : DebugState.Design;
294294
}
295295

296-
function createMiddleWare(testMode: boolean): Redux.Middleware<{}, IStore>[] {
296+
function createMiddleWare(testMode: boolean, postOffice: PostOffice): Redux.Middleware<{}, IStore>[] {
297297
// Create the middleware that modifies actions to queue new actions
298298
const queueableActions = createQueueableActionMiddleware();
299299

@@ -304,8 +304,7 @@ function createMiddleWare(testMode: boolean): Redux.Middleware<{}, IStore>[] {
304304
// Create the test middle ware. It sends messages that are used for testing only
305305
// Or if testing in UI Test.
306306
// tslint:disable-next-line: no-any
307-
const acquireVsCodeApi = (window as any).acquireVsCodeApi as Function;
308-
const isUITest = acquireVsCodeApi && acquireVsCodeApi().handleMessage ? true : false;
307+
const isUITest = postOffice.vscodeApi && (postOffice.vscodeApi as any).handleMessage ? true : false;
309308
const testMiddleware = testMode || isUITest ? createTestMiddleware() : undefined;
310309

311310
// Create the logger if we're not in production mode or we're forcing logging
@@ -423,7 +422,7 @@ export function createStore<M>(
423422
});
424423

425424
// Create our middleware
426-
const middleware = createMiddleWare(testMode).concat([addMessageDirectionMiddleware]);
425+
const middleware = createMiddleWare(testMode, postOffice).concat([addMessageDirectionMiddleware]);
427426

428427
// Use this reducer and middle ware to create a store
429428
const store = Redux.createStore(rootReducer, Redux.applyMiddleware(...middleware));

src/datascience-ui/react-common/postOffice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export declare function acquireVsCodeApi(): IVsCodeApi;
2929
export type PostOfficeMessage = { type: string; payload?: any };
3030
// tslint:disable-next-line: no-unnecessary-class
3131
export class PostOffice implements IDisposable {
32+
public vscodeApi: IVsCodeApi | undefined;
3233
private registered: boolean = false;
33-
private vscodeApi: IVsCodeApi | undefined;
3434
private handlers: IMessageHandler[] = [];
3535
private baseHandler = this.handleMessages.bind(this);
3636
private readonly subject = new Subject<PostOfficeMessage>();

0 commit comments

Comments
 (0)