@pulse-js/svelte 0.1.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/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # @pulse-js/svelte
2
+
3
+ The standard Svelte integration for Pulse.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@pulse-js/svelte.svg)](https://www.npmjs.com/package/@pulse-js/svelte)
6
+
7
+ ## Features
8
+
9
+ - **Store-based API**: Pulse Units (Sources and Guards) are converted into native Svelte Stores.
10
+ - **Auto-Subscription**: Works seamlessly with Svelte's `$` syntax.
11
+ - **Type Safety**: Full TypeScript support.
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install @pulse-js/core @pulse-js/svelte
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### Using Sources
22
+
23
+ Wrap your sources with `usePulse` to get a writable/readable store.
24
+
25
+ ```svelte
26
+ <script lang="ts">
27
+ import { source } from '@pulse-js/core';
28
+ import { usePulse } from '@pulse-js/svelte';
29
+
30
+ const count = source(0);
31
+ const $count = usePulse(count);
32
+ </script>
33
+
34
+ <button on:click={() => count.set(count.value + 1)}>
35
+ Count is {$count}
36
+ </button>
37
+ ```
38
+
39
+ ### Using Guards
40
+
41
+ Wrap guards with `useGuard` to get a readable store containing the status and result.
42
+
43
+ ```svelte
44
+ <script lang="ts">
45
+ import { guard } from '@pulse-js/core';
46
+ import { useGuard } from '@pulse-js/svelte';
47
+
48
+ const isAdult = guard(() => age.value >= 18);
49
+ const $isAdult = useGuard(isAdult);
50
+ </script>
51
+
52
+ {#if $isAdult.status === 'ok'}
53
+ <p>Result: {$isAdult.value}</p>
54
+ {:else if $isAdult.status === 'fail'}
55
+ <p class="error">Error: {$isAdult.reason}</p>
56
+ {/if}
57
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ useGuard: () => useGuard,
24
+ usePulse: () => usePulse
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_store = require("svelte/store");
28
+ function usePulse(unit) {
29
+ 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
+ });
33
+ }
34
+ function useGuard(guard) {
35
+ return usePulse(guard);
36
+ }
37
+ // Annotate the CommonJS export names for ESM import in node:
38
+ 0 && (module.exports = {
39
+ useGuard,
40
+ usePulse
41
+ });
@@ -0,0 +1,11 @@
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 ADDED
@@ -0,0 +1,15 @@
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
+ };
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@pulse-js/svelte",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "dist/index.cjs",
6
+ "module": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "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",
13
+ "lint": "bun x tsc --noEmit"
14
+ },
15
+ "peerDependencies": {
16
+ "svelte": "^4.0.0 || ^5.0.0"
17
+ },
18
+ "dependencies": {
19
+ "@pulse-js/core": "^0.1.8"
20
+ },
21
+ "devDependencies": {
22
+ "svelte": "^5.0.0",
23
+ "tsup": "^8.0.0",
24
+ "typescript": "^5.0.0"
25
+ }
26
+ }