@questpie/tanstack-query 3.1.0 → 3.2.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.
Files changed (2) hide show
  1. package/dist/index.mjs +55 -3
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -38,6 +38,40 @@ const wrapMutationFn = (mutationFn, errorMap) => {
38
38
  }
39
39
  };
40
40
  };
41
+ async function* streamRealtimeQuery(options) {
42
+ const { initialFetch, realtime, topic, signal, errorMap } = options;
43
+ const queue = [];
44
+ let resolveNext = null;
45
+ const notify = () => {
46
+ resolveNext?.();
47
+ resolveNext = null;
48
+ };
49
+ const handleAbort = () => notify();
50
+ const unsubscribe = realtime.subscribe(topic, (data) => {
51
+ queue.push(data);
52
+ notify();
53
+ }, signal);
54
+ signal?.addEventListener("abort", handleAbort, { once: true });
55
+ try {
56
+ try {
57
+ yield await initialFetch();
58
+ } catch (error) {
59
+ throw errorMap(error);
60
+ }
61
+ while (!signal?.aborted) {
62
+ while (queue.length > 0) yield queue.shift();
63
+ if (signal?.aborted) break;
64
+ await new Promise((resolve) => {
65
+ resolveNext = resolve;
66
+ });
67
+ resolveNext = null;
68
+ }
69
+ } finally {
70
+ signal?.removeEventListener("abort", handleAbort);
71
+ unsubscribe();
72
+ resolveNext = null;
73
+ }
74
+ }
41
75
  /**
42
76
  * Create type-safe query options proxy for TanStack Query
43
77
  *
@@ -83,7 +117,13 @@ function createQuestpieQueryOptions(client, config = {}) {
83
117
  return queryOptions({
84
118
  queryKey: qKey,
85
119
  queryFn: experimental_streamedQuery({
86
- streamFn: ({ signal }) => client.realtime.stream(topic, signal),
120
+ streamFn: ({ signal }) => streamRealtimeQuery({
121
+ initialFetch: () => collection.find(options),
122
+ realtime: client.realtime,
123
+ topic,
124
+ signal,
125
+ errorMap
126
+ }),
87
127
  reducer: (_, chunk) => chunk,
88
128
  initialValue: void 0,
89
129
  refetchMode: "append"
@@ -108,7 +148,13 @@ function createQuestpieQueryOptions(client, config = {}) {
108
148
  return queryOptions({
109
149
  queryKey: qKey,
110
150
  queryFn: experimental_streamedQuery({
111
- streamFn: ({ signal }) => client.realtime.stream(topic, signal),
151
+ streamFn: ({ signal }) => streamRealtimeQuery({
152
+ initialFetch: () => collection.count(options),
153
+ realtime: client.realtime,
154
+ topic,
155
+ signal,
156
+ errorMap
157
+ }),
112
158
  reducer: (_, chunk) => typeof chunk?.totalDocs === "number" ? chunk.totalDocs : chunk,
113
159
  initialValue: void 0,
114
160
  refetchMode: "append"
@@ -259,7 +305,13 @@ function createQuestpieQueryOptions(client, config = {}) {
259
305
  return queryOptions({
260
306
  queryKey: qKey,
261
307
  queryFn: experimental_streamedQuery({
262
- streamFn: ({ signal }) => client.realtime.stream(topic, signal),
308
+ streamFn: ({ signal }) => streamRealtimeQuery({
309
+ initialFetch: () => global.get(options),
310
+ realtime: client.realtime,
311
+ topic,
312
+ signal,
313
+ errorMap
314
+ }),
263
315
  reducer: (_, chunk) => chunk,
264
316
  initialValue: void 0,
265
317
  refetchMode: "append"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@questpie/tanstack-query",
3
- "version": "3.1.0",
3
+ "version": "3.2.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/questpie/questpie.git",