@pulse-js/core 0.1.1 → 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.
- package/README.md +21 -5
- package/dist/index.cjs +324 -0
- package/dist/index.d.cts +414 -0
- package/dist/index.d.ts +414 -0
- package/dist/index.js +290 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,15 +1,31 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
<img width="200" height="200" alt="logo" src="./pulse.svg" />
|
|
4
|
+
|
|
1
5
|
# Pulse
|
|
2
6
|
|
|
3
|
-
A semantic reactivity system for modern applications. Separate reactive data (sources) from business conditions (guards) with a declarative, composable, and observable approach.
|
|
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.
|
|
4
10
|
|
|
5
|
-
|
|
11
|
+
</div>
|
|
6
12
|
|
|
7
13
|
## Installation
|
|
8
14
|
|
|
9
15
|
```bash
|
|
10
|
-
npm install @pulse-js/core @pulse-js/
|
|
11
|
-
|
|
12
|
-
|
|
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
|
|
13
29
|
```
|
|
14
30
|
|
|
15
31
|
## Core Concepts
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
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
|
+
PulseRegistry: () => PulseRegistry,
|
|
24
|
+
evaluate: () => evaluate,
|
|
25
|
+
getCurrentGuard: () => getCurrentGuard,
|
|
26
|
+
guard: () => extendedGuard,
|
|
27
|
+
hydrate: () => hydrate,
|
|
28
|
+
registerGuardForHydration: () => registerGuardForHydration,
|
|
29
|
+
runInContext: () => runInContext,
|
|
30
|
+
source: () => source
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(index_exports);
|
|
33
|
+
|
|
34
|
+
// src/tracking.ts
|
|
35
|
+
var activeGuard = null;
|
|
36
|
+
function runInContext(guard2, fn) {
|
|
37
|
+
const prev = activeGuard;
|
|
38
|
+
activeGuard = guard2;
|
|
39
|
+
try {
|
|
40
|
+
return fn();
|
|
41
|
+
} finally {
|
|
42
|
+
activeGuard = prev;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function getCurrentGuard() {
|
|
46
|
+
return activeGuard;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// src/ssr.ts
|
|
50
|
+
var guardRegistry = /* @__PURE__ */ new Map();
|
|
51
|
+
function registerGuardForHydration(name, guard2) {
|
|
52
|
+
guardRegistry.set(name, guard2);
|
|
53
|
+
}
|
|
54
|
+
async function evaluate(guards) {
|
|
55
|
+
const state = {};
|
|
56
|
+
await Promise.all(guards.map(async (g) => {
|
|
57
|
+
while (g.pending()) {
|
|
58
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
59
|
+
}
|
|
60
|
+
}));
|
|
61
|
+
guards.forEach((g) => {
|
|
62
|
+
const name = g._name;
|
|
63
|
+
if (name) {
|
|
64
|
+
state[name] = g.state();
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
return state;
|
|
68
|
+
}
|
|
69
|
+
function hydrate(state) {
|
|
70
|
+
Object.entries(state).forEach(([name, guardState]) => {
|
|
71
|
+
const g = guardRegistry.get(name);
|
|
72
|
+
if (g && g._hydrate) {
|
|
73
|
+
g._hydrate(guardState);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/guard.ts
|
|
79
|
+
function guard(nameOrFn, fn) {
|
|
80
|
+
const name = typeof nameOrFn === "string" ? nameOrFn : void 0;
|
|
81
|
+
const evaluator = typeof nameOrFn === "function" ? nameOrFn : fn;
|
|
82
|
+
if (!evaluator) {
|
|
83
|
+
throw new Error("Guard requires an evaluator function");
|
|
84
|
+
}
|
|
85
|
+
let state = { status: "pending" };
|
|
86
|
+
const dependents = /* @__PURE__ */ new Set();
|
|
87
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
88
|
+
let evaluationId = 0;
|
|
89
|
+
const node = {
|
|
90
|
+
addDependency(trackable) {
|
|
91
|
+
},
|
|
92
|
+
notify() {
|
|
93
|
+
evaluate2();
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const evaluate2 = () => {
|
|
97
|
+
const currentId = ++evaluationId;
|
|
98
|
+
const oldStatus = state.status;
|
|
99
|
+
const oldValue = state.value;
|
|
100
|
+
try {
|
|
101
|
+
const result = runInContext(node, () => evaluator());
|
|
102
|
+
if (result instanceof Promise) {
|
|
103
|
+
state = { status: "pending" };
|
|
104
|
+
result.then((resolved) => {
|
|
105
|
+
if (currentId === evaluationId) {
|
|
106
|
+
if (resolved === false) {
|
|
107
|
+
state = { status: "fail", reason: name ? `${name} failed` : "condition failed" };
|
|
108
|
+
} else {
|
|
109
|
+
state = { status: "ok", value: resolved };
|
|
110
|
+
}
|
|
111
|
+
notifyDependents();
|
|
112
|
+
}
|
|
113
|
+
}).catch((err) => {
|
|
114
|
+
if (currentId === evaluationId) {
|
|
115
|
+
state = { status: "fail", reason: err instanceof Error ? err.message : String(err) };
|
|
116
|
+
notifyDependents();
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
} else {
|
|
120
|
+
if (result === false) {
|
|
121
|
+
state = { status: "fail", reason: name ? `${name} failed` : "condition failed" };
|
|
122
|
+
} else {
|
|
123
|
+
state = { status: "ok", value: result };
|
|
124
|
+
}
|
|
125
|
+
if (oldStatus !== state.status || oldValue !== state.value) {
|
|
126
|
+
notifyDependents();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
} catch (err) {
|
|
130
|
+
state = { status: "fail", reason: err instanceof Error ? err.message : String(err) };
|
|
131
|
+
notifyDependents();
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
const notifyDependents = () => {
|
|
135
|
+
const deps = Array.from(dependents);
|
|
136
|
+
dependents.clear();
|
|
137
|
+
deps.forEach((dep) => dep.notify());
|
|
138
|
+
subscribers.forEach((sub) => sub({ ...state }));
|
|
139
|
+
};
|
|
140
|
+
evaluate2();
|
|
141
|
+
const handleRead = () => {
|
|
142
|
+
const activeGuard2 = getCurrentGuard();
|
|
143
|
+
if (activeGuard2 && activeGuard2 !== node) {
|
|
144
|
+
dependents.add(activeGuard2);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
const g = (() => {
|
|
148
|
+
handleRead();
|
|
149
|
+
return state.status === "ok" ? state.value : void 0;
|
|
150
|
+
});
|
|
151
|
+
g.ok = () => {
|
|
152
|
+
handleRead();
|
|
153
|
+
return state.status === "ok";
|
|
154
|
+
};
|
|
155
|
+
g.fail = () => {
|
|
156
|
+
handleRead();
|
|
157
|
+
return state.status === "fail";
|
|
158
|
+
};
|
|
159
|
+
g.pending = () => {
|
|
160
|
+
handleRead();
|
|
161
|
+
return state.status === "pending";
|
|
162
|
+
};
|
|
163
|
+
g.reason = () => {
|
|
164
|
+
handleRead();
|
|
165
|
+
return state.reason;
|
|
166
|
+
};
|
|
167
|
+
g.state = () => {
|
|
168
|
+
handleRead();
|
|
169
|
+
return state;
|
|
170
|
+
};
|
|
171
|
+
g.subscribe = (listener) => {
|
|
172
|
+
subscribers.add(listener);
|
|
173
|
+
return () => subscribers.delete(listener);
|
|
174
|
+
};
|
|
175
|
+
g._evaluate = () => evaluate2();
|
|
176
|
+
g._name = name;
|
|
177
|
+
g._hydrate = (newState) => {
|
|
178
|
+
state = newState;
|
|
179
|
+
evaluationId++;
|
|
180
|
+
notifyDependents();
|
|
181
|
+
};
|
|
182
|
+
if (name) {
|
|
183
|
+
registerGuardForHydration(name, g);
|
|
184
|
+
}
|
|
185
|
+
PulseRegistry.register(g);
|
|
186
|
+
return g;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// src/registry.ts
|
|
190
|
+
var Registry = class {
|
|
191
|
+
units = /* @__PURE__ */ new Map();
|
|
192
|
+
listeners = /* @__PURE__ */ new Set();
|
|
193
|
+
/**
|
|
194
|
+
* Registers a new unit (Source or Guard).
|
|
195
|
+
* Uses the unit's name as a key to prevent duplicates during HMR.
|
|
196
|
+
*/
|
|
197
|
+
register(unit) {
|
|
198
|
+
const key = unit._name || unit;
|
|
199
|
+
this.units.set(key, unit);
|
|
200
|
+
this.listeners.forEach((l) => l(unit));
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Retrieves all registered units.
|
|
204
|
+
*/
|
|
205
|
+
getAll() {
|
|
206
|
+
return Array.from(this.units.values());
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Subscribes to new unit registrations.
|
|
210
|
+
*
|
|
211
|
+
* @param listener - Callback receiving the newly registered unit.
|
|
212
|
+
* @returns Unsubscribe function.
|
|
213
|
+
*/
|
|
214
|
+
onRegister(listener) {
|
|
215
|
+
this.listeners.add(listener);
|
|
216
|
+
return () => {
|
|
217
|
+
this.listeners.delete(listener);
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
var PulseRegistry = new Registry();
|
|
222
|
+
|
|
223
|
+
// src/source.ts
|
|
224
|
+
function source(initialValue, options = {}) {
|
|
225
|
+
let value = initialValue;
|
|
226
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
227
|
+
const dependents = /* @__PURE__ */ new Set();
|
|
228
|
+
const s = (() => {
|
|
229
|
+
const activeGuard2 = getCurrentGuard();
|
|
230
|
+
if (activeGuard2) {
|
|
231
|
+
dependents.add(activeGuard2);
|
|
232
|
+
}
|
|
233
|
+
return value;
|
|
234
|
+
});
|
|
235
|
+
s.set = (newValue) => {
|
|
236
|
+
const equals = options.equals || ((a, b) => a === b);
|
|
237
|
+
if (!equals(value, newValue)) {
|
|
238
|
+
value = newValue;
|
|
239
|
+
subscribers.forEach((sub) => sub(value));
|
|
240
|
+
const deps = Array.from(dependents);
|
|
241
|
+
dependents.clear();
|
|
242
|
+
deps.forEach((dep) => dep.notify());
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
s.update = (updater) => {
|
|
246
|
+
s.set(updater(value));
|
|
247
|
+
};
|
|
248
|
+
s.subscribe = (listener) => {
|
|
249
|
+
subscribers.add(listener);
|
|
250
|
+
return () => subscribers.delete(listener);
|
|
251
|
+
};
|
|
252
|
+
s._name = options.name;
|
|
253
|
+
PulseRegistry.register(s);
|
|
254
|
+
return s;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// src/composition.ts
|
|
258
|
+
function isGuard(target) {
|
|
259
|
+
return typeof target === "function" && "ok" in target;
|
|
260
|
+
}
|
|
261
|
+
function guardAll(nameOrGuards, maybeGuards) {
|
|
262
|
+
const name = typeof nameOrGuards === "string" ? nameOrGuards : void 0;
|
|
263
|
+
const guards = Array.isArray(nameOrGuards) ? nameOrGuards : maybeGuards;
|
|
264
|
+
return guard(name, () => {
|
|
265
|
+
let firstFail = null;
|
|
266
|
+
for (const g of guards) {
|
|
267
|
+
if (!g.ok()) {
|
|
268
|
+
if (!firstFail) firstFail = g;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (firstFail) {
|
|
272
|
+
throw new Error(firstFail.reason() || "condition failed");
|
|
273
|
+
}
|
|
274
|
+
return true;
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
function guardAny(nameOrGuards, maybeGuards) {
|
|
278
|
+
const name = typeof nameOrGuards === "string" ? nameOrGuards : void 0;
|
|
279
|
+
const guards = Array.isArray(nameOrGuards) ? nameOrGuards : maybeGuards;
|
|
280
|
+
return guard(name, () => {
|
|
281
|
+
let allFails = [];
|
|
282
|
+
for (const g of guards) {
|
|
283
|
+
if (g.ok()) return true;
|
|
284
|
+
allFails.push(g.reason() || "failed");
|
|
285
|
+
}
|
|
286
|
+
throw new Error(allFails.length > 0 ? allFails.join(" and ") : "no conditions met");
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
function guardNot(nameOrTarget, maybeTarget) {
|
|
290
|
+
const name = typeof nameOrTarget === "string" ? nameOrTarget : void 0;
|
|
291
|
+
const target = typeof nameOrTarget === "string" ? maybeTarget : nameOrTarget;
|
|
292
|
+
return guard(name, () => {
|
|
293
|
+
if (isGuard(target)) {
|
|
294
|
+
return !target.ok();
|
|
295
|
+
}
|
|
296
|
+
return !target();
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
function guardCompute(name, dependencies, processor) {
|
|
300
|
+
return guard(name, () => {
|
|
301
|
+
const values = dependencies.map((dep) => typeof dep === "function" ? dep() : dep);
|
|
302
|
+
return processor(...values);
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
var guardExtensions = {
|
|
306
|
+
all: guardAll,
|
|
307
|
+
any: guardAny,
|
|
308
|
+
not: guardNot,
|
|
309
|
+
compute: guardCompute
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
// src/index.ts
|
|
313
|
+
var extendedGuard = Object.assign(guard, guardExtensions);
|
|
314
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
315
|
+
0 && (module.exports = {
|
|
316
|
+
PulseRegistry,
|
|
317
|
+
evaluate,
|
|
318
|
+
getCurrentGuard,
|
|
319
|
+
guard,
|
|
320
|
+
hydrate,
|
|
321
|
+
registerGuardForHydration,
|
|
322
|
+
runInContext,
|
|
323
|
+
source
|
|
324
|
+
});
|