@instantdb/react 0.22.89-experimental.drewh-ssr.20285043124.1 → 0.22.89-experimental.sync-table-fixes.20282627123.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 (40) hide show
  1. package/dist/commonjs/index.d.ts +2 -2
  2. package/dist/commonjs/index.d.ts.map +1 -1
  3. package/dist/commonjs/index.js +1 -2
  4. package/dist/commonjs/index.js.map +1 -1
  5. package/dist/esm/index.d.ts +2 -2
  6. package/dist/esm/index.d.ts.map +1 -1
  7. package/dist/esm/index.js +2 -2
  8. package/dist/esm/index.js.map +1 -1
  9. package/dist/standalone/index.js +1962 -2079
  10. package/dist/standalone/index.umd.cjs +14 -14
  11. package/package.json +6 -18
  12. package/src/index.ts +0 -2
  13. package/tsconfig.json +0 -1
  14. package/dist/commonjs/next-ssr/HydrationStreamProvider.d.ts +0 -66
  15. package/dist/commonjs/next-ssr/HydrationStreamProvider.d.ts.map +0 -1
  16. package/dist/commonjs/next-ssr/HydrationStreamProvider.js +0 -135
  17. package/dist/commonjs/next-ssr/HydrationStreamProvider.js.map +0 -1
  18. package/dist/commonjs/next-ssr/htmlescape.d.ts +0 -3
  19. package/dist/commonjs/next-ssr/htmlescape.d.ts.map +0 -1
  20. package/dist/commonjs/next-ssr/htmlescape.js +0 -25
  21. package/dist/commonjs/next-ssr/htmlescape.js.map +0 -1
  22. package/dist/commonjs/next-ssr/index.d.ts +0 -50
  23. package/dist/commonjs/next-ssr/index.d.ts.map +0 -1
  24. package/dist/commonjs/next-ssr/index.js +0 -158
  25. package/dist/commonjs/next-ssr/index.js.map +0 -1
  26. package/dist/esm/next-ssr/HydrationStreamProvider.d.ts +0 -66
  27. package/dist/esm/next-ssr/HydrationStreamProvider.d.ts.map +0 -1
  28. package/dist/esm/next-ssr/HydrationStreamProvider.js +0 -98
  29. package/dist/esm/next-ssr/HydrationStreamProvider.js.map +0 -1
  30. package/dist/esm/next-ssr/htmlescape.d.ts +0 -3
  31. package/dist/esm/next-ssr/htmlescape.d.ts.map +0 -1
  32. package/dist/esm/next-ssr/htmlescape.js +0 -21
  33. package/dist/esm/next-ssr/htmlescape.js.map +0 -1
  34. package/dist/esm/next-ssr/index.d.ts +0 -50
  35. package/dist/esm/next-ssr/index.d.ts.map +0 -1
  36. package/dist/esm/next-ssr/index.js +0 -148
  37. package/dist/esm/next-ssr/index.js.map +0 -1
  38. package/src/next-ssr/HydrationStreamProvider.tsx +0 -193
  39. package/src/next-ssr/htmlescape.ts +0 -24
  40. package/src/next-ssr/index.tsx +0 -264
