@pixotope/query 0.0.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/index.cjs ADDED
@@ -0,0 +1,693 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
+ var __getProtoOf = Object.getPrototypeOf;
10
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
11
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
+ var __spreadValues = (a, b) => {
14
+ for (var prop in b || (b = {}))
15
+ if (__hasOwnProp.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ if (__getOwnPropSymbols)
18
+ for (var prop of __getOwnPropSymbols(b)) {
19
+ if (__propIsEnum.call(b, prop))
20
+ __defNormalProp(a, prop, b[prop]);
21
+ }
22
+ return a;
23
+ };
24
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
+ var __objRest = (source, exclude) => {
26
+ var target = {};
27
+ for (var prop in source)
28
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
29
+ target[prop] = source[prop];
30
+ if (source != null && __getOwnPropSymbols)
31
+ for (var prop of __getOwnPropSymbols(source)) {
32
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
33
+ target[prop] = source[prop];
34
+ }
35
+ return target;
36
+ };
37
+ var __commonJS = (cb, mod) => function __require() {
38
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
39
+ };
40
+ var __export = (target, all) => {
41
+ for (var name in all)
42
+ __defProp(target, name, { get: all[name], enumerable: true });
43
+ };
44
+ var __copyProps = (to, from, except, desc) => {
45
+ if (from && typeof from === "object" || typeof from === "function") {
46
+ for (let key of __getOwnPropNames(from))
47
+ if (!__hasOwnProp.call(to, key) && key !== except)
48
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
49
+ }
50
+ return to;
51
+ };
52
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
53
+ // If the importer is in node compatibility mode or this is not an ESM
54
+ // file that has been converted to a CommonJS file using a Babel-
55
+ // compatible transform (i.e. "__esModule" has not been set), then set
56
+ // "default" to the CommonJS "module.exports" for node compatibility.
57
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
58
+ mod
59
+ ));
60
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
61
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
62
+ var __async = (__this, __arguments, generator) => {
63
+ return new Promise((resolve, reject) => {
64
+ var fulfilled = (value) => {
65
+ try {
66
+ step(generator.next(value));
67
+ } catch (e) {
68
+ reject(e);
69
+ }
70
+ };
71
+ var rejected = (value) => {
72
+ try {
73
+ step(generator.throw(value));
74
+ } catch (e) {
75
+ reject(e);
76
+ }
77
+ };
78
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
79
+ step((generator = generator.apply(__this, __arguments)).next());
80
+ });
81
+ };
82
+
83
+ // ../utils/dist/comparison/index.js
84
+ var require_comparison = __commonJS({
85
+ "../utils/dist/comparison/index.js"(exports2, module2) {
86
+ "use strict";
87
+ var __defProp2 = Object.defineProperty;
88
+ var __getOwnPropDesc2 = Object.getOwnPropertyDescriptor;
89
+ var __getOwnPropNames2 = Object.getOwnPropertyNames;
90
+ var __hasOwnProp2 = Object.prototype.hasOwnProperty;
91
+ var __export2 = (target, all) => {
92
+ for (var name in all)
93
+ __defProp2(target, name, { get: all[name], enumerable: true });
94
+ };
95
+ var __copyProps2 = (to, from, except, desc) => {
96
+ if (from && typeof from === "object" || typeof from === "function") {
97
+ for (let key of __getOwnPropNames2(from))
98
+ if (!__hasOwnProp2.call(to, key) && key !== except)
99
+ __defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
100
+ }
101
+ return to;
102
+ };
103
+ var __toCommonJS2 = (mod) => __copyProps2(__defProp2({}, "__esModule", { value: true }), mod);
104
+ var comparison_exports = {};
105
+ __export2(comparison_exports, {
106
+ shallowEqual: () => shallowEqual2
107
+ });
108
+ module2.exports = __toCommonJS2(comparison_exports);
109
+ function shallowEqual2(objA, objB) {
110
+ if (Object.is(objA, objB)) {
111
+ return true;
112
+ }
113
+ if (typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null) {
114
+ return false;
115
+ }
116
+ if (objA instanceof Map && objB instanceof Map) {
117
+ if (objA.size !== objB.size) return false;
118
+ for (const [key, value] of objA) {
119
+ if (!Object.is(value, objB.get(key))) {
120
+ return false;
121
+ }
122
+ }
123
+ return true;
124
+ }
125
+ if (objA instanceof Set && objB instanceof Set) {
126
+ if (objA.size !== objB.size) return false;
127
+ for (const value of objA) {
128
+ if (!objB.has(value)) {
129
+ return false;
130
+ }
131
+ }
132
+ return true;
133
+ }
134
+ const keysA = Object.keys(objA);
135
+ if (keysA.length !== Object.keys(objB).length) {
136
+ return false;
137
+ }
138
+ for (let i = 0; i < keysA.length; i++) {
139
+ if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !Object.is(objA[keysA[i]], objB[keysA[i]])) {
140
+ return false;
141
+ }
142
+ }
143
+ return true;
144
+ }
145
+ }
146
+ });
147
+
148
+ // src/index.ts
149
+ var index_exports = {};
150
+ __export(index_exports, {
151
+ bindClientSocketProxy: () => bindClientSocketProxy,
152
+ buildMutationHook: () => buildMutationHook,
153
+ buildQueryHook: () => buildQueryHook,
154
+ buildServiceSubscriptionHook: () => buildServiceSubscriptionHook,
155
+ useQuery: () => useQuery,
156
+ useSubscription: () => useSubscription
157
+ });
158
+ module.exports = __toCommonJS(index_exports);
159
+
160
+ // src/factories.ts
161
+ var import_react3 = require("react");
162
+
163
+ // src/useMutation.ts
164
+ var import_react = require("react");
165
+ var ComparisonFailError = class _ComparisonFailError extends Error {
166
+ constructor(message, payload) {
167
+ super(message);
168
+ __publicField(this, "payload");
169
+ this.name = this.constructor.name;
170
+ this.payload = payload;
171
+ if (Error.captureStackTrace) {
172
+ Error.captureStackTrace(this, _ComparisonFailError);
173
+ }
174
+ }
175
+ };
176
+ var useMutation = (options) => {
177
+ const {
178
+ mutationFn,
179
+ compareOrThrow,
180
+ onError,
181
+ onSettled,
182
+ onSuccess,
183
+ prepareParams,
184
+ params,
185
+ timeout
186
+ } = options;
187
+ const [status, setStatus] = (0, import_react.useState)("idle");
188
+ const [error, setError] = (0, import_react.useState)(null);
189
+ const [result, setResult] = (0, import_react.useState)(void 0);
190
+ const mutationFnRef = (0, import_react.useRef)(mutationFn);
191
+ const internalOptionsRef = (0, import_react.useRef)({
192
+ params,
193
+ timeout
194
+ });
195
+ const internalCallbackRefistry = {
196
+ onSuccess,
197
+ onError,
198
+ onSettled,
199
+ compareOrThrow,
200
+ prepareParams
201
+ };
202
+ const internalCallbackRef = (0, import_react.useRef)(internalCallbackRefistry);
203
+ internalCallbackRef.current = internalCallbackRefistry;
204
+ const mutateAsync = (0, import_react.useCallback)(
205
+ (params2, overrideTargetMachine) => __async(null, null, function* () {
206
+ var _a, _b, _c, _d, _e, _f;
207
+ setStatus("loading");
208
+ setError(null);
209
+ try {
210
+ const promises = [
211
+ mutationFnRef.current(
212
+ internalCallbackRef.current.prepareParams ? internalCallbackRef.current.prepareParams(
213
+ params2,
214
+ internalOptionsRef.current.params
215
+ ) : params2,
216
+ overrideTargetMachine
217
+ ),
218
+ ...internalOptionsRef.current.timeout ? [
219
+ new Promise(
220
+ (resolve) => setTimeout(
221
+ () => resolve("timeout"),
222
+ internalOptionsRef.current.timeout
223
+ )
224
+ )
225
+ ] : []
226
+ ];
227
+ const res = yield Promise.race(promises);
228
+ if (res === "timeout") {
229
+ throw new Error("timeout");
230
+ }
231
+ if ((_b = (_a = internalCallbackRef.current).compareOrThrow) == null ? void 0 : _b.call(_a, res)) {
232
+ throw new ComparisonFailError(JSON.stringify(res), res);
233
+ }
234
+ setResult(res);
235
+ setStatus("success");
236
+ (_d = (_c = internalCallbackRef.current).onSuccess) == null ? void 0 : _d.call(_c, res, params2);
237
+ (_f = (_e = internalCallbackRef.current).onSettled) == null ? void 0 : _f.call(_e, res, null, params2);
238
+ return res;
239
+ } catch (error2) {
240
+ setError(error2);
241
+ setStatus("error");
242
+ if (internalCallbackRef.current.onError) {
243
+ internalCallbackRef.current.onError(error2, params2);
244
+ }
245
+ if (internalCallbackRef.current.onSettled) {
246
+ internalCallbackRef.current.onSettled(void 0, error2, params2);
247
+ }
248
+ throw error2;
249
+ }
250
+ }),
251
+ []
252
+ );
253
+ const mutate = (0, import_react.useCallback)(
254
+ (variables, options2) => {
255
+ mutateAsync(variables, options2 == null ? void 0 : options2.overrideTargetMachine).then(
256
+ (data) => {
257
+ var _a, _b;
258
+ (_a = options2 == null ? void 0 : options2.onSuccess) == null ? void 0 : _a.call(options2, data, variables);
259
+ (_b = options2 == null ? void 0 : options2.onSettled) == null ? void 0 : _b.call(options2, data, null, variables);
260
+ },
261
+ (error2) => {
262
+ var _a, _b;
263
+ (_a = options2 == null ? void 0 : options2.onError) == null ? void 0 : _a.call(options2, error2, variables);
264
+ (_b = options2 == null ? void 0 : options2.onSettled) == null ? void 0 : _b.call(options2, void 0, error2, variables);
265
+ }
266
+ );
267
+ },
268
+ [mutateAsync]
269
+ );
270
+ return {
271
+ mutateAsync,
272
+ status,
273
+ result,
274
+ mutate,
275
+ reset: () => setStatus("idle"),
276
+ error
277
+ };
278
+ };
279
+
280
+ // src/useQuery.ts
281
+ var import_react2 = require("react");
282
+ function normalizeQueryKey(queryKey) {
283
+ return JSON.stringify(queryKey);
284
+ }
285
+ var useQuery = (_a) => {
286
+ var _b = _a, {
287
+ queryFn
288
+ } = _b, options = __objRest(_b, [
289
+ "queryFn"
290
+ ]);
291
+ const {
292
+ enabled = true,
293
+ refetchOnWindowFocus = false,
294
+ refetchInterval = 0,
295
+ initialData,
296
+ select,
297
+ onError,
298
+ onSuccess,
299
+ onSettled,
300
+ timeout,
301
+ compareOrThrow,
302
+ stateless,
303
+ key: queryKeyPassed
304
+ } = options;
305
+ const queryKey = normalizeQueryKey(queryKeyPassed != null ? queryKeyPassed : []);
306
+ const promiseResolutionInProgress = (0, import_react2.useRef)(false);
307
+ const lastFetchedAt = (0, import_react2.useRef)(null);
308
+ const lastQueryKey = (0, import_react2.useRef)(void 0);
309
+ const queryFnRef = (0, import_react2.useRef)(queryFn);
310
+ queryFnRef.current = queryFn;
311
+ const internalCallbackRef = (0, import_react2.useRef)({});
312
+ internalCallbackRef.current = {
313
+ onError,
314
+ onSuccess,
315
+ onSettled,
316
+ compareOrThrow,
317
+ select
318
+ };
319
+ const internalOptionsRef = (0, import_react2.useRef)({
320
+ enabled,
321
+ refetchOnWindowFocus,
322
+ refetchInterval,
323
+ compareOrThrow,
324
+ stateless,
325
+ timeout
326
+ });
327
+ internalOptionsRef.current = {
328
+ enabled,
329
+ refetchOnWindowFocus,
330
+ refetchInterval,
331
+ compareOrThrow,
332
+ stateless,
333
+ timeout
334
+ };
335
+ const [status, setStatus] = (0, import_react2.useState)(() => {
336
+ return "idle";
337
+ });
338
+ const [error, setError] = (0, import_react2.useState)(null);
339
+ const [data, setData] = (0, import_react2.useState)(() => {
340
+ if (initialData !== void 0) {
341
+ if (typeof initialData === "function") {
342
+ return initialData();
343
+ }
344
+ return initialData;
345
+ }
346
+ return void 0;
347
+ });
348
+ const handleSetData = (0, import_react2.useCallback)((data2) => {
349
+ if (internalOptionsRef.current.stateless) {
350
+ return;
351
+ }
352
+ setData(data2);
353
+ }, []);
354
+ const shouldRefetch = (0, import_react2.useCallback)(() => {
355
+ var _a2;
356
+ if (!internalOptionsRef.current.enabled) {
357
+ return false;
358
+ }
359
+ if (lastFetchedAt.current === null) {
360
+ return true;
361
+ }
362
+ return (/* @__PURE__ */ new Date()).getTime() - lastFetchedAt.current > ((_a2 = internalOptionsRef.current) == null ? void 0 : _a2.refetchInterval);
363
+ }, []);
364
+ const refetch = (0, import_react2.useCallback)(
365
+ (params = void 0) => __async(null, null, function* () {
366
+ var _a2, _b2, _c, _d, _e, _f, _g, _h;
367
+ if (promiseResolutionInProgress.current || !internalOptionsRef.current.enabled) {
368
+ return;
369
+ }
370
+ setStatus("loading");
371
+ setError(null);
372
+ try {
373
+ const promises = [
374
+ queryFnRef.current(params),
375
+ ...internalOptionsRef.current.timeout ? [
376
+ new Promise(
377
+ (resolve) => setTimeout(
378
+ () => resolve("timeout"),
379
+ internalOptionsRef.current.timeout
380
+ )
381
+ )
382
+ ] : []
383
+ ];
384
+ promiseResolutionInProgress.current = true;
385
+ const res = yield Promise.race(promises);
386
+ promiseResolutionInProgress.current = false;
387
+ if (res === "timeout") {
388
+ throw new Error("timeout");
389
+ }
390
+ handleSetData(res);
391
+ if (internalCallbackRef.current.compareOrThrow && internalCallbackRef.current.compareOrThrow(res) === true) {
392
+ throw new Error("compareOrThrow");
393
+ }
394
+ setStatus("success");
395
+ lastFetchedAt.current = (/* @__PURE__ */ new Date()).getTime();
396
+ (_b2 = (_a2 = internalCallbackRef.current).onSuccess) == null ? void 0 : _b2.call(_a2, res);
397
+ (_d = (_c = internalCallbackRef.current).onSettled) == null ? void 0 : _d.call(_c, res, void 0);
398
+ } catch (error2) {
399
+ setError(error2);
400
+ setStatus("error");
401
+ (_f = (_e = internalCallbackRef.current).onError) == null ? void 0 : _f.call(_e, error2);
402
+ (_h = (_g = internalCallbackRef.current).onSettled) == null ? void 0 : _h.call(_g, void 0, error2);
403
+ }
404
+ }),
405
+ [handleSetData]
406
+ );
407
+ const reset = (0, import_react2.useCallback)(() => {
408
+ setData(void 0);
409
+ setStatus("idle");
410
+ setError(null);
411
+ refetch();
412
+ }, [refetch]);
413
+ (0, import_react2.useEffect)(() => {
414
+ if (enabled) {
415
+ refetch();
416
+ }
417
+ }, [enabled, refetch, queryKey]);
418
+ (0, import_react2.useEffect)(() => {
419
+ if (lastQueryKey.current === void 0) {
420
+ lastQueryKey.current = queryKey;
421
+ refetch();
422
+ return;
423
+ }
424
+ if (queryKey !== lastQueryKey.current) {
425
+ lastQueryKey.current = queryKey;
426
+ refetch();
427
+ }
428
+ }, [queryKey, refetch, enabled]);
429
+ (0, import_react2.useEffect)(() => {
430
+ if (refetchOnWindowFocus) {
431
+ const queryHandleFocus = () => {
432
+ if (shouldRefetch()) {
433
+ refetch();
434
+ }
435
+ };
436
+ window.addEventListener("focus", queryHandleFocus);
437
+ return () => {
438
+ window.removeEventListener("focus", queryHandleFocus);
439
+ };
440
+ }
441
+ }, [refetchOnWindowFocus, refetch, shouldRefetch]);
442
+ (0, import_react2.useEffect)(() => {
443
+ if (refetchInterval !== 0) {
444
+ const interval = setInterval(
445
+ () => {
446
+ if (shouldRefetch()) {
447
+ refetch();
448
+ }
449
+ },
450
+ Math.max(refetchInterval, 1e3)
451
+ );
452
+ return () => {
453
+ clearInterval(interval);
454
+ };
455
+ }
456
+ }, [refetch, refetchInterval, shouldRefetch]);
457
+ return {
458
+ data: select && data !== void 0 ? select(data) : data,
459
+ error,
460
+ status,
461
+ refetch,
462
+ reset
463
+ };
464
+ };
465
+
466
+ // src/factories.ts
467
+ var useCallMutationBase = (clientProxy, functionName, service, options) => {
468
+ const _a = (0, import_react3.useMemo)(() => options != null ? options : {}, [options]), {
469
+ avoidTimeout = true,
470
+ customTimeout = void 0
471
+ } = _a, rest = __objRest(_a, [
472
+ "avoidTimeout",
473
+ "customTimeout"
474
+ ]);
475
+ const mutationFn = (0, import_react3.useCallback)(
476
+ (parameters) => __async(null, null, function* () {
477
+ try {
478
+ const res = yield clientProxy.callAsync({
479
+ service,
480
+ functionName: functionName.toString(),
481
+ parameters,
482
+ options: {
483
+ avoidTimeout,
484
+ customTimeout
485
+ }
486
+ });
487
+ return res.message.Result;
488
+ } catch (e) {
489
+ throw e.message;
490
+ }
491
+ }),
492
+ [avoidTimeout, customTimeout, functionName, service]
493
+ );
494
+ return useMutation(__spreadValues({
495
+ mutationFn
496
+ }, rest));
497
+ };
498
+ function buildMutationHook(socketClientProxy, genTargetService) {
499
+ return function useCallMutationBuilder(func, options) {
500
+ const targetService = genTargetService(options == null ? void 0 : options.machine);
501
+ return useCallMutationBase(socketClientProxy, func, targetService, options);
502
+ };
503
+ }
504
+ var useCallQueryBase = (clientProxy, functionName, params, service, options) => {
505
+ const _a = (0, import_react3.useMemo)(() => options != null ? options : {}, [options]), {
506
+ avoidTimeout = true,
507
+ customTimeout = void 0
508
+ } = _a, rest = __objRest(_a, [
509
+ "avoidTimeout",
510
+ "customTimeout"
511
+ ]);
512
+ const queryFn = (0, import_react3.useCallback)(
513
+ (refetchParams, overrideTargetMachine) => __async(null, null, function* () {
514
+ try {
515
+ const res = yield clientProxy.callAsync({
516
+ service: overrideTargetMachine != null ? overrideTargetMachine : service,
517
+ functionName: functionName.toString(),
518
+ parameters: refetchParams != null ? refetchParams : params,
519
+ options: {
520
+ avoidTimeout,
521
+ customTimeout
522
+ }
523
+ });
524
+ return res.message.Result;
525
+ } catch (e) {
526
+ if (e instanceof Error) {
527
+ throw e;
528
+ }
529
+ throw new Error(e);
530
+ }
531
+ }),
532
+ [avoidTimeout, customTimeout, functionName, service, params]
533
+ );
534
+ return useQuery(__spreadValues({
535
+ queryFn
536
+ }, rest));
537
+ };
538
+ function buildQueryHook(clientSocketProxy, getTargetService) {
539
+ return function useCallQueryBuilder(func, params, options) {
540
+ const targetService = getTargetService(options == null ? void 0 : options.machine);
541
+ return useCallQueryBase(clientSocketProxy, func, params, targetService, options);
542
+ };
543
+ }
544
+
545
+ // src/useSubscriptionCore.ts
546
+ var import_comparison = __toESM(require_comparison(), 1);
547
+ var import_react4 = require("react");
548
+ function useSubscriptionCore(options) {
549
+ const {
550
+ enabled = true,
551
+ initialState = void 0,
552
+ onSubscribed,
553
+ stateless,
554
+ compareOrThrow,
555
+ subscriberFn,
556
+ onMessage,
557
+ onError,
558
+ select,
559
+ isEqual = import_comparison.shallowEqual
560
+ } = options;
561
+ const [status, setStatus] = (0, import_react4.useState)("idle");
562
+ const [data, setData] = (0, import_react4.useState)(initialState);
563
+ const unsubcribeRef = (0, import_react4.useRef)(void 0);
564
+ const initialStateRef = (0, import_react4.useRef)(initialState);
565
+ const subscriberFnRef = (0, import_react4.useRef)(subscriberFn);
566
+ const callbackRefRegistry = {
567
+ onMessage,
568
+ onError,
569
+ select,
570
+ compareOrThrow,
571
+ onSubscribed,
572
+ isEqual
573
+ };
574
+ const internalCallbacksRef = (0, import_react4.useRef)(callbackRefRegistry);
575
+ internalCallbacksRef.current = callbackRefRegistry;
576
+ const updateStatus = (0, import_react4.useCallback)(
577
+ (newStatus, message) => {
578
+ if (stateless) {
579
+ return;
580
+ }
581
+ setStatus((prev) => {
582
+ var _a, _b;
583
+ if (prev === newStatus) {
584
+ return prev;
585
+ }
586
+ if (newStatus === "subscribed" && message) {
587
+ (_b = (_a = internalCallbacksRef.current).onSubscribed) == null ? void 0 : _b.call(_a, message);
588
+ }
589
+ return newStatus;
590
+ });
591
+ },
592
+ [stateless]
593
+ );
594
+ const connection = (0, import_react4.useMemo)(
595
+ () => ({
596
+ acknowledge: () => {
597
+ updateStatus("subscribed");
598
+ }
599
+ }),
600
+ [updateStatus]
601
+ );
602
+ const handler = (0, import_react4.useCallback)(
603
+ (message) => __async(null, null, function* () {
604
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
605
+ if ((_b = (_a = internalCallbacksRef.current).compareOrThrow) == null ? void 0 : _b.call(_a, message)) {
606
+ (_d = (_c = internalCallbacksRef.current).onError) == null ? void 0 : _d.call(_c, message);
607
+ return;
608
+ }
609
+ const selectedMessage = (_g = (_f = (_e = internalCallbacksRef.current).select) == null ? void 0 : _f.call(_e, message)) != null ? _g : message;
610
+ if (!stateless) {
611
+ setData((prev) => {
612
+ if (isEqual(prev, selectedMessage)) {
613
+ return prev;
614
+ }
615
+ return selectedMessage;
616
+ });
617
+ }
618
+ updateStatus("subscribed", message);
619
+ (_i = (_h = internalCallbacksRef.current).onMessage) == null ? void 0 : _i.call(_h, message);
620
+ }),
621
+ [stateless, updateStatus]
622
+ );
623
+ const unsubscribe = (0, import_react4.useCallback)(() => {
624
+ var _a;
625
+ updateStatus("idle");
626
+ (_a = unsubcribeRef.current) == null ? void 0 : _a.call(unsubcribeRef);
627
+ unsubcribeRef.current = void 0;
628
+ }, [updateStatus]);
629
+ const subscribe = (0, import_react4.useCallback)(() => {
630
+ var _a;
631
+ (_a = unsubcribeRef.current) == null ? void 0 : _a.call(unsubcribeRef);
632
+ updateStatus("subscribing");
633
+ setData(initialStateRef.current);
634
+ unsubcribeRef.current = subscriberFnRef.current(handler, connection);
635
+ }, [connection, handler, updateStatus]);
636
+ (0, import_react4.useEffect)(() => {
637
+ if (enabled) {
638
+ subscribe();
639
+ } else {
640
+ unsubscribe();
641
+ }
642
+ return () => {
643
+ unsubscribe();
644
+ };
645
+ }, [enabled, subscribe, unsubscribe]);
646
+ return {
647
+ status,
648
+ data,
649
+ reSubscribe: subscribe
650
+ };
651
+ }
652
+
653
+ // src/useSubscription.ts
654
+ function useSubscription(subscriptionHandler, options) {
655
+ const { topic, service, maxDownwardDepth = 1e3 } = options;
656
+ return useSubscriptionCore(__spreadProps(__spreadValues({}, options), {
657
+ subscriberFn: (handler) => {
658
+ const unsubscribe = subscriptionHandler({
659
+ service,
660
+ topic,
661
+ callback: (message) => {
662
+ handler(message);
663
+ },
664
+ maxDownwardDepth
665
+ });
666
+ return unsubscribe;
667
+ }
668
+ }));
669
+ }
670
+ function buildServiceSubscriptionHook(subscriptionHandler, targetMachine) {
671
+ return function useSubscriptionBuilder(options) {
672
+ const targetService = targetMachine(options.machine);
673
+ return useSubscription(
674
+ subscriptionHandler,
675
+ __spreadValues({
676
+ service: targetService
677
+ }, options)
678
+ );
679
+ };
680
+ }
681
+ function bindClientSocketProxy(socketClientProxy) {
682
+ return (...args) => socketClientProxy.mirror(...args)[0];
683
+ }
684
+ // Annotate the CommonJS export names for ESM import in node:
685
+ 0 && (module.exports = {
686
+ bindClientSocketProxy,
687
+ buildMutationHook,
688
+ buildQueryHook,
689
+ buildServiceSubscriptionHook,
690
+ useQuery,
691
+ useSubscription
692
+ });
693
+ //# sourceMappingURL=index.cjs.map