@supatype/svelte 0.9.1 → 0.10.0
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/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +19 -11
- package/.turbo/turbo-typecheck.log +1 -1
- package/CHANGELOG.md +40 -1
- package/dist/createChannel.d.ts +27 -0
- package/dist/createChannel.d.ts.map +1 -0
- package/dist/createChannel.js +50 -0
- package/dist/createChannel.js.map +1 -0
- package/dist/createQuery.d.ts +1 -1
- package/dist/createQuery.d.ts.map +1 -1
- package/dist/createQuery.js +28 -7
- package/dist/createQuery.js.map +1 -1
- package/dist/createRealtimeConnection.d.ts +14 -0
- package/dist/createRealtimeConnection.d.ts.map +1 -0
- package/dist/createRealtimeConnection.js +18 -0
- package/dist/createRealtimeConnection.js.map +1 -0
- package/dist/createSubscription.d.ts.map +1 -1
- package/dist/createSubscription.js +1 -1
- package/dist/createSubscription.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/createChannel.ts +64 -0
- package/src/createQuery.ts +28 -7
- package/src/createRealtimeConnection.ts +28 -0
- package/src/createSubscription.ts +2 -2
- package/src/index.ts +6 -0
- package/tests/createChannel-private.test.ts +61 -0
- package/tests/createChannel-reactive-name.test.ts +63 -0
- package/tests/createChannel-typed-name.test.ts +15 -0
- package/tests/createChannel.test.ts +60 -0
- package/tests/createQuery-first-render.test.ts +90 -0
- package/tests/createQuery-reactive-options.test.ts +38 -0
- package/tests/createSubscription-status.test.ts +19 -4
- package/tsconfig.tsbuildinfo +1 -1
package/.turbo/turbo-build.log
CHANGED
package/.turbo/turbo-test.log
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
1
|
|
|
2
|
-
> @supatype/svelte@0.
|
|
2
|
+
> @supatype/svelte@0.10.0 test /home/runner/work/supatype/supatype/packages/svelte
|
|
3
3
|
> vitest run --maxWorkers=50%
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
[1m[46m RUN [49m[22m [36mv4.0.18 [39m[90m/home/runner/work/supatype/supatype/packages/svelte[39m
|
|
7
7
|
|
|
8
|
-
[32m✓[39m tests/
|
|
9
|
-
|
|
10
|
-
[32m✓[39m tests/
|
|
11
|
-
|
|
12
|
-
[32m✓[39m tests/
|
|
13
|
-
[32m✓[39m tests/
|
|
8
|
+
[32m✓[39m tests/createQuery-first-render.test.ts [2m([22m[2m4 tests[22m[2m)[22m[33m 628[2mms[22m[39m
|
|
9
|
+
[33m[2m✓[22m[39m is loading when enabled with plain options [33m 516[2mms[22m[39m
|
|
10
|
+
[32m✓[39m tests/createChannel-private.test.ts [2m([22m[2m3 tests[22m[2m)[22m[33m 437[2mms[22m[39m
|
|
11
|
+
[33m[2m✓[22m[39m joins as private once the server announces private_channels [33m 338[2mms[22m[39m
|
|
12
|
+
[32m✓[39m tests/createSubscription-status.test.ts [2m([22m[2m3 tests[22m[2m)[22m[33m 304[2mms[22m[39m
|
|
13
|
+
[32m✓[39m tests/createChannel-reactive-name.test.ts [2m([22m[2m2 tests[22m[2m)[22m[32m 140[2mms[22m[39m
|
|
14
|
+
[32m✓[39m tests/createChannel.test.ts [2m([22m[2m3 tests[22m[2m)[22m[32m 287[2mms[22m[39m
|
|
15
|
+
[32m✓[39m tests/thrown-errors.test.ts [2m([22m[2m5 tests[22m[2m)[22m[32m 250[2mms[22m[39m
|
|
16
|
+
[32m✓[39m tests/createAuth-oauth.test.ts [2m([22m[2m3 tests[22m[2m)[22m[32m 44[2mms[22m[39m
|
|
17
|
+
[32m✓[39m tests/createQuery-reactive-options.test.ts [2m([22m[2m5 tests[22m[2m)[22m[32m 206[2mms[22m[39m
|
|
18
|
+
[32m✓[39m tests/createAuth-signup.test.ts [2m([22m[2m2 tests[22m[2m)[22m[32m 43[2mms[22m[39m
|
|
19
|
+
[32m✓[39m tests/createQuery-stale.test.ts [2m([22m[2m4 tests[22m[2m)[22m[32m 140[2mms[22m[39m
|
|
20
|
+
[32m✓[39m tests/createChannel-typed-name.test.ts [2m([22m[2m1 test[22m[2m)[22m[32m 39[2mms[22m[39m
|
|
21
|
+
[32m✓[39m tests/createAuth-unmount.test.ts [2m([22m[2m1 test[22m[2m)[22m[32m 62[2mms[22m[39m
|
|
14
22
|
|
|
15
|
-
[2m Test Files [22m [1m[
|
|
16
|
-
[2m Tests [22m [1m[
|
|
17
|
-
[2m Start at [22m
|
|
18
|
-
[2m Duration [22m
|
|
23
|
+
[2m Test Files [22m [1m[32m12 passed[39m[22m[90m (12)[39m
|
|
24
|
+
[2m Tests [22m [1m[32m36 passed[39m[22m[90m (36)[39m
|
|
25
|
+
[2m Start at [22m 15:46:04
|
|
26
|
+
[2m Duration [22m 22.08s[2m (transform 11.56s, setup 0ms, import 25.27s, tests 2.58s, environment 7ms)[22m
|
|
19
27
|
|
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## 0.9.3
|
|
4
|
+
|
|
5
|
+
### Features
|
|
6
|
+
|
|
7
|
+
* **createChannel:** the channel name is a `ChannelName`: a topic the schema declares autocompletes (`lot:${string}`), and any other string is still accepted, since an `open` server admits it.
|
|
8
|
+
* **createChannel:** `createChannel(name, setup)` joins one channel and hands it to `setup` to add any number of
|
|
9
|
+
row, presence and broadcast listeners, which `createSubscription` (one table, one listener) cannot. It returns
|
|
10
|
+
`status` and `error` as `createSubscription` does, and `isConnected` and `connectionState`. `name` is a string or
|
|
11
|
+
a store: when it changes the old channel is left, its listeners go with it, and `setup` runs on the
|
|
12
|
+
new one. The channel is left on teardown.
|
|
13
|
+
* **createRealtimeConnection:** `isConnected` and `connectionState` for the realtime socket itself.
|
|
14
|
+
* **createChannel:** takes `{ private: true }` as a third argument, passed to the channel join (see the
|
|
15
|
+
client changelog). A refused or withdrawn private join is status "error" with `error` a
|
|
16
|
+
`RealtimeChannelError` whose `reason` says why.
|
|
17
|
+
* **createChannel:** returns `refusal`, the latest broadcast, presence track or payload the server refused on the
|
|
18
|
+
channel (`{ channel, code, message }`, `code` one of `send_denied`, `track_denied`,
|
|
19
|
+
`payload_refused`), or null. The channel stays joined: `status` does not change for it.
|
|
20
|
+
|
|
21
|
+
### Bug Fixes
|
|
22
|
+
|
|
23
|
+
* **createSubscription:** `data` is the table's rows. It is read once the subscription has joined and again after
|
|
24
|
+
each re-join, so rows that existed before subscribing are held and changes made while the socket was
|
|
25
|
+
down are not lost. Each change is then an upsert by key (`id`, or the new `key` option, a column or
|
|
26
|
+
an array of them): a row seen twice is listed once, an update to a row not held adds it, and a delete
|
|
27
|
+
of a key not held does nothing. A `filter` narrows the read too. Before this `data` started empty,
|
|
28
|
+
so an update or delete of a pre-existing row was dropped and a repeated insert was listed twice.
|
|
29
|
+
|
|
30
|
+
## 0.9.1 (2026-11-09)
|
|
4
31
|
|
|
5
32
|
### Breaking Changes
|
|
6
33
|
|
|
@@ -22,3 +49,15 @@
|
|
|
22
49
|
* **createQuery:** only the newest read is shown. A refetch while a read was in flight let the older
|
|
23
50
|
read finish last and replace the newer rows with its own rows, its error or its throw, and end
|
|
24
51
|
loading while the newer read was still out. A throw from a replaced read is still logged.
|
|
52
|
+
|
|
53
|
+
## 0.9.0 (2026-11-04)
|
|
54
|
+
|
|
55
|
+
### Breaking Changes
|
|
56
|
+
|
|
57
|
+
* **createMutation:** `mutate` returns no rows unless asked: `data` is `null` after an insert, update,
|
|
58
|
+
upsert or delete, as with the client's own writes. Pass `{ select: true }` for every column or
|
|
59
|
+
`{ select: "id,title" }` for some, as `.select()` takes them. Returning a row needs the right
|
|
60
|
+
to read it, so a table whose rules let a caller create but not read refused every insert made
|
|
61
|
+
through `createMutation`. A delete returns `null` rather than `[]`, and its rows with `select`.
|
|
62
|
+
|
|
63
|
+
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type Readable } from "svelte/store";
|
|
2
|
+
import type { AugmentedDatabase, AnyDatabase, ChannelActionRefusal, ChannelName, ChannelOptions, ChannelSubscription, SupatypeError } from "@supatype/client";
|
|
3
|
+
import { type SubscriptionState } from "@supatype/client/bindings";
|
|
4
|
+
import { type RealtimeConnectionStore } from "./createRealtimeConnection.js";
|
|
5
|
+
export interface ChannelStore extends RealtimeConnectionStore {
|
|
6
|
+
/** This channel's join: "connecting", "connected", "disconnected" or "error". */
|
|
7
|
+
status: Readable<SubscriptionState>;
|
|
8
|
+
/** Why the server refused the join, while the status is "error"; null otherwise. */
|
|
9
|
+
error: Readable<SupatypeError | null>;
|
|
10
|
+
/** The latest send, track or payload the server refused on this channel; the status does not move for it. */
|
|
11
|
+
refusal: Readable<ChannelActionRefusal | null>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Join one channel and register whatever listeners `setup` adds to it: row changes, presence and
|
|
15
|
+
* broadcast together, which `createSubscription` (one table, one listener) cannot do.
|
|
16
|
+
*
|
|
17
|
+
* `name` may be a string or a store. `setup` runs straight away, and again on the new channel
|
|
18
|
+
* whenever the store changes, after the old channel is left and its listeners with it. The channel
|
|
19
|
+
* is left when the component is destroyed. With runes, `toStore(() => name)` from `svelte/store`
|
|
20
|
+
* turns reactive state into a store.
|
|
21
|
+
*
|
|
22
|
+
* `options.private` asks for a private channel (a schema `Channel` declaration guards it): see
|
|
23
|
+
* `ChannelOptions`. A refused or withdrawn join shows as `error`, a `RealtimeChannelError` whose `reason`
|
|
24
|
+
* says why.
|
|
25
|
+
*/
|
|
26
|
+
export declare function createChannel<TRow = Record<string, unknown>, TDatabase extends AnyDatabase = AugmentedDatabase>(name: ChannelName | Readable<ChannelName>, setup: (channel: ChannelSubscription<TRow>) => void, options?: ChannelOptions | undefined): ChannelStore;
|
|
27
|
+
//# sourceMappingURL=createChannel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createChannel.d.ts","sourceRoot":"","sources":["../src/createChannel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAA;AAEtD,OAAO,KAAK,EAAE,iBAAiB,EAAE,WAAW,EAAE,oBAAoB,EAAE,WAAW,EAAE,cAAc,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAC7J,OAAO,EAAe,KAAK,iBAAiB,EAAE,MAAM,2BAA2B,CAAA;AAE/E,OAAO,EAA4B,KAAK,uBAAuB,EAAE,MAAM,+BAA+B,CAAA;AAEtG,MAAM,WAAW,YAAa,SAAQ,uBAAuB;IAC3D,iFAAiF;IACjF,MAAM,EAAE,QAAQ,CAAC,iBAAiB,CAAC,CAAA;IACnC,oFAAoF;IACpF,KAAK,EAAE,QAAQ,CAAC,aAAa,GAAG,IAAI,CAAC,CAAA;IACrC,6GAA6G;IAC7G,OAAO,EAAE,QAAQ,CAAC,oBAAoB,GAAG,IAAI,CAAC,CAAA;CAC/C;AAOD;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,SAAS,SAAS,WAAW,GAAG,iBAAiB,EAC7G,IAAI,EAAE,WAAW,GAAG,QAAQ,CAAC,WAAW,CAAC,EACzC,KAAK,EAAE,CAAC,OAAO,EAAE,mBAAmB,CAAC,IAAI,CAAC,KAAK,IAAI,EACnD,OAAO,CAAC,EAAE,cAAc,GAAG,SAAS,GACnC,YAAY,CAyBd"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
import { onDestroy } from "svelte";
|
|
3
|
+
import { channelSlot } from "@supatype/client/bindings";
|
|
4
|
+
import { getSupatypeClient } from "./context.js";
|
|
5
|
+
import { createRealtimeConnection } from "./createRealtimeConnection.js";
|
|
6
|
+
/** A name given as a store, told apart from a plain string by its `subscribe`. */
|
|
7
|
+
function isReadable(name) {
|
|
8
|
+
return typeof name !== "string";
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Join one channel and register whatever listeners `setup` adds to it: row changes, presence and
|
|
12
|
+
* broadcast together, which `createSubscription` (one table, one listener) cannot do.
|
|
13
|
+
*
|
|
14
|
+
* `name` may be a string or a store. `setup` runs straight away, and again on the new channel
|
|
15
|
+
* whenever the store changes, after the old channel is left and its listeners with it. The channel
|
|
16
|
+
* is left when the component is destroyed. With runes, `toStore(() => name)` from `svelte/store`
|
|
17
|
+
* turns reactive state into a store.
|
|
18
|
+
*
|
|
19
|
+
* `options.private` asks for a private channel (a schema `Channel` declaration guards it): see
|
|
20
|
+
* `ChannelOptions`. A refused or withdrawn join shows as `error`, a `RealtimeChannelError` whose `reason`
|
|
21
|
+
* says why.
|
|
22
|
+
*/
|
|
23
|
+
export function createChannel(name, setup, options) {
|
|
24
|
+
const client = getSupatypeClient();
|
|
25
|
+
const connection = createRealtimeConnection();
|
|
26
|
+
const status = writable("connecting");
|
|
27
|
+
const error = writable(null);
|
|
28
|
+
const refusal = writable(null);
|
|
29
|
+
const slot = channelSlot(client.realtime, setup, (view) => {
|
|
30
|
+
status.set(view.status);
|
|
31
|
+
error.set(view.error);
|
|
32
|
+
if (view.status === "connecting")
|
|
33
|
+
refusal.set(null);
|
|
34
|
+
}, options, (refused) => {
|
|
35
|
+
refusal.set(refused);
|
|
36
|
+
});
|
|
37
|
+
// A store calls a new subscriber at once with its value, which is the first join.
|
|
38
|
+
if (isReadable(name))
|
|
39
|
+
onDestroy(name.subscribe((next) => slot.join(next)));
|
|
40
|
+
else
|
|
41
|
+
slot.join(name);
|
|
42
|
+
onDestroy(slot.leave);
|
|
43
|
+
return {
|
|
44
|
+
status: { subscribe: status.subscribe },
|
|
45
|
+
error: { subscribe: error.subscribe },
|
|
46
|
+
refusal: { subscribe: refusal.subscribe },
|
|
47
|
+
...connection,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=createChannel.js.map
|
|
@@ -0,0 +1 @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,kFAAkF;IAClF,IAAI,UAAU,CAAC,IAAI,CAAC;QAAE,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;;QACrE,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACpB,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IAErB,OAAO;QACL,MAAM,EAAE,EAAE,SAAS,EAAE,MAAM,CAAC,SAAS,EAAE;QACvC,KAAK,EAAE,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE;QACrC,OAAO,EAAE,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,EAAE;QACzC,GAAG,UAAU;KACd,CAAA;AACH,CAAC"}
|
package/dist/createQuery.d.ts
CHANGED
|
@@ -17,5 +17,5 @@ export interface QueryStore<TRow> {
|
|
|
17
17
|
loading: Readable<boolean>;
|
|
18
18
|
refetch: () => Promise<void>;
|
|
19
19
|
}
|
|
20
|
-
export declare function createQuery<TDatabase extends AnyDatabase = AugmentedDatabase, TTable extends keyof TDatabase["public"]["Tables"] & string = keyof TDatabase["public"]["Tables"] & string, TRow = TDatabase["public"]["Tables"][TTable]["Row"]>(table: TTable, options?: QueryOptions | undefined): QueryStore<TRow>;
|
|
20
|
+
export declare function createQuery<TDatabase extends AnyDatabase = AugmentedDatabase, TTable extends keyof TDatabase["public"]["Tables"] & string = keyof TDatabase["public"]["Tables"] & string, TRow = TDatabase["public"]["Tables"][TTable]["Row"]>(table: TTable, options?: QueryOptions | Readable<QueryOptions | undefined> | undefined): QueryStore<TRow>;
|
|
21
21
|
//# sourceMappingURL=createQuery.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createQuery.d.ts","sourceRoot":"","sources":["../src/createQuery.ts"],"names":[],"mappings":"AACA,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAA;AACtD,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,SAAS,CAAC,GAAG,SAAS,GACtE,UAAU,CAAC,IAAI,CAAC,CAqClB"}
|
package/dist/createQuery.js
CHANGED
|
@@ -6,17 +6,34 @@ import { latestRowsReader } from "@supatype/client/bindings";
|
|
|
6
6
|
export function createQuery(table, options) {
|
|
7
7
|
const client = getSupatypeClient();
|
|
8
8
|
const { data, error, loading, read } = latestRows(client, table);
|
|
9
|
+
// The options as last read, and the store they come from when they are one.
|
|
10
|
+
let current;
|
|
11
|
+
let optionsStore;
|
|
12
|
+
if (isReadable(options))
|
|
13
|
+
optionsStore = options;
|
|
14
|
+
else
|
|
15
|
+
current = options;
|
|
9
16
|
const fetchData = async () => {
|
|
10
|
-
if (
|
|
11
|
-
await read(
|
|
17
|
+
if (current?.enabled !== false)
|
|
18
|
+
await read(current);
|
|
12
19
|
};
|
|
13
|
-
// Auto-fetch on creation
|
|
14
|
-
void fetchData();
|
|
15
|
-
// Re-run when the signed-in identity changes. See `onIdentityChange` for which changes count,
|
|
16
|
-
// and why the fetch above is not enough on its own.
|
|
17
|
-
//
|
|
18
20
|
// `onDestroy` is safe here for the same reason `getSupatypeClient()` above is: both need a
|
|
19
21
|
// component's init, and this store is read out of context, so it already had one.
|
|
22
|
+
if (optionsStore !== undefined) {
|
|
23
|
+
// 0.9.1 release test (FF25): the options were read once, so a sort, page or filter held in
|
|
24
|
+
// component state changed nothing. A store calls a new subscriber at once with its value,
|
|
25
|
+
// which is the first read, then again on every change. With runes, `toStore(() => ({ ... }))`
|
|
26
|
+
// from `svelte/store` turns `$state` into one.
|
|
27
|
+
onDestroy(optionsStore.subscribe((next) => {
|
|
28
|
+
current = next;
|
|
29
|
+
void fetchData();
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
void fetchData();
|
|
34
|
+
}
|
|
35
|
+
// Re-run when the signed-in identity changes. See `onIdentityChange` for which changes count,
|
|
36
|
+
// and why the fetch above is not enough on its own.
|
|
20
37
|
onDestroy(onIdentityChange(client.auth, () => void fetchData()));
|
|
21
38
|
return {
|
|
22
39
|
data: { subscribe: data.subscribe },
|
|
@@ -25,6 +42,10 @@ export function createQuery(table, options) {
|
|
|
25
42
|
refetch: fetchData,
|
|
26
43
|
};
|
|
27
44
|
}
|
|
45
|
+
/** Options given as a store, told apart from a plain options object by its `subscribe`. */
|
|
46
|
+
function isReadable(options) {
|
|
47
|
+
return options !== undefined && "subscribe" in options && typeof options.subscribe === "function";
|
|
48
|
+
}
|
|
28
49
|
/** The stores a query shows, fed by reads of which only the newest may change them. */
|
|
29
50
|
function latestRows(client, table) {
|
|
30
51
|
const data = writable(null);
|
package/dist/createQuery.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createQuery.js","sourceRoot":"","sources":["../src/createQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAClC,OAAO,EAAE,QAAQ,EAAiB,MAAM,cAAc,CAAA;AAEtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAChD,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAA;AAkB5D,MAAM,UAAU,WAAW,CAKzB,KAAa,EACb,
|
|
1
|
+
{"version":3,"file":"createQuery.js","sourceRoot":"","sources":["../src/createQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAClC,OAAO,EAAE,QAAQ,EAAiB,MAAM,cAAc,CAAA;AAEtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAChD,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAA;AAkB5D,MAAM,UAAU,WAAW,CAKzB,KAAa,EACb,OAAuE;IAEvE,MAAM,MAAM,GAAG,iBAAiB,EAAa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KAAK,SAAS,IAAI,WAAW,IAAI,OAAO,IAAI,OAAO,OAAO,CAAC,SAAS,KAAK,UAAU,CAAA;AACnG,CAAC;AAED,uFAAuF;AACvF,SAAS,UAAU,CAAO,MAAiB,EAAE,KAAa;IACxD,MAAM,IAAI,GAAG,QAAQ,CAAgB,IAAI,CAAC,CAAA;IAC1C,MAAM,KAAK,GAAG,QAAQ,CAAuB,IAAI,CAAC,CAAA;IAClD,MAAM,OAAO,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;IAC/B,MAAM,IAAI,GAAG,gBAAgB,CAAO,MAAM,EAAE,KAAK,EAAE,gBAAgB,KAAK,IAAI,EAAE;QAC5E,OAAO,EAAE,GAAG,EAAE;YACZ,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;YACjB,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;QACjB,CAAC;QACD,OAAO,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE;YACzB,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;YACd,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;YAClB,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;QACpB,CAAC;KACF,CAAC,CAAA;IACF,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,CAAA;AACvC,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Readable } from "svelte/store";
|
|
2
|
+
import type { AugmentedDatabase, AnyDatabase, RealtimeConnectionState } from "@supatype/client";
|
|
3
|
+
export interface RealtimeConnectionStore {
|
|
4
|
+
/** "connecting" covers the first connect, a reconnect after a drop, and the sign-in swap. */
|
|
5
|
+
connectionState: Readable<RealtimeConnectionState>;
|
|
6
|
+
/** True while the realtime socket is open. A channel may still be waiting on its join. */
|
|
7
|
+
isConnected: Readable<boolean>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The realtime socket's own state, which no channel status reports: a drop and a first connect both
|
|
11
|
+
* read "connecting" on a channel. Reported by `watchConnection`, shared with Vue and Solid.
|
|
12
|
+
*/
|
|
13
|
+
export declare function createRealtimeConnection<TDatabase extends AnyDatabase = AugmentedDatabase>(): RealtimeConnectionStore;
|
|
14
|
+
//# sourceMappingURL=createRealtimeConnection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createRealtimeConnection.d.ts","sourceRoot":"","sources":["../src/createRealtimeConnection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAA;AAE/D,OAAO,KAAK,EAAE,iBAAiB,EAAE,WAAW,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAA;AAI/F,MAAM,WAAW,uBAAuB;IACtC,6FAA6F;IAC7F,eAAe,EAAE,QAAQ,CAAC,uBAAuB,CAAC,CAAA;IAClD,0FAA0F;IAC1F,WAAW,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;CAC/B;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,SAAS,SAAS,WAAW,GAAG,iBAAiB,KAAK,uBAAuB,CAUrH"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { derived, writable } from "svelte/store";
|
|
2
|
+
import { onDestroy } from "svelte";
|
|
3
|
+
import { watchConnection } from "@supatype/client/bindings";
|
|
4
|
+
import { getSupatypeClient } from "./context.js";
|
|
5
|
+
/**
|
|
6
|
+
* The realtime socket's own state, which no channel status reports: a drop and a first connect both
|
|
7
|
+
* read "connecting" on a channel. Reported by `watchConnection`, shared with Vue and Solid.
|
|
8
|
+
*/
|
|
9
|
+
export function createRealtimeConnection() {
|
|
10
|
+
const client = getSupatypeClient();
|
|
11
|
+
const state = writable(client.realtime.connectionState);
|
|
12
|
+
onDestroy(watchConnection(client.realtime, (next) => state.set(next)));
|
|
13
|
+
return {
|
|
14
|
+
connectionState: { subscribe: state.subscribe },
|
|
15
|
+
isConnected: derived(state, ($state) => $state === "connected"),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=createRealtimeConnection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createRealtimeConnection.js","sourceRoot":"","sources":["../src/createRealtimeConnection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAiB,MAAM,cAAc,CAAA;AAC/D,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAElC,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAC3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAShD;;;GAGG;AACH,MAAM,UAAU,wBAAwB;IACtC,MAAM,MAAM,GAAG,iBAAiB,EAAa,CAAA;IAC7C,MAAM,KAAK,GAAG,QAAQ,CAA0B,MAAM,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAA;IAEhF,SAAS,CAAC,eAAe,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IAEtE,OAAO;QACL,eAAe,EAAE,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE;QAC/C,WAAW,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,KAAK,WAAW,CAAC;KAChE,CAAA;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createSubscription.d.ts","sourceRoot":"","sources":["../src/createSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAA;AAEtD,OAAO,KAAK,EAAE,iBAAiB,
|
|
1
|
+
{"version":3,"file":"createSubscription.d.ts","sourceRoot":"","sources":["../src/createSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAA;AAEtD,OAAO,KAAK,EAAE,iBAAiB,EAAa,WAAW,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAChG,OAAO,EAAc,KAAK,iBAAiB,EAAE,KAAK,iBAAiB,EAAE,MAAM,2BAA2B,CAAA;AAGtG,mGAAmG;AACnG,MAAM,MAAM,mBAAmB,GAAG,iBAAiB,CAAA;AAEnD,MAAM,WAAW,iBAAiB,CAAC,IAAI;IACrC,IAAI,EAAE,QAAQ,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAA;IAC7B,KAAK,EAAE,QAAQ,CAAC,aAAa,GAAG,IAAI,CAAC,CAAA;IACrC,MAAM,EAAE,QAAQ,CAAC,iBAAiB,CAAC,CAAA;CACpC;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,SAAS,SAAS,WAAW,GAAG,iBAAiB,EACjD,MAAM,SAAS,MAAM,SAAS,CAAC,QAAQ,CAAC,CAAC,QAAQ,CAAC,GAAG,MAAM,GAAG,MAAM,SAAS,CAAC,QAAQ,CAAC,CAAC,QAAQ,CAAC,GAAG,MAAM,EAC1G,IAAI,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,EAEnD,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,mBAAmB,GAAG,SAAS,GACxC,iBAAiB,CAAC,IAAI,CAAC,CAqBzB"}
|
|
@@ -13,7 +13,7 @@ export function createSubscription(table, options) {
|
|
|
13
13
|
const data = writable(null);
|
|
14
14
|
const error = writable(null);
|
|
15
15
|
const status = writable("connecting");
|
|
16
|
-
const unsubscribe = watchTable(client
|
|
16
|
+
const unsubscribe = watchTable(client, table, options, {
|
|
17
17
|
rows: (rows) => data.set(rows),
|
|
18
18
|
status: (view) => {
|
|
19
19
|
status.set(view.status);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createSubscription.js","sourceRoot":"","sources":["../src/createSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAiB,MAAM,cAAc,CAAA;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAElC,OAAO,EAAE,UAAU,EAAkD,MAAM,2BAA2B,CAAA;AACtG,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAWhD;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAKhC,KAAa,EACb,OAAyC;IAEzC,MAAM,MAAM,GAAG,iBAAiB,EAAa,CAAA;IAC7C,MAAM,IAAI,GAAG,QAAQ,CAAgB,IAAI,CAAC,CAAA;IAC1C,MAAM,KAAK,GAAG,QAAQ,CAAuB,IAAI,CAAC,CAAA;IAClD,MAAM,MAAM,GAAG,QAAQ,CAAoB,YAAY,CAAC,CAAA;IAExD,MAAM,WAAW,GAAG,UAAU,CAAO,
|
|
1
|
+
{"version":3,"file":"createSubscription.js","sourceRoot":"","sources":["../src/createSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAiB,MAAM,cAAc,CAAA;AACtD,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AAElC,OAAO,EAAE,UAAU,EAAkD,MAAM,2BAA2B,CAAA;AACtG,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAWhD;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAKhC,KAAa,EACb,OAAyC;IAEzC,MAAM,MAAM,GAAG,iBAAiB,EAAa,CAAA;IAC7C,MAAM,IAAI,GAAG,QAAQ,CAAgB,IAAI,CAAC,CAAA;IAC1C,MAAM,KAAK,GAAG,QAAQ,CAAuB,IAAI,CAAC,CAAA;IAClD,MAAM,MAAM,GAAG,QAAQ,CAAoB,YAAY,CAAC,CAAA;IAExD,MAAM,WAAW,GAAG,UAAU,CAAO,MAAmB,EAAE,KAAK,EAAE,OAAO,EAAE;QACxE,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC;QAC9B,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;YACf,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;YACvB,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QACvB,CAAC;KACF,CAAC,CAAA;IAEF,SAAS,CAAC,WAAW,CAAC,CAAA;IAEtB,OAAO;QACL,IAAI,EAAE,EAAE,SAAS,EAAE,IAAI,CAAC,SAAS,EAAE;QACnC,KAAK,EAAE,EAAE,SAAS,EAAE,KAAK,CAAC,SAAS,EAAE;QACrC,MAAM,EAAE,EAAE,SAAS,EAAE,MAAM,CAAC,SAAS,EAAE;KACxC,CAAA;AACH,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -9,4 +9,8 @@ export { createSubscription } from "./createSubscription.js";
|
|
|
9
9
|
export type { SubscriptionOptions, SubscriptionStore } from "./createSubscription.js";
|
|
10
10
|
export { createFunction } from "./createFunction.js";
|
|
11
11
|
export type { FunctionStore } from "./createFunction.js";
|
|
12
|
+
export { createChannel } from "./createChannel.js";
|
|
13
|
+
export type { ChannelStore } from "./createChannel.js";
|
|
14
|
+
export { createRealtimeConnection } from "./createRealtimeConnection.js";
|
|
15
|
+
export type { RealtimeConnectionStore } from "./createRealtimeConnection.js";
|
|
12
16
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAEnE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,YAAY,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAEhE,OAAO,EAAE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AM,oBAAoB,CAAA;AAEtD,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAA;AACxE,YAAY,EAAE,uBAAuB,EAAE,MAAM,+BAA+B,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -5,4 +5,6 @@ export { createMutation } from "./createMutation.js";
|
|
|
5
5
|
export { createAuth } from "./createAuth.js";
|
|
6
6
|
export { createSubscription } from "./createSubscription.js";
|
|
7
7
|
export { createFunction } from "./createFunction.js";
|
|
8
|
+
export { createChannel } from "./createChannel.js";
|
|
9
|
+
export { createRealtimeConnection } from "./createRealtimeConnection.js";
|
|
8
10
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,+CAA+C;AAE/C,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAEnE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAG9C,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAGpD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAG5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAG5D,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,+CAA+C;AAE/C,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAEnE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAG9C,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAGpD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAG5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAG5D,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AAGpD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAGlD,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supatype/svelte",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Svelte stores for Supatype, createQuery, createMutation, createAuth, createSubscription",
|
|
6
6
|
"type": "module",
|
|
@@ -14,14 +14,14 @@
|
|
|
14
14
|
},
|
|
15
15
|
"peerDependencies": {
|
|
16
16
|
"svelte": "^5.0.0",
|
|
17
|
-
"@supatype/client": "0.
|
|
17
|
+
"@supatype/client": "0.10.0"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"@types/node": "^22",
|
|
21
21
|
"svelte": "^5.0.0",
|
|
22
22
|
"typescript": "^5",
|
|
23
23
|
"vitest": "^4.0.18",
|
|
24
|
-
"@supatype/client": "0.
|
|
24
|
+
"@supatype/client": "0.10.0"
|
|
25
25
|
},
|
|
26
26
|
"scripts": {
|
|
27
27
|
"build": "tsc --project tsconfig.json",
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { writable, type Readable } from "svelte/store"
|
|
2
|
+
import { onDestroy } from "svelte"
|
|
3
|
+
import type { AugmentedDatabase, AnyDatabase, ChannelActionRefusal, ChannelName, ChannelOptions, ChannelSubscription, SupatypeError } from "@supatype/client"
|
|
4
|
+
import { channelSlot, type SubscriptionState } from "@supatype/client/bindings"
|
|
5
|
+
import { getSupatypeClient } from "./context.js"
|
|
6
|
+
import { createRealtimeConnection, type RealtimeConnectionStore } from "./createRealtimeConnection.js"
|
|
7
|
+
|
|
8
|
+
export interface ChannelStore extends RealtimeConnectionStore {
|
|
9
|
+
/** This channel's join: "connecting", "connected", "disconnected" or "error". */
|
|
10
|
+
status: Readable<SubscriptionState>
|
|
11
|
+
/** Why the server refused the join, while the status is "error"; null otherwise. */
|
|
12
|
+
error: Readable<SupatypeError | null>
|
|
13
|
+
/** The latest send, track or payload the server refused on this channel; the status does not move for it. */
|
|
14
|
+
refusal: Readable<ChannelActionRefusal | null>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** A name given as a store, told apart from a plain string by its `subscribe`. */
|
|
18
|
+
function isReadable(name: ChannelName | Readable<ChannelName>): name is Readable<ChannelName> {
|
|
19
|
+
return typeof name !== "string"
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Join one channel and register whatever listeners `setup` adds to it: row changes, presence and
|
|
24
|
+
* broadcast together, which `createSubscription` (one table, one listener) cannot do.
|
|
25
|
+
*
|
|
26
|
+
* `name` may be a string or a store. `setup` runs straight away, and again on the new channel
|
|
27
|
+
* whenever the store changes, after the old channel is left and its listeners with it. The channel
|
|
28
|
+
* is left when the component is destroyed. With runes, `toStore(() => name)` from `svelte/store`
|
|
29
|
+
* turns reactive state into a store.
|
|
30
|
+
*
|
|
31
|
+
* `options.private` asks for a private channel (a schema `Channel` declaration guards it): see
|
|
32
|
+
* `ChannelOptions`. A refused or withdrawn join shows as `error`, a `RealtimeChannelError` whose `reason`
|
|
33
|
+
* says why.
|
|
34
|
+
*/
|
|
35
|
+
export function createChannel<TRow = Record<string, unknown>, TDatabase extends AnyDatabase = AugmentedDatabase>(
|
|
36
|
+
name: ChannelName | Readable<ChannelName>,
|
|
37
|
+
setup: (channel: ChannelSubscription<TRow>) => void,
|
|
38
|
+
options?: ChannelOptions | undefined,
|
|
39
|
+
): ChannelStore {
|
|
40
|
+
const client = getSupatypeClient<TDatabase>()
|
|
41
|
+
const connection = createRealtimeConnection<TDatabase>()
|
|
42
|
+
const status = writable<SubscriptionState>("connecting")
|
|
43
|
+
const error = writable<SupatypeError | null>(null)
|
|
44
|
+
const refusal = writable<ChannelActionRefusal | null>(null)
|
|
45
|
+
const slot = channelSlot<TRow>(client.realtime, setup, (view) => {
|
|
46
|
+
status.set(view.status)
|
|
47
|
+
error.set(view.error)
|
|
48
|
+
if (view.status === "connecting") refusal.set(null)
|
|
49
|
+
}, options, (refused) => {
|
|
50
|
+
refusal.set(refused)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
// A store calls a new subscriber at once with its value, which is the first join.
|
|
54
|
+
if (isReadable(name)) onDestroy(name.subscribe((next) => slot.join(next)))
|
|
55
|
+
else slot.join(name)
|
|
56
|
+
onDestroy(slot.leave)
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
status: { subscribe: status.subscribe },
|
|
60
|
+
error: { subscribe: error.subscribe },
|
|
61
|
+
refusal: { subscribe: refusal.subscribe },
|
|
62
|
+
...connection,
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/createQuery.ts
CHANGED
|
@@ -27,22 +27,36 @@ export function createQuery<
|
|
|
27
27
|
TRow = TDatabase["public"]["Tables"][TTable]["Row"],
|
|
28
28
|
>(
|
|
29
29
|
table: TTable,
|
|
30
|
-
options?: QueryOptions | undefined,
|
|
30
|
+
options?: QueryOptions | Readable<QueryOptions | undefined> | undefined,
|
|
31
31
|
): QueryStore<TRow> {
|
|
32
32
|
const client = getSupatypeClient<TDatabase>()
|
|
33
33
|
const { data, error, loading, read } = latestRows<TRow>(client as AnyClient, table)
|
|
34
|
+
// The options as last read, and the store they come from when they are one.
|
|
35
|
+
let current: QueryOptions | undefined
|
|
36
|
+
let optionsStore: Readable<QueryOptions | undefined> | undefined
|
|
37
|
+
if (isReadable(options)) optionsStore = options
|
|
38
|
+
else current = options
|
|
34
39
|
const fetchData = async () => {
|
|
35
|
-
if (
|
|
40
|
+
if (current?.enabled !== false) await read(current)
|
|
36
41
|
}
|
|
37
42
|
|
|
38
|
-
//
|
|
39
|
-
|
|
43
|
+
// `onDestroy` is safe here for the same reason `getSupatypeClient()` above is: both need a
|
|
44
|
+
// component's init, and this store is read out of context, so it already had one.
|
|
45
|
+
if (optionsStore !== undefined) {
|
|
46
|
+
// 0.9.1 release test (FF25): the options were read once, so a sort, page or filter held in
|
|
47
|
+
// component state changed nothing. A store calls a new subscriber at once with its value,
|
|
48
|
+
// which is the first read, then again on every change. With runes, `toStore(() => ({ ... }))`
|
|
49
|
+
// from `svelte/store` turns `$state` into one.
|
|
50
|
+
onDestroy(optionsStore.subscribe((next) => {
|
|
51
|
+
current = next
|
|
52
|
+
void fetchData()
|
|
53
|
+
}))
|
|
54
|
+
} else {
|
|
55
|
+
void fetchData()
|
|
56
|
+
}
|
|
40
57
|
|
|
41
58
|
// Re-run when the signed-in identity changes. See `onIdentityChange` for which changes count,
|
|
42
59
|
// and why the fetch above is not enough on its own.
|
|
43
|
-
//
|
|
44
|
-
// `onDestroy` is safe here for the same reason `getSupatypeClient()` above is: both need a
|
|
45
|
-
// component's init, and this store is read out of context, so it already had one.
|
|
46
60
|
onDestroy(onIdentityChange(client.auth, () => void fetchData()))
|
|
47
61
|
|
|
48
62
|
return {
|
|
@@ -53,6 +67,13 @@ export function createQuery<
|
|
|
53
67
|
}
|
|
54
68
|
}
|
|
55
69
|
|
|
70
|
+
/** Options given as a store, told apart from a plain options object by its `subscribe`. */
|
|
71
|
+
function isReadable(
|
|
72
|
+
options: QueryOptions | Readable<QueryOptions | undefined> | undefined,
|
|
73
|
+
): options is Readable<QueryOptions | undefined> {
|
|
74
|
+
return options !== undefined && "subscribe" in options && typeof options.subscribe === "function"
|
|
75
|
+
}
|
|
76
|
+
|
|
56
77
|
/** The stores a query shows, fed by reads of which only the newest may change them. */
|
|
57
78
|
function latestRows<TRow>(client: AnyClient, table: string) {
|
|
58
79
|
const data = writable<TRow[] | null>(null)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { derived, writable, type Readable } from "svelte/store"
|
|
2
|
+
import { onDestroy } from "svelte"
|
|
3
|
+
import type { AugmentedDatabase, AnyDatabase, RealtimeConnectionState } from "@supatype/client"
|
|
4
|
+
import { watchConnection } from "@supatype/client/bindings"
|
|
5
|
+
import { getSupatypeClient } from "./context.js"
|
|
6
|
+
|
|
7
|
+
export interface RealtimeConnectionStore {
|
|
8
|
+
/** "connecting" covers the first connect, a reconnect after a drop, and the sign-in swap. */
|
|
9
|
+
connectionState: Readable<RealtimeConnectionState>
|
|
10
|
+
/** True while the realtime socket is open. A channel may still be waiting on its join. */
|
|
11
|
+
isConnected: Readable<boolean>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The realtime socket's own state, which no channel status reports: a drop and a first connect both
|
|
16
|
+
* read "connecting" on a channel. Reported by `watchConnection`, shared with Vue and Solid.
|
|
17
|
+
*/
|
|
18
|
+
export function createRealtimeConnection<TDatabase extends AnyDatabase = AugmentedDatabase>(): RealtimeConnectionStore {
|
|
19
|
+
const client = getSupatypeClient<TDatabase>()
|
|
20
|
+
const state = writable<RealtimeConnectionState>(client.realtime.connectionState)
|
|
21
|
+
|
|
22
|
+
onDestroy(watchConnection(client.realtime, (next) => state.set(next)))
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
connectionState: { subscribe: state.subscribe },
|
|
26
|
+
isConnected: derived(state, ($state) => $state === "connected"),
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { writable, type Readable } from "svelte/store"
|
|
2
2
|
import { onDestroy } from "svelte"
|
|
3
|
-
import type { AugmentedDatabase, AnyDatabase, SupatypeError } from "@supatype/client"
|
|
3
|
+
import type { AugmentedDatabase, AnyClient, AnyDatabase, SupatypeError } from "@supatype/client"
|
|
4
4
|
import { watchTable, type SubscriptionState, type TableWatchOptions } from "@supatype/client/bindings"
|
|
5
5
|
import { getSupatypeClient } from "./context.js"
|
|
6
6
|
|
|
@@ -32,7 +32,7 @@ export function createSubscription<
|
|
|
32
32
|
const error = writable<SupatypeError | null>(null)
|
|
33
33
|
const status = writable<SubscriptionState>("connecting")
|
|
34
34
|
|
|
35
|
-
const unsubscribe = watchTable<TRow>(client
|
|
35
|
+
const unsubscribe = watchTable<TRow>(client as AnyClient, table, options, {
|
|
36
36
|
rows: (rows) => data.set(rows),
|
|
37
37
|
status: (view) => {
|
|
38
38
|
status.set(view.status)
|
package/src/index.ts
CHANGED
|
@@ -16,3 +16,9 @@ export type { SubscriptionOptions, SubscriptionStore } from "./createSubscriptio
|
|
|
16
16
|
|
|
17
17
|
export { createFunction } from "./createFunction.js"
|
|
18
18
|
export type { FunctionStore } from "./createFunction.js"
|
|
19
|
+
|
|
20
|
+
export { createChannel } from "./createChannel.js"
|
|
21
|
+
export type { ChannelStore } from "./createChannel.js"
|
|
22
|
+
|
|
23
|
+
export { createRealtimeConnection } from "./createRealtimeConnection.js"
|
|
24
|
+
export type { RealtimeConnectionStore } from "./createRealtimeConnection.js"
|