Skip to content

Commit 168ead5

Browse files
committed
Add env & improve platform assistant & improve command system
1 parent 1554bf1 commit 168ead5

29 files changed

+390
-155
lines changed

.changeset/pre.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@
4444
"shiny-doodles-jump",
4545
"slick-roses-fix",
4646
"social-donkeys-cross",
47+
"soft-cases-share",
4748
"stale-groups-poke",
4849
"tender-jeans-occur",
4950
"true-suits-fly",
51+
"vast-places-rhyme",
5052
"wicked-spoons-fry"
5153
]
5254
}

.changeset/soft-cases-share.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@pulse-editor/shared-utils": patch
3+
"@pulse-editor/react-api": patch
4+
---
5+
6+
Add queue to command react hook

.changeset/vast-places-rhyme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@pulse-editor/shared-utils": patch
3+
"@pulse-editor/react-api": patch
4+
---
5+
6+
Add env hook

npm-packages/react-api/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# @pulse-editor/react-api
22

3+
## 0.1.1-alpha.36
4+
5+
### Patch Changes
6+
7+
- Add queue to command react hook
8+
- Updated dependencies
9+
- @pulse-editor/shared-utils@0.1.1-alpha.36
10+
11+
## 0.1.1-alpha.35
12+
13+
### Patch Changes
14+
15+
- Add env hook
16+
- Updated dependencies
17+
- @pulse-editor/shared-utils@0.1.1-alpha.35
18+
319
## 0.1.1-alpha.34
420

521
### Patch Changes

npm-packages/react-api/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pulse-editor/react-api",
3-
"version": "0.1.1-alpha.34",
3+
"version": "0.1.1-alpha.36",
44
"main": "dist/main.js",
55
"files": [
66
"dist"
@@ -38,7 +38,7 @@
3838
"typescript-eslint": "^8.30.1"
3939
},
4040
"peerDependencies": {
41-
"@pulse-editor/shared-utils": "0.1.1-alpha.34",
41+
"@pulse-editor/shared-utils": "0.1.1-alpha.36",
4242
"react": "^19.0.0",
4343
"react-dom": "^19.0.0"
4444
}

npm-packages/react-api/src/hooks/editor/use-command.ts

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {
44
IMCMessageTypeEnum,
55
ReceiverHandler,
66
} from "@pulse-editor/shared-utils";
7+
import { useEffect, useRef, useState } from "react";
78
import useIMC from "../../lib/use-imc";
8-
import { useEffect, useState } from "react";
99

