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