@livon/client 0.27.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.js ADDED
@@ -0,0 +1,314 @@
1
+ const DEFAULT_REQUEST_KEY = 'livon.client.request';
2
+ const setClientRequest = ({ client, registry, requestKey })=>{
3
+ if ('object' != typeof client || null === client || !('setRequest' in client)) return;
4
+ const candidate = client;
5
+ if ('function' != typeof candidate.setRequest) return;
6
+ candidate.setRequest((event, payload)=>{
7
+ const request = registry.state.get(requestKey);
8
+ if (!request) throw new Error('Client request handler is not available.');
9
+ return request(event, payload);
10
+ });
11
+ };
12
+ const buildClientEventEnvelope = (envelope)=>{
13
+ if ('payload' in envelope) return {
14
+ ...envelope,
15
+ payload: envelope.payload
16
+ };
17
+ return {
18
+ ...envelope,
19
+ error: envelope.error
20
+ };
21
+ };
22
+ const clientModule = (client, options = {})=>{
23
+ const register = (registry)=>{
24
+ const requestKey = options.requestKey ?? DEFAULT_REQUEST_KEY;
25
+ setClientRequest({
26
+ client,
27
+ registry,
28
+ requestKey
29
+ });
30
+ registry.onReceive((envelope, _ctx, next)=>{
31
+ client.emitEvent(buildClientEventEnvelope(envelope));
32
+ return next();
33
+ });
34
+ };
35
+ return {
36
+ name: options.name ?? 'client-module',
37
+ register
38
+ };
39
+ };
40
+ const isRecord = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
41
+ const isArray = (value)=>Array.isArray(value);
42
+ const capitalize = (value)=>0 === value.length ? value : value.slice(0, 1).toUpperCase() + value.slice(1);
43
+ const camelCaseName = (value)=>{
44
+ if (!value) return value;
45
+ return value.replace(/[^a-zA-Z0-9]+(.)/g, (_, group)=>String(group).toUpperCase()).replace(/^./, (char)=>char.toLowerCase());
46
+ };
47
+ const fieldMethodName = (owner, field)=>`$${camelCaseName(owner)}${capitalize(field)}`;
48
+ const fieldEventName = (owner, field)=>`$${owner}.${field}`;
49
+ const walkAst = (node, visit)=>{
50
+ visit(node);
51
+ node.children?.forEach((child)=>walkAst(child, visit));
52
+ };
53
+ const collectOperations = (root)=>{
54
+ const operations = [];
55
+ walkAst(root, (node)=>{
56
+ if ('operation' !== node.type || !node.name) return;
57
+ const input = node.children?.[0];
58
+ const output = node.children?.[1];
59
+ operations.push({
60
+ name: node.name,
61
+ input,
62
+ output,
63
+ event: node.name
64
+ });
65
+ });
66
+ return operations;
67
+ };
68
+ const collectFieldOperations = (root)=>{
69
+ const registry = new Map();
70
+ walkAst(root, (node)=>{
71
+ if ('field' !== node.type) return;
72
+ const constraints = node.constraints;
73
+ const owner = 'string' == typeof constraints?.owner ? constraints.owner : void 0;
74
+ const field = 'string' == typeof constraints?.field ? constraints.field : void 0;
75
+ if (!owner || !field) return;
76
+ const children = node.children ?? [];
77
+ const dependsOn = children[0];
78
+ const input = 3 === children.length ? children[1] : void 0;
79
+ const output = 3 === children.length ? children[2] : children[1];
80
+ if (!dependsOn) return;
81
+ const spec = {
82
+ owner,
83
+ field,
84
+ input,
85
+ output,
86
+ event: fieldEventName(owner, field)
87
+ };
88
+ if (!registry.has(owner)) registry.set(owner, new Map());
89
+ registry.get(owner).set(field, spec);
90
+ });
91
+ return registry;
92
+ };
93
+ const hydrateByNode = ({ value, node, registry, request })=>{
94
+ if (!node) return value;
95
+ if ('array' === node.type && isArray(value)) {
96
+ const child = node.children?.[0];
97
+ value.forEach((item, index)=>{
98
+ value[index] = hydrateByNode({
99
+ value: item,
100
+ node: child,
101
+ registry,
102
+ request
103
+ });
104
+ });
105
+ return value;
106
+ }
107
+ if ('tuple' === node.type && isArray(value)) {
108
+ const children = node.children ?? [];
109
+ value.forEach((item, index)=>{
110
+ value[index] = hydrateByNode({
111
+ value: item,
112
+ node: children[index],
113
+ registry,
114
+ request
115
+ });
116
+ });
117
+ return value;
118
+ }
119
+ if ('object' === node.type && isRecord(value)) {
120
+ const typeName = node.name;
121
+ if (typeName && registry.has(typeName)) attachFieldOperations({
122
+ target: value,
123
+ typeName,
124
+ registry,
125
+ request
126
+ });
127
+ const fields = node.children ?? [];
128
+ fields.forEach((fieldNode)=>{
129
+ if ('field' !== fieldNode.type || !fieldNode.name) return;
130
+ const child = fieldNode.children?.[0];
131
+ if (!child) return;
132
+ value[fieldNode.name] = hydrateByNode({
133
+ value: value[fieldNode.name],
134
+ node: child,
135
+ registry,
136
+ request
137
+ });
138
+ });
139
+ return value;
140
+ }
141
+ if ('field' === node.type) {
142
+ const child = node.children?.[0];
143
+ return hydrateByNode({
144
+ value,
145
+ node: child,
146
+ registry,
147
+ request
148
+ });
149
+ }
150
+ return value;
151
+ };
152
+ const attachFieldOperations = ({ target, typeName, registry, request })=>{
153
+ const operations = registry.get(typeName);
154
+ if (!operations) return;
155
+ if (!Object.isExtensible(target)) return;
156
+ operations.forEach((spec, fieldName)=>{
157
+ if (fieldName in target) return;
158
+ Object.defineProperty(target, fieldName, {
159
+ enumerable: false,
160
+ configurable: true,
161
+ value: async (input)=>{
162
+ const payload = {
163
+ dependsOn: target,
164
+ input
165
+ };
166
+ const result = await request(spec.event, payload);
167
+ return hydrateByNode({
168
+ value: result,
169
+ node: spec.output,
170
+ registry,
171
+ request
172
+ });
173
+ }
174
+ });
175
+ });
176
+ };
177
+ const normalizeFieldPayload = (payload)=>{
178
+ if (isRecord(payload) && 'dependsOn' in payload) {
179
+ const dependsOn = payload.dependsOn;
180
+ const input = 'input' in payload ? payload.input : void 0;
181
+ if (void 0 === input) return {
182
+ dependsOn
183
+ };
184
+ return {
185
+ dependsOn,
186
+ input
187
+ };
188
+ }
189
+ return {
190
+ dependsOn: payload
191
+ };
192
+ };
193
+ const createClientCore = ({ ast })=>{
194
+ const operations = collectOperations(ast);
195
+ const fieldRegistry = collectFieldOperations(ast);
196
+ let request;
197
+ const client = {};
198
+ client.setRequest = (next)=>{
199
+ request = next;
200
+ };
201
+ const globalHandlers = new Map();
202
+ const globalEnabled = new Map();
203
+ const roomHandlers = new Map();
204
+ const roomEnabled = new Map();
205
+ const registerHandlers = (handlers, roomId)=>{
206
+ const entries = Object.entries(handlers).filter(([, handler])=>'function' == typeof handler);
207
+ if (0 === entries.length) return;
208
+ if (!roomId) return void entries.forEach(([event, handler])=>{
209
+ globalHandlers.set(event, handler);
210
+ globalEnabled.set(event, true);
211
+ });
212
+ const roomMap = roomHandlers.get(roomId) ?? new Map();
213
+ const enabledMap = roomEnabled.get(roomId) ?? new Map();
214
+ entries.forEach(([event, handler])=>{
215
+ roomMap.set(event, handler);
216
+ enabledMap.set(event, true);
217
+ });
218
+ roomHandlers.set(roomId, roomMap);
219
+ roomEnabled.set(roomId, enabledMap);
220
+ };
221
+ const toggleHandler = ({ enabled, event, roomId })=>{
222
+ if (!roomId) return void globalEnabled.set(event, enabled);
223
+ const enabledMap = roomEnabled.get(roomId) ?? new Map();
224
+ enabledMap.set(event, enabled);
225
+ roomEnabled.set(roomId, enabledMap);
226
+ };
227
+ const dispatch = (envelope)=>{
228
+ const ctx = {
229
+ eventId: envelope.id,
230
+ event: envelope.event,
231
+ status: envelope.status,
232
+ metadata: envelope.metadata,
233
+ context: envelope.context,
234
+ room: 'string' == typeof envelope.metadata?.room ? String(envelope.metadata.room) : void 0
235
+ };
236
+ const roomId = ctx.room;
237
+ if (roomId) {
238
+ const enabledMap = roomEnabled.get(roomId);
239
+ const roomMap = roomHandlers.get(roomId);
240
+ const handler = roomMap?.get(envelope.event);
241
+ const isEnabled = enabledMap?.get(envelope.event) ?? true;
242
+ if (handler && isEnabled) handler(envelope.payload, ctx);
243
+ }
244
+ const handler = globalHandlers.get(envelope.event);
245
+ const isEnabled = globalEnabled.get(envelope.event) ?? true;
246
+ if (handler && isEnabled) handler(envelope.payload, ctx);
247
+ };
248
+ operations.forEach((op)=>{
249
+ client[op.name] = async (input)=>{
250
+ if (!request) throw new Error('Client request handler is not available.');
251
+ const result = await request(op.event, input);
252
+ return hydrateByNode({
253
+ value: result,
254
+ node: op.output,
255
+ registry: fieldRegistry,
256
+ request
257
+ });
258
+ };
259
+ });
260
+ fieldRegistry.forEach((fields, owner)=>{
261
+ fields.forEach((spec, field)=>{
262
+ const method = fieldMethodName(owner, field);
263
+ if (method in client) return;
264
+ client[method] = async (payload, input)=>{
265
+ const normalized = normalizeFieldPayload(payload);
266
+ if (!request) throw new Error('Client request handler is not available.');
267
+ const result = await request(spec.event, {
268
+ dependsOn: normalized.dependsOn,
269
+ input: input ?? normalized.input
270
+ });
271
+ return hydrateByNode({
272
+ value: result,
273
+ node: spec.output,
274
+ registry: fieldRegistry,
275
+ request
276
+ });
277
+ };
278
+ });
279
+ });
280
+ return Object.assign(client, {
281
+ __register: registerHandlers,
282
+ __toggle: (event, enabled, roomId)=>toggleHandler({
283
+ event,
284
+ enabled,
285
+ roomId
286
+ }),
287
+ emitEvent: dispatch
288
+ });
289
+ };
290
+ const createClient = (input)=>{
291
+ const client = createClientCore({
292
+ ast: input.ast
293
+ });
294
+ const register = (registry)=>{
295
+ const requestKey = input.requestKey ?? DEFAULT_REQUEST_KEY;
296
+ setClientRequest({
297
+ client,
298
+ registry,
299
+ requestKey
300
+ });
301
+ registry.onReceive((envelope, _ctx, next)=>{
302
+ client.emitEvent(buildClientEventEnvelope(envelope));
303
+ return next();
304
+ });
305
+ };
306
+ const moduleBase = {
307
+ name: input.name ?? 'client',
308
+ register
309
+ };
310
+ const moduleWithClient = Object.assign(moduleBase, client);
311
+ return moduleWithClient;
312
+ };
313
+ const createClientModule = (input)=>createClient(input);
314
+ export { clientModule, createClient, createClientModule };