@pooder/core 1.2.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/index.d.mts +11 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +33 -2
- package/dist/index.mjs +30 -1
- package/package.json +1 -1
- package/src/command.ts +10 -10
- package/src/context.ts +17 -17
- package/src/contribution/index.ts +12 -12
- package/src/contribution/registry.ts +118 -118
- package/src/disposable.ts +3 -3
- package/src/extension.ts +164 -164
- package/src/index.ts +145 -140
- package/src/run-test-full.ts +98 -98
- package/src/services/CommandService.ts +79 -79
- package/src/services/ConfigurationService.ts +107 -107
- package/src/services/WorkbenchService.ts +30 -0
- package/src/services/index.ts +2 -1
- package/src/test-extension-full.ts +79 -79
package/src/run-test-full.ts
CHANGED
|
@@ -1,98 +1,98 @@
|
|
|
1
|
-
import { Pooder } from "./index";
|
|
2
|
-
import { fullExtension } from "./test-extension-full";
|
|
3
|
-
import { ContributionPointIds } from "./contribution";
|
|
4
|
-
import CommandService from "./services/CommandService";
|
|
5
|
-
|
|
6
|
-
async function runTest() {
|
|
7
|
-
console.log("Starting Test...");
|
|
8
|
-
const app = new Pooder();
|
|
9
|
-
|
|
10
|
-
// Register the full extension
|
|
11
|
-
console.log("Registering extension...");
|
|
12
|
-
app.extensionManager.register(fullExtension);
|
|
13
|
-
const commandService = app.getService<CommandService>("CommandService")!;
|
|
14
|
-
|
|
15
|
-
// 1. Verify Command Contributions
|
|
16
|
-
console.log("\n--- Verifying Commands ---");
|
|
17
|
-
|
|
18
|
-
// 1.1 Imperative Command
|
|
19
|
-
try {
|
|
20
|
-
const res = await commandService.executeCommand(
|
|
21
|
-
"test.imperative.hello",
|
|
22
|
-
"User",
|
|
23
|
-
);
|
|
24
|
-
console.log(
|
|
25
|
-
`[Imperative] "test.imperative.hello" result: "${res}"` ===
|
|
26
|
-
`[Imperative] "test.imperative.hello" result: "Hello Imperative User"`
|
|
27
|
-
? "✅ PASS"
|
|
28
|
-
: "❌ FAIL",
|
|
29
|
-
);
|
|
30
|
-
} catch (e) {
|
|
31
|
-
console.error("❌ FAIL: Imperative command failed", e);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// 1.2 Declarative Auto-Registered Command
|
|
35
|
-
try {
|
|
36
|
-
const res = await commandService.executeCommand("test.declarative.auto");
|
|
37
|
-
console.log(
|
|
38
|
-
`[Declarative] "test.declarative.auto" result: "${res}"` ===
|
|
39
|
-
`[Declarative] "test.declarative.auto" result: "Auto Registered Result"`
|
|
40
|
-
? "✅ PASS"
|
|
41
|
-
: "❌ FAIL",
|
|
42
|
-
);
|
|
43
|
-
} catch (e) {
|
|
44
|
-
console.error("❌ FAIL: Declarative auto-registered command failed", e);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// 2. Verify Registry Entries
|
|
48
|
-
console.log("\n--- Verifying Contribution Registry ---");
|
|
49
|
-
|
|
50
|
-
const commands = app.getContributions(ContributionPointIds.COMMANDS);
|
|
51
|
-
console.log(
|
|
52
|
-
`Commands registered: ${commands.length}` === "Commands registered: 2"
|
|
53
|
-
? "✅ PASS (2 commands found)"
|
|
54
|
-
: `❌ FAIL (${commands.length} commands found)`,
|
|
55
|
-
);
|
|
56
|
-
|
|
57
|
-
const tools = app.getContributions(ContributionPointIds.TOOLS);
|
|
58
|
-
console.log(
|
|
59
|
-
`Tools registered: ${tools.length}` === "Tools registered: 1"
|
|
60
|
-
? "✅ PASS (1 tool found)"
|
|
61
|
-
: `❌ FAIL (${tools.length} tools found)`,
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
const views = app.getContributions(ContributionPointIds.VIEWS);
|
|
65
|
-
console.log(
|
|
66
|
-
`Views registered: ${views.length}` === "Views registered: 1"
|
|
67
|
-
? "✅ PASS (1 view found)"
|
|
68
|
-
: `❌ FAIL (${views.length} views found)`,
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
// 3. Unregister and Verify Cleanup
|
|
72
|
-
console.log("\n--- Verifying Unregistration/Cleanup ---");
|
|
73
|
-
// The ID is now explicit: "full-feature-test-extension"
|
|
74
|
-
const extensionId = "full-feature-test-extension";
|
|
75
|
-
app.extensionManager.unregister(extensionId);
|
|
76
|
-
|
|
77
|
-
const commandsAfter = app.getContributions(ContributionPointIds.COMMANDS);
|
|
78
|
-
console.log(
|
|
79
|
-
`Commands after unregister: ${commandsAfter.length}` ===
|
|
80
|
-
"Commands after unregister: 0"
|
|
81
|
-
? "✅ PASS"
|
|
82
|
-
: `❌ FAIL (${commandsAfter.length} left)`,
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
// Verify command service cleanup
|
|
86
|
-
const cmdMap = commandService.getCommands();
|
|
87
|
-
const hasCmd = cmdMap.has("test.declarative.auto");
|
|
88
|
-
console.log(
|
|
89
|
-
`Command service cleaned up: ${!hasCmd}` ===
|
|
90
|
-
"Command service cleaned up: true"
|
|
91
|
-
? "✅ PASS"
|
|
92
|
-
: "❌ FAIL (Command still exists)",
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
console.log("\nTest Completed.");
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
runTest().catch(console.error);
|
|
1
|
+
import { Pooder } from "./index";
|
|
2
|
+
import { fullExtension } from "./test-extension-full";
|
|
3
|
+
import { ContributionPointIds } from "./contribution";
|
|
4
|
+
import CommandService from "./services/CommandService";
|
|
5
|
+
|
|
6
|
+
async function runTest() {
|
|
7
|
+
console.log("Starting Test...");
|
|
8
|
+
const app = new Pooder();
|
|
9
|
+
|
|
10
|
+
// Register the full extension
|
|
11
|
+
console.log("Registering extension...");
|
|
12
|
+
app.extensionManager.register(fullExtension);
|
|
13
|
+
const commandService = app.getService<CommandService>("CommandService")!;
|
|
14
|
+
|
|
15
|
+
// 1. Verify Command Contributions
|
|
16
|
+
console.log("\n--- Verifying Commands ---");
|
|
17
|
+
|
|
18
|
+
// 1.1 Imperative Command
|
|
19
|
+
try {
|
|
20
|
+
const res = await commandService.executeCommand(
|
|
21
|
+
"test.imperative.hello",
|
|
22
|
+
"User",
|
|
23
|
+
);
|
|
24
|
+
console.log(
|
|
25
|
+
`[Imperative] "test.imperative.hello" result: "${res}"` ===
|
|
26
|
+
`[Imperative] "test.imperative.hello" result: "Hello Imperative User"`
|
|
27
|
+
? "✅ PASS"
|
|
28
|
+
: "❌ FAIL",
|
|
29
|
+
);
|
|
30
|
+
} catch (e) {
|
|
31
|
+
console.error("❌ FAIL: Imperative command failed", e);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 1.2 Declarative Auto-Registered Command
|
|
35
|
+
try {
|
|
36
|
+
const res = await commandService.executeCommand("test.declarative.auto");
|
|
37
|
+
console.log(
|
|
38
|
+
`[Declarative] "test.declarative.auto" result: "${res}"` ===
|
|
39
|
+
`[Declarative] "test.declarative.auto" result: "Auto Registered Result"`
|
|
40
|
+
? "✅ PASS"
|
|
41
|
+
: "❌ FAIL",
|
|
42
|
+
);
|
|
43
|
+
} catch (e) {
|
|
44
|
+
console.error("❌ FAIL: Declarative auto-registered command failed", e);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 2. Verify Registry Entries
|
|
48
|
+
console.log("\n--- Verifying Contribution Registry ---");
|
|
49
|
+
|
|
50
|
+
const commands = app.getContributions(ContributionPointIds.COMMANDS);
|
|
51
|
+
console.log(
|
|
52
|
+
`Commands registered: ${commands.length}` === "Commands registered: 2"
|
|
53
|
+
? "✅ PASS (2 commands found)"
|
|
54
|
+
: `❌ FAIL (${commands.length} commands found)`,
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
const tools = app.getContributions(ContributionPointIds.TOOLS);
|
|
58
|
+
console.log(
|
|
59
|
+
`Tools registered: ${tools.length}` === "Tools registered: 1"
|
|
60
|
+
? "✅ PASS (1 tool found)"
|
|
61
|
+
: `❌ FAIL (${tools.length} tools found)`,
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const views = app.getContributions(ContributionPointIds.VIEWS);
|
|
65
|
+
console.log(
|
|
66
|
+
`Views registered: ${views.length}` === "Views registered: 1"
|
|
67
|
+
? "✅ PASS (1 view found)"
|
|
68
|
+
: `❌ FAIL (${views.length} views found)`,
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
// 3. Unregister and Verify Cleanup
|
|
72
|
+
console.log("\n--- Verifying Unregistration/Cleanup ---");
|
|
73
|
+
// The ID is now explicit: "full-feature-test-extension"
|
|
74
|
+
const extensionId = "full-feature-test-extension";
|
|
75
|
+
app.extensionManager.unregister(extensionId);
|
|
76
|
+
|
|
77
|
+
const commandsAfter = app.getContributions(ContributionPointIds.COMMANDS);
|
|
78
|
+
console.log(
|
|
79
|
+
`Commands after unregister: ${commandsAfter.length}` ===
|
|
80
|
+
"Commands after unregister: 0"
|
|
81
|
+
? "✅ PASS"
|
|
82
|
+
: `❌ FAIL (${commandsAfter.length} left)`,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
// Verify command service cleanup
|
|
86
|
+
const cmdMap = commandService.getCommands();
|
|
87
|
+
const hasCmd = cmdMap.has("test.declarative.auto");
|
|
88
|
+
console.log(
|
|
89
|
+
`Command service cleaned up: ${!hasCmd}` ===
|
|
90
|
+
"Command service cleaned up: true"
|
|
91
|
+
? "✅ PASS"
|
|
92
|
+
: "❌ FAIL (Command still exists)",
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
console.log("\nTest Completed.");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
runTest().catch(console.error);
|
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
import { Command } from "../command";
|
|
2
|
-
import Disposable from "../disposable";
|
|
3
|
-
import { Service } from "../service";
|
|
4
|
-
|
|
5
|
-
export default class CommandService implements Service {
|
|
6
|
-
private commands: Map<string, Command> = new Map();
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Register a command
|
|
10
|
-
* @param commandId Command Name (ID)
|
|
11
|
-
* @param handler Command handler function
|
|
12
|
-
* @param thisArg The `this` context for the handler
|
|
13
|
-
* @returns Disposable to unregister the command
|
|
14
|
-
*/
|
|
15
|
-
registerCommand(
|
|
16
|
-
commandId: string,
|
|
17
|
-
handler: (...args: any[]) => any,
|
|
18
|
-
thisArg?: any,
|
|
19
|
-
): Disposable {
|
|
20
|
-
if (this.commands.has(commandId)) {
|
|
21
|
-
console.warn(
|
|
22
|
-
`Command "${commandId}" is already registered. Overwriting.`,
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const command: Command = {
|
|
27
|
-
id: commandId,
|
|
28
|
-
handler: thisArg ? handler.bind(thisArg) : handler,
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
this.commands.set(commandId, command);
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
dispose: () => {
|
|
35
|
-
if (this.commands.get(commandId) === command) {
|
|
36
|
-
this.commands.delete(commandId);
|
|
37
|
-
}
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Execute a command
|
|
44
|
-
* @param commandId Command Name (ID)
|
|
45
|
-
* @param args Arguments to pass to the handler
|
|
46
|
-
* @returns The result of the command handler
|
|
47
|
-
*/
|
|
48
|
-
async executeCommand<T = any>(commandId: string, ...args: any[]): Promise<T> {
|
|
49
|
-
const command = this.commands.get(commandId);
|
|
50
|
-
if (!command) {
|
|
51
|
-
throw new Error(`Command "${commandId}" not found.`);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
try {
|
|
55
|
-
return await command.handler(...args);
|
|
56
|
-
} catch (error) {
|
|
57
|
-
console.error(`Error executing command "${commandId}":`, error);
|
|
58
|
-
throw error;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Get all registered commands
|
|
64
|
-
*/
|
|
65
|
-
getCommands(): Map<string, Command> {
|
|
66
|
-
return this.commands;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Get a specific command
|
|
71
|
-
*/
|
|
72
|
-
getCommand(commandId: string): Command | undefined {
|
|
73
|
-
return this.commands.get(commandId);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
dispose() {
|
|
77
|
-
this.commands.clear();
|
|
78
|
-
}
|
|
79
|
-
}
|
|
1
|
+
import { Command } from "../command";
|
|
2
|
+
import Disposable from "../disposable";
|
|
3
|
+
import { Service } from "../service";
|
|
4
|
+
|
|
5
|
+
export default class CommandService implements Service {
|
|
6
|
+
private commands: Map<string, Command> = new Map();
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Register a command
|
|
10
|
+
* @param commandId Command Name (ID)
|
|
11
|
+
* @param handler Command handler function
|
|
12
|
+
* @param thisArg The `this` context for the handler
|
|
13
|
+
* @returns Disposable to unregister the command
|
|
14
|
+
*/
|
|
15
|
+
registerCommand(
|
|
16
|
+
commandId: string,
|
|
17
|
+
handler: (...args: any[]) => any,
|
|
18
|
+
thisArg?: any,
|
|
19
|
+
): Disposable {
|
|
20
|
+
if (this.commands.has(commandId)) {
|
|
21
|
+
console.warn(
|
|
22
|
+
`Command "${commandId}" is already registered. Overwriting.`,
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const command: Command = {
|
|
27
|
+
id: commandId,
|
|
28
|
+
handler: thisArg ? handler.bind(thisArg) : handler,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
this.commands.set(commandId, command);
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
dispose: () => {
|
|
35
|
+
if (this.commands.get(commandId) === command) {
|
|
36
|
+
this.commands.delete(commandId);
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Execute a command
|
|
44
|
+
* @param commandId Command Name (ID)
|
|
45
|
+
* @param args Arguments to pass to the handler
|
|
46
|
+
* @returns The result of the command handler
|
|
47
|
+
*/
|
|
48
|
+
async executeCommand<T = any>(commandId: string, ...args: any[]): Promise<T> {
|
|
49
|
+
const command = this.commands.get(commandId);
|
|
50
|
+
if (!command) {
|
|
51
|
+
throw new Error(`Command "${commandId}" not found.`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
return await command.handler(...args);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.error(`Error executing command "${commandId}":`, error);
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Get all registered commands
|
|
64
|
+
*/
|
|
65
|
+
getCommands(): Map<string, Command> {
|
|
66
|
+
return this.commands;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Get a specific command
|
|
71
|
+
*/
|
|
72
|
+
getCommand(commandId: string): Command | undefined {
|
|
73
|
+
return this.commands.get(commandId);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
dispose() {
|
|
77
|
+
this.commands.clear();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -1,107 +1,107 @@
|
|
|
1
|
-
import { Service } from "../service";
|
|
2
|
-
import EventBus from "../event";
|
|
3
|
-
import { ConfigurationContribution } from "../contribution";
|
|
4
|
-
|
|
5
|
-
export default class ConfigurationService implements Service {
|
|
6
|
-
private configValues: Map<string, any> = new Map();
|
|
7
|
-
private eventBus: EventBus = new EventBus();
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Get a configuration value.
|
|
11
|
-
*/
|
|
12
|
-
get<T = any>(key: string, defaultValue?: T): T {
|
|
13
|
-
if (this.configValues.has(key)) {
|
|
14
|
-
return this.configValues.get(key);
|
|
15
|
-
}
|
|
16
|
-
return defaultValue as T;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Update a configuration value.
|
|
21
|
-
* Emits 'change' event.
|
|
22
|
-
*/
|
|
23
|
-
update(key: string, value: any) {
|
|
24
|
-
const oldValue = this.configValues.get(key);
|
|
25
|
-
if (oldValue !== value) {
|
|
26
|
-
this.configValues.set(key, value);
|
|
27
|
-
this.eventBus.emit(`change:${key}`, { key, value, oldValue });
|
|
28
|
-
this.eventBus.emit("change", { key, value, oldValue });
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Listen for changes to a specific configuration key.
|
|
34
|
-
*/
|
|
35
|
-
onDidChange(
|
|
36
|
-
key: string,
|
|
37
|
-
callback: (event: { key: string; value: any; oldValue: any }) => void,
|
|
38
|
-
) {
|
|
39
|
-
this.eventBus.on(`change:${key}`, callback);
|
|
40
|
-
return {
|
|
41
|
-
dispose: () => this.eventBus.off(`change:${key}`, callback),
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Listen for any configuration change.
|
|
47
|
-
*/
|
|
48
|
-
onAnyChange(
|
|
49
|
-
callback: (event: { key: string; value: any; oldValue: any }) => void,
|
|
50
|
-
) {
|
|
51
|
-
this.eventBus.on("change", callback);
|
|
52
|
-
return {
|
|
53
|
-
dispose: () => this.eventBus.off("change", callback),
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Export current configuration state as a JSON-serializable object.
|
|
59
|
-
* Useful for saving configuration templates.
|
|
60
|
-
*/
|
|
61
|
-
export(): Record<string, any> {
|
|
62
|
-
const exportData: Record<string, any> = {};
|
|
63
|
-
for (const [key, value] of this.configValues) {
|
|
64
|
-
exportData[key] = value;
|
|
65
|
-
}
|
|
66
|
-
return exportData;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Import configuration from a JSON object.
|
|
71
|
-
* This will merge the provided configuration with the current state,
|
|
72
|
-
* overwriting existing keys and triggering change events.
|
|
73
|
-
*/
|
|
74
|
-
import(data: Record<string, any>): void {
|
|
75
|
-
if (!data || typeof data !== "object") {
|
|
76
|
-
console.warn("ConfigurationService: Import data must be an object.");
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
Object.entries(data).forEach(([key, value]) => {
|
|
80
|
-
this.update(key, value);
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Initialize configuration with defaults from contributions.
|
|
86
|
-
* This should be called when a contribution is registered.
|
|
87
|
-
*/
|
|
88
|
-
initializeDefaults(contributions: ConfigurationContribution[]) {
|
|
89
|
-
contributions.forEach((contrib) => {
|
|
90
|
-
if (!contrib.id) {
|
|
91
|
-
console.warn(
|
|
92
|
-
"Configuration contribution missing 'id'. Skipping default initialization.",
|
|
93
|
-
contrib,
|
|
94
|
-
);
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
if (!this.configValues.has(contrib.id) && contrib.default !== undefined) {
|
|
98
|
-
this.configValues.set(contrib.id, contrib.default);
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
dispose() {
|
|
104
|
-
this.configValues.clear();
|
|
105
|
-
// EventBus doesn't have a clear/dispose in the snippet, but it's fine for now.
|
|
106
|
-
}
|
|
107
|
-
}
|
|
1
|
+
import { Service } from "../service";
|
|
2
|
+
import EventBus from "../event";
|
|
3
|
+
import { ConfigurationContribution } from "../contribution";
|
|
4
|
+
|
|
5
|
+
export default class ConfigurationService implements Service {
|
|
6
|
+
private configValues: Map<string, any> = new Map();
|
|
7
|
+
private eventBus: EventBus = new EventBus();
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Get a configuration value.
|
|
11
|
+
*/
|
|
12
|
+
get<T = any>(key: string, defaultValue?: T): T {
|
|
13
|
+
if (this.configValues.has(key)) {
|
|
14
|
+
return this.configValues.get(key);
|
|
15
|
+
}
|
|
16
|
+
return defaultValue as T;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Update a configuration value.
|
|
21
|
+
* Emits 'change' event.
|
|
22
|
+
*/
|
|
23
|
+
update(key: string, value: any) {
|
|
24
|
+
const oldValue = this.configValues.get(key);
|
|
25
|
+
if (oldValue !== value) {
|
|
26
|
+
this.configValues.set(key, value);
|
|
27
|
+
this.eventBus.emit(`change:${key}`, { key, value, oldValue });
|
|
28
|
+
this.eventBus.emit("change", { key, value, oldValue });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Listen for changes to a specific configuration key.
|
|
34
|
+
*/
|
|
35
|
+
onDidChange(
|
|
36
|
+
key: string,
|
|
37
|
+
callback: (event: { key: string; value: any; oldValue: any }) => void,
|
|
38
|
+
) {
|
|
39
|
+
this.eventBus.on(`change:${key}`, callback);
|
|
40
|
+
return {
|
|
41
|
+
dispose: () => this.eventBus.off(`change:${key}`, callback),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Listen for any configuration change.
|
|
47
|
+
*/
|
|
48
|
+
onAnyChange(
|
|
49
|
+
callback: (event: { key: string; value: any; oldValue: any }) => void,
|
|
50
|
+
) {
|
|
51
|
+
this.eventBus.on("change", callback);
|
|
52
|
+
return {
|
|
53
|
+
dispose: () => this.eventBus.off("change", callback),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Export current configuration state as a JSON-serializable object.
|
|
59
|
+
* Useful for saving configuration templates.
|
|
60
|
+
*/
|
|
61
|
+
export(): Record<string, any> {
|
|
62
|
+
const exportData: Record<string, any> = {};
|
|
63
|
+
for (const [key, value] of this.configValues) {
|
|
64
|
+
exportData[key] = value;
|
|
65
|
+
}
|
|
66
|
+
return exportData;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Import configuration from a JSON object.
|
|
71
|
+
* This will merge the provided configuration with the current state,
|
|
72
|
+
* overwriting existing keys and triggering change events.
|
|
73
|
+
*/
|
|
74
|
+
import(data: Record<string, any>): void {
|
|
75
|
+
if (!data || typeof data !== "object") {
|
|
76
|
+
console.warn("ConfigurationService: Import data must be an object.");
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
Object.entries(data).forEach(([key, value]) => {
|
|
80
|
+
this.update(key, value);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Initialize configuration with defaults from contributions.
|
|
86
|
+
* This should be called when a contribution is registered.
|
|
87
|
+
*/
|
|
88
|
+
initializeDefaults(contributions: ConfigurationContribution[]) {
|
|
89
|
+
contributions.forEach((contrib) => {
|
|
90
|
+
if (!contrib.id) {
|
|
91
|
+
console.warn(
|
|
92
|
+
"Configuration contribution missing 'id'. Skipping default initialization.",
|
|
93
|
+
contrib,
|
|
94
|
+
);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (!this.configValues.has(contrib.id) && contrib.default !== undefined) {
|
|
98
|
+
this.configValues.set(contrib.id, contrib.default);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
dispose() {
|
|
104
|
+
this.configValues.clear();
|
|
105
|
+
// EventBus doesn't have a clear/dispose in the snippet, but it's fine for now.
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Service } from "../service";
|
|
2
|
+
import EventBus from "../event";
|
|
3
|
+
|
|
4
|
+
export default class WorkbenchService implements Service {
|
|
5
|
+
private _activeToolId: string | null = null;
|
|
6
|
+
private eventBus?: EventBus;
|
|
7
|
+
|
|
8
|
+
init() {
|
|
9
|
+
// Initialization logic if needed
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
dispose() {
|
|
13
|
+
// Cleanup logic if needed
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
setEventBus(bus: EventBus) {
|
|
17
|
+
this.eventBus = bus;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get activeToolId() {
|
|
21
|
+
return this._activeToolId;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
activate(id: string) {
|
|
25
|
+
if (this._activeToolId === id) return;
|
|
26
|
+
const previous = this._activeToolId;
|
|
27
|
+
this._activeToolId = id;
|
|
28
|
+
this.eventBus?.emit("tool:activated", { id, previous });
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/services/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import CommandService from "./CommandService";
|
|
2
2
|
import ConfigurationService from "./ConfigurationService";
|
|
3
|
+
import WorkbenchService from "./WorkbenchService";
|
|
3
4
|
|
|
4
|
-
export { CommandService, ConfigurationService };
|
|
5
|
+
export { CommandService, ConfigurationService, WorkbenchService };
|