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