Skip to content

Commit 2955b3a

Browse files
committed
Add IMC error handling
1 parent 80cb878 commit 2955b3a

File tree

12 files changed

+110
-47
lines changed

12 files changed

+110
-47
lines changed

.changeset/bumpy-parents-pull.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+
Rename pre-regsitered actions

.changeset/pre.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"changesets": [
1616
"angry-llamas-smash",
1717
"beige-pandas-rhyme",
18+
"bumpy-parents-pull",
1819
"calm-rivers-march",
1920
"chatty-trains-beam",
2021
"chubby-insects-shake",
@@ -45,6 +46,7 @@
4546
"sad-tables-join",
4647
"sharp-memes-give",
4748
"shiny-doodles-jump",
49+
"silent-glasses-kick",
4850
"slick-roses-fix",
4951
"social-donkeys-cross",
5052
"soft-cases-share",

.changeset/silent-glasses-kick.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+
Update register action hook for react api

.changeset/tough-aliens-appear.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 error handler in IMC

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.41
4+
5+
### Patch Changes
6+
7+
- Update register action hook for react api
8+
- Updated dependencies
9+
- @pulse-editor/shared-utils@0.1.1-alpha.41
10+
11+
## 0.1.1-alpha.40
12+
13+
### Patch Changes
14+
15+
- Rename pre-regsitered actions
16+
- Updated dependencies
17+
- @pulse-editor/shared-utils@0.1.1-alpha.40
18+
319
## 0.1.1-alpha.39
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.39",
3+
"version": "0.1.1-alpha.41",
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.39",
41+
"@pulse-editor/shared-utils": "0.1.1-alpha.41",
4242
"react": "^19.0.0",
4343
"react-dom": "^19.0.0"
4444
}

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ import useIMC from "../../lib/use-imc";
2121
*
2222
*/
2323
export default function useRegisterAction(
24-
name: string,
25-
description: string,
26-
parameters: Record<string, TypedVariable>,
27-
returns: Record<string, TypedVariable>,
24+
actionInfo: {
25+
name: string;
26+
description: string;
27+
parameters?: Record<string, TypedVariable>;
28+
returns?: Record<string, TypedVariable>;
29+
},
2830
callbackHandler?: (args: any) => Promise<string | void>,
2931
isExtReady: boolean = true
3032
) {
@@ -34,10 +36,10 @@ export default function useRegisterAction(
3436
const commandQueue = useRef<{ args: any; resolve: (v: any) => void }[]>([]);
3537

3638
const [action, setAction] = useState<Action>({
37-
name,
38-
description,
39-
parameters,
40-
returns,
39+
name: actionInfo.name,
40+
description: actionInfo.description,
41+
parameters: actionInfo.parameters ?? {},
42+
returns: actionInfo.returns ?? {},
4143
handler: callbackHandler,
4244
});
4345

@@ -76,7 +78,13 @@ export default function useRegisterAction(
7678
}, [action, imc, isExtReady]);
7779

7880
useEffect(() => {
79-
setAction((prev) => ({ ...prev, name, description, parameters, returns }));
81+
setAction((prev) => ({
82+
...prev,
83+
name: actionInfo.name,
84+
description: actionInfo.description,
85+
parameters: actionInfo.parameters ?? {},
86+
returns: actionInfo.returns ?? {},
87+
}));
8088
}, [callbackHandler]);
8189

8290
async function executeAction(args: any) {
@@ -94,9 +102,9 @@ export default function useRegisterAction(
94102
const { name: requestedName, args }: { name: string; args: any } =
95103
message.payload;
96104

97-
if (name === requestedName) {
105+
if (actionInfo.name === requestedName) {
98106
// Validate parameters
99-
const actionParams = parameters;
107+
const actionParams = actionInfo.parameters ?? {};
100108
if (Object.keys(args).length !== Object.keys(actionParams).length) {
101109
throw new Error(
102110
`Invalid number of parameters: expected ${
@@ -106,13 +114,13 @@ export default function useRegisterAction(
106114
}
107115

108116
for (const [key, value] of Object.entries(args)) {
109-
if (parameters[key] === undefined) {
117+
if (actionParams[key] === undefined) {
110118
throw new Error(`Invalid parameter: ${key}`);
111119
}
112-
if (typeof value !== parameters[key].type) {
120+
if (typeof value !== actionParams[key].type) {
113121
throw new Error(
114122
`Invalid type for parameter ${key}: expected ${
115-
parameters[key].type
123+
actionParams[key].type
116124
}, got ${typeof value}. Value received: ${value}`
117125
);
118126
}

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.41
4+
5+
### Patch Changes
6+
7+
- Update register action hook for react api
8+
9+
## 0.1.1-alpha.40
10+
11+
### Patch Changes
12+
13+
- Rename pre-regsitered actions
14+
315
## 0.1.1-alpha.39
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.39",
3+
"version": "0.1.1-alpha.41",
44
"main": "dist/main.js",
55
"files": [
66
"dist"

npm-packages/shared-utils/src/imc/inter-module-communication.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { messageTimeout } from "../types/constants";
12
import {
23
IMCMessage,
34
IMCMessageTypeEnum,
45
ReceiverHandlerMap,
56
} from "../types/types";
6-
import { messageTimeout } from "../types/constants";
77
import { MessageReceiver } from "./message-receiver";
88
import { MessageSender } from "./message-sender";
99

@@ -72,7 +72,10 @@ export class InterModuleCommunication {
7272
}
7373

7474
const message = event.data;
75-
if (process.env.NODE_ENV === "development" && message.from !== undefined) {
75+
if (
76+
process.env.NODE_ENV === "development" &&
77+
message.from !== undefined
78+
) {
7679
console.log(
7780
`Module ${this.thisWindowId} received message from module ${
7881
message.from
@@ -198,7 +201,17 @@ export class InterModuleCommunication {
198201
const pendingMessage = this.sender?.getPendingMessage(message.id);
199202
if (pendingMessage) {
200203
pendingMessage.resolve(message.payload);
201-
this.sender?.removePendingMessage(message.id);
204+
}
205+
}
206+
);
207+
208+
// Handle error message from the other window.
209+
this.receiverHandlerMap?.set(
210+
IMCMessageTypeEnum.SignalError,
211+
async (senderWindow: Window, message: IMCMessage) => {
212+
const pendingMessage = this.sender?.getPendingMessage(message.id);
213+
if (pendingMessage) {
214+
pendingMessage.reject(new Error(message.payload));
202215
}
203216
}
204217
);

0 commit comments

Comments
 (0)