-
Notifications
You must be signed in to change notification settings - Fork 36
/
createSocketIOGraphQLClient.ts
118 lines (101 loc) · 2.82 KB
/
createSocketIOGraphQLClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import type { Socket as IOSocket } from "socket.io-client";
import {
makePushPullAsyncIterableIterator,
withHandlers,
} from "@n1ru4l/push-pull-async-iterable-iterator";
export type ExecutionParameter = {
operation: string;
operationName?: string | null;
variables?: { [key: string]: any };
extensions?: { [key: string]: any };
};
export type SocketIOGraphQLClient<TExecutionResult = unknown> = {
execute: (
opts: ExecutionParameter
) => AsyncIterableIterator<TExecutionResult>;
destroy: () => void;
};
type OperationRecord<TExecutionResult = unknown> = {
iterator: AsyncIterableIterator<TExecutionResult>;
publishValue: (value: TExecutionResult) => void;
execute: () => void;
};
export const createSocketIOGraphQLClient = <TExecutionResult = unknown>(
socket: IOSocket
): SocketIOGraphQLClient<TExecutionResult> => {
let currentOperationId = 0;
const operations = new Map<number, OperationRecord<TExecutionResult>>();
const onExecutionResult = ({ id, isFinal, ...result }: any) => {
const record = operations.get(id);
if (!record) {
return;
}
record.publishValue(result);
if (isFinal) {
record.iterator.return?.();
operations.delete(id);
}
};
let isOffline = false;
const onDisconnect = () => {
isOffline = true;
};
const onConnect = () => {
if (isOffline) {
isOffline = false;
Array.from(operations.values()).forEach((record) => {
record.execute();
});
}
};
socket.on("@graphql/result", onExecutionResult);
socket.on("connect", onConnect);
socket.on("disconnect", onDisconnect);
const destroy = () => {
socket.off("@graphql/result", onExecutionResult);
socket.off("connect", onConnect);
socket.off("disconnect", onDisconnect);
};
const execute = ({
operation,
variables,
operationName,
extensions,
}: ExecutionParameter): AsyncIterableIterator<TExecutionResult> => {
const operationId = currentOperationId;
currentOperationId = currentOperationId + 1;
const { asyncIterableIterator: source, pushValue: publishValue } =
makePushPullAsyncIterableIterator<TExecutionResult>();
const stream = withHandlers(source, () => {
if (operations.delete(operationId) === false) {
return;
}
socket.emit("@graphql/unsubscribe", {
id: operationId,
});
source.return();
});
const record: OperationRecord<TExecutionResult> = {
execute: () => {
socket.emit("@graphql/execute", {
id: operationId,
operationName,
operation,
variables,
extensions,
});
},
iterator: stream,
publishValue,
};
operations.set(operationId, record);
if (!isOffline) {
record.execute();
}
return stream;
};
return {
execute,
destroy,
};
};