@xstate/effect 0.1.0-alpha.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.
@@ -0,0 +1,2 @@
1
+ export * from "./declarations/src/atom.js";
2
+ //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoieHN0YXRlLWVmZmVjdC1hdG9tLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuL2RlY2xhcmF0aW9ucy9zcmMvYXRvbS5kLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=
@@ -0,0 +1,88 @@
1
+ import { Cause } from 'effect';
2
+ import { Atom, AsyncResult } from 'effect/unstable/reactivity';
3
+ import { c as createEffectActor, N as NotReadyError, t as taggedState } from './state-9a718be3.js';
4
+ export { N as NotReadyError } from './state-9a718be3.js';
5
+ import 'xstate';
6
+ import 'xstate/durable';
7
+
8
+ /**
9
+ * Atoms that expose one actor to a reactive UI through
10
+ * `effect/unstable/reactivity`. Read them with the atom bindings for your
11
+ * framework, such as `@effect/atom-react`.
12
+ */
13
+
14
+ /**
15
+ * Creates atoms for an actor that runs in an `Atom.runtime`. The runtime's
16
+ * Layer must provide every service the logic requires; a missing service is
17
+ * a type error on the `runtime` argument.
18
+ *
19
+ * The actor is created with `createEffectActor` inside the `actor` atom's
20
+ * scope, so its lifetime follows the atom: it starts on first read and stops
21
+ * when the atom is released. Wrap the atoms with `Atom.keepAlive` to pin the
22
+ * actor for the registry's lifetime, or with `Atom.family` to create one
23
+ * actor per input.
24
+ */
25
+ function createActorAtoms(runtime, logic, options) {
26
+ const host = runtime;
27
+ const actor = host.atom(createEffectActor(logic, options));
28
+ const snapshot = Atom.make(get => {
29
+ const result = get(actor);
30
+ if (!AsyncResult.isSuccess(result)) {
31
+ return AsyncResult.map(result, running => running.getSnapshot());
32
+ }
33
+ const running = result.value;
34
+ const publish = () => {
35
+ get.setSelf(AsyncResult.success(running.getSnapshot()));
36
+ };
37
+ // The atom is the consumer of the actor's error: `result` reports it,
38
+ // so the observer handles `error` and XState does not report it as
39
+ // unhandled.
40
+ const subscription = running.subscribe({
41
+ next: publish,
42
+ error: publish,
43
+ complete: publish
44
+ });
45
+ get.addFinalizer(() => {
46
+ subscription.unsubscribe();
47
+ });
48
+ return AsyncResult.success(running.getSnapshot());
49
+ });
50
+ const result = Atom.make(get => {
51
+ const current = get(snapshot);
52
+ if (!AsyncResult.isSuccess(current)) {
53
+ return current;
54
+ }
55
+ const value = current.value;
56
+ if (value.status === 'error') {
57
+ return AsyncResult.failureWithPrevious(Cause.fail(value.error), {
58
+ previous: get.self()
59
+ });
60
+ }
61
+ return current;
62
+ });
63
+ const sendAtom = Atom.writable(get => AsyncResult.map(get(actor), () => undefined), (ctx, event) => {
64
+ const current = ctx.get(actor);
65
+ if (AsyncResult.isInitial(current)) {
66
+ ctx.setSelf(AsyncResult.failure(Cause.fail(new NotReadyError())));
67
+ } else if (AsyncResult.isFailure(current)) {
68
+ ctx.setSelf(AsyncResult.map(current, () => undefined));
69
+ } else {
70
+ current.value.send(event);
71
+ ctx.setSelf(AsyncResult.success(undefined));
72
+ }
73
+ });
74
+ function select(selector) {
75
+ return Atom.map(snapshot, result => AsyncResult.map(result, selector));
76
+ }
77
+ const state = select(current => taggedState(current));
78
+ return {
79
+ actor,
80
+ snapshot,
81
+ result,
82
+ send: sendAtom,
83
+ select,
84
+ state
85
+ };
86
+ }
87
+
88
+ export { createActorAtoms };
@@ -0,0 +1,2 @@
1
+ export * from "./declarations/src/index.js";
2
+ //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoieHN0YXRlLWVmZmVjdC5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi9kZWNsYXJhdGlvbnMvc3JjL2luZGV4LmQudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEifQ==