@supatype/vue 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/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/dist/useChannel.d.ts +34 -0
- package/dist/useChannel.d.ts.map +1 -0
- package/dist/useChannel.js +54 -0
- package/dist/useChannel.js.map +1 -0
- package/dist/useQuery.d.ts +9 -4
- package/dist/useQuery.d.ts.map +1 -1
- package/dist/useQuery.js +26 -16
- package/dist/useQuery.js.map +1 -1
- package/dist/useRealtimeConnection.d.ts +14 -0
- package/dist/useRealtimeConnection.d.ts.map +1 -0
- package/dist/useRealtimeConnection.js +20 -0
- package/dist/useRealtimeConnection.js.map +1 -0
- package/dist/useSubscription.d.ts.map +1 -1
- package/dist/useSubscription.js +1 -1
- package/dist/useSubscription.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +6 -0
- package/src/useChannel.ts +68 -0
- package/src/useQuery.ts +26 -15
- package/src/useRealtimeConnection.ts +30 -0
- package/src/useSubscription.ts +2 -2
- package/tests/useChannel-private.test.ts +61 -0
- package/tests/useChannel-reactive-name.test.ts +65 -0
- package/tests/useChannel-typed-name.test.ts +15 -0
- package/tests/useChannel.test.ts +60 -0
- package/tests/useQuery-first-render.test.ts +44 -0
- package/tests/useQuery-reactive-options.test.ts +68 -0
- package/tests/useSubscription-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/vue@0.
|
|
2
|
+
> @supatype/vue@0.10.0 test /home/runner/work/supatype/supatype/packages/vue
|
|
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/vue[39m
|
|
7
7
|
|
|
8
|
-
[32m✓[39m tests/
|
|
9
|
-
[32m✓[39m tests/
|
|
10
|
-
[32m✓[39m tests/
|
|
11
|
-
[32m✓[39m tests/
|
|
12
|
-
[32m✓[39m tests/
|
|
13
|
-
|
|
8
|
+
[32m✓[39m tests/useChannel-reactive-name.test.ts [2m([22m[2m3 tests[22m[2m)[22m[32m 237[2mms[22m[39m
|
|
9
|
+
[32m✓[39m tests/useChannel-private.test.ts [2m([22m[2m3 tests[22m[2m)[22m[32m 207[2mms[22m[39m
|
|
10
|
+
[32m✓[39m tests/useChannel.test.ts [2m([22m[2m3 tests[22m[2m)[22m[33m 331[2mms[22m[39m
|
|
11
|
+
[32m✓[39m tests/useSubscription-status.test.ts [2m([22m[2m3 tests[22m[2m)[22m[32m 213[2mms[22m[39m
|
|
12
|
+
[32m✓[39m tests/useQuery-reactive-options.test.ts [2m([22m[2m10 tests[22m[2m)[22m[33m 630[2mms[22m[39m
|
|
13
|
+
[33m[2m✓[22m[39m reads once on mount [33m 324[2mms[22m[39m
|
|
14
|
+
[32m✓[39m tests/thrown-errors.test.ts [2m([22m[2m5 tests[22m[2m)[22m[33m 506[2mms[22m[39m
|
|
15
|
+
[33m[2m✓[22m[39m a query still holds a returned error, unreported [33m 346[2mms[22m[39m
|
|
16
|
+
[32m✓[39m tests/useQuery-first-render.test.ts [2m([22m[2m2 tests[22m[2m)[22m[32m 20[2mms[22m[39m
|
|
17
|
+
[32m✓[39m tests/useAuth-oauth.test.ts [2m([22m[2m3 tests[22m[2m)[22m[32m 56[2mms[22m[39m
|
|
18
|
+
[32m✓[39m tests/useQuery-stale.test.ts [2m([22m[2m4 tests[22m[2m)[22m[32m 123[2mms[22m[39m
|
|
19
|
+
[32m✓[39m tests/useAuth-signup.test.ts [2m([22m[2m2 tests[22m[2m)[22m[32m 41[2mms[22m[39m
|
|
20
|
+
[32m✓[39m tests/useChannel-typed-name.test.ts [2m([22m[2m1 test[22m[2m)[22m[32m 52[2mms[22m[39m
|
|
21
|
+
[32m✓[39m tests/useAuth-unmount.test.ts [2m([22m[2m1 test[22m[2m)[22m[32m 41[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[32m40 passed[39m[22m[90m (40)[39m
|
|
25
|
+
[2m Start at [22m 15:46:29
|
|
26
|
+
[2m Duration [22m 20.84s[2m (transform 7.34s, setup 0ms, import 23.47s, tests 2.46s, environment 13ms)[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
|
+
* **useChannel:** 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
|
+
* **useChannel:** `useChannel(name, setup)` joins one channel and hands it to `setup` to add any number of
|
|
9
|
+
row, presence and broadcast listeners, which `useSubscription` (one table, one listener) cannot. It returns
|
|
10
|
+
`status` and `error` as `useSubscription` does, and `isConnected` and `connectionState`. `name` is a string or
|
|
11
|
+
a ref or getter: 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
|
+
* **useRealtimeConnection:** `isConnected` and `connectionState` for the realtime socket itself.
|
|
14
|
+
* **useChannel:** 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
|
+
* **useChannel:** 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
|
+
* **useSubscription:** `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
|
|
|
@@ -25,3 +52,15 @@
|
|
|
25
52
|
* **useQuery:** only the newest read is shown. A refetch while a read was in flight let the older
|
|
26
53
|
read finish last and replace the newer rows with its own rows, its error or its throw, and end
|
|
27
54
|
loading while the newer read was still out. A throw from a replaced read is still logged.
|
|
55
|
+
|
|
56
|
+
## 0.9.0 (2026-11-04)
|
|
57
|
+
|
|
58
|
+
### Breaking Changes
|
|
59
|
+
|
|
60
|
+
* **useMutation:** `mutate` returns no rows unless asked: `data` is `null` after an insert, update,
|
|
61
|
+
upsert or delete, as with the client's own writes. Pass `{ select: true }` for every column or
|
|
62
|
+
`{ select: "id,title" }` for some, as `.select()` takes them. Returning a row needs the right
|
|
63
|
+
to read it, so a table whose rules let a caller create but not read refused every insert made
|
|
64
|
+
through `useMutation`. A delete returns `null` rather than `[]`, and its rows with `select`.
|
|
65
|
+
|
|
66
|
+
|
package/dist/index.d.ts
CHANGED
|
@@ -9,4 +9,8 @@ export { useSubscription } from "./useSubscription.js";
|
|
|
9
9
|
export type { UseSubscriptionOptions, UseSubscriptionReturn, SubscriptionEvent } from "./useSubscription.js";
|
|
10
10
|
export { useFunction } from "./useFunction.js";
|
|
11
11
|
export type { UseFunctionReturn } from "./useFunction.js";
|
|
12
|
+
export { useChannel } from "./useChannel.js";
|
|
13
|
+
export type { UseChannelReturn } from "./useChannel.js";
|
|
14
|
+
export { useRealtimeConnection } from "./useRealtimeConnection.js";
|
|
15
|
+
export type { UseRealtimeConnectionReturn } from "./useRealtimeConnection.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,cAAc,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAExE,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,YAAY,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAEpE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAE7F,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AACtD,YAAY,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AAE5G,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,YAAY,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAExE,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,YAAY,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,eAAe,CAAA;AAEpE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,YAAY,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAE7F,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAEjD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AACtD,YAAY,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AAE5G,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,YAAY,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAEzD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAC5C,YAAY,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAEvD,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA;AAClE,YAAY,EAAE,2BAA2B,EAAE,MAAM,4BAA4B,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -5,4 +5,6 @@ export { useMutation } from "./useMutation.js";
|
|
|
5
5
|
export { useAuth } from "./useAuth.js";
|
|
6
6
|
export { useSubscription } from "./useSubscription.js";
|
|
7
7
|
export { useFunction } from "./useFunction.js";
|
|
8
|
+
export { useChannel } from "./useChannel.js";
|
|
9
|
+
export { useRealtimeConnection } from "./useRealtimeConnection.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,8CAA8C;AAE9C,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAExE,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAGxC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAG9C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAGtC,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAGtD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,8CAA8C;AAE9C,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAExE,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAGxC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAG9C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAGtC,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAGtD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAG9C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAG5C,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAA"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type MaybeRefOrGetter, type Ref } from "vue";
|
|
2
|
+
import type { AugmentedDatabase, AnyDatabase, ChannelActionRefusal, ChannelName, ChannelOptions, ChannelSubscription, SupatypeError } from "@supatype/client";
|
|
3
|
+
import { type SubscriptionState } from "@supatype/client/bindings";
|
|
4
|
+
import { type UseRealtimeConnectionReturn } from "./useRealtimeConnection.js";
|
|
5
|
+
export interface UseChannelReturn extends UseRealtimeConnectionReturn {
|
|
6
|
+
/** This channel's join: "connecting", "connected", "disconnected" or "error". */
|
|
7
|
+
status: Ref<SubscriptionState>;
|
|
8
|
+
/** Why the server refused the join, while the status is "error"; null otherwise. */
|
|
9
|
+
error: Ref<SupatypeError | null>;
|
|
10
|
+
/** The latest send, track or payload the server refused on this channel; the status does not move for it. */
|
|
11
|
+
refusal: Ref<ChannelActionRefusal | null>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Join one channel and register whatever listeners `setup` adds to it: row changes, presence and
|
|
15
|
+
* broadcast together, which `useSubscription` (one table, one listener) cannot do.
|
|
16
|
+
*
|
|
17
|
+
* `name` may be a string, a ref or a getter. `setup` runs on mount, and again on the new channel
|
|
18
|
+
* whenever the name changes, after the old channel is left and its listeners with it. The channel
|
|
19
|
+
* is left when the component unmounts.
|
|
20
|
+
*
|
|
21
|
+
* `options.private` asks for a private channel (a schema `Channel` declaration guards it): see
|
|
22
|
+
* `ChannelOptions`. A refused or withdrawn join shows as `error`, a `RealtimeChannelError` whose `reason`
|
|
23
|
+
* says why.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const { status, isConnected } = useChannel(() => `auction:${props.id}`, (channel) => {
|
|
28
|
+
* channel.on("postgres_changes", { event: "INSERT", table: "bid" }, (p) => bids.value.push(p.new))
|
|
29
|
+
* channel.onBroadcast("outbid", (p) => notify(p))
|
|
30
|
+
* })
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare function useChannel<TRow = Record<string, unknown>, TDatabase extends AnyDatabase = AugmentedDatabase>(name: MaybeRefOrGetter<ChannelName>, setup: (channel: ChannelSubscription<TRow>) => void, options?: ChannelOptions | undefined): UseChannelReturn;
|
|
34
|
+
//# sourceMappingURL=useChannel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useChannel.d.ts","sourceRoot":"","sources":["../src/useChannel.ts"],"names":[],"mappings":"AAAA,OAAO,EAA+C,KAAK,gBAAgB,EAAE,KAAK,GAAG,EAAE,MAAM,KAAK,CAAA;AAClG,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,EAAyB,KAAK,2BAA2B,EAAE,MAAM,4BAA4B,CAAA;AAEpG,MAAM,WAAW,gBAAiB,SAAQ,2BAA2B;IACnE,iFAAiF;IACjF,MAAM,EAAE,GAAG,CAAC,iBAAiB,CAAC,CAAA;IAC9B,oFAAoF;IACpF,KAAK,EAAE,GAAG,CAAC,aAAa,GAAG,IAAI,CAAC,CAAA;IAChC,6GAA6G;IAC7G,OAAO,EAAE,GAAG,CAAC,oBAAoB,GAAG,IAAI,CAAC,CAAA;CAC1C;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,UAAU,CAAC,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,SAAS,SAAS,WAAW,GAAG,iBAAiB,EAC1G,IAAI,EAAE,gBAAgB,CAAC,WAAW,CAAC,EACnC,KAAK,EAAE,CAAC,OAAO,EAAE,mBAAmB,CAAC,IAAI,CAAC,KAAK,IAAI,EACnD,OAAO,CAAC,EAAE,cAAc,GAAG,SAAS,GACnC,gBAAgB,CA4BlB"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ref, onMounted, onUnmounted, toValue, watch } from "vue";
|
|
2
|
+
import { channelSlot } from "@supatype/client/bindings";
|
|
3
|
+
import { useSupatype } from "./context.js";
|
|
4
|
+
import { useRealtimeConnection } from "./useRealtimeConnection.js";
|
|
5
|
+
/**
|
|
6
|
+
* Join one channel and register whatever listeners `setup` adds to it: row changes, presence and
|
|
7
|
+
* broadcast together, which `useSubscription` (one table, one listener) cannot do.
|
|
8
|
+
*
|
|
9
|
+
* `name` may be a string, a ref or a getter. `setup` runs on mount, and again on the new channel
|
|
10
|
+
* whenever the name changes, after the old channel is left and its listeners with it. The channel
|
|
11
|
+
* is left when the component unmounts.
|
|
12
|
+
*
|
|
13
|
+
* `options.private` asks for a private channel (a schema `Channel` declaration guards it): see
|
|
14
|
+
* `ChannelOptions`. A refused or withdrawn join shows as `error`, a `RealtimeChannelError` whose `reason`
|
|
15
|
+
* says why.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const { status, isConnected } = useChannel(() => `auction:${props.id}`, (channel) => {
|
|
20
|
+
* channel.on("postgres_changes", { event: "INSERT", table: "bid" }, (p) => bids.value.push(p.new))
|
|
21
|
+
* channel.onBroadcast("outbid", (p) => notify(p))
|
|
22
|
+
* })
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function useChannel(name, setup, options) {
|
|
26
|
+
const client = useSupatype();
|
|
27
|
+
const connection = useRealtimeConnection();
|
|
28
|
+
const status = ref("connecting");
|
|
29
|
+
const error = ref(null);
|
|
30
|
+
const refusal = ref(null);
|
|
31
|
+
const slot = channelSlot(client.realtime, setup, (view) => {
|
|
32
|
+
status.value = view.status;
|
|
33
|
+
error.value = view.error;
|
|
34
|
+
if (view.status === "connecting")
|
|
35
|
+
refusal.value = null;
|
|
36
|
+
}, options, (refused) => {
|
|
37
|
+
refusal.value = refused;
|
|
38
|
+
});
|
|
39
|
+
let mounted = false;
|
|
40
|
+
onMounted(() => {
|
|
41
|
+
mounted = true;
|
|
42
|
+
slot.join(toValue(name));
|
|
43
|
+
});
|
|
44
|
+
watch(() => toValue(name), (next) => {
|
|
45
|
+
if (mounted)
|
|
46
|
+
slot.join(next);
|
|
47
|
+
});
|
|
48
|
+
onUnmounted(() => {
|
|
49
|
+
mounted = false;
|
|
50
|
+
slot.leave();
|
|
51
|
+
});
|
|
52
|
+
return { status, error, refusal, ...connection };
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=useChannel.js.map
|
|
@@ -0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,OAAO,CAAC,IAAI,CAAC,CAAC,CAAA;IAC1B,CAAC,CAAC,CAAA;IACF,KAAK,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,EAAE;QAClC,IAAI,OAAO;YAAE,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC9B,CAAC,CAAC,CAAA;IACF,WAAW,CAAC,GAAG,EAAE;QACf,OAAO,GAAG,KAAK,CAAA;QACf,IAAI,CAAC,KAAK,EAAE,CAAA;IACd,CAAC,CAAC,CAAA;IAEF,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,UAAU,EAAE,CAAA;AAClD,CAAC"}
|
package/dist/useQuery.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Ref } from "vue";
|
|
1
|
+
import { type MaybeRefOrGetter, type Ref } from "vue";
|
|
2
2
|
import type { AugmentedDatabase, AnyDatabase, SupatypeError } from "@supatype/client";
|
|
3
3
|
export interface UseQueryOptions {
|
|
4
4
|
columns?: string | undefined;
|
|
@@ -25,12 +25,17 @@ export interface UseQueryReturn<TRow> {
|
|
|
25
25
|
* <script setup>
|
|
26
26
|
* import { useQuery } from '@supatype/vue'
|
|
27
27
|
*
|
|
28
|
-
* const
|
|
28
|
+
* const page = ref(0)
|
|
29
|
+
* const { data: posts, loading, error, refetch } = useQuery('posts', () => ({
|
|
29
30
|
* order: { column: 'created_at', ascending: false },
|
|
30
31
|
* limit: 10,
|
|
31
|
-
*
|
|
32
|
+
* offset: page.value * 10,
|
|
33
|
+
* }))
|
|
32
34
|
* </script>
|
|
33
35
|
* ```
|
|
36
|
+
*
|
|
37
|
+
* `options` may be a plain object, a ref or a getter. The query runs again whenever what a ref or
|
|
38
|
+
* getter holds changes, including a ref nested inside it such as `enabled`.
|
|
34
39
|
*/
|
|
35
|
-
export declare function useQuery<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?: UseQueryOptions | undefined): UseQueryReturn<TRow>;
|
|
40
|
+
export declare function useQuery<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?: MaybeRefOrGetter<UseQueryOptions | undefined>): UseQueryReturn<TRow>;
|
|
36
41
|
//# sourceMappingURL=useQuery.d.ts.map
|
package/dist/useQuery.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../src/useQuery.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"useQuery.d.ts","sourceRoot":"","sources":["../src/useQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAA2D,KAAK,gBAAgB,EAAE,KAAK,GAAG,EAAE,MAAM,KAAK,CAAA;AAC9G,OAAO,KAAK,EAAE,iBAAiB,EAAa,WAAW,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAKhG,MAAM,WAAW,eAAe;IAC9B,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAA;IAC5C,KAAK,CAAC,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,SAAS,CAAA;IAC3D,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC3B,OAAO,CAAC,EAAE,GAAG,CAAC,OAAO,CAAC,GAAG,OAAO,GAAG,SAAS,CAAA;CAC7C;AAED,MAAM,WAAW,cAAc,CAAC,IAAI;IAClC,IAAI,EAAE,GAAG,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAA;IACxB,KAAK,EAAE,GAAG,CAAC,aAAa,GAAG,IAAI,CAAC,CAAA;IAChC,OAAO,EAAE,GAAG,CAAC,OAAO,CAAC,CAAA;IACrB,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;CAC7B;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,QAAQ,CACtB,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,gBAAgB,CAAC,eAAe,GAAG,SAAS,CAAC,GACtD,cAAc,CAAC,IAAI,CAAC,CAqBtB"}
|
package/dist/useQuery.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ref, shallowRef, onMounted, onUnmounted, watch } from "vue";
|
|
1
|
+
import { ref, shallowRef, onMounted, onUnmounted, toValue, watch } from "vue";
|
|
2
2
|
import { onIdentityChange } from "@supatype/client";
|
|
3
3
|
import { useSupatype } from "./context.js";
|
|
4
4
|
import { latestRowsReader } from "@supatype/client/bindings";
|
|
@@ -10,43 +10,53 @@ import { latestRowsReader } from "@supatype/client/bindings";
|
|
|
10
10
|
* <script setup>
|
|
11
11
|
* import { useQuery } from '@supatype/vue'
|
|
12
12
|
*
|
|
13
|
-
* const
|
|
13
|
+
* const page = ref(0)
|
|
14
|
+
* const { data: posts, loading, error, refetch } = useQuery('posts', () => ({
|
|
14
15
|
* order: { column: 'created_at', ascending: false },
|
|
15
16
|
* limit: 10,
|
|
16
|
-
*
|
|
17
|
+
* offset: page.value * 10,
|
|
18
|
+
* }))
|
|
17
19
|
* </script>
|
|
18
20
|
* ```
|
|
21
|
+
*
|
|
22
|
+
* `options` may be a plain object, a ref or a getter. The query runs again whenever what a ref or
|
|
23
|
+
* getter holds changes, including a ref nested inside it such as `enabled`.
|
|
19
24
|
*/
|
|
20
25
|
export function useQuery(table, options) {
|
|
21
26
|
const client = useSupatype();
|
|
22
|
-
const
|
|
27
|
+
const enabledAtSetup = isEnabled(toValue(options)?.enabled);
|
|
28
|
+
const { data, error, loading, read } = latestRows(client, table, enabledAtSetup);
|
|
23
29
|
const fetchData = async () => {
|
|
24
|
-
|
|
25
|
-
|
|
30
|
+
const current = toValue(options);
|
|
31
|
+
if (isEnabled(current?.enabled))
|
|
32
|
+
await read(current);
|
|
26
33
|
};
|
|
27
34
|
onMounted(fetchData);
|
|
28
35
|
// Re-run when the signed-in identity changes. See `onIdentityChange` for which changes count,
|
|
29
36
|
// and why the fetch above is not enough on its own.
|
|
30
37
|
onUnmounted(onIdentityChange(client.auth, () => void fetchData()));
|
|
31
|
-
//
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
fetchData();
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
+
// 0.9.1 release test (FF25): the options were read once, so a sort, page or filter held in
|
|
39
|
+
// component state changed nothing. Deep, so a ref inside the options (`enabled`) and a change
|
|
40
|
+
// inside a reactive object both count; `fetchData` skips the read while disabled.
|
|
41
|
+
watch(() => toValue(options), () => void fetchData(), { deep: true });
|
|
38
42
|
return { data, error, loading, refetch: fetchData };
|
|
39
43
|
}
|
|
40
44
|
/** `enabled` as a plain boolean: a ref is read now, and absent means enabled. */
|
|
41
45
|
function isEnabled(enabled) {
|
|
42
46
|
return enabled === undefined || (typeof enabled === "boolean" ? enabled : enabled.value);
|
|
43
47
|
}
|
|
44
|
-
/**
|
|
45
|
-
|
|
48
|
+
/**
|
|
49
|
+
* The refs a query shows, fed by reads of which only the newest may change them.
|
|
50
|
+
*
|
|
51
|
+
* `loading` starts as `firstReadComing`. 0.9.2 release test (N-FF9): it started false, so the
|
|
52
|
+
* render before `onMounted` began the read showed `loading: false, data: null`, which an app
|
|
53
|
+
* reads as "done, nothing found".
|
|
54
|
+
*/
|
|
55
|
+
function latestRows(client, table, firstReadComing) {
|
|
46
56
|
// Shallow: each fetch replaces the rows whole, and `ref` would type them as `UnwrapRef<TRow>`.
|
|
47
57
|
const data = shallowRef(null);
|
|
48
58
|
const error = ref(null);
|
|
49
|
-
const loading = ref(
|
|
59
|
+
const loading = ref(firstReadComing);
|
|
50
60
|
const read = latestRowsReader(client, table, `useQuery("${table}")`, {
|
|
51
61
|
started: () => {
|
|
52
62
|
loading.value = true;
|
package/dist/useQuery.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../src/useQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,KAAK,
|
|
1
|
+
{"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../src/useQuery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,OAAO,EAAE,KAAK,EAAmC,MAAM,KAAK,CAAA;AAE9G,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAC1C,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAA;AAkB5D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,QAAQ,CAKtB,KAAa,EACb,OAAuD;IAEvD,MAAM,MAAM,GAAG,WAAW,EAAa,CAAA;IACvC,MAAM,cAAc,GAAG,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,CAA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G,CAAC,eAAe,CAAC,CAAA;IACpC,MAAM,IAAI,GAAG,gBAAgB,CAAO,MAAM,EAAE,KAAK,EAAE,aAAa,KAAK,IAAI,EAAE;QACzE,OAAO,EAAE,GAAG,EAAE;YACZ,OAAO,CAAC,KAAK,GAAG,IAAI,CAAA;YACpB,KAAK,CAAC,KAAK,GAAG,IAAI,CAAA;QACpB,CAAC;QACD,OAAO,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE;YACzB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAA;YACjB,KAAK,CAAC,KAAK,GAAG,OAAO,CAAA;YACrB,OAAO,CAAC,KAAK,GAAG,KAAK,CAAA;QACvB,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 ComputedRef, type Ref } from "vue";
|
|
2
|
+
import type { AugmentedDatabase, AnyDatabase, RealtimeConnectionState } from "@supatype/client";
|
|
3
|
+
export interface UseRealtimeConnectionReturn {
|
|
4
|
+
/** "connecting" covers the first connect, a reconnect after a drop, and the sign-in swap. */
|
|
5
|
+
connectionState: Ref<RealtimeConnectionState>;
|
|
6
|
+
/** True while the realtime socket is open. A channel may still be waiting on its join. */
|
|
7
|
+
isConnected: ComputedRef<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 Svelte and Solid.
|
|
12
|
+
*/
|
|
13
|
+
export declare function useRealtimeConnection<TDatabase extends AnyDatabase = AugmentedDatabase>(): UseRealtimeConnectionReturn;
|
|
14
|
+
//# sourceMappingURL=useRealtimeConnection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRealtimeConnection.d.ts","sourceRoot":"","sources":["../src/useRealtimeConnection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyC,KAAK,WAAW,EAAE,KAAK,GAAG,EAAE,MAAM,KAAK,CAAA;AACvF,OAAO,KAAK,EAAE,iBAAiB,EAAE,WAAW,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAA;AAI/F,MAAM,WAAW,2BAA2B;IAC1C,6FAA6F;IAC7F,eAAe,EAAE,GAAG,CAAC,uBAAuB,CAAC,CAAA;IAC7C,0FAA0F;IAC1F,WAAW,EAAE,WAAW,CAAC,OAAO,CAAC,CAAA;CAClC;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,SAAS,SAAS,WAAW,GAAG,iBAAiB,KAAK,2BAA2B,CAatH"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { computed, ref, onMounted, onUnmounted } from "vue";
|
|
2
|
+
import { watchConnection } from "@supatype/client/bindings";
|
|
3
|
+
import { useSupatype } from "./context.js";
|
|
4
|
+
/**
|
|
5
|
+
* The realtime socket's own state, which no channel status reports: a drop and a first connect both
|
|
6
|
+
* read "connecting" on a channel. Reported by `watchConnection`, shared with Svelte and Solid.
|
|
7
|
+
*/
|
|
8
|
+
export function useRealtimeConnection() {
|
|
9
|
+
const client = useSupatype();
|
|
10
|
+
const connectionState = ref(client.realtime.connectionState);
|
|
11
|
+
let stop = null;
|
|
12
|
+
onMounted(() => {
|
|
13
|
+
stop = watchConnection(client.realtime, (state) => {
|
|
14
|
+
connectionState.value = state;
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
onUnmounted(() => stop?.());
|
|
18
|
+
return { connectionState, isConnected: computed(() => connectionState.value === "connected") };
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=useRealtimeConnection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRealtimeConnection.js","sourceRoot":"","sources":["../src/useRealtimeConnection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,GAAG,EAAE,SAAS,EAAE,WAAW,EAA8B,MAAM,KAAK,CAAA;AAEvF,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAS1C;;;GAGG;AACH,MAAM,UAAU,qBAAqB;IACnC,MAAM,MAAM,GAAG,WAAW,EAAa,CAAA;IACvC,MAAM,eAAe,GAAG,GAAG,CAA0B,MAAM,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAA;IACrF,IAAI,IAAI,GAAwB,IAAI,CAAA;IAEpC,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,GAAG,eAAe,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,EAAE;YAChD,eAAe,CAAC,KAAK,GAAG,KAAK,CAAA;QAC/B,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IACF,WAAW,CAAC,GAAG,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,CAAA;IAE3B,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,QAAQ,CAAC,GAAG,EAAE,CAAC,eAAe,CAAC,KAAK,KAAK,WAAW,CAAC,EAAE,CAAA;AAChG,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSubscription.d.ts","sourceRoot":"","sources":["../src/useSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAA2C,KAAK,GAAG,EAAE,MAAM,KAAK,CAAA;AACvE,OAAO,KAAK,EAAE,iBAAiB,
|
|
1
|
+
{"version":3,"file":"useSubscription.d.ts","sourceRoot":"","sources":["../src/useSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAA2C,KAAK,GAAG,EAAE,MAAM,KAAK,CAAA;AACvE,OAAO,KAAK,EAAE,iBAAiB,EAAa,WAAW,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAC/G,OAAO,EAAc,KAAK,iBAAiB,EAAE,KAAK,iBAAiB,EAAE,MAAM,2BAA2B,CAAA;AAGtG,MAAM,MAAM,iBAAiB,GAAG,aAAa,CAAA;AAE7C,mGAAmG;AACnG,MAAM,MAAM,sBAAsB,GAAG,iBAAiB,CAAA;AAEtD,MAAM,WAAW,qBAAqB,CAAC,IAAI;IACzC,IAAI,EAAE,GAAG,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAA;IACxB,KAAK,EAAE,GAAG,CAAC,aAAa,GAAG,IAAI,CAAC,CAAA;IAChC,MAAM,EAAE,GAAG,CAAC,iBAAiB,CAAC,CAAA;CAC/B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,eAAe,CAC7B,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,sBAAsB,GAAG,SAAS,GAC3C,qBAAqB,CAAC,IAAI,CAAC,CA0B7B"}
|
package/dist/useSubscription.js
CHANGED
|
@@ -27,7 +27,7 @@ export function useSubscription(table, options) {
|
|
|
27
27
|
const status = ref("connecting");
|
|
28
28
|
let unsubscribe = null;
|
|
29
29
|
onMounted(() => {
|
|
30
|
-
unsubscribe = watchTable(client
|
|
30
|
+
unsubscribe = watchTable(client, table, options, {
|
|
31
31
|
rows: (rows) => {
|
|
32
32
|
data.value = rows;
|
|
33
33
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSubscription.js","sourceRoot":"","sources":["../src/useSubscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW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AO,EAAE;YAClE,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE;gBACb,IAAI,CAAC,KAAK,GAAG,IAAI,CAAA;YACnB,CAAC;YACD,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;gBACf,MAAM,CAAC,KAAK,GAAG,IAAI,CAAC,MAAM,CAAA;gBAC1B,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAA;YAC1B,CAAC;SACF,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IAEF,WAAW,CAAC,GAAG,EAAE;QACf,WAAW,EAAE,EAAE,CAAA;IACjB,CAAC,CAAC,CAAA;IAEF,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,CAAA;AAChC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supatype/vue",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Vue composables for Supatype, useQuery, useMutation, useAuth, useSubscription",
|
|
6
6
|
"type": "module",
|
|
@@ -14,14 +14,14 @@
|
|
|
14
14
|
},
|
|
15
15
|
"peerDependencies": {
|
|
16
16
|
"vue": "^3.3.0",
|
|
17
|
-
"@supatype/client": "0.
|
|
17
|
+
"@supatype/client": "0.10.0"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
20
|
"@types/node": "^22",
|
|
21
21
|
"typescript": "^5",
|
|
22
22
|
"vitest": "^4.0.18",
|
|
23
23
|
"vue": "^3.5.0",
|
|
24
|
-
"@supatype/client": "0.
|
|
24
|
+
"@supatype/client": "0.10.0"
|
|
25
25
|
},
|
|
26
26
|
"scripts": {
|
|
27
27
|
"build": "tsc --project tsconfig.json",
|
package/src/index.ts
CHANGED
|
@@ -16,3 +16,9 @@ export type { UseSubscriptionOptions, UseSubscriptionReturn, SubscriptionEvent }
|
|
|
16
16
|
|
|
17
17
|
export { useFunction } from "./useFunction.js"
|
|
18
18
|
export type { UseFunctionReturn } from "./useFunction.js"
|
|
19
|
+
|
|
20
|
+
export { useChannel } from "./useChannel.js"
|
|
21
|
+
export type { UseChannelReturn } from "./useChannel.js"
|
|
22
|
+
|
|
23
|
+
export { useRealtimeConnection } from "./useRealtimeConnection.js"
|
|
24
|
+
export type { UseRealtimeConnectionReturn } from "./useRealtimeConnection.js"
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ref, onMounted, onUnmounted, toValue, watch, type MaybeRefOrGetter, type Ref } from "vue"
|
|
2
|
+
import type { AugmentedDatabase, AnyDatabase, ChannelActionRefusal, ChannelName, ChannelOptions, ChannelSubscription, SupatypeError } from "@supatype/client"
|
|
3
|
+
import { channelSlot, type SubscriptionState } from "@supatype/client/bindings"
|
|
4
|
+
import { useSupatype } from "./context.js"
|
|
5
|
+
import { useRealtimeConnection, type UseRealtimeConnectionReturn } from "./useRealtimeConnection.js"
|
|
6
|
+
|
|
7
|
+
export interface UseChannelReturn extends UseRealtimeConnectionReturn {
|
|
8
|
+
/** This channel's join: "connecting", "connected", "disconnected" or "error". */
|
|
9
|
+
status: Ref<SubscriptionState>
|
|
10
|
+
/** Why the server refused the join, while the status is "error"; null otherwise. */
|
|
11
|
+
error: Ref<SupatypeError | null>
|
|
12
|
+
/** The latest send, track or payload the server refused on this channel; the status does not move for it. */
|
|
13
|
+
refusal: Ref<ChannelActionRefusal | null>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Join one channel and register whatever listeners `setup` adds to it: row changes, presence and
|
|
18
|
+
* broadcast together, which `useSubscription` (one table, one listener) cannot do.
|
|
19
|
+
*
|
|
20
|
+
* `name` may be a string, a ref or a getter. `setup` runs on mount, and again on the new channel
|
|
21
|
+
* whenever the name changes, after the old channel is left and its listeners with it. The channel
|
|
22
|
+
* is left when the component unmounts.
|
|
23
|
+
*
|
|
24
|
+
* `options.private` asks for a private channel (a schema `Channel` declaration guards it): see
|
|
25
|
+
* `ChannelOptions`. A refused or withdrawn join shows as `error`, a `RealtimeChannelError` whose `reason`
|
|
26
|
+
* says why.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const { status, isConnected } = useChannel(() => `auction:${props.id}`, (channel) => {
|
|
31
|
+
* channel.on("postgres_changes", { event: "INSERT", table: "bid" }, (p) => bids.value.push(p.new))
|
|
32
|
+
* channel.onBroadcast("outbid", (p) => notify(p))
|
|
33
|
+
* })
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export function useChannel<TRow = Record<string, unknown>, TDatabase extends AnyDatabase = AugmentedDatabase>(
|
|
37
|
+
name: MaybeRefOrGetter<ChannelName>,
|
|
38
|
+
setup: (channel: ChannelSubscription<TRow>) => void,
|
|
39
|
+
options?: ChannelOptions | undefined,
|
|
40
|
+
): UseChannelReturn {
|
|
41
|
+
const client = useSupatype<TDatabase>()
|
|
42
|
+
const connection = useRealtimeConnection<TDatabase>()
|
|
43
|
+
const status = ref<SubscriptionState>("connecting")
|
|
44
|
+
const error = ref<SupatypeError | null>(null)
|
|
45
|
+
const refusal = ref<ChannelActionRefusal | null>(null)
|
|
46
|
+
const slot = channelSlot<TRow>(client.realtime, setup, (view) => {
|
|
47
|
+
status.value = view.status
|
|
48
|
+
error.value = view.error
|
|
49
|
+
if (view.status === "connecting") refusal.value = null
|
|
50
|
+
}, options, (refused) => {
|
|
51
|
+
refusal.value = refused
|
|
52
|
+
})
|
|
53
|
+
let mounted = false
|
|
54
|
+
|
|
55
|
+
onMounted(() => {
|
|
56
|
+
mounted = true
|
|
57
|
+
slot.join(toValue(name))
|
|
58
|
+
})
|
|
59
|
+
watch(() => toValue(name), (next) => {
|
|
60
|
+
if (mounted) slot.join(next)
|
|
61
|
+
})
|
|
62
|
+
onUnmounted(() => {
|
|
63
|
+
mounted = false
|
|
64
|
+
slot.leave()
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
return { status, error, refusal, ...connection }
|
|
68
|
+
}
|
package/src/useQuery.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ref, shallowRef, onMounted, onUnmounted, watch, type Ref } from "vue"
|
|
1
|
+
import { ref, shallowRef, onMounted, onUnmounted, toValue, watch, type MaybeRefOrGetter, type Ref } from "vue"
|
|
2
2
|
import type { AugmentedDatabase, AnyClient, AnyDatabase, SupatypeError } from "@supatype/client"
|
|
3
3
|
import { onIdentityChange } from "@supatype/client"
|
|
4
4
|
import { useSupatype } from "./context.js"
|
|
@@ -28,12 +28,17 @@ export interface UseQueryReturn<TRow> {
|
|
|
28
28
|
* <script setup>
|
|
29
29
|
* import { useQuery } from '@supatype/vue'
|
|
30
30
|
*
|
|
31
|
-
* const
|
|
31
|
+
* const page = ref(0)
|
|
32
|
+
* const { data: posts, loading, error, refetch } = useQuery('posts', () => ({
|
|
32
33
|
* order: { column: 'created_at', ascending: false },
|
|
33
34
|
* limit: 10,
|
|
34
|
-
*
|
|
35
|
+
* offset: page.value * 10,
|
|
36
|
+
* }))
|
|
35
37
|
* </script>
|
|
36
38
|
* ```
|
|
39
|
+
*
|
|
40
|
+
* `options` may be a plain object, a ref or a getter. The query runs again whenever what a ref or
|
|
41
|
+
* getter holds changes, including a ref nested inside it such as `enabled`.
|
|
37
42
|
*/
|
|
38
43
|
export function useQuery<
|
|
39
44
|
TDatabase extends AnyDatabase = AugmentedDatabase,
|
|
@@ -41,12 +46,14 @@ export function useQuery<
|
|
|
41
46
|
TRow = TDatabase["public"]["Tables"][TTable]["Row"],
|
|
42
47
|
>(
|
|
43
48
|
table: TTable,
|
|
44
|
-
options?: UseQueryOptions | undefined
|
|
49
|
+
options?: MaybeRefOrGetter<UseQueryOptions | undefined>,
|
|
45
50
|
): UseQueryReturn<TRow> {
|
|
46
51
|
const client = useSupatype<TDatabase>()
|
|
47
|
-
const
|
|
52
|
+
const enabledAtSetup = isEnabled(toValue(options)?.enabled)
|
|
53
|
+
const { data, error, loading, read } = latestRows<TRow>(client as AnyClient, table, enabledAtSetup)
|
|
48
54
|
const fetchData = async () => {
|
|
49
|
-
|
|
55
|
+
const current = toValue(options)
|
|
56
|
+
if (isEnabled(current?.enabled)) await read(current)
|
|
50
57
|
}
|
|
51
58
|
|
|
52
59
|
onMounted(fetchData)
|
|
@@ -55,12 +62,10 @@ export function useQuery<
|
|
|
55
62
|
// and why the fetch above is not enough on its own.
|
|
56
63
|
onUnmounted(onIdentityChange(client.auth, () => void fetchData()))
|
|
57
64
|
|
|
58
|
-
//
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
})
|
|
63
|
-
}
|
|
65
|
+
// 0.9.1 release test (FF25): the options were read once, so a sort, page or filter held in
|
|
66
|
+
// component state changed nothing. Deep, so a ref inside the options (`enabled`) and a change
|
|
67
|
+
// inside a reactive object both count; `fetchData` skips the read while disabled.
|
|
68
|
+
watch(() => toValue(options), () => void fetchData(), { deep: true })
|
|
64
69
|
|
|
65
70
|
return { data, error, loading, refetch: fetchData }
|
|
66
71
|
}
|
|
@@ -70,12 +75,18 @@ function isEnabled(enabled: Ref<boolean> | boolean | undefined): boolean {
|
|
|
70
75
|
return enabled === undefined || (typeof enabled === "boolean" ? enabled : enabled.value)
|
|
71
76
|
}
|
|
72
77
|
|
|
73
|
-
/**
|
|
74
|
-
|
|
78
|
+
/**
|
|
79
|
+
* The refs a query shows, fed by reads of which only the newest may change them.
|
|
80
|
+
*
|
|
81
|
+
* `loading` starts as `firstReadComing`. 0.9.2 release test (N-FF9): it started false, so the
|
|
82
|
+
* render before `onMounted` began the read showed `loading: false, data: null`, which an app
|
|
83
|
+
* reads as "done, nothing found".
|
|
84
|
+
*/
|
|
85
|
+
function latestRows<TRow>(client: AnyClient, table: string, firstReadComing: boolean) {
|
|
75
86
|
// Shallow: each fetch replaces the rows whole, and `ref` would type them as `UnwrapRef<TRow>`.
|
|
76
87
|
const data = shallowRef<TRow[] | null>(null)
|
|
77
88
|
const error = ref<SupatypeError | null>(null)
|
|
78
|
-
const loading = ref(
|
|
89
|
+
const loading = ref(firstReadComing)
|
|
79
90
|
const read = latestRowsReader<TRow>(client, table, `useQuery("${table}")`, {
|
|
80
91
|
started: () => {
|
|
81
92
|
loading.value = true
|