@evolu/svelte 1.0.0-preview.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 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/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # Evolu for Svelte
2
+
3
+ [Evolu](https://github.com/evoluhq/evolu) for [Svelte](https://svelte.dev/).
4
+
5
+ ## Documentation
6
+
7
+ For detailed information and usage examples, please visit [evolu.dev](https://www.evolu.dev).
8
+
9
+ ## Community
10
+
11
+ The Evolu community is on [GitHub Discussions](https://github.com/evoluhq/evolu/discussions), where you can ask questions and voice ideas.
12
+
13
+ To chat with other community members, you can join the [Evolu Discord](https://discord.gg/2J8yyyyxtZ).
14
+
15
+ [![Twitter URL](https://img.shields.io/twitter/url/https/twitter.com/evoluhq.svg?style=social&label=Follow%20%40evoluhq)](https://twitter.com/evoluhq)
@@ -0,0 +1,53 @@
1
+ /**
2
+ * This file needs to be named .svelte.ts to be recognized by the Svelte
3
+ * compiler in the app itself.
4
+ */
5
+ import type { Evolu, EvoluDeps, EvoluSchema, InferRow, Query, Row } from "@evolu/common/evolu";
6
+ export declare const evoluSvelteDeps: EvoluDeps;
7
+ /**
8
+ * Load and subscribe to the Query, and return an object with `rows` property
9
+ * that are automatically updated when data changes.
10
+ *
11
+ * ### Example
12
+ *
13
+ * ```ts
14
+ * // Create your query
15
+ * const allTodos = evolu.createQuery((db) => ...);
16
+ *
17
+ * // Get all rows.
18
+ * const allTodosState = queryState(evolu, () => allTodos);
19
+ * ```
20
+ *
21
+ * ### Example
22
+ *
23
+ * ```ts
24
+ * // some kind of state
25
+ * let someKindOfState = $state('someId');
26
+ *
27
+ * // derive your query based other props
28
+ * const allTodos = $derived(evolu.createQuery((db) => use someKindOfState here ));
29
+ *
30
+ * // Get all rows, once someKindOfState changes, this allTodosState will be updated with the evolu query result
31
+ * const allTodosState = queryState(evolu, () => allTodos);
32
+ *
33
+ * // use allTodosState.rows in further calculations / UI
34
+ * ```
35
+ */
36
+ export declare function queryState<R extends Row, Schema extends EvoluSchema, MappedRow = InferRow<Query<R>>>(evolu: Evolu<Schema>,
37
+ /**
38
+ * Can be a normal query or a derived query.
39
+ *
40
+ * Svelte reactivity: it needs to be a callback, if the query is $derived this
41
+ * will re-trigger the load/subscription based on the new query.
42
+ */
43
+ observedQuery: () => Query<R> | undefined, options?: {
44
+ /**
45
+ * This is a little helper so that you can map the results instead of using
46
+ * a $derive operation.
47
+ *
48
+ * @param row
49
+ */
50
+ mapping?: (row: R) => MappedRow;
51
+ }): {
52
+ readonly rows: Array<MappedRow>;
53
+ };
@@ -0,0 +1,82 @@
1
+ /**
2
+ * This file needs to be named .svelte.ts to be recognized by the Svelte
3
+ * compiler in the app itself.
4
+ */
5
+ import { evoluWebDeps } from "@evolu/web";
6
+ // just in case we need to add some svelte specific deps
7
+ export const evoluSvelteDeps = Object.assign({}, evoluWebDeps);
8
+ /**
9
+ * Load and subscribe to the Query, and return an object with `rows` property
10
+ * that are automatically updated when data changes.
11
+ *
12
+ * ### Example
13
+ *
14
+ * ```ts
15
+ * // Create your query
16
+ * const allTodos = evolu.createQuery((db) => ...);
17
+ *
18
+ * // Get all rows.
19
+ * const allTodosState = queryState(evolu, () => allTodos);
20
+ * ```
21
+ *
22
+ * ### Example
23
+ *
24
+ * ```ts
25
+ * // some kind of state
26
+ * let someKindOfState = $state('someId');
27
+ *
28
+ * // derive your query based other props
29
+ * const allTodos = $derived(evolu.createQuery((db) => use someKindOfState here ));
30
+ *
31
+ * // Get all rows, once someKindOfState changes, this allTodosState will be updated with the evolu query result
32
+ * const allTodosState = queryState(evolu, () => allTodos);
33
+ *
34
+ * // use allTodosState.rows in further calculations / UI
35
+ * ```
36
+ */
37
+ export function queryState(evolu,
38
+ /**
39
+ * Can be a normal query or a derived query.
40
+ *
41
+ * Svelte reactivity: it needs to be a callback, if the query is $derived this
42
+ * will re-trigger the load/subscription based on the new query.
43
+ */
44
+ observedQuery, options) {
45
+ {
46
+ // writing to this variable - svelte's compiler will track it
47
+ let writableState = $state([]);
48
+ function updateState(rows) {
49
+ if (options === null || options === void 0 ? void 0 : options.mapping) {
50
+ // re-assigning because somehow typescript thinks its still nullable here
51
+ // remove again once no issue anymore
52
+ const mapper = options.mapping;
53
+ writableState = rows.map((row) => mapper(row));
54
+ }
55
+ writableState = rows;
56
+ }
57
+ $effect(() => {
58
+ const query = observedQuery();
59
+ if (!query) {
60
+ return;
61
+ }
62
+ // always setting the state on first load
63
+ // for a) if the query changes we definitely need current content immediately
64
+ // for b) if you sub/unsub in a very short time can cause the subscription not to trigger a callback
65
+ // => this is also for HMR
66
+ void evolu.loadQuery(query).then(updateState);
67
+ const unsubEvoluSub = evolu.subscribeQuery(query)(() => {
68
+ const rows = evolu.getQueryRows(query);
69
+ updateState(rows);
70
+ });
71
+ return () => {
72
+ unsubEvoluSub();
73
+ };
74
+ });
75
+ return {
76
+ // Svelte reactivity: it needs to be a getter
77
+ get rows() {
78
+ return writableState;
79
+ },
80
+ };
81
+ }
82
+ }
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@evolu/svelte",
3
+ "version": "1.0.0-preview.0",
4
+ "description": "Evolu for Svelte",
5
+ "keywords": [
6
+ "evolu",
7
+ "svelte"
8
+ ],
9
+ "license": "MIT",
10
+ "repository": "evoluhq/evolu",
11
+ "bugs": {
12
+ "url": "https://github.com/evoluhq/evolu/issues"
13
+ },
14
+ "homepage": "https://evolu.dev",
15
+ "files": [
16
+ "dist",
17
+ "!dist/**/*.test.*",
18
+ "!dist/**/*.spec.*"
19
+ ],
20
+ "sideEffects": [],
21
+ "svelte": "./dist/index.svelte.js",
22
+ "types": "./dist/index.svelte.d.ts",
23
+ "type": "module",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.svelte.d.ts",
27
+ "svelte": "./dist/index.svelte.js"
28
+ }
29
+ },
30
+ "devDependencies": {
31
+ "@sveltejs/package": "^2.3.11",
32
+ "@tsconfig/svelte": "^5.0.4",
33
+ "svelte": "^5.33.17",
34
+ "svelte-check": "^4.2.1",
35
+ "typescript": "^5.8.3",
36
+ "@evolu/common": "6.0.0-preview.0",
37
+ "@evolu/tsconfig": "0.0.2",
38
+ "@evolu/web": "1.0.0-preview.0"
39
+ },
40
+ "peerDependencies": {
41
+ "@evolu/common": "^6.0.0-preview.0",
42
+ "@evolu/web": "^1.0.0-preview.0",
43
+ "svelte": "^5.0.0"
44
+ },
45
+ "publishConfig": {
46
+ "access": "public"
47
+ },
48
+ "scripts": {
49
+ "dev": "tsc --watch",
50
+ "build": "shx rm -rf dist && tsc && pnpm run package",
51
+ "preview": "vite preview",
52
+ "package": "svelte-package",
53
+ "check": "svelte-check --tsconfig ./tsconfig.json",
54
+ "clean": "shx rm -rf .turbo .svelte-kit node_modules dist"
55
+ }
56
+ }