@pulse-js/core 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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 ZtaMDev
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
1
+ MIT License
2
+
3
+ Copyright (c) 2025 ZtaMDev
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
21
  SOFTWARE.
package/README.md CHANGED
@@ -1,172 +1,172 @@
1
- # Pulse
2
-
3
- A semantic reactivity system for modern applications. Separate reactive data (sources) from business conditions (guards) with a declarative, composable, and observable approach.
4
-
5
- Pulse differs from traditional signals or state managers by treating "Conditions" as first-class citizens. Instead of embedding complex boolean logic inside components or selectors, you define semantic **Guards** that can be observed, composed, and debugged independently.
6
-
7
- ## Installation
8
-
9
- ```bash
10
- npm install @pulse-js/core @pulse-js/react @pulse-js/devtools
11
- # or
12
- bun add @pulse-js/core @pulse-js/react @pulse-js/devtools
13
- ```
14
-
15
- ## Core Concepts
16
-
17
- ### Sources (Refined Data)
18
-
19
- Sources are the primitive containers for your application state. They hold values and notify dependents when those values change.
20
-
21
- ```typescript
22
- import { source } from "@pulse-js/core";
23
-
24
- // Create a source
25
- const user = source({ name: "Alice", id: 1 });
26
- const rawCount = source(0);
27
-
28
- // Read the value (dependencies are tracked automatically if called inside a Guard)
29
- console.log(user());
30
-
31
- // Update the value
32
- user.set({ name: "Bob", id: 1 });
33
-
34
- // Update using a callback
35
- rawCount.update((n) => n + 1);
36
- ```
37
-
38
- ### Guards (Semantic Logic)
39
-
40
- Guards represent business rules or derivations. They are not just boolean flags; they track their own state including `status` (ok, fail, pending) and `reason` (why it failed).
41
-
42
- ```typescript
43
- import { guard } from "@pulse-js/core";
44
-
45
- // Synchronous Guard
46
- const isAdmin = guard("is-admin", () => {
47
- const u = user();
48
- if (u.role !== "admin") return false; // Implicitly sets status to 'fail'
49
- return true;
50
- });
51
-
52
- // Guards can be checked explicitly
53
- if (isAdmin.ok()) {
54
- // Grant access
55
- } else {
56
- console.log(isAdmin.reason()); // e.g. "is-admin failed"
57
- }
58
- ```
59
-
60
- ### Async Guards
61
-
62
- Pulse handles asynchronous logic natively. Guards can return Promises, and their status will automatically transition from `pending` to `ok` or `fail`.
63
-
64
- ```typescript
65
- const isServerOnline = guard("check-server", async () => {
66
- const response = await fetch("/health");
67
- if (!response.ok) throw new Error("Server unreachable");
68
- return true;
69
- });
70
-
71
- // Check status synchronously non-blocking
72
- if (isServerOnline.pending()) {
73
- showSpinner();
74
- }
75
- ```
76
-
77
- ### Composition
78
-
79
- Guards can be composed using logical operators. This creates a semantic tree of conditions that is easy to debug.
80
-
81
- ```typescript
82
- import { guard } from "@pulse-js/core";
83
-
84
- // .all() - Success only if ALL pass. Fails with the reason of the first failure.
85
- const canCheckout = guard.all("can-checkout", [
86
- isAuthenticated,
87
- hasItemsInCart,
88
- isServerOnline,
89
- ]);
90
-
91
- // .any() - Success if AT LEAST ONE passes.
92
- const hasAccess = guard.any("has-access", [isAdmin, isEditor]);
93
-
94
- // .not() - Inverts the logical result.
95
- const isGuest = guard.not("is-guest", isAuthenticated);
96
- ```
97
-
98
- ### Computed Values
99
-
100
- You can derive new data from sources or other guards using `guard.compute`.
101
-
102
- ```typescript
103
- const fullName = guard.compute(
104
- "full-name",
105
- [firstName, lastName],
106
- (first, last) => {
107
- return `${first} ${last}`;
108
- }
109
- );
110
- ```
111
-
112
- ## Server-Side Rendering (SSR)
113
-
114
- Pulse is designed with SSR in mind. It supports isomorphic rendering where async guards can be evaluated on the server, their state serialized, and then hydrated on the client.
115
-
116
- ### Server Side
117
-
118
- ```typescript
119
- import { evaluate } from "@pulse-js/core";
120
-
121
- // 1. Evaluate critical guards on the server
122
- const hydrationState = await evaluate([isUserAuthenticated, appSettings]);
123
-
124
- // 2. Serialize this state into your HTML
125
- const html = `
126
- <script>window.__PULSE_STATE__ = ${JSON.stringify(hydrationState)}</script>
127
- `;
128
- ```
129
-
130
- ### Client Side (Hydration)
131
-
132
- ```typescript
133
- import { hydrate } from "@pulse-js/core";
134
-
135
- // 1. Hydrate before rendering
136
- hydrate(window.__PULSE_STATE__);
137
- ```
138
-
139
- ## API Reference
140
-
141
- ### `source<T>(initialValue: T, options?: SourceOptions)`
142
-
143
- Creates a reactive source.
144
-
145
- - `options.name`: Unique string name (highly recommended for debugging).
146
- - `options.equals`: Custom equality function `(prev, next) => boolean`.
147
-
148
- Methods:
149
-
150
- - `.set(value: T)`: Updates the value.
151
- - `.update(fn: (current: T) => T)`: Updates value using a transform.
152
- - `.subscribe(fn: (value: T) => void)`: Manual subscription.
153
-
154
- ### `guard<T>(name: string, evaluator: () => T | Promise<T>)`
155
-
156
- Creates a semantic guard.
157
-
158
- Methods:
159
-
160
- - `.ok()`: Returns true if status is 'ok'.
161
- - `.fail()`: Returns true if status is 'fail'.
162
- - `.pending()`: Returns true if evaluating async.
163
- - `.reason()`: Returns the failure message.
164
- - `.state()`: Returns full `{ status, value, reason }` object.
165
- - `.subscribe(fn: (state: GuardState) => void)`: Manual subscription.
166
-
167
- ---
168
-
169
- ## Ecosystem
170
-
171
- - **@pulse-js/react**: React bindings and hooks.
172
- - **@pulse-js/devtools**: Visual debugging tools.
1
+ # Pulse
2
+
3
+ A semantic reactivity system for modern applications. Separate reactive data (sources) from business conditions (guards) with a declarative, composable, and observable approach.
4
+
5
+ Pulse differs from traditional signals or state managers by treating "Conditions" as first-class citizens. Instead of embedding complex boolean logic inside components or selectors, you define semantic **Guards** that can be observed, composed, and debugged independently.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @pulse-js/core @pulse-js/react @pulse-js/tools
11
+ # or
12
+ bun add @pulse-js/core @pulse-js/react @pulse-js/tools
13
+ ```
14
+
15
+ ## Core Concepts
16
+
17
+ ### Sources (Refined Data)
18
+
19
+ Sources are the primitive containers for your application state. They hold values and notify dependents when those values change.
20
+
21
+ ```typescript
22
+ import { source } from "@pulse-js/core";
23
+
24
+ // Create a source
25
+ const user = source({ name: "Alice", id: 1 });
26
+ const rawCount = source(0);
27
+
28
+ // Read the value (dependencies are tracked automatically if called inside a Guard)
29
+ console.log(user());
30
+
31
+ // Update the value
32
+ user.set({ name: "Bob", id: 1 });
33
+
34
+ // Update using a callback
35
+ rawCount.update((n) => n + 1);
36
+ ```
37
+
38
+ ### Guards (Semantic Logic)
39
+
40
+ Guards represent business rules or derivations. They are not just boolean flags; they track their own state including `status` (ok, fail, pending) and `reason` (why it failed).
41
+
42
+ ```typescript
43
+ import { guard } from "@pulse-js/core";
44
+
45
+ // Synchronous Guard
46
+ const isAdmin = guard("is-admin", () => {
47
+ const u = user();
48
+ if (u.role !== "admin") return false; // Implicitly sets status to 'fail'
49
+ return true;
50
+ });
51
+
52
+ // Guards can be checked explicitly
53
+ if (isAdmin.ok()) {
54
+ // Grant access
55
+ } else {
56
+ console.log(isAdmin.reason()); // e.g. "is-admin failed"
57
+ }
58
+ ```
59
+
60
+ ### Async Guards
61
+
62
+ Pulse handles asynchronous logic natively. Guards can return Promises, and their status will automatically transition from `pending` to `ok` or `fail`.
63
+
64
+ ```typescript
65
+ const isServerOnline = guard("check-server", async () => {
66
+ const response = await fetch("/health");
67
+ if (!response.ok) throw new Error("Server unreachable");
68
+ return true;
69
+ });
70
+
71
+ // Check status synchronously non-blocking
72
+ if (isServerOnline.pending()) {
73
+ showSpinner();
74
+ }
75
+ ```
76
+
77
+ ### Composition
78
+
79
+ Guards can be composed using logical operators. This creates a semantic tree of conditions that is easy to debug.
80
+
81
+ ```typescript
82
+ import { guard } from "@pulse-js/core";
83
+
84
+ // .all() - Success only if ALL pass. Fails with the reason of the first failure.
85
+ const canCheckout = guard.all("can-checkout", [
86
+ isAuthenticated,
87
+ hasItemsInCart,
88
+ isServerOnline,
89
+ ]);
90
+
91
+ // .any() - Success if AT LEAST ONE passes.
92
+ const hasAccess = guard.any("has-access", [isAdmin, isEditor]);
93
+
94
+ // .not() - Inverts the logical result.
95
+ const isGuest = guard.not("is-guest", isAuthenticated);
96
+ ```
97
+
98
+ ### Computed Values
99
+
100
+ You can derive new data from sources or other guards using `guard.compute`.
101
+
102
+ ```typescript
103
+ const fullName = guard.compute(
104
+ "full-name",
105
+ [firstName, lastName],
106
+ (first, last) => {
107
+ return `${first} ${last}`;
108
+ }
109
+ );
110
+ ```
111
+
112
+ ## Server-Side Rendering (SSR)
113
+
114
+ Pulse is designed with SSR in mind. It supports isomorphic rendering where async guards can be evaluated on the server, their state serialized, and then hydrated on the client.
115
+
116
+ ### Server Side
117
+
118
+ ```typescript
119
+ import { evaluate } from "@pulse-js/core";
120
+
121
+ // 1. Evaluate critical guards on the server
122
+ const hydrationState = await evaluate([isUserAuthenticated, appSettings]);
123
+
124
+ // 2. Serialize this state into your HTML
125
+ const html = `
126
+ <script>window.__PULSE_STATE__ = ${JSON.stringify(hydrationState)}</script>
127
+ `;
128
+ ```
129
+
130
+ ### Client Side (Hydration)
131
+
132
+ ```typescript
133
+ import { hydrate } from "@pulse-js/core";
134
+
135
+ // 1. Hydrate before rendering
136
+ hydrate(window.__PULSE_STATE__);
137
+ ```
138
+
139
+ ## API Reference
140
+
141
+ ### `source<T>(initialValue: T, options?: SourceOptions)`
142
+
143
+ Creates a reactive source.
144
+
145
+ - `options.name`: Unique string name (highly recommended for debugging).
146
+ - `options.equals`: Custom equality function `(prev, next) => boolean`.
147
+
148
+ Methods:
149
+
150
+ - `.set(value: T)`: Updates the value.
151
+ - `.update(fn: (current: T) => T)`: Updates value using a transform.
152
+ - `.subscribe(fn: (value: T) => void)`: Manual subscription.
153
+
154
+ ### `guard<T>(name: string, evaluator: () => T | Promise<T>)`
155
+
156
+ Creates a semantic guard.
157
+
158
+ Methods:
159
+
160
+ - `.ok()`: Returns true if status is 'ok'.
161
+ - `.fail()`: Returns true if status is 'fail'.
162
+ - `.pending()`: Returns true if evaluating async.
163
+ - `.reason()`: Returns the failure message.
164
+ - `.state()`: Returns full `{ status, value, reason }` object.
165
+ - `.subscribe(fn: (state: GuardState) => void)`: Manual subscription.
166
+
167
+ ---
168
+
169
+ ## Ecosystem
170
+
171
+ - **@pulse-js/react**: React bindings and hooks.
172
+ - **@pulse-js/tools**: Visual debugging tools.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pulse-js/core",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "module": "dist/index.js",
5
5
  "main": "dist/index.cjs",
6
6
  "types": "dist/index.d.ts",
@@ -19,7 +19,8 @@
19
19
  "guards",
20
20
  "declarative",
21
21
  "ssr",
22
- "devtools"
22
+ "devtools",
23
+ "tools"
23
24
  ],
24
25
  "author": "ZtaMDev",
25
26
  "repository": {