@pulse-js/svelte 0.1.1 → 0.1.3
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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Readable } from 'svelte/store';
|
|
2
|
-
import {
|
|
2
|
+
import { Guard, GuardState, Source } from '@pulse-js/core';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Pulse-JS Svelte Integration
|
|
@@ -16,10 +16,10 @@ import { Source, Guard, GuardState } from '@pulse-js/core';
|
|
|
16
16
|
* - For Sources: Returns a stable object { value: T } where .value is reactive.
|
|
17
17
|
* - For Guards: Returns a proxy that behaves like the GuardState, but is reactive to updates.
|
|
18
18
|
*/
|
|
19
|
+
declare function usePulse<T>(unit: Guard<T>): GuardState<T>;
|
|
19
20
|
declare function usePulse<T>(unit: Source<T>): {
|
|
20
21
|
value: T;
|
|
21
22
|
};
|
|
22
|
-
declare function usePulse<T>(unit: Guard<T>): GuardState<T>;
|
|
23
23
|
/**
|
|
24
24
|
* useGuard (Svelte 5 Runes)
|
|
25
25
|
* Alias for usePulse(guard).
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Readable } from 'svelte/store';
|
|
2
|
-
import {
|
|
2
|
+
import { Guard, GuardState, Source } from '@pulse-js/core';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Pulse-JS Svelte Integration
|
|
@@ -16,10 +16,10 @@ import { Source, Guard, GuardState } from '@pulse-js/core';
|
|
|
16
16
|
* - For Sources: Returns a stable object { value: T } where .value is reactive.
|
|
17
17
|
* - For Guards: Returns a proxy that behaves like the GuardState, but is reactive to updates.
|
|
18
18
|
*/
|
|
19
|
+
declare function usePulse<T>(unit: Guard<T>): GuardState<T>;
|
|
19
20
|
declare function usePulse<T>(unit: Source<T>): {
|
|
20
21
|
value: T;
|
|
21
22
|
};
|
|
22
|
-
declare function usePulse<T>(unit: Guard<T>): GuardState<T>;
|
|
23
23
|
/**
|
|
24
24
|
* useGuard (Svelte 5 Runes)
|
|
25
25
|
* Alias for usePulse(guard).
|
package/package.json
CHANGED
|
@@ -1,31 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pulse-js/svelte",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"main": "dist/index.cjs",
|
|
6
|
-
"module": "dist/index.js",
|
|
5
|
+
"main": "dist/index.svelte.cjs",
|
|
6
|
+
"module": "dist/index.svelte.js",
|
|
7
7
|
"svelte": "src/index.svelte.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"types": "./dist/index.d.ts",
|
|
11
10
|
"svelte": "./src/index.svelte.ts",
|
|
12
|
-
"
|
|
13
|
-
"
|
|
11
|
+
"types": "./dist/index.svelte.d.ts",
|
|
12
|
+
"import": "./dist/index.svelte.js",
|
|
13
|
+
"require": "./dist/index.svelte.cjs",
|
|
14
|
+
"default": "./dist/index.svelte.js"
|
|
14
15
|
}
|
|
15
16
|
},
|
|
16
|
-
"types": "dist/index.d.ts",
|
|
17
|
+
"types": "dist/index.svelte.d.ts",
|
|
17
18
|
"files": [
|
|
18
|
-
"dist"
|
|
19
|
+
"dist",
|
|
20
|
+
"src"
|
|
19
21
|
],
|
|
20
22
|
"scripts": {
|
|
21
|
-
"build": "bun x tsup src/index.svelte.ts --
|
|
23
|
+
"build": "bun x tsup src/index.svelte.ts --format esm,cjs --clean --dts --external svelte,@pulse-js/core",
|
|
22
24
|
"lint": "bun x tsc --noEmit"
|
|
23
25
|
},
|
|
24
26
|
"peerDependencies": {
|
|
25
27
|
"svelte": "^4.0.0 || ^5.0.0"
|
|
26
28
|
},
|
|
27
29
|
"dependencies": {
|
|
28
|
-
"@pulse-js/core": "^0.1.
|
|
30
|
+
"@pulse-js/core": "^0.1.9"
|
|
29
31
|
},
|
|
30
32
|
"devDependencies": {
|
|
31
33
|
"svelte": "^5.0.0",
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { readable, type Readable } from 'svelte/store';
|
|
2
|
+
import type { Guard, Source, GuardState } from '@pulse-js/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Pulse-JS Svelte Integration
|
|
6
|
+
*
|
|
7
|
+
* This package provides two ways to consume Pulse state:
|
|
8
|
+
* 1. Runes (Svelte 5+): Native, fine-grained reactivity using $state and $effect.
|
|
9
|
+
* 2. Stores (Svelte 4/5): Legacy Svelte stores for backward compatibility.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* usePulse (Svelte 5 Runes)
|
|
14
|
+
*
|
|
15
|
+
* Returns a reactive wrapper for a Pulse Unit.
|
|
16
|
+
*
|
|
17
|
+
* - For Sources: Returns a stable object { value: T } where .value is reactive.
|
|
18
|
+
* - For Guards: Returns a proxy that behaves like the GuardState, but is reactive to updates.
|
|
19
|
+
*/
|
|
20
|
+
export function usePulse<T>(unit: Guard<T>): GuardState<T>;
|
|
21
|
+
export function usePulse<T>(unit: Source<T>): { value: T };
|
|
22
|
+
export function usePulse<T>(unit: any): any {
|
|
23
|
+
const isGuard = unit !== null && typeof unit === 'function' && 'state' in unit;
|
|
24
|
+
|
|
25
|
+
if (isGuard) {
|
|
26
|
+
const g = unit as Guard<T>;
|
|
27
|
+
// In Svelte 5, to keep the identity of the returned object stable while
|
|
28
|
+
// the internal state changes, we use a container and a Proxy.
|
|
29
|
+
const container = $state({ data: g.state() });
|
|
30
|
+
|
|
31
|
+
$effect(() => {
|
|
32
|
+
return g.subscribe(v => {
|
|
33
|
+
container.data = v;
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return new Proxy({}, {
|
|
38
|
+
get(_, prop) {
|
|
39
|
+
// Accessing container.data[prop] inside the get trap ensures
|
|
40
|
+
// Svelte tracks the dependency on the 'container.data' property.
|
|
41
|
+
return container.data[prop as keyof GuardState<T>];
|
|
42
|
+
},
|
|
43
|
+
// Ensure spread operators and other object features work
|
|
44
|
+
ownKeys() {
|
|
45
|
+
return Reflect.ownKeys(container.data);
|
|
46
|
+
},
|
|
47
|
+
getOwnPropertyDescriptor(_, prop) {
|
|
48
|
+
return Reflect.getOwnPropertyDescriptor(container.data, prop);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
} else {
|
|
52
|
+
const s = unit as Source<T>;
|
|
53
|
+
// For Sources, we return a "box" object. This is a common pattern in Svelte 5
|
|
54
|
+
// for primitive or frequently-swapped values.
|
|
55
|
+
const box = $state({ value: s() });
|
|
56
|
+
|
|
57
|
+
$effect(() => {
|
|
58
|
+
return s.subscribe(v => {
|
|
59
|
+
box.value = v;
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return box;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* useGuard (Svelte 5 Runes)
|
|
69
|
+
* Alias for usePulse(guard).
|
|
70
|
+
*/
|
|
71
|
+
export function useGuard<T>(guard: Guard<T>): GuardState<T> {
|
|
72
|
+
return usePulse(guard);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* usePulseStore (Legacy)
|
|
77
|
+
* Returns a Svelte Readable store. Use this in Svelte 4 or if you prefer store syntax.
|
|
78
|
+
*/
|
|
79
|
+
export function usePulseStore<T>(unit: Source<T>): Readable<T>;
|
|
80
|
+
export function usePulseStore<T>(unit: Guard<T>): Readable<GuardState<T>>;
|
|
81
|
+
export function usePulseStore<T>(unit: Guard<T> | Source<T>): Readable<any> {
|
|
82
|
+
const isGuard = unit !== null && typeof unit === 'function' && 'state' in unit;
|
|
83
|
+
return readable(isGuard ? (unit as Guard<any>).state() : (unit as Source<any>)(), (set) => {
|
|
84
|
+
return (unit as any).subscribe(set);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
File without changes
|
|
File without changes
|