@virentia/effector 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,61 @@
1
+ # @virentia/effector
2
+
3
+ Effector-shaped compatibility layer powered by `@virentia/core`.
4
+
5
+ Use this package when existing code already has an Effector-style shape and you want to run it on Virentia primitives.
6
+
7
+ ## Links
8
+
9
+ - Documentation: [movpushmov.dev/virentia/effector](https://movpushmov.dev/virentia/effector/)
10
+
11
+ ## Install
12
+
13
+ ```sh
14
+ pnpm add @virentia/effector
15
+ ```
16
+
17
+ ## Counter
18
+
19
+ ```ts
20
+ import { allSettled, createEvent, createStore, fork } from "@virentia/effector";
21
+
22
+ const incremented = createEvent<number>();
23
+ const $count = createStore(0).on(incremented, (count, amount) => count + amount);
24
+
25
+ const appScope = fork();
26
+
27
+ await allSettled(incremented, {
28
+ scope: appScope,
29
+ params: 2,
30
+ });
31
+
32
+ console.log(appScope.getState($count)); // 2
33
+ ```
34
+
35
+ ## Effects
36
+
37
+ ```ts
38
+ const loadUserFx = createEffect<string, { id: string; name: string }>(async (id) => {
39
+ const response = await fetch(`/api/users/${id}`);
40
+ return response.json();
41
+ });
42
+
43
+ const result = await allSettled(loadUserFx, {
44
+ scope: appScope,
45
+ params: "user:1",
46
+ });
47
+ ```
48
+
49
+ Effects expose Effector-shaped lifecycle units: `done`, `fail`, `finally`, `doneData`, `failData`, `pending`, and `inFlight`.
50
+
51
+ ## Main API
52
+
53
+ `createEvent`, `createStore`, `createEffect`, `sample`, `combine`, `split`, `createApi`, `restore`, `attach`, `fork`, `allSettled`, `serialize`, `hydrate`, `scopeBind`, `is`.
54
+
55
+ ## Notes
56
+
57
+ The bridge keeps Effector-shaped names around Virentia core primitives. `fork` creates a Virentia scope. `allSettled` uses `params`, while core `allSettled` uses `payload`.
58
+
59
+ ## License
60
+
61
+ MIT © 2026 movpushmov
package/dist/index.cjs ADDED
@@ -0,0 +1,537 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
+ get: ((k) => from[k]).bind(null, key),
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
20
+ value: mod,
21
+ enumerable: true
22
+ }) : target, mod));
23
+ //#endregion
24
+ let _virentia_core = require("@virentia/core");
25
+ _virentia_core = __toESM(_virentia_core, 1);
26
+ //#region lib/types.ts
27
+ const unitKind = Symbol("virentia.effector.unit");
28
+ //#endregion
29
+ //#region lib/guards.ts
30
+ const is = {
31
+ unit(value) {
32
+ return isUnit(value);
33
+ },
34
+ event(value) {
35
+ return isEvent(value);
36
+ },
37
+ store(value) {
38
+ return isStore(value);
39
+ },
40
+ effect(value) {
41
+ return isEffect(value);
42
+ },
43
+ targetable(value) {
44
+ return isTargetable(value);
45
+ }
46
+ };
47
+ function isUnit(value) {
48
+ return Boolean(value && (typeof value === "object" || typeof value === "function") && unitKind in value);
49
+ }
50
+ function isEvent(value) {
51
+ return isUnit(value) && value[unitKind] === "event";
52
+ }
53
+ function isStore(value) {
54
+ return isUnit(value) && value[unitKind] === "store";
55
+ }
56
+ function isEffect(value) {
57
+ return isUnit(value) && value[unitKind] === "effect";
58
+ }
59
+ function isTargetable(value) {
60
+ return isEvent(value) || isEffect(value) || isStore(value) && typeof value.setState === "function";
61
+ }
62
+ function isScope(value) {
63
+ return Boolean(value && typeof value === "object" && "__core" in value && !(unitKind in value));
64
+ }
65
+ function isScopeError(error) {
66
+ return error instanceof Error && error.message === "Scope is required";
67
+ }
68
+ //#endregion
69
+ //#region lib/shared.ts
70
+ const defaultScope = _virentia_core.scope();
71
+ const compatScopes = /* @__PURE__ */ new WeakMap();
72
+ const storesBySid = /* @__PURE__ */ new Map();
73
+ const nativeStoreKeys = new Set([
74
+ "node",
75
+ "writable",
76
+ "subscribe",
77
+ "map",
78
+ "filter",
79
+ "filterMap"
80
+ ]);
81
+ function createCompatScope(scope) {
82
+ const existing = compatScopes.get(scope);
83
+ if (existing) return existing;
84
+ const compatScope = {
85
+ __core: scope,
86
+ __changedSids: /* @__PURE__ */ new Set(),
87
+ getState(store) {
88
+ return store.getState(this);
89
+ }
90
+ };
91
+ compatScopes.set(scope, compatScope);
92
+ return compatScope;
93
+ }
94
+ function inScope(scope, fn) {
95
+ if (scope) return _virentia_core.scoped(scope.__core, fn);
96
+ try {
97
+ return fn();
98
+ } catch (error) {
99
+ if (!isScopeError(error)) throw error;
100
+ return _virentia_core.scoped(defaultScope, fn);
101
+ }
102
+ }
103
+ function callWithFallback(unit) {
104
+ return ((...args) => {
105
+ try {
106
+ return unit(...args).catch((error) => {
107
+ if (!isScopeError(error)) throw error;
108
+ return _virentia_core.scoped(defaultScope, () => unit(...args));
109
+ });
110
+ } catch (error) {
111
+ if (!isScopeError(error)) throw error;
112
+ return _virentia_core.scoped(defaultScope, () => unit(...args));
113
+ }
114
+ });
115
+ }
116
+ function readSource(source) {
117
+ if (isStore(source)) return source.getState();
118
+ if (Array.isArray(source)) return source.map((store) => store.getState());
119
+ return Object.fromEntries(Object.entries(source).map(([key, store]) => [key, store.getState()]));
120
+ }
121
+ function sourceToClock(source) {
122
+ if (!source) throw new Error("sample: clock or source is required");
123
+ if (isStore(source)) return source;
124
+ return Object.values(source);
125
+ }
126
+ function passesFilter(filter, source, clock) {
127
+ if (!filter) return true;
128
+ if (isStore(filter)) return filter.getState();
129
+ return filter(source, clock);
130
+ }
131
+ function launchTarget(target, payload) {
132
+ for (const unit of toArray(target)) if (isStore(unit)) unit.setState(payload);
133
+ else unit(payload);
134
+ }
135
+ function computeCombined(shape, fn) {
136
+ const value = readSource(shape);
137
+ return fn ? fn(value) : value;
138
+ }
139
+ function registerStore(store) {
140
+ if (store.sid) storesBySid.set(store.sid, store);
141
+ }
142
+ function markScopeChanged(scope, sid) {
143
+ if (sid) createCompatScope(scope ?? defaultScope).__changedSids.add(sid);
144
+ }
145
+ function applyStoreValues(scope, values) {
146
+ if (Array.isArray(values)) {
147
+ for (const [store, value] of values) store.setState(value, scope);
148
+ return;
149
+ }
150
+ if (values instanceof Map) {
151
+ for (const [store, value] of values) applyStoreValue(scope, store, value);
152
+ return;
153
+ }
154
+ for (const [sid, value] of Object.entries(values)) applyStoreValue(scope, sid, value);
155
+ }
156
+ function getName(input) {
157
+ if (typeof input === "string") return input;
158
+ if (input && (typeof input === "object" || typeof input === "function") && "shortName" in input) return String(input.shortName ?? "unit");
159
+ if (input && (typeof input === "object" || typeof input === "function") && "name" in input) return String(input.name ?? "unit");
160
+ return "unit";
161
+ }
162
+ function toArray(value) {
163
+ return Array.isArray(value) ? value : [value];
164
+ }
165
+ function applyStoreValue(scope, storeOrSid, value) {
166
+ if (typeof storeOrSid === "string") {
167
+ storesBySid.get(storeOrSid)?.setState(value, scope);
168
+ return;
169
+ }
170
+ storeOrSid.setState(value, scope);
171
+ }
172
+ //#endregion
173
+ //#region lib/store.ts
174
+ function createStore(defaultState, config) {
175
+ const box = _virentia_core.store({ value: defaultState });
176
+ const updates = createEvent({ name: `${config?.name ?? "store"} updates` });
177
+ const store = createStoreFromBox(box, updates, defaultState, config?.name, config?.sid);
178
+ registerStore(store);
179
+ box.subscribe((next, scope) => {
180
+ markScopeChanged(scope, store.sid);
181
+ _virentia_core.run({
182
+ unit: updates.__core.node,
183
+ payload: next.value,
184
+ scope
185
+ });
186
+ });
187
+ return store;
188
+ }
189
+ function createStoreFromBox(box, updates, defaultState, name = "store", sid) {
190
+ const result = {
191
+ [unitKind]: "store",
192
+ __box: box,
193
+ __core: updates,
194
+ node: updates.__core.node,
195
+ shortName: name,
196
+ sid,
197
+ defaultState,
198
+ updates,
199
+ getType: () => name,
200
+ getState(scope) {
201
+ return readBox(box, scope);
202
+ },
203
+ setState(value, scope) {
204
+ writeBox(box, value, scope);
205
+ },
206
+ watch(fn) {
207
+ fn(readBox(box));
208
+ return box.subscribe((next) => {
209
+ fn(next.value);
210
+ });
211
+ },
212
+ map(fn) {
213
+ const mapped = createStore(fn(result.getState()));
214
+ _virentia_core.scoped(defaultScope, () => {
215
+ _virentia_core.reaction(() => {
216
+ mapped.setState(fn(result.getState()));
217
+ });
218
+ });
219
+ return mapped;
220
+ },
221
+ on(trigger, reducer) {
222
+ _virentia_core.reaction({
223
+ on: trigger,
224
+ run: (payload) => {
225
+ result.setState(reducer(result.getState(), payload));
226
+ }
227
+ });
228
+ return result;
229
+ },
230
+ reset(trigger) {
231
+ for (const unit of toArray(trigger)) _virentia_core.reaction({
232
+ on: unit,
233
+ run: () => {
234
+ result.setState(defaultState);
235
+ }
236
+ });
237
+ return result;
238
+ }
239
+ };
240
+ return result;
241
+ }
242
+ function wrapNativeStore(store, defaultState, name) {
243
+ const updates = createEvent(`${name}.updates`);
244
+ const result = {
245
+ [unitKind]: "store",
246
+ __core: updates,
247
+ node: updates.__core.node,
248
+ shortName: name,
249
+ defaultState,
250
+ updates,
251
+ getType: () => name,
252
+ getState(scope) {
253
+ return readNativeStore(store, scope);
254
+ },
255
+ watch(fn) {
256
+ fn(readNativeStore(store));
257
+ return store.subscribe((next) => {
258
+ fn(next);
259
+ });
260
+ },
261
+ map(fn) {
262
+ const mapped = createStore(fn(result.getState()));
263
+ _virentia_core.scoped(defaultScope, () => {
264
+ _virentia_core.reaction(() => {
265
+ mapped.setState(fn(result.getState()));
266
+ });
267
+ });
268
+ return mapped;
269
+ },
270
+ on() {
271
+ throw new Error("Store is read-only");
272
+ },
273
+ reset() {
274
+ throw new Error("Store is read-only");
275
+ }
276
+ };
277
+ store.subscribe((next, scope) => {
278
+ _virentia_core.run({
279
+ unit: updates.__core.node,
280
+ payload: next,
281
+ scope
282
+ });
283
+ });
284
+ return result;
285
+ }
286
+ function readBox(box, scope) {
287
+ return inScope(scope, () => box.value);
288
+ }
289
+ function writeBox(box, value, scope) {
290
+ inScope(scope, () => {
291
+ box.value = value;
292
+ });
293
+ }
294
+ function readNativeStore(store, scope) {
295
+ return inScope(scope, () => {
296
+ const keys = Reflect.ownKeys(store).filter((key) => !nativeStoreKeys.has(key));
297
+ if (keys.length === 1 && keys[0] === "value") return Reflect.get(store, "value");
298
+ return Object.fromEntries(keys.map((key) => [key, Reflect.get(store, key)]));
299
+ });
300
+ }
301
+ //#endregion
302
+ //#region lib/operators.ts
303
+ function sample(config) {
304
+ const target = config.target ?? createEvent();
305
+ const clocks = toArray(config.clock ?? sourceToClock(config.source));
306
+ for (const clock of clocks) _virentia_core.reaction({
307
+ on: clock,
308
+ run: (clockPayload) => {
309
+ const sourceValue = config.source === void 0 ? void 0 : readSource(config.source);
310
+ if (!passesFilter(config.filter, sourceValue, clockPayload)) return;
311
+ launchTarget(target, config.fn ? config.source === void 0 ? config.fn(clockPayload, clockPayload) : config.fn(sourceValue, clockPayload) : config.source === void 0 ? clockPayload : sourceValue);
312
+ }
313
+ });
314
+ return Array.isArray(target) ? target[0] : target;
315
+ }
316
+ function combine(...args) {
317
+ const fn = typeof args[args.length - 1] === "function" ? args.pop() : void 0;
318
+ const shape = args.length === 1 ? args[0] : args;
319
+ const result = createStore(computeCombined(shape, fn));
320
+ _virentia_core.scoped(defaultScope, () => {
321
+ _virentia_core.reaction(() => {
322
+ result.setState(computeCombined(shape, fn));
323
+ });
324
+ });
325
+ return result;
326
+ }
327
+ function split(sourceOrConfig, maybeCases) {
328
+ const source = "source" in sourceOrConfig ? sourceOrConfig.source : sourceOrConfig;
329
+ const match = "source" in sourceOrConfig ? sourceOrConfig.match : maybeCases ?? {};
330
+ const result = {};
331
+ if (typeof match === "function") {
332
+ const configuredCases = "source" in sourceOrConfig ? sourceOrConfig.cases ?? {} : {};
333
+ for (const key of Object.keys(configuredCases)) result[key] = configuredCases[key] ?? createEvent();
334
+ } else for (const key of Object.keys(match)) result[key] = createEvent();
335
+ result.__ = createEvent();
336
+ _virentia_core.reaction({
337
+ on: source,
338
+ run: (payload) => {
339
+ launchTarget(result[(typeof match === "function" ? match(payload) : Object.keys(match).find((caseName) => match[caseName](payload))) ?? "__"] ?? result.__, payload);
340
+ }
341
+ });
342
+ return result;
343
+ }
344
+ function createApi(store, reducers) {
345
+ const result = {};
346
+ for (const key of Object.keys(reducers)) {
347
+ const event = createEvent(String(key));
348
+ store.on(event, reducers[key]);
349
+ result[key] = event;
350
+ }
351
+ return result;
352
+ }
353
+ function restore(unit, defaultState) {
354
+ const store = createStore(defaultState);
355
+ const source = isEffect(unit) ? unit.doneData : unit;
356
+ store.on(source, (_, payload) => payload);
357
+ return store;
358
+ }
359
+ //#endregion
360
+ //#region lib/watch.ts
361
+ function watchUnit(unit, fn) {
362
+ const subscription = _virentia_core.reaction({
363
+ on: unit,
364
+ run: fn
365
+ });
366
+ return () => subscription.stop();
367
+ }
368
+ //#endregion
369
+ //#region lib/event.ts
370
+ function createEvent(nameOrConfig) {
371
+ const name = typeof nameOrConfig === "string" ? nameOrConfig : nameOrConfig?.name ?? "unit";
372
+ return wrapEvent(_virentia_core.event(), name);
373
+ }
374
+ function wrapEvent(event, name = "event") {
375
+ const result = callWithFallback(typeof event === "function" ? event : ((...payload) => _virentia_core.allSettled(event, { payload: payload[0] })));
376
+ Object.assign(result, {
377
+ [unitKind]: "event",
378
+ __core: event,
379
+ node: event.node,
380
+ shortName: name,
381
+ getType: () => name,
382
+ watch(fn) {
383
+ return watchUnit(result, fn);
384
+ },
385
+ map(fn) {
386
+ return wrapEvent(event.map(fn));
387
+ },
388
+ filter(config) {
389
+ const fn = typeof config === "function" ? config : config.fn;
390
+ return wrapEvent(event.filter(fn));
391
+ },
392
+ filterMap(fn) {
393
+ return wrapEvent(event.filterMap(fn));
394
+ },
395
+ prepend(fn) {
396
+ const prepended = createEvent();
397
+ sample({
398
+ clock: prepended,
399
+ fn,
400
+ target: result
401
+ });
402
+ return prepended;
403
+ }
404
+ });
405
+ return result;
406
+ }
407
+ //#endregion
408
+ //#region lib/effect.ts
409
+ function createEffect(handlerOrConfig) {
410
+ let handler = typeof handlerOrConfig === "function" ? handlerOrConfig : handlerOrConfig?.handler;
411
+ const effectName = getName(handlerOrConfig);
412
+ const fx = _virentia_core.effect((params) => {
413
+ if (!handler) throw new Error("Effect handler is not defined");
414
+ return handler(params);
415
+ });
416
+ const result = callWithFallback(((...params) => fx(...params)));
417
+ Object.assign(result, {
418
+ [unitKind]: "effect",
419
+ __core: fx,
420
+ node: fx.node,
421
+ shortName: effectName,
422
+ getType: () => effectName,
423
+ watch(fn) {
424
+ return watchUnit(wrapEvent(fx.started), fn);
425
+ },
426
+ done: wrapEvent(fx.done),
427
+ fail: wrapEvent(fx.fail),
428
+ finally: wrapEvent(fx.finally),
429
+ doneData: wrapEvent(fx.doneData),
430
+ failData: wrapEvent(fx.failData),
431
+ pending: wrapNativeStore(fx.$pending, false, `${effectName}.pending`),
432
+ inFlight: wrapNativeStore(fx.$inFlight, 0, `${effectName}.inFlight`),
433
+ prepend(fn) {
434
+ const prepended = createEvent();
435
+ sample({
436
+ clock: prepended,
437
+ fn,
438
+ target: result
439
+ });
440
+ return prepended;
441
+ }
442
+ });
443
+ const use = ((nextHandler) => {
444
+ handler = nextHandler;
445
+ return result;
446
+ });
447
+ use.getCurrent = () => {
448
+ if (!handler) throw new Error("Effect handler is not defined");
449
+ return handler;
450
+ };
451
+ result.use = use;
452
+ return result;
453
+ }
454
+ //#endregion
455
+ //#region lib/attach.ts
456
+ function attach(config) {
457
+ return createEffect({
458
+ name: config.name ?? getName(config.effect),
459
+ handler: (params) => {
460
+ const hasSource = config.source !== void 0;
461
+ const sourceValue = hasSource ? readSource(config.source) : void 0;
462
+ if (isEffect(config.effect)) {
463
+ const nextParams = config.mapParams ? config.mapParams(params, sourceValue) : params;
464
+ return config.effect.use.getCurrent()(nextParams);
465
+ }
466
+ return hasSource ? config.effect(sourceValue, params) : config.effect(params);
467
+ }
468
+ });
469
+ }
470
+ //#endregion
471
+ //#region lib/persistence.ts
472
+ function serialize(scope, config = {}) {
473
+ const compatScope = createCompatScope(scope.__core);
474
+ const onlyChanges = config.onlyChanges ?? true;
475
+ const ignored = new Set((config.ignore ?? []).map((item) => typeof item === "string" ? item : item.sid).filter((sid) => typeof sid === "string"));
476
+ const result = {};
477
+ for (const [sid, store] of storesBySid) {
478
+ if (ignored.has(sid)) continue;
479
+ if (onlyChanges && !compatScope.__changedSids.has(sid)) continue;
480
+ result[sid] = store.getState(compatScope);
481
+ }
482
+ return result;
483
+ }
484
+ function hydrate(scope, config) {
485
+ applyStoreValues(scope, config.values);
486
+ }
487
+ //#endregion
488
+ //#region lib/scope.ts
489
+ function fork(config) {
490
+ const nextScope = createCompatScope(_virentia_core.scope());
491
+ if (config?.values) hydrate(nextScope, { values: config.values });
492
+ return nextScope;
493
+ }
494
+ async function allSettled(unitOrScope, options = {}) {
495
+ if (isScope(unitOrScope)) return;
496
+ const scope = options.scope ?? createCompatScope(defaultScope);
497
+ const unit = unitOrScope;
498
+ if (isStore(unit)) {
499
+ unit.setState(options.params, scope);
500
+ return;
501
+ }
502
+ if (isEffect(unit)) try {
503
+ return {
504
+ status: "done",
505
+ value: await _virentia_core.scoped(scope.__core, () => unit(options.params))
506
+ };
507
+ } catch (value) {
508
+ return {
509
+ status: "fail",
510
+ value
511
+ };
512
+ }
513
+ await _virentia_core.allSettled(unit.__core, {
514
+ scope: scope.__core,
515
+ payload: options.params
516
+ });
517
+ }
518
+ function scopeBind(unit, config) {
519
+ const scope = config?.scope ?? createCompatScope(defaultScope);
520
+ return (...payload) => _virentia_core.scoped(scope.__core, () => unit(...payload));
521
+ }
522
+ //#endregion
523
+ exports.allSettled = allSettled;
524
+ exports.attach = attach;
525
+ exports.combine = combine;
526
+ exports.createApi = createApi;
527
+ exports.createEffect = createEffect;
528
+ exports.createEvent = createEvent;
529
+ exports.createStore = createStore;
530
+ exports.fork = fork;
531
+ exports.hydrate = hydrate;
532
+ exports.is = is;
533
+ exports.restore = restore;
534
+ exports.sample = sample;
535
+ exports.scopeBind = scopeBind;
536
+ exports.serialize = serialize;
537
+ exports.split = split;