@orpc/swr 0.0.0 → 2.0.0-beta.21

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.d.mts CHANGED
@@ -53,13 +53,15 @@ type FetcherOptions<TClientContext extends ClientContext> = object extends TClie
53
53
  };
54
54
  type SubscriberOptions<TClientContext extends ClientContext> = FetcherOptions<TClientContext> & {
55
55
  /**
56
- * Determines how data is handled when the subscription refetches.
57
- * - `reset`: Replace existing data with new data
58
- * - `append`: Add new data to existing data
56
+ * Determines how data is handled when the subscription is subscribed again
57
+ *
58
+ * - `'reset'`: Clears existing data before streaming new data.
59
+ * - `'append'`: Adds new streamed chunks to the existing data.
60
+ * - `'replace'`: Buffers streamed data and replaces existing data after the stream completes.
59
61
  *
60
62
  * @default 'reset'
61
63
  */
62
- refetchMode?: 'reset' | 'append';
64
+ refetchMode?: 'append' | 'reset' | 'replace';
63
65
  /**
64
66
  * Maximum number of chunks to store.
65
67
  * When exceeded, the oldest chunks will be removed.
package/dist/index.d.ts CHANGED
@@ -53,13 +53,15 @@ type FetcherOptions<TClientContext extends ClientContext> = object extends TClie
53
53
  };
54
54
  type SubscriberOptions<TClientContext extends ClientContext> = FetcherOptions<TClientContext> & {
55
55
  /**
56
- * Determines how data is handled when the subscription refetches.
57
- * - `reset`: Replace existing data with new data
58
- * - `append`: Add new data to existing data
56
+ * Determines how data is handled when the subscription is subscribed again
57
+ *
58
+ * - `'reset'`: Clears existing data before streaming new data.
59
+ * - `'append'`: Adds new streamed chunks to the existing data.
60
+ * - `'replace'`: Buffers streamed data and replaces existing data after the stream completes.
59
61
  *
60
62
  * @default 'reset'
61
63
  */
62
- refetchMode?: 'reset' | 'append';
64
+ refetchMode?: 'append' | 'reset' | 'replace';
63
65
  /**
64
66
  * Maximum number of chunks to store.
65
67
  * When exceeded, the oldest chunks will be removed.
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { isPlainObject, resolveMaybeOptionalOptions, isAsyncIteratorObject, toArray, bindMethods, getOrBind, get, isTypescriptObject } from '@orpc/shared';
1
+ import { isPlainObject, resolveMaybeOptionalOptions, isAsyncIteratorObject, toArray, bindMethods, getOrBind, isTypescriptObject } from '@orpc/shared';
2
2
  import { RECURSIVE_CLIENT_UNWRAP_KEYS } from '@orpc/client';
3
3
 
4
4
  function generateOperationKey(path, options = {}) {
@@ -101,17 +101,35 @@ class ProcedureUtils extends SharedUtils {
101
101
  if (!isAsyncIteratorObject(iterator)) {
102
102
  throw new TypeError(".subscriber requires an AsyncIteratorObject output");
103
103
  }
104
+ let hasPreviousData = false;
104
105
  if (refetchMode === "reset") {
105
106
  next(void 0, void 0);
107
+ } else if (refetchMode === "replace") {
108
+ next(void 0, (old) => {
109
+ hasPreviousData = old !== void 0;
110
+ return old;
111
+ });
106
112
  }
113
+ const shouldUpdateDataDuringStream = refetchMode !== "replace" || !hasPreviousData;
114
+ let buffer = [];
107
115
  for await (const event of iterator) {
108
- next(void 0, (old) => {
109
- const newData = Array.isArray(old) ? [...old, event] : [event];
110
- if (typeof maxChunks === "number" && newData.length > maxChunks) {
111
- return newData.slice(newData.length - maxChunks);
116
+ if (shouldUpdateDataDuringStream) {
117
+ next(void 0, (old) => {
118
+ const newData = Array.isArray(old) ? [...old, event] : [event];
119
+ if (typeof maxChunks === "number" && newData.length > maxChunks) {
120
+ return newData.slice(newData.length - maxChunks);
121
+ }
122
+ return newData;
123
+ });
124
+ } else {
125
+ buffer = [...buffer, event];
126
+ if (typeof maxChunks === "number" && buffer.length > maxChunks) {
127
+ buffer = buffer.slice(buffer.length - maxChunks);
112
128
  }
113
- return newData;
114
- });
129
+ }
130
+ }
131
+ if (!shouldUpdateDataDuringStream) {
132
+ next(void 0, buffer);
115
133
  }
116
134
  } catch (error) {
117
135
  if (!controller.signal.aborted) {
@@ -182,7 +200,7 @@ function createRouterUtils(client, options = {}) {
182
200
  const recursive = new Proxy(utils, {
183
201
  get(target, prop) {
184
202
  const value = getOrBind(target, prop);
185
- const nextClient = get(client, [prop]);
203
+ const nextClient = client[prop];
186
204
  if (typeof prop !== "string" || RECURSIVE_CLIENT_UNWRAP_KEYS.has(prop) || !isTypescriptObject(nextClient)) {
187
205
  return value;
188
206
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/swr",
3
3
  "type": "module",
4
- "version": "0.0.0",
4
+ "version": "2.0.0-beta.21",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.dev",
7
7
  "repository": {
@@ -30,8 +30,8 @@
30
30
  "swr": ">=2.4.2"
31
31
  },
32
32
  "dependencies": {
33
- "@orpc/client": "*",
34
- "@orpc/shared": "*"
33
+ "@orpc/client": "2.0.0-beta.21",
34
+ "@orpc/shared": "2.0.0-beta.21"
35
35
  },
36
36
  "devDependencies": {
37
37
  "swr": "^2.4.2",