@instantdb/solidjs 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/.tshy/build.json +8 -0
- package/.tshy/commonjs.json +16 -0
- package/.tshy/esm.json +15 -0
- package/.turbo/turbo-build.log +69 -0
- package/dist/commonjs/InstantSolidDatabase.d.ts +156 -0
- package/dist/commonjs/InstantSolidDatabase.d.ts.map +1 -0
- package/dist/commonjs/InstantSolidDatabase.js +265 -0
- package/dist/commonjs/InstantSolidDatabase.js.map +1 -0
- package/dist/commonjs/InstantSolidRoom.d.ts +97 -0
- package/dist/commonjs/InstantSolidRoom.d.ts.map +1 -0
- package/dist/commonjs/InstantSolidRoom.js +200 -0
- package/dist/commonjs/InstantSolidRoom.js.map +1 -0
- package/dist/commonjs/index.d.ts +6 -0
- package/dist/commonjs/index.d.ts.map +1 -0
- package/dist/commonjs/index.js +24 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/version.d.ts +3 -0
- package/dist/commonjs/version.d.ts.map +1 -0
- package/dist/commonjs/version.js +5 -0
- package/dist/commonjs/version.js.map +1 -0
- package/dist/esm/InstantSolidDatabase.d.ts +156 -0
- package/dist/esm/InstantSolidDatabase.d.ts.map +1 -0
- package/dist/esm/InstantSolidDatabase.js +257 -0
- package/dist/esm/InstantSolidDatabase.js.map +1 -0
- package/dist/esm/InstantSolidRoom.d.ts +97 -0
- package/dist/esm/InstantSolidRoom.d.ts.map +1 -0
- package/dist/esm/InstantSolidRoom.js +191 -0
- package/dist/esm/InstantSolidRoom.js.map +1 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +18 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/version.d.ts +3 -0
- package/dist/esm/version.d.ts.map +1 -0
- package/dist/esm/version.js +3 -0
- package/dist/esm/version.js.map +1 -0
- package/dist/standalone/index.js +7010 -0
- package/dist/standalone/index.umd.cjs +7 -0
- package/package.json +63 -0
- package/src/InstantSolidDatabase.ts +363 -0
- package/src/InstantSolidRoom.ts +319 -0
- package/src/index.ts +191 -0
- package/src/version.ts +2 -0
- package/tsconfig.dev.json +14 -0
- package/tsconfig.json +45 -0
- package/vite.config.ts +20 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { txInit, init as core_init, coerceQuery, InstantError, } from '@instantdb/core';
|
|
2
|
+
import { createSignal, createEffect, onCleanup, createMemo } from 'solid-js';
|
|
3
|
+
import { InstantSolidRoom, rooms } from './InstantSolidRoom.js';
|
|
4
|
+
import version from './version.js';
|
|
5
|
+
const defaultState = {
|
|
6
|
+
isLoading: true,
|
|
7
|
+
data: undefined,
|
|
8
|
+
pageInfo: undefined,
|
|
9
|
+
error: undefined,
|
|
10
|
+
};
|
|
11
|
+
const defaultAuthState = {
|
|
12
|
+
isLoading: true,
|
|
13
|
+
user: undefined,
|
|
14
|
+
error: undefined,
|
|
15
|
+
};
|
|
16
|
+
function stateForResult(result) {
|
|
17
|
+
return {
|
|
18
|
+
isLoading: !Boolean(result),
|
|
19
|
+
data: undefined,
|
|
20
|
+
pageInfo: undefined,
|
|
21
|
+
error: undefined,
|
|
22
|
+
...(result ? result : {}),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export class InstantSolidDatabase {
|
|
26
|
+
tx = txInit();
|
|
27
|
+
auth;
|
|
28
|
+
storage;
|
|
29
|
+
core;
|
|
30
|
+
constructor(core) {
|
|
31
|
+
this.core = core;
|
|
32
|
+
this.auth = this.core.auth;
|
|
33
|
+
this.storage = this.core.storage;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Returns a unique ID for a given `name`. It's stored in local storage,
|
|
37
|
+
* so you will get the same ID across sessions.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* const deviceId = await db.getLocalId('device');
|
|
41
|
+
*/
|
|
42
|
+
getLocalId = (name) => {
|
|
43
|
+
return this.core.getLocalId(name);
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Use this to write data! You can create, update, delete, and link objects
|
|
47
|
+
*
|
|
48
|
+
* @see https://instantdb.com/docs/instaml
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const goalId = id();
|
|
52
|
+
* db.transact(db.tx.goals[goalId].update({title: "Get fit"}))
|
|
53
|
+
*/
|
|
54
|
+
transact = (chunks) => {
|
|
55
|
+
return this.core.transact(chunks);
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* One time query for the logged in state.
|
|
59
|
+
*
|
|
60
|
+
* @see https://instantdb.com/docs/auth
|
|
61
|
+
* @example
|
|
62
|
+
* const user = await db.getAuth();
|
|
63
|
+
* console.log('logged in as', user.email)
|
|
64
|
+
*/
|
|
65
|
+
getAuth() {
|
|
66
|
+
return this.core.getAuth();
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Use this for one-off queries.
|
|
70
|
+
* Returns local data if available, otherwise fetches from the server.
|
|
71
|
+
*
|
|
72
|
+
* @see https://instantdb.com/docs/instaql
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* const resp = await db.queryOnce({ goals: {} });
|
|
76
|
+
* console.log(resp.data.goals)
|
|
77
|
+
*/
|
|
78
|
+
queryOnce = (query, opts) => {
|
|
79
|
+
return this.core.queryOnce(query, opts);
|
|
80
|
+
};
|
|
81
|
+
// -----------
|
|
82
|
+
// Solid reactive hooks
|
|
83
|
+
/**
|
|
84
|
+
* Use this to query your data!
|
|
85
|
+
*
|
|
86
|
+
* @see https://instantdb.com/docs/instaql
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* const state = db.useQuery({ goals: {} });
|
|
90
|
+
* // state().isLoading, state().error, state().data
|
|
91
|
+
*/
|
|
92
|
+
useQuery = (query, opts) => {
|
|
93
|
+
const [state, setState] = createSignal(defaultState);
|
|
94
|
+
createEffect(() => {
|
|
95
|
+
if (!query) {
|
|
96
|
+
setState(() => defaultState);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
let q = query;
|
|
100
|
+
if (opts && 'ruleParams' in opts) {
|
|
101
|
+
q = { $$ruleParams: opts['ruleParams'], ...q };
|
|
102
|
+
}
|
|
103
|
+
const coerced = coerceQuery(q);
|
|
104
|
+
const prev = this.core._reactor.getPreviousResult(coerced);
|
|
105
|
+
if (prev) {
|
|
106
|
+
setState(() => stateForResult(prev));
|
|
107
|
+
}
|
|
108
|
+
const unsub = this.core.subscribeQuery(coerced, (result) => {
|
|
109
|
+
setState(() => Object.assign({
|
|
110
|
+
isLoading: false,
|
|
111
|
+
data: undefined,
|
|
112
|
+
pageInfo: undefined,
|
|
113
|
+
error: undefined,
|
|
114
|
+
}, result));
|
|
115
|
+
});
|
|
116
|
+
onCleanup(unsub);
|
|
117
|
+
});
|
|
118
|
+
return state;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Listen for the logged in state. This is useful
|
|
122
|
+
* for deciding when to show a login screen.
|
|
123
|
+
*
|
|
124
|
+
* @see https://instantdb.com/docs/auth
|
|
125
|
+
* @example
|
|
126
|
+
* function App() {
|
|
127
|
+
* const auth = db.useAuth();
|
|
128
|
+
* // auth().isLoading, auth().user, auth().error
|
|
129
|
+
* }
|
|
130
|
+
*/
|
|
131
|
+
useAuth = () => {
|
|
132
|
+
const [state, setState] = createSignal(this.core._reactor._currentUserCached ?? defaultAuthState);
|
|
133
|
+
createEffect(() => {
|
|
134
|
+
const unsub = this.core.subscribeAuth((auth) => {
|
|
135
|
+
setState({ isLoading: false, ...auth });
|
|
136
|
+
});
|
|
137
|
+
onCleanup(unsub);
|
|
138
|
+
});
|
|
139
|
+
return state;
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* Subscribe to the currently logged in user.
|
|
143
|
+
* If the user is not logged in, this will throw an Error.
|
|
144
|
+
*
|
|
145
|
+
* @see https://instantdb.com/docs/auth
|
|
146
|
+
* @example
|
|
147
|
+
* function UserDisplay() {
|
|
148
|
+
* const user = db.useUser();
|
|
149
|
+
* return <div>Logged in as: {user().email}</div>
|
|
150
|
+
* }
|
|
151
|
+
*/
|
|
152
|
+
useUser = () => {
|
|
153
|
+
const auth = this.useAuth();
|
|
154
|
+
return createMemo(() => {
|
|
155
|
+
const { user } = auth();
|
|
156
|
+
if (!user) {
|
|
157
|
+
throw new InstantError('useUser must be used within an auth-protected route');
|
|
158
|
+
}
|
|
159
|
+
return user;
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* Listen for connection status changes to Instant.
|
|
164
|
+
*
|
|
165
|
+
* @see https://www.instantdb.com/docs/patterns#connection-status
|
|
166
|
+
* @example
|
|
167
|
+
* function App() {
|
|
168
|
+
* const status = db.useConnectionStatus();
|
|
169
|
+
* return <div>Connection state: {status()}</div>
|
|
170
|
+
* }
|
|
171
|
+
*/
|
|
172
|
+
useConnectionStatus = () => {
|
|
173
|
+
const [status, setStatus] = createSignal(this.core._reactor.status);
|
|
174
|
+
createEffect(() => {
|
|
175
|
+
const unsub = this.core.subscribeConnectionStatus((newStatus) => {
|
|
176
|
+
setStatus(() => newStatus);
|
|
177
|
+
});
|
|
178
|
+
onCleanup(unsub);
|
|
179
|
+
});
|
|
180
|
+
return status;
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* A hook that returns a unique ID for a given `name`. localIds are
|
|
184
|
+
* stored in local storage, so you will get the same ID across sessions.
|
|
185
|
+
*
|
|
186
|
+
* Initially returns `null`, and then loads the localId.
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* const deviceId = db.useLocalId('device');
|
|
190
|
+
* // deviceId() is null initially, then the ID string
|
|
191
|
+
*/
|
|
192
|
+
useLocalId = (name) => {
|
|
193
|
+
const [localId, setLocalId] = createSignal(null);
|
|
194
|
+
createEffect(() => {
|
|
195
|
+
let mounted = true;
|
|
196
|
+
this.getLocalId(name).then((id) => {
|
|
197
|
+
if (mounted) {
|
|
198
|
+
setLocalId(() => id);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
onCleanup(() => {
|
|
202
|
+
mounted = false;
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
return localId;
|
|
206
|
+
};
|
|
207
|
+
/**
|
|
208
|
+
* Obtain a handle to a room, which allows you to listen to topics and presence data
|
|
209
|
+
*
|
|
210
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* const room = db.room('chat', roomId);
|
|
214
|
+
* const presence = db.rooms.usePresence(room);
|
|
215
|
+
*/
|
|
216
|
+
room(type = '_defaultRoomType', id = '_defaultRoomId') {
|
|
217
|
+
return new InstantSolidRoom(this.core, type, id);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Hooks for working with rooms
|
|
221
|
+
*
|
|
222
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* const room = db.room('chat', roomId);
|
|
226
|
+
* const presence = db.rooms.usePresence(room);
|
|
227
|
+
* const publish = db.rooms.usePublishTopic(room, 'emoji');
|
|
228
|
+
*/
|
|
229
|
+
rooms = rooms;
|
|
230
|
+
}
|
|
231
|
+
// -----------
|
|
232
|
+
// init
|
|
233
|
+
/**
|
|
234
|
+
* The first step: init your application!
|
|
235
|
+
*
|
|
236
|
+
* Visit https://instantdb.com/dash to get your `appId` :)
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* import { init } from "@instantdb/solidjs"
|
|
240
|
+
*
|
|
241
|
+
* const db = init({ appId: "my-app-id" })
|
|
242
|
+
*
|
|
243
|
+
* // You can also provide a schema for type safety and editor autocomplete!
|
|
244
|
+
*
|
|
245
|
+
* import { init } from "@instantdb/solidjs"
|
|
246
|
+
* import schema from "../instant.schema.ts";
|
|
247
|
+
*
|
|
248
|
+
* const db = init({ appId: "my-app-id", schema })
|
|
249
|
+
*/
|
|
250
|
+
export function init(config) {
|
|
251
|
+
const coreDb = core_init(config, undefined, undefined, {
|
|
252
|
+
'@instantdb/solidjs': version,
|
|
253
|
+
});
|
|
254
|
+
return new InstantSolidDatabase(coreDb);
|
|
255
|
+
}
|
|
256
|
+
export const init_experimental = init;
|
|
257
|
+
//# sourceMappingURL=InstantSolidDatabase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InstantSolidDatabase.js","sourceRoot":"","sources":["../../src/InstantSolidDatabase.ts"],"names":[],"mappings":"AAAA,OAAO,EAgBL,MAAM,EAEN,IAAI,IAAI,SAAS,EACjB,WAAW,EAGX,YAAY,GAEb,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAG7E,OAAO,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAChE,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,MAAM,YAAY,GAAG;IACnB,SAAS,EAAE,IAAI;IACf,IAAI,EAAE,SAAS;IACf,QAAQ,EAAE,SAAS;IACnB,KAAK,EAAE,SAAS;CACR,CAAC;AAEX,MAAM,gBAAgB,GAAc;IAClC,SAAS,EAAE,IAAI;IACf,IAAI,EAAE,SAAS;IACf,KAAK,EAAE,SAAS;CACjB,CAAC;AAEF,SAAS,cAAc,CAAC,MAAW;IACjC,OAAO;QACL,SAAS,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC;QAC3B,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,SAAS;QACnB,KAAK,EAAE,SAAS;QAChB,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC;KAC1B,CAAC;AACJ,CAAC;AAED,MAAM,OAAO,oBAAoB;IAMxB,EAAE,GAAG,MAAM,EAAU,CAAC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AAU,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;gBACvB,CAAC;YACH,CAAC,CAAC,CAAC;YACH,SAAS,CAAC,GAAG,EAAE;gBACb,OAAO,GAAG,KAAK,CAAC;YAClB,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC;IAEF;;;;;;;;OAQG;IACH,IAAI,CACF,OAAiB,kBAA8B,EAC/C,KAAa,gBAAgB;QAE7B,OAAO,IAAI,gBAAgB,CAA0B,IAAI,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;IAC5E,CAAC;IAED;;;;;;;;;OASG;IACH,KAAK,GAAG,KAAK,CAAC;CACf;AAED,cAAc;AACd,OAAO;AAEP;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,IAAI,CAIlB,MAEC;IAED,MAAM,MAAM,GAAG,SAAS,CAAmB,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE;QACvE,oBAAoB,EAAE,OAAO;KAC9B,CAAC,CAAC;IACH,OAAO,IAAI,oBAAoB,CAAmB,MAAM,CAAC,CAAC;AAC5D,CAAC;AAED,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,CAAC","sourcesContent":["import {\n // types\n type AuthState,\n type User,\n type ConnectionStatus,\n type TransactionChunk,\n type RoomSchemaShape,\n type InstaQLOptions,\n type InstantConfig,\n type PageInfoResponse,\n type InstaQLLifecycleState,\n type InstaQLResponse,\n type ValidQuery,\n // classes\n Auth,\n Storage,\n txInit,\n InstantCoreDatabase,\n init as core_init,\n coerceQuery,\n InstantSchemaDef,\n RoomsOf,\n InstantError,\n IInstantDatabase,\n} from '@instantdb/core';\n\nimport { createSignal, createEffect, onCleanup, createMemo } from 'solid-js';\nimport type { Accessor } from 'solid-js';\n\nimport { InstantSolidRoom, rooms } from './InstantSolidRoom.js';\nimport version from './version.js';\n\nconst defaultState = {\n isLoading: true,\n data: undefined,\n pageInfo: undefined,\n error: undefined,\n} as const;\n\nconst defaultAuthState: AuthState = {\n isLoading: true,\n user: undefined,\n error: undefined,\n};\n\nfunction stateForResult(result: any) {\n return {\n isLoading: !Boolean(result),\n data: undefined,\n pageInfo: undefined,\n error: undefined,\n ...(result ? result : {}),\n };\n}\n\nexport class InstantSolidDatabase<\n Schema extends InstantSchemaDef<any, any, any>,\n UseDates extends boolean = false,\n Rooms extends RoomSchemaShape = RoomsOf<Schema>,\n> implements IInstantDatabase<Schema>\n{\n public tx = txInit<Schema>();\n\n public auth: Auth;\n public storage: Storage;\n public core: InstantCoreDatabase<Schema, UseDates>;\n\n constructor(core: InstantCoreDatabase<Schema, UseDates>) {\n this.core = core;\n this.auth = this.core.auth;\n this.storage = this.core.storage;\n }\n\n /**\n * Returns a unique ID for a given `name`. It's stored in local storage,\n * so you will get the same ID across sessions.\n *\n * @example\n * const deviceId = await db.getLocalId('device');\n */\n getLocalId = (name: string): Promise<string> => {\n return this.core.getLocalId(name);\n };\n\n /**\n * Use this to write data! You can create, update, delete, and link objects\n *\n * @see https://instantdb.com/docs/instaml\n *\n * @example\n * const goalId = id();\n * db.transact(db.tx.goals[goalId].update({title: \"Get fit\"}))\n */\n transact = (\n chunks: TransactionChunk<any, any> | TransactionChunk<any, any>[],\n ) => {\n return this.core.transact(chunks);\n };\n\n /**\n * One time query for the logged in state.\n *\n * @see https://instantdb.com/docs/auth\n * @example\n * const user = await db.getAuth();\n * console.log('logged in as', user.email)\n */\n getAuth(): Promise<User | null> {\n return this.core.getAuth();\n }\n\n /**\n * Use this for one-off queries.\n * Returns local data if available, otherwise fetches from the server.\n *\n * @see https://instantdb.com/docs/instaql\n *\n * @example\n * const resp = await db.queryOnce({ goals: {} });\n * console.log(resp.data.goals)\n */\n queryOnce = <Q extends ValidQuery<Q, Schema>>(\n query: Q,\n opts?: InstaQLOptions,\n ): Promise<{\n data: InstaQLResponse<Schema, Q, UseDates>;\n pageInfo: PageInfoResponse<Q>;\n }> => {\n return this.core.queryOnce(query, opts);\n };\n\n // -----------\n // Solid reactive hooks\n\n /**\n * Use this to query your data!\n *\n * @see https://instantdb.com/docs/instaql\n *\n * @example\n * const state = db.useQuery({ goals: {} });\n * // state().isLoading, state().error, state().data\n */\n useQuery = <Q extends ValidQuery<Q, Schema>>(\n query: null | Q,\n opts?: InstaQLOptions,\n ): Accessor<InstaQLLifecycleState<Schema, Q, UseDates>> => {\n const [state, setState] = createSignal<\n InstaQLLifecycleState<Schema, Q, UseDates>\n >(defaultState as InstaQLLifecycleState<Schema, Q, UseDates>);\n\n createEffect(() => {\n if (!query) {\n setState(\n () => defaultState as InstaQLLifecycleState<Schema, Q, UseDates>,\n );\n return;\n }\n\n let q = query;\n if (opts && 'ruleParams' in opts) {\n q = { $$ruleParams: (opts as any)['ruleParams'], ...q };\n }\n\n const coerced = coerceQuery(q);\n const prev = this.core._reactor.getPreviousResult(coerced);\n if (prev) {\n setState(\n () =>\n stateForResult(prev) as InstaQLLifecycleState<Schema, Q, UseDates>,\n );\n }\n\n const unsub = this.core.subscribeQuery<Q, UseDates>(coerced, (result) => {\n setState(\n () =>\n Object.assign(\n {\n isLoading: false,\n data: undefined,\n pageInfo: undefined,\n error: undefined,\n },\n result,\n ) as InstaQLLifecycleState<Schema, Q, UseDates>,\n );\n });\n\n onCleanup(unsub);\n });\n\n return state;\n };\n\n /**\n * Listen for the logged in state. This is useful\n * for deciding when to show a login screen.\n *\n * @see https://instantdb.com/docs/auth\n * @example\n * function App() {\n * const auth = db.useAuth();\n * // auth().isLoading, auth().user, auth().error\n * }\n */\n useAuth = (): Accessor<AuthState> => {\n const [state, setState] = createSignal<AuthState>(\n this.core._reactor._currentUserCached ?? defaultAuthState,\n );\n\n createEffect(() => {\n const unsub = this.core.subscribeAuth((auth) => {\n setState({ isLoading: false, ...auth });\n });\n\n onCleanup(unsub);\n });\n\n return state;\n };\n\n /**\n * Subscribe to the currently logged in user.\n * If the user is not logged in, this will throw an Error.\n *\n * @see https://instantdb.com/docs/auth\n * @example\n * function UserDisplay() {\n * const user = db.useUser();\n * return <div>Logged in as: {user().email}</div>\n * }\n */\n useUser = (): Accessor<User> => {\n const auth = this.useAuth();\n return createMemo(() => {\n const { user } = auth();\n if (!user) {\n throw new InstantError(\n 'useUser must be used within an auth-protected route',\n );\n }\n return user;\n });\n };\n\n /**\n * Listen for connection status changes to Instant.\n *\n * @see https://www.instantdb.com/docs/patterns#connection-status\n * @example\n * function App() {\n * const status = db.useConnectionStatus();\n * return <div>Connection state: {status()}</div>\n * }\n */\n useConnectionStatus = (): Accessor<ConnectionStatus> => {\n const [status, setStatus] = createSignal<ConnectionStatus>(\n this.core._reactor.status as ConnectionStatus,\n );\n\n createEffect(() => {\n const unsub = this.core.subscribeConnectionStatus((newStatus) => {\n setStatus(() => newStatus);\n });\n\n onCleanup(unsub);\n });\n\n return status;\n };\n\n /**\n * A hook that returns a unique ID for a given `name`. localIds are\n * stored in local storage, so you will get the same ID across sessions.\n *\n * Initially returns `null`, and then loads the localId.\n *\n * @example\n * const deviceId = db.useLocalId('device');\n * // deviceId() is null initially, then the ID string\n */\n useLocalId = (name: string): Accessor<string | null> => {\n const [localId, setLocalId] = createSignal<string | null>(null);\n\n createEffect(() => {\n let mounted = true;\n this.getLocalId(name).then((id) => {\n if (mounted) {\n setLocalId(() => id);\n }\n });\n onCleanup(() => {\n mounted = false;\n });\n });\n\n return localId;\n };\n\n /**\n * Obtain a handle to a room, which allows you to listen to topics and presence data\n *\n * @see https://instantdb.com/docs/presence-and-topics\n *\n * @example\n * const room = db.room('chat', roomId);\n * const presence = db.rooms.usePresence(room);\n */\n room<RoomType extends keyof Rooms>(\n type: RoomType = '_defaultRoomType' as RoomType,\n id: string = '_defaultRoomId',\n ) {\n return new InstantSolidRoom<Schema, Rooms, RoomType>(this.core, type, id);\n }\n\n /**\n * Hooks for working with rooms\n *\n * @see https://instantdb.com/docs/presence-and-topics\n *\n * @example\n * const room = db.room('chat', roomId);\n * const presence = db.rooms.usePresence(room);\n * const publish = db.rooms.usePublishTopic(room, 'emoji');\n */\n rooms = rooms;\n}\n\n// -----------\n// init\n\n/**\n * The first step: init your application!\n *\n * Visit https://instantdb.com/dash to get your `appId` :)\n *\n * @example\n * import { init } from \"@instantdb/solidjs\"\n *\n * const db = init({ appId: \"my-app-id\" })\n *\n * // You can also provide a schema for type safety and editor autocomplete!\n *\n * import { init } from \"@instantdb/solidjs\"\n * import schema from \"../instant.schema.ts\";\n *\n * const db = init({ appId: \"my-app-id\", schema })\n */\nexport function init<\n Schema extends InstantSchemaDef<any, any, any>,\n UseDates extends boolean = false,\n>(\n config: Omit<InstantConfig<Schema, UseDates>, 'useDateObjects'> & {\n useDateObjects?: UseDates;\n },\n): InstantSolidDatabase<Schema, UseDates> {\n const coreDb = core_init<Schema, UseDates>(config, undefined, undefined, {\n '@instantdb/solidjs': version,\n });\n return new InstantSolidDatabase<Schema, UseDates>(coreDb);\n}\n\nexport const init_experimental = init;\n"]}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { type PresenceOpts, type PresenceResponse, type RoomSchemaShape, InstantCoreDatabase, InstantSchemaDef } from '@instantdb/core';
|
|
2
|
+
import type { Accessor } from 'solid-js';
|
|
3
|
+
export type PresenceHandle<PresenceShape, Keys extends keyof PresenceShape> = PresenceResponse<PresenceShape, Keys> & {
|
|
4
|
+
publishPresence: (data: Partial<PresenceShape>) => void;
|
|
5
|
+
};
|
|
6
|
+
export type TypingIndicatorOpts = {
|
|
7
|
+
timeout?: number | null;
|
|
8
|
+
stopOnEnter?: boolean;
|
|
9
|
+
writeOnly?: boolean;
|
|
10
|
+
};
|
|
11
|
+
export type TypingIndicatorHandle<PresenceShape> = {
|
|
12
|
+
active: Accessor<PresenceShape[]>;
|
|
13
|
+
setActive(active: boolean): void;
|
|
14
|
+
inputProps: {
|
|
15
|
+
onKeyDown: (e: KeyboardEvent) => void;
|
|
16
|
+
onBlur: () => void;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export declare const defaultActivityStopTimeout = 1000;
|
|
20
|
+
/**
|
|
21
|
+
* Listen for broadcasted events given a room and topic.
|
|
22
|
+
*
|
|
23
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
24
|
+
* @example
|
|
25
|
+
* function App({ roomId }) {
|
|
26
|
+
* const room = db.room('chats', roomId);
|
|
27
|
+
* db.rooms.useTopicEffect(room, 'emoji', (message, peer) => {
|
|
28
|
+
* console.log(peer.name, 'sent', message);
|
|
29
|
+
* });
|
|
30
|
+
* // ...
|
|
31
|
+
* }
|
|
32
|
+
*/
|
|
33
|
+
export declare function useTopicEffect<RoomSchema extends RoomSchemaShape, RoomType extends keyof RoomSchema, TopicType extends keyof RoomSchema[RoomType]['topics']>(room: InstantSolidRoom<any, RoomSchema, RoomType>, topic: TopicType, onEvent: (event: RoomSchema[RoomType]['topics'][TopicType], peer: RoomSchema[RoomType]['presence']) => any): void;
|
|
34
|
+
/**
|
|
35
|
+
* Broadcast an event to a room.
|
|
36
|
+
*
|
|
37
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
38
|
+
* @example
|
|
39
|
+
* function App({ roomId }) {
|
|
40
|
+
* const room = db.room('chat', roomId);
|
|
41
|
+
* const publishTopic = db.rooms.usePublishTopic(room, "emoji");
|
|
42
|
+
*
|
|
43
|
+
* return (
|
|
44
|
+
* <button onClick={() => publishTopic({ emoji: "🔥" })}>Send emoji</button>
|
|
45
|
+
* );
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
*/
|
|
49
|
+
export declare function usePublishTopic<RoomSchema extends RoomSchemaShape, RoomType extends keyof RoomSchema, TopicType extends keyof RoomSchema[RoomType]['topics']>(room: InstantSolidRoom<any, RoomSchema, RoomType>, topic: TopicType): (data: RoomSchema[RoomType]['topics'][TopicType]) => void;
|
|
50
|
+
/**
|
|
51
|
+
* Listen for peer's presence data in a room, and publish the current user's presence.
|
|
52
|
+
*
|
|
53
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
54
|
+
* @example
|
|
55
|
+
* function App({ roomId }) {
|
|
56
|
+
* const presence = db.rooms.usePresence(room, { keys: ["name", "avatar"] });
|
|
57
|
+
* // presence().peers, presence().isLoading, presence().publishPresence
|
|
58
|
+
* }
|
|
59
|
+
*/
|
|
60
|
+
export declare function usePresence<RoomSchema extends RoomSchemaShape, RoomType extends keyof RoomSchema, Keys extends keyof RoomSchema[RoomType]['presence']>(room: InstantSolidRoom<any, RoomSchema, RoomType>, opts?: PresenceOpts<RoomSchema[RoomType]['presence'], Keys>): Accessor<PresenceHandle<RoomSchema[RoomType]['presence'], Keys>>;
|
|
61
|
+
/**
|
|
62
|
+
* Publishes presence data to a room
|
|
63
|
+
*
|
|
64
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
65
|
+
* @example
|
|
66
|
+
* function App({ roomId, nickname }) {
|
|
67
|
+
* const room = db.room('chat', roomId);
|
|
68
|
+
* db.rooms.useSyncPresence(room, { nickname });
|
|
69
|
+
* }
|
|
70
|
+
*/
|
|
71
|
+
export declare function useSyncPresence<RoomSchema extends RoomSchemaShape, RoomType extends keyof RoomSchema>(room: InstantSolidRoom<any, RoomSchema, RoomType>, data: Partial<RoomSchema[RoomType]['presence']>, deps?: any[]): void;
|
|
72
|
+
/**
|
|
73
|
+
* Manage typing indicator state
|
|
74
|
+
*
|
|
75
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
76
|
+
* @example
|
|
77
|
+
* function App({ roomId }) {
|
|
78
|
+
* const room = db.room('chat', roomId);
|
|
79
|
+
* const typing = db.rooms.useTypingIndicator(room, "chat-input");
|
|
80
|
+
* // typing.active(), typing.setActive(bool), typing.inputProps
|
|
81
|
+
* }
|
|
82
|
+
*/
|
|
83
|
+
export declare function useTypingIndicator<RoomSchema extends RoomSchemaShape, RoomType extends keyof RoomSchema>(room: InstantSolidRoom<any, RoomSchema, RoomType>, inputName: string, opts?: TypingIndicatorOpts): TypingIndicatorHandle<RoomSchema[RoomType]['presence']>;
|
|
84
|
+
export declare const rooms: {
|
|
85
|
+
useTopicEffect: typeof useTopicEffect;
|
|
86
|
+
usePublishTopic: typeof usePublishTopic;
|
|
87
|
+
usePresence: typeof usePresence;
|
|
88
|
+
useSyncPresence: typeof useSyncPresence;
|
|
89
|
+
useTypingIndicator: typeof useTypingIndicator;
|
|
90
|
+
};
|
|
91
|
+
export declare class InstantSolidRoom<Schema extends InstantSchemaDef<any, any, any>, RoomSchema extends RoomSchemaShape, RoomType extends keyof RoomSchema> {
|
|
92
|
+
core: InstantCoreDatabase<Schema, boolean>;
|
|
93
|
+
type: RoomType;
|
|
94
|
+
id: string;
|
|
95
|
+
constructor(core: InstantCoreDatabase<Schema, boolean>, type: RoomType, id: string);
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=InstantSolidRoom.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InstantSolidRoom.d.ts","sourceRoot":"","sources":["../../src/InstantSolidRoom.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,mBAAmB,EACnB,gBAAgB,EACjB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAKzC,MAAM,MAAM,cAAc,CACxB,aAAa,EACb,IAAI,SAAS,MAAM,aAAa,IAC9B,gBAAgB,CAAC,aAAa,EAAE,IAAI,CAAC,GAAG;IAC1C,eAAe,EAAE,CAAC,IAAI,EAAE,OAAO,CAAC,aAAa,CAAC,KAAK,IAAI,CAAC;CACzD,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,WAAW,CAAC,EAAE,OAAO,CAAC;IAEtB,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,qBAAqB,CAAC,aAAa,IAAI;IACjD,MAAM,EAAE,QAAQ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,CAAC,MAAM,EAAE,OAAO,CAAC,EAC1C,IAAI,EAAE,QAAQ,EACd,EAAE,EAAE,MAAM;CAMb"}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { createSignal, createEffect, onCleanup, createMemo } from 'solid-js';
|
|
2
|
+
export const defaultActivityStopTimeout = 1_000;
|
|
3
|
+
// ------
|
|
4
|
+
// Topics
|
|
5
|
+
/**
|
|
6
|
+
* Listen for broadcasted events given a room and topic.
|
|
7
|
+
*
|
|
8
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
9
|
+
* @example
|
|
10
|
+
* function App({ roomId }) {
|
|
11
|
+
* const room = db.room('chats', roomId);
|
|
12
|
+
* db.rooms.useTopicEffect(room, 'emoji', (message, peer) => {
|
|
13
|
+
* console.log(peer.name, 'sent', message);
|
|
14
|
+
* });
|
|
15
|
+
* // ...
|
|
16
|
+
* }
|
|
17
|
+
*/
|
|
18
|
+
export function useTopicEffect(room, topic, onEvent) {
|
|
19
|
+
createEffect(() => {
|
|
20
|
+
const unsub = room.core._reactor.subscribeTopic(room.type, room.id, topic, (event, peer) => {
|
|
21
|
+
onEvent(event, peer);
|
|
22
|
+
});
|
|
23
|
+
onCleanup(unsub);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Broadcast an event to a room.
|
|
28
|
+
*
|
|
29
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
30
|
+
* @example
|
|
31
|
+
* function App({ roomId }) {
|
|
32
|
+
* const room = db.room('chat', roomId);
|
|
33
|
+
* const publishTopic = db.rooms.usePublishTopic(room, "emoji");
|
|
34
|
+
*
|
|
35
|
+
* return (
|
|
36
|
+
* <button onClick={() => publishTopic({ emoji: "🔥" })}>Send emoji</button>
|
|
37
|
+
* );
|
|
38
|
+
* }
|
|
39
|
+
*
|
|
40
|
+
*/
|
|
41
|
+
export function usePublishTopic(room, topic) {
|
|
42
|
+
createEffect(() => {
|
|
43
|
+
const unsub = room.core._reactor.joinRoom(room.type, room.id);
|
|
44
|
+
onCleanup(unsub);
|
|
45
|
+
});
|
|
46
|
+
return (data) => {
|
|
47
|
+
room.core._reactor.publishTopic({
|
|
48
|
+
roomType: room.type,
|
|
49
|
+
roomId: room.id,
|
|
50
|
+
topic,
|
|
51
|
+
data,
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
// ---------
|
|
56
|
+
// Presence
|
|
57
|
+
/**
|
|
58
|
+
* Listen for peer's presence data in a room, and publish the current user's presence.
|
|
59
|
+
*
|
|
60
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
61
|
+
* @example
|
|
62
|
+
* function App({ roomId }) {
|
|
63
|
+
* const presence = db.rooms.usePresence(room, { keys: ["name", "avatar"] });
|
|
64
|
+
* // presence().peers, presence().isLoading, presence().publishPresence
|
|
65
|
+
* }
|
|
66
|
+
*/
|
|
67
|
+
export function usePresence(room, opts = {}) {
|
|
68
|
+
const [state, setState] = createSignal((room.core._reactor.getPresence(room.type, room.id, opts) ?? {
|
|
69
|
+
peers: {},
|
|
70
|
+
isLoading: true,
|
|
71
|
+
}));
|
|
72
|
+
createEffect(() => {
|
|
73
|
+
const unsub = room.core._reactor.subscribePresence(room.type, room.id, opts, (data) => {
|
|
74
|
+
setState(data);
|
|
75
|
+
});
|
|
76
|
+
onCleanup(unsub);
|
|
77
|
+
});
|
|
78
|
+
const publishPresence = (data) => {
|
|
79
|
+
room.core._reactor.publishPresence(room.type, room.id, data);
|
|
80
|
+
};
|
|
81
|
+
return createMemo(() => ({
|
|
82
|
+
...state(),
|
|
83
|
+
publishPresence,
|
|
84
|
+
}));
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Publishes presence data to a room
|
|
88
|
+
*
|
|
89
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
90
|
+
* @example
|
|
91
|
+
* function App({ roomId, nickname }) {
|
|
92
|
+
* const room = db.room('chat', roomId);
|
|
93
|
+
* db.rooms.useSyncPresence(room, { nickname });
|
|
94
|
+
* }
|
|
95
|
+
*/
|
|
96
|
+
export function useSyncPresence(room, data, deps) {
|
|
97
|
+
createEffect(() => {
|
|
98
|
+
const unsub = room.core._reactor.joinRoom(room.type, room.id, data);
|
|
99
|
+
onCleanup(unsub);
|
|
100
|
+
});
|
|
101
|
+
createEffect(() => {
|
|
102
|
+
// Track deps if provided, otherwise track serialized data
|
|
103
|
+
if (deps) {
|
|
104
|
+
deps.forEach((d) => d);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
JSON.stringify(data);
|
|
108
|
+
}
|
|
109
|
+
room.core._reactor.publishPresence(room.type, room.id, data);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
// -----------------
|
|
113
|
+
// Typing Indicator
|
|
114
|
+
/**
|
|
115
|
+
* Manage typing indicator state
|
|
116
|
+
*
|
|
117
|
+
* @see https://instantdb.com/docs/presence-and-topics
|
|
118
|
+
* @example
|
|
119
|
+
* function App({ roomId }) {
|
|
120
|
+
* const room = db.room('chat', roomId);
|
|
121
|
+
* const typing = db.rooms.useTypingIndicator(room, "chat-input");
|
|
122
|
+
* // typing.active(), typing.setActive(bool), typing.inputProps
|
|
123
|
+
* }
|
|
124
|
+
*/
|
|
125
|
+
export function useTypingIndicator(room, inputName, opts = {}) {
|
|
126
|
+
let timeoutId = null;
|
|
127
|
+
const presence = rooms.usePresence(room, {
|
|
128
|
+
keys: [inputName],
|
|
129
|
+
});
|
|
130
|
+
const active = createMemo(() => {
|
|
131
|
+
if (opts?.writeOnly)
|
|
132
|
+
return [];
|
|
133
|
+
// Access presence to track it
|
|
134
|
+
presence();
|
|
135
|
+
const presenceSnapshot = room.core._reactor.getPresence(room.type, room.id);
|
|
136
|
+
return Object.values(presenceSnapshot?.peers ?? {}).filter((p) => p[inputName] === true);
|
|
137
|
+
});
|
|
138
|
+
const setActive = (isActive) => {
|
|
139
|
+
room.core._reactor.publishPresence(room.type, room.id, {
|
|
140
|
+
[inputName]: isActive,
|
|
141
|
+
});
|
|
142
|
+
if (timeoutId) {
|
|
143
|
+
clearTimeout(timeoutId);
|
|
144
|
+
timeoutId = null;
|
|
145
|
+
}
|
|
146
|
+
if (!isActive)
|
|
147
|
+
return;
|
|
148
|
+
if (opts?.timeout === null || opts?.timeout === 0)
|
|
149
|
+
return;
|
|
150
|
+
timeoutId = setTimeout(() => {
|
|
151
|
+
room.core._reactor.publishPresence(room.type, room.id, {
|
|
152
|
+
[inputName]: null,
|
|
153
|
+
});
|
|
154
|
+
}, opts?.timeout ?? defaultActivityStopTimeout);
|
|
155
|
+
};
|
|
156
|
+
const onKeyDown = (e) => {
|
|
157
|
+
const isEnter = opts?.stopOnEnter && e.key === 'Enter';
|
|
158
|
+
const isActive = !isEnter;
|
|
159
|
+
setActive(isActive);
|
|
160
|
+
};
|
|
161
|
+
const onBlur = () => {
|
|
162
|
+
setActive(false);
|
|
163
|
+
};
|
|
164
|
+
return {
|
|
165
|
+
active,
|
|
166
|
+
setActive,
|
|
167
|
+
inputProps: { onKeyDown, onBlur },
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
// --------------
|
|
171
|
+
// Hooks namespace
|
|
172
|
+
export const rooms = {
|
|
173
|
+
useTopicEffect,
|
|
174
|
+
usePublishTopic,
|
|
175
|
+
usePresence,
|
|
176
|
+
useSyncPresence,
|
|
177
|
+
useTypingIndicator,
|
|
178
|
+
};
|
|
179
|
+
// ------------
|
|
180
|
+
// Class
|
|
181
|
+
export class InstantSolidRoom {
|
|
182
|
+
core;
|
|
183
|
+
type;
|
|
184
|
+
id;
|
|
185
|
+
constructor(core, type, id) {
|
|
186
|
+
this.core = core;
|
|
187
|
+
this.type = type;
|
|
188
|
+
this.id = id;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
//# sourceMappingURL=InstantSolidRoom.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InstantSolidRoom.js","sourceRoot":"","sources":["../../src/InstantSol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CAAuC;IAC3C,IAAI,CAAW;IACf,EAAE,CAAS;IAEX,YACE,IAA0C,EAC1C,IAAc,EACd,EAAU;QAEV,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;IACf,CAAC;CACF","sourcesContent":["import {\n type PresenceOpts,\n type PresenceResponse,\n type RoomSchemaShape,\n InstantCoreDatabase,\n InstantSchemaDef,\n} from '@instantdb/core';\n\nimport { createSignal, createEffect, onCleanup, createMemo } from 'solid-js';\nimport type { Accessor } from 'solid-js';\n\n// ------\n// Types\n\nexport type PresenceHandle<\n PresenceShape,\n Keys extends keyof PresenceShape,\n> = PresenceResponse<PresenceShape, Keys> & {\n publishPresence: (data: Partial<PresenceShape>) => void;\n};\n\nexport type TypingIndicatorOpts = {\n timeout?: number | null;\n stopOnEnter?: boolean;\n // Perf opt - `active` will always be an empty array\n writeOnly?: boolean;\n};\n\nexport type TypingIndicatorHandle<PresenceShape> = {\n active: Accessor<PresenceShape[]>;\n setActive(active: boolean): void;\n inputProps: {\n onKeyDown: (e: KeyboardEvent) => void;\n onBlur: () => void;\n };\n};\n\nexport const defaultActivityStopTimeout = 1_000;\n\n// ------\n// Topics\n\n/**\n * Listen for broadcasted events given a room and topic.\n *\n * @see https://instantdb.com/docs/presence-and-topics\n * @example\n * function App({ roomId }) {\n * const room = db.room('chats', roomId);\n * db.rooms.useTopicEffect(room, 'emoji', (message, peer) => {\n * console.log(peer.name, 'sent', message);\n * });\n * // ...\n * }\n */\nexport function useTopicEffect<\n RoomSchema extends RoomSchemaShape,\n RoomType extends keyof RoomSchema,\n TopicType extends keyof RoomSchema[RoomType]['topics'],\n>(\n room: InstantSolidRoom<any, RoomSchema, RoomType>,\n topic: TopicType,\n onEvent: (\n event: RoomSchema[RoomType]['topics'][TopicType],\n peer: RoomSchema[RoomType]['presence'],\n ) => any,\n): void {\n createEffect(() => {\n const unsub = room.core._reactor.subscribeTopic(\n room.type,\n room.id,\n topic,\n (event: any, peer: any) => {\n onEvent(event, peer);\n },\n );\n\n onCleanup(unsub);\n });\n}\n\n/**\n * Broadcast an event to a room.\n *\n * @see https://instantdb.com/docs/presence-and-topics\n * @example\n * function App({ roomId }) {\n * const room = db.room('chat', roomId);\n * const publishTopic = db.rooms.usePublishTopic(room, \"emoji\");\n *\n * return (\n * <button onClick={() => publishTopic({ emoji: \"🔥\" })}>Send emoji</button>\n * );\n * }\n *\n */\nexport function usePublishTopic<\n RoomSchema extends RoomSchemaShape,\n RoomType extends keyof RoomSchema,\n TopicType extends keyof RoomSchema[RoomType]['topics'],\n>(\n room: InstantSolidRoom<any, RoomSchema, RoomType>,\n topic: TopicType,\n): (data: RoomSchema[RoomType]['topics'][TopicType]) => void {\n createEffect(() => {\n const unsub = room.core._reactor.joinRoom(room.type as string, room.id);\n onCleanup(unsub);\n });\n\n return (data: RoomSchema[RoomType]['topics'][TopicType]) => {\n room.core._reactor.publishTopic({\n roomType: room.type,\n roomId: room.id,\n topic,\n data,\n });\n };\n}\n\n// ---------\n// Presence\n\n/**\n * Listen for peer's presence data in a room, and publish the current user's presence.\n *\n * @see https://instantdb.com/docs/presence-and-topics\n * @example\n * function App({ roomId }) {\n * const presence = db.rooms.usePresence(room, { keys: [\"name\", \"avatar\"] });\n * // presence().peers, presence().isLoading, presence().publishPresence\n * }\n */\nexport function usePresence<\n RoomSchema extends RoomSchemaShape,\n RoomType extends keyof RoomSchema,\n Keys extends keyof RoomSchema[RoomType]['presence'],\n>(\n room: InstantSolidRoom<any, RoomSchema, RoomType>,\n opts: PresenceOpts<RoomSchema[RoomType]['presence'], Keys> = {},\n): Accessor<PresenceHandle<RoomSchema[RoomType]['presence'], Keys>> {\n const [state, setState] = createSignal<\n PresenceResponse<RoomSchema[RoomType]['presence'], Keys>\n >(\n (room.core._reactor.getPresence(room.type, room.id, opts) ?? {\n peers: {},\n isLoading: true,\n }) as PresenceResponse<RoomSchema[RoomType]['presence'], Keys>,\n );\n\n createEffect(() => {\n const unsub = room.core._reactor.subscribePresence(\n room.type,\n room.id,\n opts,\n (data: any) => {\n setState(data);\n },\n );\n\n onCleanup(unsub);\n });\n\n const publishPresence = (data: Partial<RoomSchema[RoomType]['presence']>) => {\n room.core._reactor.publishPresence(room.type, room.id, data);\n };\n\n return createMemo(() => ({\n ...state(),\n publishPresence,\n }));\n}\n\n/**\n * Publishes presence data to a room\n *\n * @see https://instantdb.com/docs/presence-and-topics\n * @example\n * function App({ roomId, nickname }) {\n * const room = db.room('chat', roomId);\n * db.rooms.useSyncPresence(room, { nickname });\n * }\n */\nexport function useSyncPresence<\n RoomSchema extends RoomSchemaShape,\n RoomType extends keyof RoomSchema,\n>(\n room: InstantSolidRoom<any, RoomSchema, RoomType>,\n data: Partial<RoomSchema[RoomType]['presence']>,\n deps?: any[],\n): void {\n createEffect(() => {\n const unsub = room.core._reactor.joinRoom(\n room.type as string,\n room.id,\n data,\n );\n onCleanup(unsub);\n });\n\n createEffect(() => {\n // Track deps if provided, otherwise track serialized data\n if (deps) {\n deps.forEach((d) => d);\n } else {\n JSON.stringify(data);\n }\n room.core._reactor.publishPresence(room.type, room.id, data);\n });\n}\n\n// -----------------\n// Typing Indicator\n\n/**\n * Manage typing indicator state\n *\n * @see https://instantdb.com/docs/presence-and-topics\n * @example\n * function App({ roomId }) {\n * const room = db.room('chat', roomId);\n * const typing = db.rooms.useTypingIndicator(room, \"chat-input\");\n * // typing.active(), typing.setActive(bool), typing.inputProps\n * }\n */\nexport function useTypingIndicator<\n RoomSchema extends RoomSchemaShape,\n RoomType extends keyof RoomSchema,\n>(\n room: InstantSolidRoom<any, RoomSchema, RoomType>,\n inputName: string,\n opts: TypingIndicatorOpts = {},\n): TypingIndicatorHandle<RoomSchema[RoomType]['presence']> {\n let timeoutId: ReturnType<typeof setTimeout> | null = null;\n\n const presence = rooms.usePresence(room, {\n keys: [inputName] as (keyof RoomSchema[RoomType]['presence'])[],\n });\n\n const active = createMemo(() => {\n if (opts?.writeOnly) return [];\n // Access presence to track it\n presence();\n const presenceSnapshot = room.core._reactor.getPresence(room.type, room.id);\n return Object.values(presenceSnapshot?.peers ?? {}).filter(\n (p: any) => p[inputName] === true,\n );\n });\n\n const setActive = (isActive: boolean) => {\n room.core._reactor.publishPresence(room.type, room.id, {\n [inputName]: isActive,\n } as unknown as Partial<RoomSchema[RoomType]['presence']>);\n\n if (timeoutId) {\n clearTimeout(timeoutId);\n timeoutId = null;\n }\n\n if (!isActive) return;\n\n if (opts?.timeout === null || opts?.timeout === 0) return;\n\n timeoutId = setTimeout(() => {\n room.core._reactor.publishPresence(room.type, room.id, {\n [inputName]: null,\n } as Partial<RoomSchema[RoomType]['presence']>);\n }, opts?.timeout ?? defaultActivityStopTimeout);\n };\n\n const onKeyDown = (e: KeyboardEvent) => {\n const isEnter = opts?.stopOnEnter && e.key === 'Enter';\n const isActive = !isEnter;\n setActive(isActive);\n };\n\n const onBlur = () => {\n setActive(false);\n };\n\n return {\n active,\n setActive,\n inputProps: { onKeyDown, onBlur },\n };\n}\n\n// --------------\n// Hooks namespace\n\nexport const rooms = {\n useTopicEffect,\n usePublishTopic,\n usePresence,\n useSyncPresence,\n useTypingIndicator,\n};\n\n// ------------\n// Class\n\nexport class InstantSolidRoom<\n Schema extends InstantSchemaDef<any, any, any>,\n RoomSchema extends RoomSchemaShape,\n RoomType extends keyof RoomSchema,\n> {\n core: InstantCoreDatabase<Schema, boolean>;\n type: RoomType;\n id: string;\n\n constructor(\n core: InstantCoreDatabase<Schema, boolean>,\n type: RoomType,\n id: string,\n ) {\n this.core = core;\n this.type = type;\n this.id = id;\n }\n}\n"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { id, tx, lookup, i, InstantAPIError, SyncTableCallbackEventType, type QueryResponse, type InstantQuery, type InstantQueryResult, type InstantSchema, type InstantObject, type InstantEntity, type InstantSchemaDatabase, type InstantUnknownSchemaDef, type IInstantDatabase, type User, type AuthState, type Query, type Config, type InstaQLParams, type ConnectionStatus, type ValidQuery, type PresencePeer, type AttrsDefs, type CardinalityKind, type DataAttrDef, type EntitiesDef, type EntitiesWithLinks, type EntityDef, type InstantGraph, type InstantConfig, type LinkAttrDef, type LinkDef, type LinksDef, type ResolveAttrs, type ValueTypes, type InstaQLEntity, type InstaQLFields, type InstaQLResult, type InstaQLEntitySubquery, type RoomsOf, type RoomsDef, type PresenceOf, type TopicsOf, type TopicOf, type RoomHandle, type TransactionChunk, type InstantUnknownSchema, type InstantSchemaDef, type BackwardsCompatibleSchema, type InstantRules, type UpdateParams, type LinkParams, type CreateParams, type ExchangeCodeForTokenParams, type SendMagicCodeParams, type SendMagicCodeResponse, type SignInWithIdTokenParams, type VerifyMagicCodeParams, type VerifyResponse, type FileOpts, type UploadFileResponse, type DeleteFileResponse, type SyncTableCallback, type SyncTableCallbackEvent, type SyncTableInitialSyncBatch, type SyncTableInitialSyncComplete, type SyncTableSyncTransaction, type SyncTableLoadFromStorage, type SyncTableSetupError, StoreInterface, createInstantRouteHandler, type StoreInterfaceStoreName } from '@instantdb/core';
|
|
2
|
+
import { InstantSolidDatabase } from './InstantSolidDatabase.js';
|
|
3
|
+
import { init, init_experimental } from './InstantSolidDatabase.js';
|
|
4
|
+
import { InstantSolidRoom } from './InstantSolidRoom.js';
|
|
5
|
+
export { id, tx, lookup, init, init_experimental, InstantSolidDatabase, InstantSolidRoom, i, InstantAPIError, SyncTableCallbackEventType, type Config, type InstantConfig, type InstantUnknownSchemaDef, type Query, type QueryResponse, type InstantObject, type User, type AuthState, type ConnectionStatus, type InstantQuery, type InstantQueryResult, type InstantSchema, type InstantEntity, type InstantSchemaDatabase, type IInstantDatabase, type InstaQLParams, type ValidQuery, type InstaQLFields, type PresencePeer, type AttrsDefs, type CardinalityKind, type DataAttrDef, type EntitiesDef, type EntitiesWithLinks, type EntityDef, type InstantGraph, type LinkAttrDef, type LinkDef, type LinksDef, type ResolveAttrs, type ValueTypes, type InstaQLEntity, type InstaQLResult, type InstaQLEntitySubquery, type RoomsOf, type RoomsDef, type TransactionChunk, type PresenceOf, type TopicsOf, type TopicOf, type RoomHandle, type InstantUnknownSchema, type InstantSchemaDef, type BackwardsCompatibleSchema, type InstantRules, type UpdateParams, type LinkParams, type CreateParams, type ExchangeCodeForTokenParams, type SendMagicCodeParams, type SendMagicCodeResponse, type SignInWithIdTokenParams, type VerifyMagicCodeParams, type VerifyResponse, type FileOpts, type UploadFileResponse, type DeleteFileResponse, StoreInterface, type StoreInterfaceStoreName, type SyncTableCallback, type SyncTableCallbackEvent, type SyncTableInitialSyncBatch, type SyncTableInitialSyncComplete, type SyncTableSyncTransaction, type SyncTableLoadFromStorage, type SyncTableSetupError, createInstantRouteHandler, };
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,EAAE,EACF,EAAE,EACF,MAAM,EACN,CAAC,EAGD,eAAe,EAGf,0BAA0B,EAG1B,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,kBAAkB,EACvB,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,qBAAqB,EAC1B,KAAK,uBAAuB,EAC5B,KAAK,gBAAgB,EACrB,KAAK,IAAI,EACT,KAAK,SAAS,EACd,KAAK,KAAK,EACV,KAAK,MAAM,EACX,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,UAAU,EAGf,KAAK,YAAY,EAGjB,KAAK,SAAS,EACd,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,iBAAiB,EACtB,KAAK,SAAS,EACd,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,WAAW,EAChB,KAAK,OAAO,EACZ,KAAK,QAAQ,EACb,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,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TableCallbackEventType, StoreInterface, createInstantRouteHandler, } from '@instantdb/core';
|
|
6
|
+
import { InstantSolidDatabase } from './InstantSolidDatabase.js';
|
|
7
|
+
import { init, init_experimental } from './InstantSolidDatabase.js';
|
|
8
|
+
import { InstantSolidRoom } from './InstantSolidRoom.js';
|
|
9
|
+
export { id, tx, lookup, init, init_experimental, InstantSolidDatabase, InstantSolidRoom, i,
|
|
10
|
+
// error
|
|
11
|
+
InstantAPIError,
|
|
12
|
+
// sync table enums
|
|
13
|
+
SyncTableCallbackEventType,
|
|
14
|
+
// custom store
|
|
15
|
+
StoreInterface,
|
|
16
|
+
// Server helper
|
|
17
|
+
createInstantRouteHandler, };
|
|
18
|
+
//# sourceMappingURL=index.js.map
|