1010
/**
1111
* Register an extension command to listen to IMC messages from the core,
@@ -17,33 +17,38 @@ import { useEffect, useState } from "react";
1717
*/
1818
export default function useCommand(
1919
commandInfo: CommandInfo,
20-
callbackHandler?: (args: any) => Promise<string | void>
20+
callbackHandler?: (args: any) => Promise<string | void>,
21+
isExtReady: boolean = true
2122
) {
2223
const { isReady, imc } = useIMC(getReceiverHandlerMap());
2324

2425
const [handler, setHandler] = useState<
2526
((args: any) => Promise<any>) | undefined
2627
>(undefined);
2728

29+
// Queue to hold commands until extension is ready
30+
const commandQueue = useRef<{ args: any; resolve: (v: any) => void }[]>([]);
31+
32+
async function executeCommand(args: any) {
33+
if (!handler) return;
34+
35+
const res = await handler(args);
36+
return res;
37+
}
38+
2839
function getReceiverHandlerMap() {
2940
const receiverHandlerMap = new Map<IMCMessageTypeEnum, ReceiverHandler>([
3041
[
3142
IMCMessageTypeEnum.EditorRunExtCommand,
32-
async (senderWindow: Window, message: IMCMessage) => {
43+
async (_senderWindow: Window, message: IMCMessage) => {
3344
if (!commandInfo) {
3445
throw new Error("Extension command is not available");
3546
}
3647

37-
const {
38-
name,
39-
args,
40-
}: {
41-
name: string;
42-
args: any;
43-
} = message.payload;
48+
const { name, args }: { name: string; args: any } = message.payload;
4449

4550
if (name === commandInfo.name) {
46-
// Check if the parameters match the command's parameters
51+
// Validate parameters
4752
const commandParameters = commandInfo.parameters;
4853
if (
4954
Object.keys(args).length !== Object.keys(commandParameters).length
@@ -54,6 +59,7 @@ export default function useCommand(
5459
}, got ${Object.keys(args).length}`
5560
);
5661
}
62+
5763
for (const [key, value] of Object.entries(args)) {
5864
if (commandInfo.parameters[key] === undefined) {
5965
throw new Error(`Invalid parameter: ${key}`);
@@ -67,23 +73,37 @@ export default function useCommand(
6773
}
6874
}
6975

70-
// Execute the command handler with the parameters
71-
if (handler) {
72-
const res = await handler(args);
73-
if (res) {
74-
return res;
75-
}
76+
// If extension is ready, execute immediately
77+
if (isExtReady) {
78+
return await executeCommand(args);
7679
}
80+
81+
// Otherwise, queue the command and return when executed
82+
return new Promise((resolve) => {
83+
commandQueue.current.push({ args, resolve });
84+
});
7785
}
7886
},
7987
],
8088
]);
8189
return receiverHandlerMap;
8290
}
8391

92+
// Flush queued commands when isExtReady becomes true
93+
useEffect(() => {
94+
if (isExtReady && commandQueue.current.length > 0) {
95+
const pending = [...commandQueue.current];
96+
commandQueue.current = [];
97+
pending.forEach(async ({ args, resolve }) => {
98+
const res = await executeCommand(args);
99+
resolve(res);
100+
});
101+
}
102+
}, [isExtReady]);
103+
84104
useEffect(() => {
85105
imc?.updateReceiverHandlerMap(getReceiverHandlerMap());
86-
}, [handler, imc]);
106+
}, [handler, imc, isExtReady]);
87107

88108
useEffect(() => {
89109
setHandler(() => callbackHandler);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { IMCMessage, IMCMessageTypeEnum } from "@pulse-editor/shared-utils";
2+
import { useEffect, useState } from "react";
3+
import useIMC from "../../lib/use-imc";
4+
5+
export default function usePulseEnv() {
6+
const receiverHandlerMap = new Map<
7+
IMCMessageTypeEnum,
8+
(senderWindow: Window, message: IMCMessage) => Promise<void>
9+
>();
10+
11+
const { imc, isReady } = useIMC(receiverHandlerMap);
12+
const [envs, setEnvs] = useState<Record<string, string>>({});
13+
14+
useEffect(() => {
15+
if (isReady) {
16+
imc?.sendMessage(IMCMessageTypeEnum.EditorGetEnv).then((env) => {
17+
setEnvs(env);
18+
});
19+
}
20+
}, [isReady]);
21+
22+
return {
23+
isReady,
24+
envs,
25+
};
26+
}

npm-packages/react-api/src/main.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
import useAgentTools from "./hooks/agent/use-agent-tools";
22
import useAgents from "./hooks/agent/use-agents";
3+
import useCommand from "./hooks/editor/use-command";
34
import useFileView from "./hooks/editor/use-file-view";
45
import useLoading from "./hooks/editor/use-loading";
56
import useNotification from "./hooks/editor/use-notification";
67
import useTheme from "./hooks/editor/use-theme";
78
import useToolbar from "./hooks/editor/use-toolbar";
8-
import useCommand from "./hooks/editor/use-command";
99

1010
import useImageGen from "./hooks/ai-modality/use-image-gen";
1111
import useLLM from "./hooks/ai-modality/use-llm";
1212
import useOCR from "./hooks/ai-modality/use-ocr";
1313
import useSTT from "./hooks/ai-modality/use-stt";
1414
import useTTS from "./hooks/ai-modality/use-tts";
1515
import useVideoGen from "./hooks/ai-modality/use-video-gen";
16+
import usePulseEnv from "./hooks/editor/use-env";
1617
import useTerminal from "./hooks/terminal/use-terminal";
1718

1819
export {
1920
useAgentTools,
2021
useAgents,
22+
useCommand,
2123
useFileView,
22-
useNotification,
23-
useTheme,
24-
useToolbar,
2524
useImageGen,
26-
useVideoGen,
2725
useLLM,
26+
useLoading,
27+
useNotification,
2828
useOCR,
29+
usePulseEnv,
2930
useSTT,
3031
useTTS,
31-
useCommand,
3232
useTerminal,
33-
useLoading,
33+
useTheme,
34+
useToolbar,
35+
useVideoGen,
3436
};

npm-packages/shared-utils/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
# @pulse-editor/shared-utils
22

3+
## 0.1.1-alpha.36
4+
5+
### Patch Changes
6+
7+
- Add queue to command react hook
8+
9+
## 0.1.1-alpha.35
10+
11+
### Patch Changes
12+
13+
- Add env hook
14+
315
## 0.1.1-alpha.34
416

517
### Patch Changes

npm-packages/shared-utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pulse-editor/shared-utils",
3-
"version": "0.1.1-alpha.34",
3+
"version": "0.1.1-alpha.36",
44
"main": "dist/main.js",
55
"files": [
66
"dist"

0 commit comments

Comments
 (0)