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