@@ -1,24 +0,0 @@
1
- // --------------------------------------------------------------------------------
2
- //
3
- // copied from
4
- // https://github.com/vercel/next.js/blob/6bc07792a4462a4bf921a72ab30dc4ab2c4e1bda/packages/next/src/server/htmlescape.ts
5
- // License: https://github.com/vercel/next.js/blob/6bc07792a4462a4bf921a72ab30dc4ab2c4e1bda/packages/next/license.md
6
- //
7
- // --------------------------------------------------------------------------------
8
-
9
- // This utility is based on https://github.com/zertosh/htmlescape
10
- // License: https://github.com/zertosh/htmlescape/blob/0527ca7156a524d256101bb310a9f970f63078ad/LICENSE
11
-
12
- const ESCAPE_LOOKUP: Record<string, string> = {
13
- '&': '\\u0026',
14
- '>': '\\u003e',
15
- '<': '\\u003c',
16
- '\u2028': '\\u2028',
17
- '\u2029': '\\u2029',
18
- };
19
-
20
- export const ESCAPE_REGEX = /[&><\u2028\u2029]/g;
21
-
22
- export function htmlEscapeJsonString(str: string): string {
23
- return str.replace(ESCAPE_REGEX, (match) => ESCAPE_LOOKUP[match]!);
24
- }
@@ -1,264 +0,0 @@
1
- 'use client';
2
- import {
3
- AuthState,
4
- FrameworkClient,
5
- InstantConfig,
6
- InstantSchemaDef,
7
- InstantUnknownSchema,
8
- InstaQLResponse,
9
- PageInfoResponse,
10
- RuleParams,
11
- User,
12
- ValidQuery,
13
- } from '@instantdb/core';
14
- import { createContext, useContext, useRef, useState } from 'react';
15
- import {
16
- createHydrationStreamProvider,
17
- isServer,
18
- } from './HydrationStreamProvider.tsx';
19
- import version from '../version.ts';
20
-
21
- import InstantReactWebDatabase from '../InstantReactWebDatabase.ts';
22
- import { InstantReactAbstractDatabase } from '@instantdb/react-common';
23
-
24
- type InstantSuspenseProviderProps<
25
- Schema extends InstantSchemaDef<any, any, any>,
26
- > = {
27
- nonce?: string;
28
- children: React.ReactNode;
29
- db?: InstantReactWebDatabase<Schema, any>;
30
- config?: Omit<InstantConfig<any, any>, 'schema'> & {
31
- schema: string;
32
- };
33
- user?: User | null;
34
- };
35
-
36
- const stream = createHydrationStreamProvider<any>();
37
-
38
- type SuspenseQueryContextValue = {
39
- useSuspenseQuery: (query: any, opts?: SuspenseQueryOpts) => any;
40
- ssrUser: User | null | undefined;
41
- };
42
-
43
- const SuspsenseQueryContext = createContext<SuspenseQueryContextValue | null>(
44
- null,
45
- );
46
-
47
- // Creates a typed useSuspense hook
48
- export const createUseSuspenseQuery = <
49
- Schema extends InstantSchemaDef<any, any, any>,
50
- UseDates extends boolean,
51
- >(
52
- _db: InstantReactWebDatabase<Schema, UseDates>,
53
- ): (<Q extends ValidQuery<Q, Schema>>(
54
- q: Q,
55
- opts?: {
56
- ruleParams: RuleParams;
57
- },
58
- ) => {
59
- data: InstaQLResponse<Schema, Q, NonNullable<UseDates>>;
60
- pageInfo?: PageInfoResponse<Q>;
61
- }) => {
62
- return <Q extends ValidQuery<Q, Schema>>(q: any, opts: any) => {
63
- const ctx = useContext(SuspsenseQueryContext);
64
- if (!ctx) {
65
- throw new Error(
66
- 'useSuspenseQuery must be used within a SuspenseQueryProvider',
67
- );
68
- }
69
- return ctx.useSuspenseQuery(q, opts) as any;
70
- };
71
- };
72
-
73
- type SuspenseQueryOpts = {
74
- ruleParams: RuleParams;
75
- };
76
-
77
- export const InstantSuspenseProvider = (
78
- props: InstantSuspenseProviderProps<any>,
79
- ) => {
80
- const clientRef = useRef<FrameworkClient | null>(null);
81
-
82
- if (!props.db) {
83
- throw new Error(
84
- 'Must provide either a db or config to InstantSuspenseProvider',
85
- );
86
- }
87
-
88
- const db = useRef<InstantReactAbstractDatabase<any, any>>(props.db);
89
-
90
- const [trackedKeys] = useState(() => new Set<string>());
91
-
92
- if (!clientRef.current) {
93
- if (props.user && !props.user.refresh_token) {
94
- throw new Error(
95
- 'User must have a refresh_token field. Recieved: ' +
96
- JSON.stringify(props.user, null, 2),
97
- );
98
- }
99
- clientRef.current = new FrameworkClient({
100
- token: props.user?.refresh_token,
101
- db: db.current.core,
102
- });
103
- }
104
-
105
- if (isServer) {
106
- clientRef.current.subscribe((result) => {
107
- const { queryHash } = result;
108
- trackedKeys.add(queryHash);
109
- });
110
- }
111
-
112
- const useSuspenseQuery = (query: any, opts: SuspenseQueryOpts) => {
113
- const nonSuspenseResult = db.current.useQuery(query, {
114
- ...opts,
115
- });
116
-
117
- if (nonSuspenseResult.data) {
118
- return {
119
- data: nonSuspenseResult.data,
120
- pageInfo: nonSuspenseResult.pageInfo,
121
- };
122
- }
123
-
124
- // should never happen (typeguard)
125
- if (!clientRef.current) {
126
- throw new Error('Client ref not set up');
127
- }
128
-
129
- let entry = clientRef.current.getExistingResultForQuery(query, {
130
- ruleParams: opts?.ruleParams,
131
- });
132
-
133
- if (!entry) {
134
- entry = clientRef.current!.query(query, opts);
135
- }
136
-
137
- if (entry.status === 'pending') {
138
- throw entry.promise;
139
- }
140
-
141
- if (entry.status === 'error') {
142
- throw entry.error;
143
- }
144
-
145
- if (entry.status === 'success') {
146
- const data = entry.data;
147
- const result = clientRef.current.completeIsomorphic(
148
- query,
149
- data.triples,
150
- data.attrs,
151
- data.pageInfo,
152
- );
153
-
154
- return result;
155
- }
156
- };
157
-
158
- return (
159
- <SuspsenseQueryContext.Provider
160
- value={{ useSuspenseQuery, ssrUser: props.user }}
161
- >
162
- <stream.Provider
163
- nonce={props.nonce}
164
- onFlush={() => {
165
- const toSend: { queryKey: string; value: any }[] = [];
166
- for (const [key, value] of clientRef.current!.resultMap.entries()) {
167
- if (trackedKeys.has(key) && value.status === 'success') {
168
- toSend.push({
169
- queryKey: key,
170
- value: value.data,
171
- });
172
- }
173
- }
174
-
175
- trackedKeys.clear();
176
- return toSend;
177
- }}
178
- onEntries={(entries) => {
179
- entries.forEach((entry) => {
180
- clientRef.current!.addQueryResult(entry.queryKey, entry.value);
181
- });
182
- }}
183
- >
184
- {props.children}
185
- </stream.Provider>
186
- </SuspsenseQueryContext.Provider>
187
- );
188
- };
189
-
190
- /**
191
- *
192
- * The first step: init your application!
193
- *
194
- * Visit https://instantdb.com/dash to get your `appId` :)
195
- *
196
- * @example
197
- * import { init } from "@instantdb/react"
198
- *
199
- * const db = init({ appId: "my-app-id" })
200
- *
201
- * // You can also provide a schema for type safety and editor autocomplete!
202
- *
203
- * import { init } from "@instantdb/react"
204
- * import schema from ""../instant.schema.ts";
205
- *
206
- * const db = init({ appId: "my-app-id", schema })
207
- *
208
- * // To learn more: https://instantdb.com/docs/modeling-data
209
- */
210
- export function init<
211
- Schema extends InstantSchemaDef<any, any, any> = InstantUnknownSchema,
212
- UseDates extends boolean = false,
213
- >(
214
- config: InstantConfig<Schema, UseDates>,
215
- ): InstantNextDatabase<Schema, UseDates> {
216
- return new InstantNextDatabase<Schema, UseDates>(config, {
217
- '@instantdb/react': version,
218
- });
219
- }
220
-
221
- export class InstantNextDatabase<
222
- Schema extends InstantSchemaDef<any, any, any>,
223
- UseDates extends boolean,
224
- > extends InstantReactWebDatabase<Schema, UseDates> {
225
- public useSuspenseQuery = <Q extends ValidQuery<Q, Schema>>(
226
- q: Q,
227
- opts?: {
228
- ruleParams: RuleParams;
229
- },
230
- ): {
231
- data: InstaQLResponse<Schema, Q, NonNullable<UseDates>>;
232
- pageInfo?: PageInfoResponse<Q>;
233
- } => {
234
- const ctx = useContext(SuspsenseQueryContext);
235
- if (!ctx) {
236
- throw new Error(
237
- 'useSuspenseQuery must be used within a SuspenseQueryProvider',
238
- );
239
- }
240
- return ctx.useSuspenseQuery(q, opts) as any;
241
- };
242
-
243
- useAuth = (): AuthState => {
244
- const ctx = useContext(SuspsenseQueryContext);
245
- const realAuthResult = this._useAuth();
246
- if (!ctx) {
247
- return realAuthResult;
248
- }
249
-
250
- const { ssrUser } = ctx;
251
- if (ssrUser === undefined) {
252
- return realAuthResult;
253
- }
254
- if (realAuthResult.isLoading) {
255
- return {
256
- error: undefined,
257
- isLoading: false,
258
- user: ssrUser ?? undefined, // null -> undefined for the response
259
- };
260
- }
261
-
262
- return realAuthResult;
263
- };
264
- }