mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-03 15:01:00 +02:00
Use Effect collections for RPC protocol clients
This commit is contained in:
parent
5a945af345
commit
935ded616c
3 changed files with 61 additions and 16 deletions
|
|
@ -1,4 +1,7 @@
|
|||
import { Effect, Queue, Scope } from "effect";
|
||||
import * as MutableHashMap from "effect/MutableHashMap";
|
||||
import * as MutableHashSet from "effect/MutableHashSet";
|
||||
import * as O from "effect/Option";
|
||||
import * as S from "effect/Schema";
|
||||
import * as RpcMessage from "effect/unstable/rpc/RpcMessage";
|
||||
import * as RpcSerialization from "effect/unstable/rpc/RpcSerialization";
|
||||
|
|
@ -41,10 +44,10 @@ export const makeSocketRpcProtocol = Effect.gen(function* () {
|
|||
const disconnects = yield* Queue.make<number>();
|
||||
|
||||
let nextClientId = 0;
|
||||
const clients = new Map<number, {
|
||||
const clients = MutableHashMap.empty<number, {
|
||||
readonly write: (response: RpcMessage.FromServerEncoded) => Effect.Effect<void>;
|
||||
}>();
|
||||
const clientIds = new Set<number>();
|
||||
const clientIds = MutableHashSet.empty<number>();
|
||||
|
||||
let writeRequest!: (
|
||||
clientId: number,
|
||||
|
|
@ -60,8 +63,8 @@ export const makeSocketRpcProtocol = Effect.gen(function* () {
|
|||
const clientId = nextClientId++;
|
||||
|
||||
yield* Scope.addFinalizerExit(scope, () => {
|
||||
clients.delete(clientId);
|
||||
clientIds.delete(clientId);
|
||||
MutableHashMap.remove(clients, clientId);
|
||||
MutableHashSet.remove(clientIds, clientId);
|
||||
return Queue.offer(disconnects, clientId);
|
||||
});
|
||||
|
||||
|
|
@ -80,8 +83,8 @@ export const makeSocketRpcProtocol = Effect.gen(function* () {
|
|||
),
|
||||
);
|
||||
|
||||
clients.set(clientId, { write });
|
||||
clientIds.add(clientId);
|
||||
MutableHashMap.set(clients, clientId, { write });
|
||||
MutableHashSet.add(clientIds, clientId);
|
||||
|
||||
yield* socket.runRaw((data) =>
|
||||
Effect.try({
|
||||
|
|
@ -126,13 +129,13 @@ export const makeSocketRpcProtocol = Effect.gen(function* () {
|
|||
writeRequest = writeRequest_;
|
||||
return Effect.succeed({
|
||||
disconnects,
|
||||
send: (clientId, response) => {
|
||||
const client = clients.get(clientId);
|
||||
if (client === undefined) return Effect.void;
|
||||
return Effect.orDie(client.write(response));
|
||||
},
|
||||
send: (clientId, response) =>
|
||||
O.match(MutableHashMap.get(clients, clientId), {
|
||||
onNone: () => Effect.void,
|
||||
onSome: (client) => Effect.orDie(client.write(response)),
|
||||
}),
|
||||
end: () => Effect.void,
|
||||
clientIds: Effect.sync(() => clientIds),
|
||||
clientIds: Effect.sync(() => new Set(clientIds)),
|
||||
initialMessage: Effect.succeedNone,
|
||||
supportsAck: true,
|
||||
supportsTransferables: false,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue