@evolu/svelte 3.0.0-next.0 → 3.0.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/LICENSE +21 -0
- package/dist/index.svelte.d.ts +1 -1
- package/dist/index.svelte.d.ts.map +1 -1
- package/dist/index.svelte.js +2 -2
- package/package.json +20 -18
- package/src/lib/index.svelte.ts +158 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Evolu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.svelte.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* compiler in the app itself.
|
|
4
4
|
*/
|
|
5
5
|
import type { AppOwner, Evolu, EvoluSchema, InferRow, Query, Row } from "@evolu/common/local-first";
|
|
6
|
-
export declare const evoluSvelteDeps:
|
|
6
|
+
export declare const evoluSvelteDeps: any;
|
|
7
7
|
/**
|
|
8
8
|
* Load and subscribe to the Query, and return an object with `rows` property
|
|
9
9
|
* that are automatically updated when data changes.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.svelte.d.ts","sourceRoot":"","sources":["../src/lib/index.svelte.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACV,QAAQ,EACR,KAAK,EACL,WAAW,EACX,QAAQ,EACR,KAAK,EAEL,GAAG,EACJ,MAAM,2BAA2B,CAAC;AAKnC,eAAO,MAAM,eAAe
|
|
1
|
+
{"version":3,"file":"index.svelte.d.ts","sourceRoot":"","sources":["../src/lib/index.svelte.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACV,QAAQ,EACR,KAAK,EACL,WAAW,EACX,QAAQ,EACR,KAAK,EAEL,GAAG,EACJ,MAAM,2BAA2B,CAAC;AAKnC,eAAO,MAAM,eAAe,KAAkC,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,eAAO,MAAM,UAAU,GACrB,CAAC,SAAS,GAAG,EACb,MAAM,SAAS,WAAW,EAC1B,SAAS,GAAG,QAAQ,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAEtC,OAAO,KAAK,CAAC,MAAM,CAAC;AACpB;;;;;GAKG;AACH,eAAe,MAAM,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,SAAS,EACjD,UAAU;IACR;;;;;OAKG;IACH,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,SAAS,CAAC;CACjC,KACA;IAAE,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,SAAS,CAAC,CAAA;CA2CnC,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM,SAAS,WAAW,EACtD,OAAO,KAAK,CAAC,MAAM,CAAC,KACnB;IACD,QAAQ,CAAC,OAAO,EAAE,QAAQ,GAAG,SAAS,CAAC;CAkBxC,CAAC"}
|
package/dist/index.svelte.js
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { createEvoluDeps } from "@evolu/web";
|
|
6
6
|
// just in case we need to add some svelte specific deps
|
|
7
|
-
// eslint-disable-next-line
|
|
8
|
-
export const evoluSvelteDeps = createEvoluDeps();
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
8
|
+
export const evoluSvelteDeps = /*#__PURE__*/ createEvoluDeps();
|
|
9
9
|
/**
|
|
10
10
|
* Load and subscribe to the Query, and return an object with `rows` property
|
|
11
11
|
* that are automatically updated when data changes.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@evolu/svelte",
|
|
3
|
-
"version": "3.0.0
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Evolu for Svelte",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"evolu",
|
|
@@ -10,7 +10,10 @@
|
|
|
10
10
|
"bugs": {
|
|
11
11
|
"url": "https://github.com/evoluhq/evolu/issues"
|
|
12
12
|
},
|
|
13
|
-
"repository":
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/evoluhq/evolu.git"
|
|
16
|
+
},
|
|
14
17
|
"license": "MIT",
|
|
15
18
|
"sideEffects": false,
|
|
16
19
|
"type": "module",
|
|
@@ -20,30 +23,23 @@
|
|
|
20
23
|
"svelte": "./dist/index.svelte.js"
|
|
21
24
|
}
|
|
22
25
|
},
|
|
23
|
-
"svelte": "./dist/index.svelte.js",
|
|
24
26
|
"types": "./dist/index.svelte.d.ts",
|
|
25
27
|
"files": [
|
|
26
28
|
"dist",
|
|
29
|
+
"src/**",
|
|
27
30
|
"!dist/**/*.test.*",
|
|
28
31
|
"!dist/**/*.spec.*"
|
|
29
32
|
],
|
|
30
|
-
"scripts": {
|
|
31
|
-
"build": "tsc --build && npm run package",
|
|
32
|
-
"prepack": "npm run build",
|
|
33
|
-
"check": "svelte-check --tsconfig ./tsconfig.json",
|
|
34
|
-
"package": "svelte-package",
|
|
35
|
-
"prepublishOnly": "npm run package",
|
|
36
|
-
"preview": "vite preview"
|
|
37
|
-
},
|
|
38
33
|
"devDependencies": {
|
|
39
|
-
"@evolu/common": "8.0.0-next.0",
|
|
40
|
-
"@evolu/typescript-config": "*",
|
|
41
|
-
"@evolu/web": "3.0.0-next.0",
|
|
42
34
|
"@sveltejs/package": "^2.5.7",
|
|
43
35
|
"@tsconfig/svelte": "^5.0.6",
|
|
44
|
-
"
|
|
45
|
-
"svelte
|
|
46
|
-
"
|
|
36
|
+
"@typescript/native": "npm:typescript@^7.0.2",
|
|
37
|
+
"svelte": "^5.56.8",
|
|
38
|
+
"svelte-check": "^4.7.4",
|
|
39
|
+
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
40
|
+
"@evolu/typescript-config": "0.0.2",
|
|
41
|
+
"@evolu/web": "3.0.0",
|
|
42
|
+
"@evolu/common": "8.0.0"
|
|
47
43
|
},
|
|
48
44
|
"peerDependencies": {
|
|
49
45
|
"@evolu/common": "^8.0.0-next.0",
|
|
@@ -52,5 +48,11 @@
|
|
|
52
48
|
},
|
|
53
49
|
"publishConfig": {
|
|
54
50
|
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "tsc --build tsconfig.build.json && pnpm package",
|
|
54
|
+
"check": "svelte-check --tsconfig ./tsconfig.json",
|
|
55
|
+
"package": "svelte-package",
|
|
56
|
+
"preview": "vite preview"
|
|
55
57
|
}
|
|
56
|
-
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file needs to be named .svelte.ts to be recognized by the Svelte
|
|
3
|
+
* compiler in the app itself.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
AppOwner,
|
|
8
|
+
Evolu,
|
|
9
|
+
EvoluSchema,
|
|
10
|
+
InferRow,
|
|
11
|
+
Query,
|
|
12
|
+
QueryRows,
|
|
13
|
+
Row,
|
|
14
|
+
} from "@evolu/common/local-first";
|
|
15
|
+
import { createEvoluDeps } from "@evolu/web";
|
|
16
|
+
|
|
17
|
+
// just in case we need to add some svelte specific deps
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
19
|
+
export const evoluSvelteDeps = /*#__PURE__*/ createEvoluDeps();
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Load and subscribe to the Query, and return an object with `rows` property
|
|
23
|
+
* that are automatically updated when data changes.
|
|
24
|
+
*
|
|
25
|
+
* ### Example
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* // Create your query
|
|
29
|
+
* const allTodos = evolu.createQuery((db) =>
|
|
30
|
+
* db.selectFrom("todo").selectAll(),
|
|
31
|
+
* );
|
|
32
|
+
*
|
|
33
|
+
* // Get all rows.
|
|
34
|
+
* const allTodosState = queryState(evolu, () => allTodos);
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* ### Example
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* // some kind of state
|
|
41
|
+
* let someKindOfState = $state("someId");
|
|
42
|
+
*
|
|
43
|
+
* // derive your query based other props
|
|
44
|
+
* const allTodos = $derived(
|
|
45
|
+
* evolu.createQuery((db) =>
|
|
46
|
+
* db.selectFrom("todo").where("id", "=", someKindOfState).selectAll(),
|
|
47
|
+
* ),
|
|
48
|
+
* );
|
|
49
|
+
*
|
|
50
|
+
* // Get all rows, once someKindOfState changes, this allTodosState will be updated with the evolu query result
|
|
51
|
+
* const allTodosState = queryState(evolu, () => allTodos);
|
|
52
|
+
*
|
|
53
|
+
* // use allTodosState.rows in further calculations / UI
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export const queryState = <
|
|
57
|
+
R extends Row,
|
|
58
|
+
Schema extends EvoluSchema,
|
|
59
|
+
MappedRow = InferRow<Query<Schema, R>>,
|
|
60
|
+
>(
|
|
61
|
+
evolu: Evolu<Schema>,
|
|
62
|
+
/**
|
|
63
|
+
* Can be a normal query or a derived query.
|
|
64
|
+
*
|
|
65
|
+
* Svelte reactivity: it needs to be a callback, if the query is $derived this
|
|
66
|
+
* will re-trigger the load/subscription based on the new query.
|
|
67
|
+
*/
|
|
68
|
+
observedQuery: () => Query<Schema, R> | undefined,
|
|
69
|
+
options?: {
|
|
70
|
+
/**
|
|
71
|
+
* This is a little helper so that you can map the results instead of using
|
|
72
|
+
* a $derive operation.
|
|
73
|
+
*
|
|
74
|
+
* @param row
|
|
75
|
+
*/
|
|
76
|
+
mapping?: (row: R) => MappedRow;
|
|
77
|
+
},
|
|
78
|
+
): { readonly rows: Array<MappedRow> } => {
|
|
79
|
+
{
|
|
80
|
+
// writing to this variable - svelte's compiler will track it
|
|
81
|
+
let writableState: Array<MappedRow> = $state([]);
|
|
82
|
+
|
|
83
|
+
const updateState = (rows: QueryRows<R>): void => {
|
|
84
|
+
if (options?.mapping) {
|
|
85
|
+
// re-assigning because somehow typescript thinks its still nullable here
|
|
86
|
+
// remove again once no issue anymore
|
|
87
|
+
const mapper = options.mapping;
|
|
88
|
+
writableState = rows.map((row) => mapper(row));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
writableState = rows as Array<MappedRow>;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
$effect(() => {
|
|
95
|
+
const query = observedQuery();
|
|
96
|
+
|
|
97
|
+
if (!query) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// always setting the state on first load
|
|
102
|
+
// for a) if the query changes we definitely need current content immediately
|
|
103
|
+
// for b) if you sub/unsub in a very short time can cause the subscription not to trigger a callback
|
|
104
|
+
// => this is also for HMR
|
|
105
|
+
void evolu.loadQuery(query).then(updateState);
|
|
106
|
+
|
|
107
|
+
return evolu.subscribeQuery(query)(() => {
|
|
108
|
+
const rows = evolu.getQueryRows(query);
|
|
109
|
+
|
|
110
|
+
updateState(rows);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
// Svelte reactivity: it needs to be a getter
|
|
116
|
+
get rows() {
|
|
117
|
+
return writableState;
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Get the {@link AppOwner} promise that resolves when available.
|
|
125
|
+
*
|
|
126
|
+
* ### Example
|
|
127
|
+
*
|
|
128
|
+
* ```ts
|
|
129
|
+
* import { appOwnerState } from "@evolu/svelte";
|
|
130
|
+
*
|
|
131
|
+
* const owner = appOwnerState(evolu);
|
|
132
|
+
*
|
|
133
|
+
* // use owner.current in your Svelte templates
|
|
134
|
+
* // it will be undefined initially and set once the promise resolves
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
export const appOwnerState = <Schema extends EvoluSchema>(
|
|
138
|
+
evolu: Evolu<Schema>,
|
|
139
|
+
): {
|
|
140
|
+
readonly current: AppOwner | undefined;
|
|
141
|
+
} => {
|
|
142
|
+
{
|
|
143
|
+
// writing to this variable - svelte's compiler will track it
|
|
144
|
+
let writableState = $state<AppOwner | undefined>(undefined);
|
|
145
|
+
|
|
146
|
+
$effect(() => {
|
|
147
|
+
// TODO: Review, it was promise, it isn't anymore.
|
|
148
|
+
writableState = evolu.appOwner;
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
// Svelte reactivity: it needs to be a getter
|
|
153
|
+
get current() {
|
|
154
|
+
return writableState;
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
};
|