@pulse-js/svelte 0.1.0 → 0.1.2

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.
@@ -17,25 +17,60 @@ var __copyProps = (to, from, except, desc) => {
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
20
+ // src/index.svelte.ts
21
+ var index_svelte_exports = {};
22
+ __export(index_svelte_exports, {
23
23
  useGuard: () => useGuard,
24
- usePulse: () => usePulse
24
+ usePulse: () => usePulse,
25
+ usePulseStore: () => usePulseStore
25
26
  });
26
- module.exports = __toCommonJS(index_exports);
27
+ module.exports = __toCommonJS(index_svelte_exports);
27
28
  var import_store = require("svelte/store");
28
29
  function usePulse(unit) {
29
30
  const isGuard = unit !== null && typeof unit === "function" && "state" in unit;
30
- return (0, import_store.readable)(isGuard ? unit.state() : unit(), (set) => {
31
- return unit.subscribe(set);
32
- });
31
+ if (isGuard) {
32
+ const g = unit;
33
+ const container = $state({ data: g.state() });
34
+ $effect(() => {
35
+ return g.subscribe((v) => {
36
+ container.data = v;
37
+ });
38
+ });
39
+ return new Proxy({}, {
40
+ get(_, prop) {
41
+ return container.data[prop];
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;
53
+ const box = $state({ value: s() });
54
+ $effect(() => {
55
+ return s.subscribe((v) => {
56
+ box.value = v;
57
+ });
58
+ });
59
+ return box;
60
+ }
33
61
  }
34
62
  function useGuard(guard) {
35
63
  return usePulse(guard);
36
64
  }
65
+ function usePulseStore(unit) {
66
+ const isGuard = unit !== null && typeof unit === "function" && "state" in unit;
67
+ return (0, import_store.readable)(isGuard ? unit.state() : unit(), (set) => {
68
+ return unit.subscribe(set);
69
+ });
70
+ }
37
71
  // Annotate the CommonJS export names for ESM import in node:
38
72
  0 && (module.exports = {
39
73
  useGuard,
40
- usePulse
74
+ usePulse,
75
+ usePulseStore
41
76
  });
@@ -0,0 +1,35 @@
1
+ import { Readable } from 'svelte/store';
2
+ import { Guard, GuardState, Source } 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
+ * usePulse (Svelte 5 Runes)
13
+ *
14
+ * Returns a reactive wrapper for a Pulse Unit.
15
+ *
16
+ * - For Sources: Returns a stable object { value: T } where .value is reactive.
17
+ * - For Guards: Returns a proxy that behaves like the GuardState, but is reactive to updates.
18
+ */
19
+ declare function usePulse<T>(unit: Guard<T>): GuardState<T>;
20
+ declare function usePulse<T>(unit: Source<T>): {
21
+ value: T;
22
+ };
23
+ /**
24
+ * useGuard (Svelte 5 Runes)
25
+ * Alias for usePulse(guard).
26
+ */
27
+ declare function useGuard<T>(guard: Guard<T>): GuardState<T>;
28
+ /**
29
+ * usePulseStore (Legacy)
30
+ * Returns a Svelte Readable store. Use this in Svelte 4 or if you prefer store syntax.
31
+ */
32
+ declare function usePulseStore<T>(unit: Source<T>): Readable<T>;
33
+ declare function usePulseStore<T>(unit: Guard<T>): Readable<GuardState<T>>;
34
+
35
+ export { useGuard, usePulse, usePulseStore };
@@ -0,0 +1,35 @@
1
+ import { Readable } from 'svelte/store';
2
+ import { Guard, GuardState, Source } 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
+ * usePulse (Svelte 5 Runes)
13
+ *
14
+ * Returns a reactive wrapper for a Pulse Unit.
15
+ *
16
+ * - For Sources: Returns a stable object { value: T } where .value is reactive.
17
+ * - For Guards: Returns a proxy that behaves like the GuardState, but is reactive to updates.
18
+ */
19
+ declare function usePulse<T>(unit: Guard<T>): GuardState<T>;
20
+ declare function usePulse<T>(unit: Source<T>): {
21
+ value: T;
22
+ };
23
+ /**
24
+ * useGuard (Svelte 5 Runes)
25
+ * Alias for usePulse(guard).
26
+ */
27
+ declare function useGuard<T>(guard: Guard<T>): GuardState<T>;
28
+ /**
29
+ * usePulseStore (Legacy)
30
+ * Returns a Svelte Readable store. Use this in Svelte 4 or if you prefer store syntax.
31
+ */
32
+ declare function usePulseStore<T>(unit: Source<T>): Readable<T>;
33
+ declare function usePulseStore<T>(unit: Guard<T>): Readable<GuardState<T>>;
34
+
35
+ export { useGuard, usePulse, usePulseStore };
@@ -0,0 +1,49 @@
1
+ // src/index.svelte.ts
2
+ import { readable } from "svelte/store";
3
+ function usePulse(unit) {
4
+ const isGuard = unit !== null && typeof unit === "function" && "state" in unit;
5
+ if (isGuard) {
6
+ const g = unit;
7
+ const container = $state({ data: g.state() });
8
+ $effect(() => {
9
+ return g.subscribe((v) => {
10
+ container.data = v;
11
+ });
12
+ });
13
+ return new Proxy({}, {
14
+ get(_, prop) {
15
+ return container.data[prop];
16
+ },
17
+ // Ensure spread operators and other object features work
18
+ ownKeys() {
19
+ return Reflect.ownKeys(container.data);
20
+ },
21
+ getOwnPropertyDescriptor(_, prop) {
22
+ return Reflect.getOwnPropertyDescriptor(container.data, prop);
23
+ }
24
+ });
25
+ } else {
26
+ const s = unit;
27
+ const box = $state({ value: s() });
28
+ $effect(() => {
29
+ return s.subscribe((v) => {
30
+ box.value = v;
31
+ });
32
+ });
33
+ return box;
34
+ }
35
+ }
36
+ function useGuard(guard) {
37
+ return usePulse(guard);
38
+ }
39
+ function usePulseStore(unit) {
40
+ const isGuard = unit !== null && typeof unit === "function" && "state" in unit;
41
+ return readable(isGuard ? unit.state() : unit(), (set) => {
42
+ return unit.subscribe(set);
43
+ });
44
+ }
45
+ export {
46
+ useGuard,
47
+ usePulse,
48
+ usePulseStore
49
+ };
package/package.json CHANGED
@@ -1,15 +1,26 @@
1
1
  {
2
2
  "name": "@pulse-js/svelte",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
- "main": "dist/index.cjs",
6
- "module": "dist/index.js",
7
- "types": "dist/index.d.ts",
5
+ "main": "dist/index.svelte.cjs",
6
+ "module": "dist/index.svelte.js",
7
+ "svelte": "src/index.svelte.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.svelte.d.ts",
11
+ "svelte": "./src/index.svelte.ts",
12
+ "import": "./dist/index.svelte.js",
13
+ "require": "./dist/index.svelte.cjs",
14
+ "default": "./dist/index.svelte.js"
15
+ }
16
+ },
17
+ "types": "dist/index.svelte.d.ts",
8
18
  "files": [
9
- "dist"
19
+ "dist",
20
+ "src"
10
21
  ],
11
22
  "scripts": {
12
- "build": "bun x tsup src/index.ts --format esm,cjs --clean --external svelte,@pulse-js/core && bun x tsc src/index.ts --declaration --emitDeclarationOnly --outDir dist --skipLibCheck",
23
+ "build": "bun x tsup src/index.svelte.ts --format esm,cjs --clean --dts --external svelte,@pulse-js/core",
13
24
  "lint": "bun x tsc --noEmit"
14
25
  },
15
26
  "peerDependencies": {
@@ -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
+ }
package/dist/index.d.ts DELETED
@@ -1,11 +0,0 @@
1
- import { type Readable } from 'svelte/store';
2
- import type { Guard, Source, GuardState } from '@pulse-js/core';
3
- /**
4
- * Hook to consume a Pulse Unit (Source or Guard) as a Svelte Store.
5
- */
6
- export declare function usePulse<T>(unit: Source<T>): Readable<T>;
7
- export declare function usePulse<T>(unit: Guard<T>): Readable<GuardState<T>>;
8
- /**
9
- * Explicit hook for Pulse Guards in Svelte.
10
- */
11
- export declare function useGuard<T>(guard: Guard<T>): Readable<GuardState<T>>;
package/dist/index.js DELETED
@@ -1,15 +0,0 @@
1
- // src/index.ts
2
- import { readable } from "svelte/store";
3
- function usePulse(unit) {
4
- const isGuard = unit !== null && typeof unit === "function" && "state" in unit;
5
- return readable(isGuard ? unit.state() : unit(), (set) => {
6
- return unit.subscribe(set);
7
- });
8
- }
9
- function useGuard(guard) {
10
- return usePulse(guard);
11
- }
12
- export {
13
- useGuard,
14
- usePulse
15
- };