Skip to content

Commit 350f24d

Browse files
committed
feat: improve utils
1 parent fd037fd commit 350f24d

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

packages/core/src/node/__tests__/host-docks.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,15 @@ describe('devToolsDockHost', () => {
167167
it('should preserve dock in values() after update', () => {
168168
const host = new DevToolsDockHost(mockContext)
169169

170-
host.register({
170+
const { update } = host.register({
171171
type: 'iframe',
172172
id: 'test',
173173
title: 'Original',
174174
icon: 'icon',
175175
url: 'http://localhost:3000',
176176
})
177177

178-
host.update({
179-
type: 'iframe',
180-
id: 'test',
178+
update({
181179
title: 'Updated',
182180
icon: 'newicon',
183181
url: 'http://localhost:3001',

packages/core/src/node/host-docks.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,23 @@ export class DevToolsDockHost implements DevToolsDockHostType {
1414
return Array.from(this.views.values())
1515
}
1616

17-
register(view: DevToolsDockUserEntry, force?: boolean): void {
17+
register<T extends DevToolsDockUserEntry>(view: T, force?: boolean): {
18+
update: (patch: Partial<T>) => void
19+
} {
1820
if (this.views.has(view.id) && !force) {
1921
throw new Error(`Dock with id "${view.id}" is already registered`)
2022
}
2123
this.views.set(view.id, view)
2224
this.events.emit('dock:entry:updated', view)
25+
26+
return {
27+
update: (patch) => {
28+
if (patch.id && patch.id !== view.id) {
29+
throw new Error(`Cannot change the id of a dock. Use register() to add new docks.`)
30+
}
31+
this.update(Object.assign(this.views.get(view.id)!, patch))
32+
},
33+
}
2334
}
2435

2536
update(view: DevToolsDockUserEntry): void {

packages/kit/src/types/docks.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import type { EventEmitter } from './events'
22

33
export interface DevToolsDockHost {
4-
views: Map<string, DevToolsDockUserEntry>
5-
events: EventEmitter<{
4+
readonly views: Map<string, DevToolsDockUserEntry>
5+
readonly events: EventEmitter<{
66
'dock:entry:updated': (entry: DevToolsDockUserEntry) => void
77
}>
88

9-
register: (entry: DevToolsDockUserEntry) => void
9+
register: <T extends DevToolsDockUserEntry>(entry: T, force?: boolean) => {
10+
update: (patch: Partial<T>) => void
11+
}
1012
update: (entry: DevToolsDockUserEntry) => void
1113
values: () => DevToolsDockUserEntry[]
1214
}

0 commit comments

Comments
 (0)