@pulse-js/svelte 0.1.0 → 0.1.1

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/dist/index.cjs CHANGED
@@ -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 { Source, Guard, 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
+ * 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: Source<T>): {
20
+ value: T;
21
+ };
22
+ declare function usePulse<T>(unit: Guard<T>): GuardState<T>;
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 };
package/dist/index.d.ts CHANGED
@@ -1,11 +1,35 @@
1
- import { type Readable } from 'svelte/store';
2
- import type { Guard, Source, GuardState } from '@pulse-js/core';
1
+ import { Readable } from 'svelte/store';
2
+ import { Source, Guard, GuardState } from '@pulse-js/core';
3
+
3
4
  /**
4
- * Hook to consume a Pulse Unit (Source or Guard) as a Svelte Store.
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.
5
10
  */
6
- export declare function usePulse<T>(unit: Source<T>): Readable<T>;
7
- export declare function usePulse<T>(unit: Guard<T>): Readable<GuardState<T>>;
8
11
  /**
9
- * Explicit hook for Pulse Guards in Svelte.
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.
10
18
  */
11
- export declare function useGuard<T>(guard: Guard<T>): Readable<GuardState<T>>;
19
+ declare function usePulse<T>(unit: Source<T>): {
20
+ value: T;
21
+ };
22
+ declare function usePulse<T>(unit: Guard<T>): GuardState<T>;
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 };
package/dist/index.js CHANGED
@@ -1,15 +1,49 @@
1
- // src/index.ts
1
+ // src/index.svelte.ts
2
2
  import { readable } from "svelte/store";
3
3
  function usePulse(unit) {
4
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
- });
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
+ }
8
35
  }
9
36
  function useGuard(guard) {
10
37
  return usePulse(guard);
11
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
+ }
12
45
  export {
13
46
  useGuard,
14
- usePulse
47
+ usePulse,
48
+ usePulseStore
15
49
  };
package/package.json CHANGED
@@ -1,15 +1,24 @@
1
1
  {
2
2
  "name": "@pulse-js/svelte",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.js",
7
+ "svelte": "src/index.svelte.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "svelte": "./src/index.svelte.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
7
16
  "types": "dist/index.d.ts",
8
17
  "files": [
9
18
  "dist"
10
19
  ],
11
20
  "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",
21
+ "build": "bun x tsup src/index.svelte.ts --entry.index=src/index.svelte.ts --format esm,cjs --clean --dts --external svelte,@pulse-js/core",
13
22
  "lint": "bun x tsc --noEmit"
14
23
  },
15
24
  "peerDependencies": {