Skip to content

Commit bb93c9e

Browse files
authored
Merge pull request #843 from mathuo/842-api-init-bug
bug: setup onDidAcitvePanelChange subscription quicker
2 parents ca09ae5 + c6dcef5 commit bb93c9e

File tree

3 files changed

+55
-23
lines changed

3 files changed

+55
-23
lines changed

packages/dockview-core/src/__tests__/dockview/dockviewGroupPanel.spec.ts

+47-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { DockviewComponent } from '../../dockview/dockviewComponent';
22
import { DockviewGroupPanel } from '../../dockview/dockviewGroupPanel';
33
import { fromPartial } from '@total-typescript/shoehorn';
44
import { GroupOptions } from '../../dockview/dockviewGroupPanelModel';
5-
import { DockviewPanel } from '../../dockview/dockviewPanel';
5+
import { DockviewPanel, IDockviewPanel } from '../../dockview/dockviewPanel';
66
import { DockviewPanelModelMock } from '../__mocks__/mockDockviewPanelModel';
77
import { IContentRenderer, ITabRenderer } from '../../dockview/types';
88
import { OverlayRenderContainer } from '../../overlay/overlayRenderContainer';
9+
import { IDockviewPanelModel } from '../../dockview/dockviewPanelModel';
10+
import { ContentContainer } from '../../dockview/components/panel/content';
911

1012
describe('dockviewGroupPanel', () => {
1113
test('default minimum/maximium width/height', () => {
@@ -24,6 +26,50 @@ describe('dockviewGroupPanel', () => {
2426
expect(cut.maximumWidth).toBe(Number.MAX_SAFE_INTEGER);
2527
});
2628

29+
test('that onDidActivePanelChange is configured at inline', () => {
30+
const accessor = fromPartial<DockviewComponent>({
31+
onDidActivePanelChange: jest.fn(),
32+
onDidAddPanel: jest.fn(),
33+
onDidRemovePanel: jest.fn(),
34+
options: {},
35+
api: {},
36+
renderer: 'always',
37+
overlayRenderContainer: {
38+
attach: jest.fn(),
39+
detatch: jest.fn(),
40+
},
41+
doSetGroupActive: jest.fn(),
42+
});
43+
const options = fromPartial<GroupOptions>({});
44+
45+
const cut = new DockviewGroupPanel(accessor, 'test_id', options);
46+
47+
let counter = 0;
48+
49+
cut.api.onDidActivePanelChange((event) => {
50+
counter++;
51+
});
52+
53+
cut.model.openPanel(
54+
fromPartial<IDockviewPanel>({
55+
updateParentGroup: jest.fn(),
56+
view: {
57+
tab: { element: document.createElement('div') },
58+
content: new ContentContainer(accessor, cut.model),
59+
},
60+
api: {
61+
renderer: 'onlyWhenVisible',
62+
onDidTitleChange: jest.fn(),
63+
onDidParametersChange: jest.fn(),
64+
},
65+
layout: jest.fn(),
66+
runEvents: jest.fn(),
67+
})
68+
);
69+
70+
expect(counter).toBe(1);
71+
});
72+
2773
test('group constraints', () => {
2874
const accessor = fromPartial<DockviewComponent>({
2975
onDidActivePanelChange: jest.fn(),

packages/dockview-core/src/api/dockviewGroupPanelApi.ts

+2-22
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
DockviewGroupLocation,
77
} from '../dockview/dockviewGroupPanelModel';
88
import { Emitter, Event } from '../events';
9-
import { MutableDisposable } from '../lifecycle';
109
import { GridviewPanelApi, GridviewPanelApiImpl } from './gridviewPanelApi';
1110

1211
export interface DockviewGroupMoveParams {
@@ -41,17 +40,14 @@ const NOT_INITIALIZED_MESSAGE =
4140
'dockview: DockviewGroupPanelApiImpl not initialized';
4241

4342
export class DockviewGroupPanelApiImpl extends GridviewPanelApiImpl {
44-
private readonly _mutableDisposable = new MutableDisposable();
45-
4643
private _group: DockviewGroupPanel | undefined;
4744

4845
readonly _onDidLocationChange =
4946
new Emitter<DockviewGroupPanelFloatingChangeEvent>();
5047
readonly onDidLocationChange: Event<DockviewGroupPanelFloatingChangeEvent> =
5148
this._onDidLocationChange.event;
5249

53-
private readonly _onDidActivePanelChange =
54-
new Emitter<DockviewGroupChangeEvent>();
50+
readonly _onDidActivePanelChange = new Emitter<DockviewGroupChangeEvent>();
5551
readonly onDidActivePanelChange = this._onDidActivePanelChange.event;
5652

5753
get location(): DockviewGroupLocation {
@@ -66,8 +62,7 @@ export class DockviewGroupPanelApiImpl extends GridviewPanelApiImpl {
6662

6763
this.addDisposables(
6864
this._onDidLocationChange,
69-
this._onDidActivePanelChange,
70-
this._mutableDisposable
65+
this._onDidActivePanelChange
7166
);
7267
}
7368

@@ -140,21 +135,6 @@ export class DockviewGroupPanelApiImpl extends GridviewPanelApiImpl {
140135
}
141136

142137
initialize(group: DockviewGroupPanel): void {
143-
/**
144-
* TODO: Annoying initialization order caveat, find a better way to initialize and avoid needing null checks
145-
*
146-
* Due to the order on initialization we know that the model isn't defined until later in the same stack-frame of setup.
147-
* By queuing a microtask we can ensure the setup is completed within the same stack-frame, but after everything else has
148-
* finished ensuring the `model` is defined.
149-
*/
150-
151138
this._group = group;
152-
153-
queueMicrotask(() => {
154-
this._mutableDisposable.value =
155-
this._group!.model.onDidActivePanelChange((event) => {
156-
this._onDidActivePanelChange.fire(event);
157-
});
158-
});
159139
}
160140
}

packages/dockview-core/src/dockview/dockviewGroupPanel.ts

+6
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ export class DockviewGroupPanel
124124
options,
125125
this
126126
);
127+
128+
this.addDisposables(
129+
this.model.onDidActivePanelChange((event) => {
130+
this.api._onDidActivePanelChange.fire(event);
131+
})
132+
);
127133
}
128134

129135
override focus(): void {

0 commit comments

Comments
 (0)