jazz-vue 0.8.51 → 0.9.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 +7 -5
- package/CHANGELOG.md +17 -0
- package/dist/composables.d.ts +10079 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +213 -266
- package/dist/provider-BZnz6FpX.js +69 -0
- package/dist/provider.d.ts +48 -0
- package/dist/testing.d.ts +20 -0
- package/dist/testing.js +26 -0
- package/dist/tests/testUtils.d.ts +8 -0
- package/dist/tests/useAcceptInvite.test.d.ts +1 -0
- package/dist/tests/useAccount.test.d.ts +1 -0
- package/dist/tests/useAccountOrGuest.test.d.ts +1 -0
- package/dist/tests/useCoState.test.d.ts +1 -0
- package/package.json +15 -5
- package/src/auth/useDemoAuth.ts +1 -1
- package/src/composables.ts +250 -0
- package/src/index.ts +4 -1
- package/src/provider.ts +106 -0
- package/src/testing.ts +27 -0
- package/src/tests/testUtils.ts +66 -0
- package/src/tests/useAcceptInvite.test.ts +53 -0
- package/src/tests/useAccount.test.ts +51 -0
- package/src/tests/useAccountOrGuest.test.ts +80 -0
- package/src/tests/useCoState.test.ts +127 -0
- package/vite.config.ts +12 -12
- package/dist/createJazzVueApp.d.ts +0 -29
- package/src/createJazzVueApp.ts +0 -356
@@ -0,0 +1,51 @@
|
|
1
|
+
// @vitest-environment happy-dom
|
2
|
+
|
3
|
+
import { Account, CoMap, co } from "jazz-tools";
|
4
|
+
import { describe, expect, it } from "vitest";
|
5
|
+
import { createUseAccountComposables, useAccount } from "../composables.js";
|
6
|
+
import { createJazzTestAccount } from "../testing.js";
|
7
|
+
import { withJazzTestSetup } from "./testUtils.js";
|
8
|
+
|
9
|
+
describe("useAccount", () => {
|
10
|
+
it("should return the correct value", async () => {
|
11
|
+
const account = await createJazzTestAccount();
|
12
|
+
|
13
|
+
const [result] = withJazzTestSetup(() => useAccount(), {
|
14
|
+
account,
|
15
|
+
});
|
16
|
+
|
17
|
+
expect(result.me.value).toEqual(account);
|
18
|
+
});
|
19
|
+
|
20
|
+
it("should load nested values if requested", async () => {
|
21
|
+
class AccountRoot extends CoMap {
|
22
|
+
value = co.string;
|
23
|
+
}
|
24
|
+
|
25
|
+
class AccountSchema extends Account {
|
26
|
+
root = co.ref(AccountRoot);
|
27
|
+
|
28
|
+
migrate() {
|
29
|
+
if (!this._refs.root) {
|
30
|
+
this.root = AccountRoot.create({ value: "123" }, { owner: this });
|
31
|
+
}
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
const { useAccount } = createUseAccountComposables<AccountSchema>();
|
36
|
+
|
37
|
+
const account = await createJazzTestAccount({ AccountSchema });
|
38
|
+
|
39
|
+
const [result] = withJazzTestSetup(
|
40
|
+
() =>
|
41
|
+
useAccount({
|
42
|
+
root: {},
|
43
|
+
}),
|
44
|
+
{
|
45
|
+
account,
|
46
|
+
},
|
47
|
+
);
|
48
|
+
|
49
|
+
expect(result.me.value?.root?.value).toBe("123");
|
50
|
+
});
|
51
|
+
});
|
@@ -0,0 +1,80 @@
|
|
1
|
+
// @vitest-environment happy-dom
|
2
|
+
|
3
|
+
import { Account, CoMap, co } from "jazz-tools";
|
4
|
+
import { describe, expect, it } from "vitest";
|
5
|
+
import {
|
6
|
+
createUseAccountComposables,
|
7
|
+
useAccountOrGuest,
|
8
|
+
} from "../composables.js";
|
9
|
+
import { createJazzTestAccount, createJazzTestGuest } from "../testing.js";
|
10
|
+
import { withJazzTestSetup } from "./testUtils.js";
|
11
|
+
|
12
|
+
describe("useAccountOrGuest", () => {
|
13
|
+
it("should return the correct me value", async () => {
|
14
|
+
const account = await createJazzTestAccount();
|
15
|
+
|
16
|
+
const [result] = withJazzTestSetup(() => useAccountOrGuest(), {
|
17
|
+
account,
|
18
|
+
});
|
19
|
+
|
20
|
+
expect(result.me.value).toEqual(account);
|
21
|
+
});
|
22
|
+
|
23
|
+
it("should return the guest agent if the account is a guest", async () => {
|
24
|
+
const account = await createJazzTestGuest();
|
25
|
+
|
26
|
+
const [result] = withJazzTestSetup(() => useAccountOrGuest(), {
|
27
|
+
account,
|
28
|
+
});
|
29
|
+
|
30
|
+
expect(result.me.value).toBe(account.guest);
|
31
|
+
});
|
32
|
+
|
33
|
+
it("should load nested values if requested", async () => {
|
34
|
+
class AccountRoot extends CoMap {
|
35
|
+
value = co.string;
|
36
|
+
}
|
37
|
+
|
38
|
+
class AccountSchema extends Account {
|
39
|
+
root = co.ref(AccountRoot);
|
40
|
+
|
41
|
+
migrate() {
|
42
|
+
if (!this._refs.root) {
|
43
|
+
this.root = AccountRoot.create({ value: "123" }, { owner: this });
|
44
|
+
}
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
const account = await createJazzTestAccount({ AccountSchema });
|
49
|
+
const { useAccountOrGuest } = createUseAccountComposables<AccountSchema>();
|
50
|
+
|
51
|
+
const [result] = withJazzTestSetup(
|
52
|
+
() =>
|
53
|
+
useAccountOrGuest({
|
54
|
+
root: {},
|
55
|
+
}),
|
56
|
+
{
|
57
|
+
account,
|
58
|
+
},
|
59
|
+
);
|
60
|
+
|
61
|
+
// @ts-expect-error
|
62
|
+
expect(result.me.value?.root?.value).toBe("123");
|
63
|
+
});
|
64
|
+
|
65
|
+
it("should not load nested values if the account is a guest", async () => {
|
66
|
+
const account = await createJazzTestGuest();
|
67
|
+
|
68
|
+
const [result] = withJazzTestSetup(
|
69
|
+
() =>
|
70
|
+
useAccountOrGuest({
|
71
|
+
root: {},
|
72
|
+
}),
|
73
|
+
{
|
74
|
+
account,
|
75
|
+
},
|
76
|
+
);
|
77
|
+
|
78
|
+
expect(result.me.value).toBe(account.guest);
|
79
|
+
});
|
80
|
+
});
|
@@ -0,0 +1,127 @@
|
|
1
|
+
// @vitest-environment happy-dom
|
2
|
+
|
3
|
+
import { CoMap, co } from "jazz-tools";
|
4
|
+
import { createJazzTestAccount } from "jazz-tools/testing";
|
5
|
+
import { describe, expect, it } from "vitest";
|
6
|
+
import { useCoState } from "../index.js";
|
7
|
+
import { withJazzTestSetup } from "./testUtils.js";
|
8
|
+
|
9
|
+
describe("useCoState", () => {
|
10
|
+
it("should return the correct value", async () => {
|
11
|
+
class TestMap extends CoMap {
|
12
|
+
content = co.string;
|
13
|
+
}
|
14
|
+
|
15
|
+
const account = await createJazzTestAccount();
|
16
|
+
|
17
|
+
const map = TestMap.create(
|
18
|
+
{
|
19
|
+
content: "123",
|
20
|
+
},
|
21
|
+
{ owner: account },
|
22
|
+
);
|
23
|
+
|
24
|
+
const [result] = withJazzTestSetup(() => useCoState(TestMap, map.id, {}), {
|
25
|
+
account,
|
26
|
+
});
|
27
|
+
|
28
|
+
expect(result.value?.content).toBe("123");
|
29
|
+
});
|
30
|
+
|
31
|
+
it("should update the value when the coValue changes", async () => {
|
32
|
+
class TestMap extends CoMap {
|
33
|
+
content = co.string;
|
34
|
+
}
|
35
|
+
|
36
|
+
const account = await createJazzTestAccount();
|
37
|
+
|
38
|
+
const map = TestMap.create(
|
39
|
+
{
|
40
|
+
content: "123",
|
41
|
+
},
|
42
|
+
{ owner: account },
|
43
|
+
);
|
44
|
+
|
45
|
+
const [result] = withJazzTestSetup(() => useCoState(TestMap, map.id, {}), {
|
46
|
+
account,
|
47
|
+
});
|
48
|
+
|
49
|
+
expect(result.value?.content).toBe("123");
|
50
|
+
|
51
|
+
map.content = "456";
|
52
|
+
|
53
|
+
expect(result.value?.content).toBe("456");
|
54
|
+
});
|
55
|
+
|
56
|
+
it("should load nested values if requested", async () => {
|
57
|
+
class TestNestedMap extends CoMap {
|
58
|
+
content = co.string;
|
59
|
+
}
|
60
|
+
|
61
|
+
class TestMap extends CoMap {
|
62
|
+
content = co.string;
|
63
|
+
nested = co.ref(TestNestedMap);
|
64
|
+
}
|
65
|
+
|
66
|
+
const account = await createJazzTestAccount();
|
67
|
+
|
68
|
+
const map = TestMap.create(
|
69
|
+
{
|
70
|
+
content: "123",
|
71
|
+
nested: TestNestedMap.create(
|
72
|
+
{
|
73
|
+
content: "456",
|
74
|
+
},
|
75
|
+
{ owner: account },
|
76
|
+
),
|
77
|
+
},
|
78
|
+
{ owner: account },
|
79
|
+
);
|
80
|
+
|
81
|
+
const [result] = withJazzTestSetup(
|
82
|
+
() =>
|
83
|
+
useCoState(TestMap, map.id, {
|
84
|
+
nested: {},
|
85
|
+
}),
|
86
|
+
{
|
87
|
+
account,
|
88
|
+
},
|
89
|
+
);
|
90
|
+
|
91
|
+
expect(result.value?.content).toBe("123");
|
92
|
+
expect(result.value?.nested?.content).toBe("456");
|
93
|
+
});
|
94
|
+
|
95
|
+
it("should load nested values on access even if not requested", async () => {
|
96
|
+
class TestNestedMap extends CoMap {
|
97
|
+
content = co.string;
|
98
|
+
}
|
99
|
+
|
100
|
+
class TestMap extends CoMap {
|
101
|
+
content = co.string;
|
102
|
+
nested = co.ref(TestNestedMap);
|
103
|
+
}
|
104
|
+
|
105
|
+
const account = await createJazzTestAccount();
|
106
|
+
|
107
|
+
const map = TestMap.create(
|
108
|
+
{
|
109
|
+
content: "123",
|
110
|
+
nested: TestNestedMap.create(
|
111
|
+
{
|
112
|
+
content: "456",
|
113
|
+
},
|
114
|
+
{ owner: account },
|
115
|
+
),
|
116
|
+
},
|
117
|
+
{ owner: account },
|
118
|
+
);
|
119
|
+
|
120
|
+
const [result] = withJazzTestSetup(() => useCoState(TestMap, map.id, {}), {
|
121
|
+
account,
|
122
|
+
});
|
123
|
+
|
124
|
+
expect(result.value?.content).toBe("123");
|
125
|
+
expect(result.value?.nested?.content).toBe("456");
|
126
|
+
});
|
127
|
+
});
|
package/vite.config.ts
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
import path from "path";
|
2
2
|
import vue from "@vitejs/plugin-vue";
|
3
|
+
import depsExternal from "rollup-plugin-node-externals";
|
3
4
|
import { defineConfig } from "vite";
|
4
5
|
import dts from "vite-plugin-dts";
|
5
6
|
|
6
7
|
export default defineConfig({
|
7
|
-
|
8
|
-
|
8
|
+
plugins: [
|
9
|
+
vue(),
|
10
|
+
dts({ include: ["src/**/*.ts"], outDir: "dist" }),
|
11
|
+
depsExternal(),
|
12
|
+
],
|
9
13
|
build: {
|
10
14
|
lib: {
|
11
|
-
entry:
|
15
|
+
entry: {
|
16
|
+
index: path.resolve(__dirname, "src/index.ts"),
|
17
|
+
testing: path.resolve(__dirname, "src/testing.ts"),
|
18
|
+
},
|
12
19
|
name: "JazzVue",
|
13
20
|
formats: ["es"],
|
14
|
-
fileName: (
|
21
|
+
fileName: (_, entryName) => `${entryName}.js`,
|
15
22
|
},
|
16
23
|
rollupOptions: {
|
17
|
-
external: ["vue"
|
18
|
-
output: {
|
19
|
-
globals: {
|
20
|
-
vue: "Vue",
|
21
|
-
"jazz-browser": "JazzBrowser",
|
22
|
-
"jazz-tools": "JazzTools",
|
23
|
-
},
|
24
|
-
},
|
24
|
+
external: ["vue"],
|
25
25
|
},
|
26
26
|
},
|
27
27
|
});
|
@@ -1,29 +0,0 @@
|
|
1
|
-
import { Account, AnonymousJazzAgent, CoValue, CoValueClass, DeeplyLoaded, DepthsIn, ID } from 'jazz-tools';
|
2
|
-
import { Component, ComputedRef, MaybeRef, Ref } from 'vue';
|
3
|
-
export declare const logoutHandler: Ref<(() => void) | undefined, (() => void) | undefined>;
|
4
|
-
export interface JazzVueApp<Acc extends Account> {
|
5
|
-
JazzProvider: Component;
|
6
|
-
useAccount(): {
|
7
|
-
me: ComputedRef<Acc>;
|
8
|
-
logOut: () => void;
|
9
|
-
};
|
10
|
-
useAccount<D extends DepthsIn<Acc>>(depth: D): {
|
11
|
-
me: ComputedRef<DeeplyLoaded<Acc, D> | undefined>;
|
12
|
-
logOut: () => void;
|
13
|
-
};
|
14
|
-
useAccountOrGuest(): {
|
15
|
-
me: ComputedRef<Acc | AnonymousJazzAgent>;
|
16
|
-
};
|
17
|
-
useAccountOrGuest<D extends DepthsIn<Acc>>(depth: D): {
|
18
|
-
me: ComputedRef<DeeplyLoaded<Acc, D> | undefined | AnonymousJazzAgent>;
|
19
|
-
};
|
20
|
-
useCoState<V extends CoValue, D>(Schema: CoValueClass<V>, id: MaybeRef<ID<V> | undefined>, depth?: D & DepthsIn<V>): Ref<DeeplyLoaded<V, D> | undefined>;
|
21
|
-
useAcceptInvite<V extends CoValue>(args: {
|
22
|
-
invitedObjectSchema: CoValueClass<V>;
|
23
|
-
onAccept: (projectID: ID<V>) => void;
|
24
|
-
forValueHint?: string;
|
25
|
-
}): void;
|
26
|
-
}
|
27
|
-
export declare function createJazzVueApp<Acc extends Account>({ AccountSchema, }?: {
|
28
|
-
AccountSchema?: any;
|
29
|
-
}): JazzVueApp<Acc>;
|
package/src/createJazzVueApp.ts
DELETED
@@ -1,356 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
BrowserContext,
|
3
|
-
BrowserGuestContext,
|
4
|
-
consumeInviteLinkFromWindowLocation,
|
5
|
-
createJazzBrowserContext,
|
6
|
-
} from "jazz-browser";
|
7
|
-
import {
|
8
|
-
Account,
|
9
|
-
AnonymousJazzAgent,
|
10
|
-
AuthMethod,
|
11
|
-
CoValue,
|
12
|
-
CoValueClass,
|
13
|
-
DeeplyLoaded,
|
14
|
-
DepthsIn,
|
15
|
-
ID,
|
16
|
-
subscribeToCoValue,
|
17
|
-
} from "jazz-tools";
|
18
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
19
|
-
import {
|
20
|
-
Component,
|
21
|
-
ComputedRef,
|
22
|
-
MaybeRef,
|
23
|
-
PropType,
|
24
|
-
Ref,
|
25
|
-
ShallowRef,
|
26
|
-
computed,
|
27
|
-
defineComponent,
|
28
|
-
inject,
|
29
|
-
onMounted,
|
30
|
-
onUnmounted,
|
31
|
-
provide,
|
32
|
-
ref,
|
33
|
-
shallowRef,
|
34
|
-
toRaw,
|
35
|
-
unref,
|
36
|
-
watch,
|
37
|
-
} from "vue";
|
38
|
-
|
39
|
-
export const logoutHandler = ref<() => void>();
|
40
|
-
|
41
|
-
export interface JazzVueApp<Acc extends Account> {
|
42
|
-
JazzProvider: Component;
|
43
|
-
|
44
|
-
useAccount(): { me: ComputedRef<Acc>; logOut: () => void };
|
45
|
-
useAccount<D extends DepthsIn<Acc>>(
|
46
|
-
depth: D,
|
47
|
-
): {
|
48
|
-
me: ComputedRef<DeeplyLoaded<Acc, D> | undefined>;
|
49
|
-
logOut: () => void;
|
50
|
-
};
|
51
|
-
|
52
|
-
useAccountOrGuest(): { me: ComputedRef<Acc | AnonymousJazzAgent> };
|
53
|
-
useAccountOrGuest<D extends DepthsIn<Acc>>(
|
54
|
-
depth: D,
|
55
|
-
): {
|
56
|
-
me: ComputedRef<DeeplyLoaded<Acc, D> | undefined | AnonymousJazzAgent>;
|
57
|
-
};
|
58
|
-
|
59
|
-
useCoState<V extends CoValue, D>(
|
60
|
-
Schema: CoValueClass<V>,
|
61
|
-
id: MaybeRef<ID<V> | undefined>,
|
62
|
-
depth?: D & DepthsIn<V>,
|
63
|
-
): Ref<DeeplyLoaded<V, D> | undefined>;
|
64
|
-
|
65
|
-
useAcceptInvite<V extends CoValue>(args: {
|
66
|
-
invitedObjectSchema: CoValueClass<V>;
|
67
|
-
onAccept: (projectID: ID<V>) => void;
|
68
|
-
forValueHint?: string;
|
69
|
-
}): void;
|
70
|
-
}
|
71
|
-
|
72
|
-
const JazzContextSymbol = Symbol("JazzContext");
|
73
|
-
|
74
|
-
export function createJazzVueApp<Acc extends Account>({
|
75
|
-
AccountSchema = Account as any,
|
76
|
-
} = {}): JazzVueApp<Acc> {
|
77
|
-
const JazzProvider = defineComponent({
|
78
|
-
name: "JazzProvider",
|
79
|
-
props: {
|
80
|
-
auth: {
|
81
|
-
type: [String, Object] as PropType<AuthMethod | "guest">,
|
82
|
-
required: true,
|
83
|
-
},
|
84
|
-
peer: {
|
85
|
-
type: String as PropType<`wss://${string}` | `ws://${string}`>,
|
86
|
-
required: true,
|
87
|
-
},
|
88
|
-
storage: {
|
89
|
-
type: String as PropType<"indexedDB" | "singleTabOPFS">,
|
90
|
-
default: undefined,
|
91
|
-
},
|
92
|
-
},
|
93
|
-
setup(props, { slots }) {
|
94
|
-
const ctx = ref<BrowserContext<Acc> | BrowserGuestContext | undefined>(
|
95
|
-
undefined,
|
96
|
-
);
|
97
|
-
|
98
|
-
const key = ref(0);
|
99
|
-
|
100
|
-
provide(JazzContextSymbol, ctx);
|
101
|
-
|
102
|
-
const initializeContext = async () => {
|
103
|
-
if (ctx.value) {
|
104
|
-
ctx.value.done?.();
|
105
|
-
ctx.value = undefined;
|
106
|
-
}
|
107
|
-
|
108
|
-
try {
|
109
|
-
const context = await createJazzBrowserContext<Acc>(
|
110
|
-
props.auth === "guest"
|
111
|
-
? { peer: props.peer, storage: props.storage }
|
112
|
-
: {
|
113
|
-
AccountSchema,
|
114
|
-
auth: props.auth,
|
115
|
-
peer: props.peer,
|
116
|
-
storage: props.storage,
|
117
|
-
},
|
118
|
-
);
|
119
|
-
|
120
|
-
ctx.value = {
|
121
|
-
...context,
|
122
|
-
logOut: () => {
|
123
|
-
logoutHandler.value?.();
|
124
|
-
// context.logOut();
|
125
|
-
key.value += 1;
|
126
|
-
},
|
127
|
-
};
|
128
|
-
} catch (e) {
|
129
|
-
console.error("Error creating Jazz browser context:", e);
|
130
|
-
}
|
131
|
-
};
|
132
|
-
|
133
|
-
onMounted(() => {
|
134
|
-
void initializeContext();
|
135
|
-
});
|
136
|
-
|
137
|
-
watch(
|
138
|
-
() => key.value,
|
139
|
-
async () => {
|
140
|
-
await initializeContext();
|
141
|
-
},
|
142
|
-
);
|
143
|
-
|
144
|
-
onUnmounted(() => {
|
145
|
-
if (ctx.value) ctx.value.done?.();
|
146
|
-
});
|
147
|
-
|
148
|
-
return () => (ctx.value ? slots.default?.() : null);
|
149
|
-
},
|
150
|
-
});
|
151
|
-
|
152
|
-
function useJazzContext() {
|
153
|
-
const context =
|
154
|
-
inject<Ref<BrowserContext<Acc> | BrowserGuestContext>>(JazzContextSymbol);
|
155
|
-
if (!context) {
|
156
|
-
throw new Error("useJazzContext must be used within a JazzProvider");
|
157
|
-
}
|
158
|
-
return context;
|
159
|
-
}
|
160
|
-
|
161
|
-
function useAccount(): { me: ComputedRef<Acc>; logOut: () => void };
|
162
|
-
function useAccount<D extends DepthsIn<Acc>>(
|
163
|
-
depth: D,
|
164
|
-
): {
|
165
|
-
me: ComputedRef<DeeplyLoaded<Acc, D> | undefined>;
|
166
|
-
logOut: () => void;
|
167
|
-
};
|
168
|
-
function useAccount<D extends DepthsIn<Acc>>(
|
169
|
-
depth?: D,
|
170
|
-
): {
|
171
|
-
me: ComputedRef<Acc | DeeplyLoaded<Acc, D> | undefined>;
|
172
|
-
logOut: () => void;
|
173
|
-
} {
|
174
|
-
const context = useJazzContext();
|
175
|
-
|
176
|
-
if (!context.value) {
|
177
|
-
throw new Error("useAccount must be used within a JazzProvider");
|
178
|
-
}
|
179
|
-
|
180
|
-
if (!("me" in context.value)) {
|
181
|
-
throw new Error(
|
182
|
-
"useAccount can't be used in a JazzProvider with auth === 'guest' - consider using useAccountOrGuest()",
|
183
|
-
);
|
184
|
-
}
|
185
|
-
|
186
|
-
const contextMe = computed(() =>
|
187
|
-
"me" in context.value ? context.value.me : undefined,
|
188
|
-
);
|
189
|
-
|
190
|
-
const me = useCoState<Acc, D>(
|
191
|
-
contextMe.value?.constructor as CoValueClass<Acc>,
|
192
|
-
contextMe.value?.id,
|
193
|
-
depth,
|
194
|
-
);
|
195
|
-
|
196
|
-
return {
|
197
|
-
me: computed(() => {
|
198
|
-
const value =
|
199
|
-
depth === undefined
|
200
|
-
? me.value || toRaw((context.value as BrowserContext<Acc>).me)
|
201
|
-
: me.value;
|
202
|
-
|
203
|
-
return value ? toRaw(value) : value;
|
204
|
-
}),
|
205
|
-
logOut: context.value.logOut,
|
206
|
-
};
|
207
|
-
}
|
208
|
-
|
209
|
-
function useAccountOrGuest(): { me: ComputedRef<Acc | AnonymousJazzAgent> };
|
210
|
-
function useAccountOrGuest<D extends DepthsIn<Acc>>(
|
211
|
-
depth: D,
|
212
|
-
): {
|
213
|
-
me: ComputedRef<DeeplyLoaded<Acc, D> | undefined | AnonymousJazzAgent>;
|
214
|
-
};
|
215
|
-
function useAccountOrGuest<D extends DepthsIn<Acc>>(
|
216
|
-
depth?: D,
|
217
|
-
): {
|
218
|
-
me: ComputedRef<
|
219
|
-
Acc | DeeplyLoaded<Acc, D> | undefined | AnonymousJazzAgent
|
220
|
-
>;
|
221
|
-
} {
|
222
|
-
const context = useJazzContext();
|
223
|
-
|
224
|
-
if (!context.value) {
|
225
|
-
throw new Error("useAccountOrGuest must be used within a JazzProvider");
|
226
|
-
}
|
227
|
-
|
228
|
-
const contextMe = computed(() =>
|
229
|
-
"me" in context.value ? context.value.me : undefined,
|
230
|
-
);
|
231
|
-
|
232
|
-
const me = useCoState<Acc, D>(
|
233
|
-
contextMe.value?.constructor as CoValueClass<Acc>,
|
234
|
-
contextMe.value?.id,
|
235
|
-
depth,
|
236
|
-
);
|
237
|
-
|
238
|
-
if ("me" in context.value) {
|
239
|
-
return {
|
240
|
-
me: computed(() =>
|
241
|
-
depth === undefined
|
242
|
-
? me.value || toRaw((context.value as BrowserContext<Acc>).me)
|
243
|
-
: me.value,
|
244
|
-
),
|
245
|
-
};
|
246
|
-
} else {
|
247
|
-
return {
|
248
|
-
me: computed(() => toRaw((context.value as BrowserGuestContext).guest)),
|
249
|
-
};
|
250
|
-
}
|
251
|
-
}
|
252
|
-
|
253
|
-
function useCoState<V extends CoValue, D>(
|
254
|
-
Schema: CoValueClass<V>,
|
255
|
-
id: MaybeRef<ID<V> | undefined>,
|
256
|
-
depth: D & DepthsIn<V> = [] as D & DepthsIn<V>,
|
257
|
-
): Ref<DeeplyLoaded<V, D> | undefined> {
|
258
|
-
const state: ShallowRef<DeeplyLoaded<V, D> | undefined> =
|
259
|
-
shallowRef(undefined);
|
260
|
-
const context = useJazzContext();
|
261
|
-
|
262
|
-
if (!context.value) {
|
263
|
-
throw new Error("useCoState must be used within a JazzProvider");
|
264
|
-
}
|
265
|
-
|
266
|
-
let unsubscribe: (() => void) | undefined;
|
267
|
-
|
268
|
-
watch(
|
269
|
-
[() => unref(id), () => context, () => Schema, () => depth],
|
270
|
-
() => {
|
271
|
-
if (unsubscribe) unsubscribe();
|
272
|
-
|
273
|
-
const idValue = unref(id);
|
274
|
-
if (!idValue) return;
|
275
|
-
|
276
|
-
unsubscribe = subscribeToCoValue(
|
277
|
-
Schema,
|
278
|
-
idValue,
|
279
|
-
"me" in context.value
|
280
|
-
? toRaw(context.value.me)
|
281
|
-
: toRaw(context.value.guest),
|
282
|
-
depth,
|
283
|
-
(value) => {
|
284
|
-
state.value = value;
|
285
|
-
},
|
286
|
-
);
|
287
|
-
},
|
288
|
-
{ deep: true, immediate: true },
|
289
|
-
);
|
290
|
-
|
291
|
-
onUnmounted(() => {
|
292
|
-
if (unsubscribe) unsubscribe();
|
293
|
-
});
|
294
|
-
|
295
|
-
const computedState = computed(() => state.value);
|
296
|
-
|
297
|
-
return computedState;
|
298
|
-
}
|
299
|
-
|
300
|
-
function useAcceptInvite<V extends CoValue>({
|
301
|
-
invitedObjectSchema,
|
302
|
-
onAccept,
|
303
|
-
forValueHint,
|
304
|
-
}: {
|
305
|
-
invitedObjectSchema: CoValueClass<V>;
|
306
|
-
onAccept: (projectID: ID<V>) => void;
|
307
|
-
forValueHint?: string;
|
308
|
-
}): void {
|
309
|
-
const context = useJazzContext();
|
310
|
-
|
311
|
-
if (!context.value) {
|
312
|
-
throw new Error("useAcceptInvite must be used within a JazzProvider");
|
313
|
-
}
|
314
|
-
|
315
|
-
if (!("me" in context.value)) {
|
316
|
-
throw new Error(
|
317
|
-
"useAcceptInvite can't be used in a JazzProvider with auth === 'guest'.",
|
318
|
-
);
|
319
|
-
}
|
320
|
-
|
321
|
-
const runInviteAcceptance = () => {
|
322
|
-
const result = consumeInviteLinkFromWindowLocation({
|
323
|
-
as: toRaw((context.value as BrowserContext<Acc>).me),
|
324
|
-
invitedObjectSchema,
|
325
|
-
forValueHint,
|
326
|
-
});
|
327
|
-
|
328
|
-
result
|
329
|
-
.then((res) => res && onAccept(res.valueID))
|
330
|
-
.catch((e) => {
|
331
|
-
console.error("Failed to accept invite", e);
|
332
|
-
});
|
333
|
-
};
|
334
|
-
|
335
|
-
onMounted(() => {
|
336
|
-
runInviteAcceptance();
|
337
|
-
});
|
338
|
-
|
339
|
-
watch(
|
340
|
-
() => onAccept,
|
341
|
-
(newOnAccept, oldOnAccept) => {
|
342
|
-
if (newOnAccept !== oldOnAccept) {
|
343
|
-
runInviteAcceptance();
|
344
|
-
}
|
345
|
-
},
|
346
|
-
);
|
347
|
-
}
|
348
|
-
|
349
|
-
return {
|
350
|
-
JazzProvider,
|
351
|
-
useAccount,
|
352
|
-
useAccountOrGuest,
|
353
|
-
useCoState,
|
354
|
-
useAcceptInvite,
|
355
|
-
};
|
356
|
-
}
|