Skip to content

Commit 08fa1af

Browse files
make ipc listener invokation async
1 parent 22dfe4b commit 08fa1af

4 files changed

Lines changed: 24 additions & 16 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ollieos",
3-
"version": "1.3.4",
3+
"version": "1.3.5",
44
"description": "",
55
"private": true,
66
"main": "server.js",

src/processes.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export interface IPCMessage {
88
data: unknown;
99
}
1010

11+
export type IPCChannelListener = (msg: IPCMessage) => Promise<void>;
12+
1113
interface IPCChannel {
1214
initiator: number;
1315
peer: number;
@@ -16,12 +18,14 @@ interface IPCChannel {
1618
peer_to_initiator_queue: IPCMessage[];
1719

1820
// pid -> set of listeners
19-
listeners: Map<number, Set<(msg: IPCMessage) => void>>;
21+
listeners: Map<number, Set<IPCChannelListener>>;
2022
}
2123

24+
export type IPCServiceOnConnectionCallback = (channel_id: number, from_pid: number) => Promise<void>;
25+
2226
interface IPCService {
2327
pid: number;
24-
on_connection: (channel_id: number, from_pid: number) => void;
28+
on_connection: IPCServiceOnConnectionCallback;
2529
}
2630

2731
export class IPCManager {
@@ -60,10 +64,12 @@ export class IPCManager {
6064
}, 10000);
6165
}
6266

63-
service_register(name: string, pid: number, on_connection: (channel_id: number, from_pid: number) => void): void {
67+
service_register(name: string, pid: number, on_connection: IPCServiceOnConnectionCallback): void {
6468
this._services.set(name, { pid, on_connection });
6569
}
6670

71+
// TODO: disconnect callback? or change the on_connection to on_event with different event types
72+
6773
service_unregister(name: string): void {
6874
this._services.delete(name);
6975
}
@@ -103,9 +109,11 @@ export class IPCManager {
103109
listeners: new Map(),
104110
});
105111

106-
// notify service of new connection
112+
// notify service of new connection without blocking
107113
const service = this._services.get(service_name)!;
108-
service.on_connection(channel_id, initiator_pid);
114+
service.on_connection(channel_id, initiator_pid).catch((err) => {
115+
console.error("IPC service on_connection error:", err);
116+
});
109117

110118
return channel_id;
111119
}
@@ -114,7 +122,7 @@ export class IPCManager {
114122
this._channels.delete(channel_id);
115123
}
116124

117-
channel_listen(channel_id: number, listening_pid: number, listener: (msg: IPCMessage) => void): boolean {
125+
channel_listen(channel_id: number, listening_pid: number, listener: IPCChannelListener): boolean {
118126
const channel = this._channels.get(channel_id);
119127
if (!channel) {
120128
return false;
@@ -132,7 +140,7 @@ export class IPCManager {
132140
return true;
133141
}
134142

135-
channel_unlisten(channel_id: number, listening_pid: number, listener: (msg: IPCMessage) => void): boolean {
143+
channel_unlisten(channel_id: number, listening_pid: number, listener: IPCChannelListener): boolean {
136144
const channel = this._channels.get(channel_id);
137145
if (!channel) {
138146
return false;
@@ -178,21 +186,21 @@ export class IPCManager {
178186
return false;
179187
}
180188

181-
// notify listeners on the receiving end
189+
// notify listeners on the receiving end without blocking
182190
const to_pid = msg.to;
183191
const listeners = channel.listeners.get(to_pid);
184192
if (listeners) {
185193
for (const listener of listeners) {
186-
listener(msg);
194+
listener(msg).catch((err) => {
195+
console.error("IPC channel listener error:", err);
196+
});
187197
}
188198
}
189199

190200
return true;
191201
}
192202
}
193203

194-
// TODO: make async
195-
196204
// TODO: could migrate the stuff where programs grab "scary" stuff like WindowManager and ProcessManager to be services
197205

198206
enum ProcessAttachment {

src/programs/ipc_bg_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export default {
1414
process.detach();
1515

1616
const ipc = term.get_ipc();
17-
ipc.service_register("ipc_bg_test", process.pid, (channel_id, from_pid) => {
18-
ipc.channel_listen(channel_id, process.pid, (msg) => {
17+
ipc.service_register("ipc_bg_test", process.pid, async (channel_id, from_pid) => {
18+
ipc.channel_listen(channel_id, process.pid, async (msg) => {
1919
term.writeln(`Received message on channel ${channel_id} from PID ${msg.from}: ${JSON.stringify(msg.data)}`);
2020
});
2121
});

0 commit comments

Comments
 (0)