jazz-svelte 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,64 @@
1
+ <script lang="ts" module>
2
+ export type Props<Acc extends Account = Account> = {
3
+ children: Snippet;
4
+ auth: AuthMethod | 'guest';
5
+ peer: `wss://${string}` | `ws://${string}`;
6
+ storage?: 'indexedDB' | 'singleTabOPFS';
7
+ schema: AccountClass<Acc>;
8
+ };
9
+ </script>
10
+
11
+ <script lang="ts" generics="Acc extends Account">
12
+ import { createJazzBrowserContext } from 'jazz-browser';
13
+ import type { AccountClass, AuthMethod } from 'jazz-tools';
14
+ import { Account } from 'jazz-tools';
15
+ import { type Snippet, setContext, untrack } from 'svelte';
16
+ import { JAZZ_CTX, type JazzContext } from './jazz.svelte.js';
17
+
18
+ let { children, auth, peer, storage, schema }: Props<Acc> = $props();
19
+
20
+ const ctx = $state<JazzContext<Acc>>({ current: undefined });
21
+ setContext<JazzContext<Acc>>(JAZZ_CTX, ctx);
22
+ let sessionCount = $state(0);
23
+
24
+ $effect(() => {
25
+ auth;
26
+ peer;
27
+ storage;
28
+ sessionCount;
29
+ return untrack(() => {
30
+ if (!auth || !peer) return;
31
+
32
+ const promiseWithDoneCallback = createJazzBrowserContext<Acc>(
33
+ auth === 'guest'
34
+ ? {
35
+ peer,
36
+ storage
37
+ }
38
+ : {
39
+ AccountSchema: schema,
40
+ auth,
41
+ peer,
42
+ storage
43
+ }
44
+ ).then((context) => {
45
+ ctx.current = {
46
+ ...context,
47
+ logOut: () => {
48
+ context.logOut();
49
+ ctx.current = undefined;
50
+ sessionCount = sessionCount + 1;
51
+ }
52
+ };
53
+ return context.done;
54
+ });
55
+ return () => {
56
+ void promiseWithDoneCallback.then((done) => done());
57
+ };
58
+ });
59
+ });
60
+ </script>
61
+
62
+ {#if ctx.current}
63
+ {@render children?.()}
64
+ {/if}
@@ -1,14 +1,15 @@
1
- import type { AccountClass, AuthMethod } from "jazz-tools";
2
- import { Account } from "jazz-tools";
3
- import { type Snippet } from "svelte";
1
+ export type Props<Acc extends Account = Account> = {
2
+ children: Snippet;
3
+ auth: AuthMethod | 'guest';
4
+ peer: `wss://${string}` | `ws://${string}`;
5
+ storage?: 'indexedDB' | 'singleTabOPFS';
6
+ schema: AccountClass<Acc>;
7
+ };
8
+ import type { AccountClass, AuthMethod } from 'jazz-tools';
9
+ import { Account } from 'jazz-tools';
10
+ import { type Snippet } from 'svelte';
4
11
  declare class __sveltets_Render<Acc extends Account> {
5
- props(): {
6
- children: Snippet;
7
- auth: AuthMethod | "guest";
8
- peer: `wss://${string}` | `ws://${string}`;
9
- storage?: "indexedDB" | "singleTabOPFS";
10
- schema?: AccountClass<Acc> | undefined;
11
- };
12
+ props(): Props<Acc>;
12
13
  events(): {};
13
14
  slots(): {};
14
15
  bindings(): "";
@@ -21,6 +22,6 @@ interface $$IsomorphicComponent {
21
22
  <Acc extends Account>(internal: unknown, props: ReturnType<__sveltets_Render<Acc>['props']> & {}): ReturnType<__sveltets_Render<Acc>['exports']>;
22
23
  z_$$bindings?: ReturnType<__sveltets_Render<any>['bindings']>;
23
24
  }
24
- declare const JazzProvider: $$IsomorphicComponent;
25
- type JazzProvider<Acc extends Account> = InstanceType<typeof JazzProvider<Acc>>;
26
- export default JazzProvider;
25
+ declare const Provider: $$IsomorphicComponent;
26
+ type Provider<Acc extends Account> = InstanceType<typeof Provider<Acc>>;
27
+ export default Provider;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- import JazzProvider from "./JazzProvider.svelte";
2
1
  export { createJazzApp } from "./jazz.svelte.js";
3
- export { JazzProvider };
4
2
  export { createInviteLink, parseInviteLink } from "jazz-browser";
5
3
  export * from "./auth/index.js";
package/dist/index.js CHANGED
@@ -1,5 +1,3 @@
1
- import JazzProvider from "./JazzProvider.svelte";
2
1
  export { createJazzApp } from "./jazz.svelte.js";
3
- export { JazzProvider };
4
2
  export { createInviteLink, parseInviteLink } from "jazz-browser";
5
3
  export * from "./auth/index.js";
@@ -1,6 +1,8 @@
1
- import { type BrowserContext, type BrowserGuestContext } from "jazz-browser";
2
- import type { AnonymousJazzAgent, CoValue, CoValueClass, DeeplyLoaded, DepthsIn, ID } from "jazz-tools";
3
- import { Account } from "jazz-tools";
1
+ import { type BrowserContext, type BrowserGuestContext } from 'jazz-browser';
2
+ import type { AccountClass, AnonymousJazzAgent, CoValue, CoValueClass, DeeplyLoaded, DepthsIn, ID } from 'jazz-tools';
3
+ import { Account } from 'jazz-tools';
4
+ import { type Component } from 'svelte';
5
+ import type { Props } from './Provider.svelte';
4
6
  /**
5
7
  * The key for the Jazz context.
6
8
  */
@@ -20,7 +22,10 @@ export declare function getJazzContext<Acc extends Account>(): JazzContext<Acc>;
20
22
  * Create a Jazz app.
21
23
  * @returns The Jazz app.
22
24
  */
23
- export declare function createJazzApp<Acc extends Account>(): {
25
+ export declare function createJazzApp<Acc extends Account = Account>({ AccountSchema }?: {
26
+ AccountSchema?: AccountClass<Acc>;
27
+ }): {
28
+ Provider: Component<Omit<Props<Acc>, "schema">, {}, string>;
24
29
  useAccount: {
25
30
  (): {
26
31
  me: Acc | undefined;
@@ -42,7 +47,7 @@ export declare function createJazzApp<Acc extends Account>(): {
42
47
  useCoState: <V extends CoValue, D extends DepthsIn<V> = []>(Schema: CoValueClass<V>, id: ID<V> | undefined, depth?: D) => {
43
48
  current?: DeeplyLoaded<V, D>;
44
49
  };
45
- useAcceptInvite: <V extends CoValue>({ invitedObjectSchema, onAccept, forValueHint, }: {
50
+ useAcceptInvite: <V extends CoValue>({ invitedObjectSchema, onAccept, forValueHint }: {
46
51
  invitedObjectSchema: CoValueClass<V>;
47
52
  onAccept: (projectID: ID<V>) => void;
48
53
  forValueHint?: string;
@@ -1,6 +1,7 @@
1
- import { consumeInviteLinkFromWindowLocation, } from "jazz-browser";
2
- import { Account, createCoValueObservable } from "jazz-tools";
3
- import { getContext, untrack } from "svelte";
1
+ import { consumeInviteLinkFromWindowLocation } from 'jazz-browser';
2
+ import { Account, createCoValueObservable } from 'jazz-tools';
3
+ import { getContext, untrack } from 'svelte';
4
+ import Provider from './Provider.svelte';
4
5
  /**
5
6
  * The key for the Jazz context.
6
7
  */
@@ -16,7 +17,14 @@ export function getJazzContext() {
16
17
  * Create a Jazz app.
17
18
  * @returns The Jazz app.
18
19
  */
19
- export function createJazzApp() {
20
+ export function createJazzApp({ AccountSchema = Account } = {}) {
21
+ // Create a provider component with the schema baked in
22
+ const ProviderWithSchema = (internal, props) => {
23
+ return Provider(internal, {
24
+ ...props,
25
+ schema: AccountSchema
26
+ });
27
+ };
20
28
  /**
21
29
  * Use the current account with a optional depth.
22
30
  * @param depth - The depth.
@@ -25,21 +33,21 @@ export function createJazzApp() {
25
33
  function useAccount(depth) {
26
34
  const ctx = getJazzContext();
27
35
  if (!ctx?.current) {
28
- throw new Error("useAccount must be used within a JazzProvider");
36
+ throw new Error('useAccount must be used within a JazzProvider');
29
37
  }
30
- if (!("me" in ctx.current)) {
38
+ if (!('me' in ctx.current)) {
31
39
  throw new Error("useAccount can't be used in a JazzProvider with auth === 'guest' - consider using useAccountOrGuest()");
32
40
  }
33
41
  const me = useCoState(ctx.current.me.constructor, ctx.current.me.id, depth);
34
42
  return {
35
43
  get me() {
36
- if (!ctx.current || !("me" in ctx.current))
44
+ if (!ctx.current || !('me' in ctx.current))
37
45
  return;
38
46
  return depth === undefined ? me.current || ctx.current.me : me.current;
39
47
  },
40
48
  logOut() {
41
49
  return ctx.current?.logOut();
42
- },
50
+ }
43
51
  };
44
52
  }
45
53
  /**
@@ -50,18 +58,18 @@ export function createJazzApp() {
50
58
  function useAccountOrGuest(depth) {
51
59
  const ctx = getJazzContext();
52
60
  if (!ctx?.current) {
53
- throw new Error("useAccountOrGuest must be used within a JazzProvider");
61
+ throw new Error('useAccountOrGuest must be used within a JazzProvider');
54
62
  }
55
- const contextMe = "me" in ctx.current ? ctx.current.me : undefined;
63
+ const contextMe = 'me' in ctx.current ? ctx.current.me : undefined;
56
64
  const me = useCoState(contextMe?.constructor, contextMe?.id, depth);
57
65
  // If the context has a me, return the account.
58
- if ("me" in ctx.current) {
66
+ if ('me' in ctx.current) {
59
67
  return {
60
68
  get me() {
61
69
  return depth === undefined
62
70
  ? me.current || ctx.current?.me
63
71
  : me.current;
64
- },
72
+ }
65
73
  };
66
74
  }
67
75
  // If the context has no me, return the guest.
@@ -69,7 +77,7 @@ export function createJazzApp() {
69
77
  return {
70
78
  get me() {
71
79
  return ctx.current?.guest;
72
- },
80
+ }
73
81
  };
74
82
  }
75
83
  }
@@ -95,7 +103,7 @@ export function createJazzApp() {
95
103
  if (!currentCtx || !id)
96
104
  return;
97
105
  // Setup subscription with current values
98
- return observable.subscribe(Schema, id, "me" in currentCtx ? currentCtx.me : currentCtx.guest, depth, () => {
106
+ return observable.subscribe(Schema, id, 'me' in currentCtx ? currentCtx.me : currentCtx.guest, depth, () => {
99
107
  // Get current value from our stable observable
100
108
  state = observable.getCurrentValue();
101
109
  });
@@ -103,7 +111,7 @@ export function createJazzApp() {
103
111
  return {
104
112
  get current() {
105
113
  return state;
106
- },
114
+ }
107
115
  };
108
116
  }
109
117
  /**
@@ -113,13 +121,13 @@ export function createJazzApp() {
113
121
  * @param forValueHint - Hint for the value.
114
122
  * @returns The accept invite hook.
115
123
  */
116
- function useAcceptInvite({ invitedObjectSchema, onAccept, forValueHint, }) {
124
+ function useAcceptInvite({ invitedObjectSchema, onAccept, forValueHint }) {
117
125
  const ctx = getJazzContext();
118
126
  const _onAccept = onAccept;
119
127
  if (!ctx.current) {
120
- throw new Error("useAcceptInvite must be used within a JazzProvider");
128
+ throw new Error('useAcceptInvite must be used within a JazzProvider');
121
129
  }
122
- if (!("me" in ctx.current)) {
130
+ if (!('me' in ctx.current)) {
123
131
  throw new Error("useAcceptInvite can't be used in a JazzProvider with auth === 'guest'.");
124
132
  }
125
133
  // Subscribe to the onAccept function.
@@ -134,21 +142,22 @@ export function createJazzApp() {
134
142
  const result = consumeInviteLinkFromWindowLocation({
135
143
  as: ctx.current.me,
136
144
  invitedObjectSchema,
137
- forValueHint,
145
+ forValueHint
138
146
  });
139
147
  // If the result is valid, call the onAccept function.
140
148
  result
141
149
  .then((result) => result && _onAccept(result?.valueID))
142
150
  .catch((e) => {
143
- console.error("Failed to accept invite", e);
151
+ console.error('Failed to accept invite', e);
144
152
  });
145
153
  });
146
154
  });
147
155
  }
148
156
  return {
157
+ Provider: ProviderWithSchema,
149
158
  useAccount,
150
159
  useAccountOrGuest,
151
160
  useCoState,
152
- useAcceptInvite,
161
+ useAcceptInvite
153
162
  };
154
163
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jazz-svelte",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "files": [
5
5
  "dist",
6
6
  "!dist/**/*.test.*",
@@ -22,7 +22,7 @@
22
22
  "svelte": "^5.0.0"
23
23
  },
24
24
  "devDependencies": {
25
- "@sveltejs/adapter-auto": "^3.0.0",
25
+ "@sveltejs/adapter-vercel": "^5.5.0",
26
26
  "@sveltejs/kit": "^2.0.0",
27
27
  "@sveltejs/package": "^2.0.0",
28
28
  "@sveltejs/vite-plugin-svelte": "^4.0.0",
@@ -41,8 +41,8 @@
41
41
  "vite": "^5.0.11"
42
42
  },
43
43
  "dependencies": {
44
- "jazz-browser": "0.8.34",
45
- "jazz-tools": "0.8.34"
44
+ "jazz-browser": "0.8.35",
45
+ "jazz-tools": "0.8.35"
46
46
  },
47
47
  "scripts": {
48
48
  "dev": "vite dev",
@@ -52,6 +52,8 @@
52
52
  "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
53
53
  "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
54
54
  "format": "prettier --write .",
55
- "lint": "prettier --check . && eslint ."
55
+ "lint": "prettier --check . && eslint .",
56
+ "format-and-lint": "pnpm run format && pnpm run lint",
57
+ "format-and-lint:fix": "pnpm run format --write && pnpm run lint --fix"
56
58
  }
57
59
  }
@@ -1,69 +0,0 @@
1
- <script lang="ts" generics="Acc extends Account">
2
- import { createJazzBrowserContext } from "jazz-browser";
3
- import type { AccountClass, AuthMethod } from "jazz-tools";
4
- import { Account } from "jazz-tools";
5
- import { type Snippet, setContext, untrack } from "svelte";
6
- import { JAZZ_CTX, type JazzContext } from "./jazz.svelte";
7
-
8
- type Props = {
9
- children: Snippet;
10
- auth: AuthMethod | "guest";
11
- peer: `wss://${string}` | `ws://${string}`;
12
- storage?: "indexedDB" | "singleTabOPFS";
13
- schema?: AccountClass<Acc>;
14
- };
15
-
16
- let {
17
- children,
18
- auth,
19
- peer,
20
- storage,
21
- schema = Account as unknown as AccountClass<Acc>,
22
- }: Props = $props();
23
-
24
- const ctx = $state<JazzContext<Acc>>({ current: undefined });
25
- setContext<JazzContext<Acc>>(JAZZ_CTX, ctx);
26
- let sessionCount = $state(0);
27
-
28
- $effect(() => {
29
- schema;
30
- auth;
31
- peer;
32
- storage;
33
- sessionCount;
34
- return untrack(() => {
35
- if (!auth || !peer) return;
36
-
37
- const promiseWithDoneCallback = createJazzBrowserContext<Acc>(
38
- auth === "guest"
39
- ? {
40
- peer,
41
- storage,
42
- }
43
- : {
44
- AccountSchema: schema,
45
- auth,
46
- peer,
47
- storage,
48
- },
49
- ).then((context) => {
50
- ctx.current = {
51
- ...context,
52
- logOut: () => {
53
- context.logOut();
54
- ctx.current = undefined;
55
- sessionCount = sessionCount + 1;
56
- },
57
- };
58
- return context.done;
59
- });
60
- return () => {
61
- void promiseWithDoneCallback.then((done) => done());
62
- };
63
- });
64
- });
65
- </script>
66
-
67
- {#if ctx.current}
68
- {@render children?.()}
69
- {/if}