oxidejs 0.3.2 → 0.3.3

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/dist/index.mjs CHANGED
@@ -1,36 +1,55 @@
1
- import { a as useEnv, i as useCtx, l as withRequestStore, o as useFetchCtx, s as useRequest, t as getRequestStore } from "./context-DQDDwFYi.mjs";
1
+ import { a as useEnv, i as useCtx, l as withRequestStore, o as useFetchCtx, s as useRequest, t as getRequestStore } from "./context-DUN_VWJF.mjs";
2
2
  import * as Effect from "effect/Effect";
3
3
  import * as Atom from "effect/unstable/reactivity/Atom";
4
4
  import * as Registry from "effect/unstable/reactivity/AtomRegistry";
5
5
  //#region src/action.ts
6
6
  const ACTION_CALL = Symbol.for("ilha.actionCall");
7
7
  const ACTION_KEY = Symbol.for("oxidejs.actionKey");
8
- const AsyncGeneratorFunction = Object.getPrototypeOf(async function* () {}).constructor;
8
+ const AsyncGeneratorFunction = Object.getPrototypeOf(async function* asyncGeneratorProbe() {
9
+ yield;
10
+ }).constructor;
9
11
  let defaultRegistry;
10
- function registry() {
12
+ const registry = function registry() {
11
13
  return defaultRegistry ??= Registry.make();
12
- }
13
- function isAsyncGeneratorFunction(fn) {
14
- return typeof fn === "function" && fn instanceof AsyncGeneratorFunction;
15
- }
16
- function splitCallOptions(args) {
17
- if (args.length === 0) return { args: [] };
18
- const last = args[args.length - 1];
19
- if (last && typeof last === "object" && "signal" in last && last.signal instanceof AbortSignal && Object.keys(last).length === 1) return {
20
- args: args.slice(0, -1),
21
- options: last
14
+ };
15
+ const isAsyncGeneratorFunction = function isAsyncGeneratorFunction(fn) {
16
+ return fn instanceof AsyncGeneratorFunction;
17
+ };
18
+ const emptyArgs = function emptyArgs() {
19
+ return [];
20
+ };
21
+ const asArgs = function asArgs(value) {
22
+ return value;
23
+ };
24
+ const readActionKey = function readActionKey(target) {
25
+ return target[ACTION_KEY];
26
+ };
27
+ const writeActionKey = function writeActionKey(target, key) {
28
+ target[ACTION_KEY] = key;
29
+ };
30
+ const denyStreamBind = function denyStreamBind() {
31
+ throw new Error("oxidejs: stream actions cannot be bound to DOM events");
32
+ };
33
+ const splitCallOptions = function splitCallOptions(args) {
34
+ if (args.length === 0) return { args: emptyArgs() };
35
+ const last = args.at(-1);
36
+ if (last instanceof Object && !Array.isArray(last) && "signal" in last && last.signal instanceof AbortSignal && Object.keys(last).length === 1) return {
37
+ args: asArgs(args.slice(0, -1)),
38
+ options: { signal: last.signal }
22
39
  };
23
40
  return { args };
24
- }
25
- function bindHandler(key, invoke, args) {
26
- const handler = ((..._ev) => invoke(...args));
41
+ };
42
+ const bindHandler = function bindHandler(key, invoke, args) {
43
+ const handler = ((..._ev) => {
44
+ invoke(...args);
45
+ });
27
46
  if (key) handler[ACTION_CALL] = {
28
- k: key,
29
- a: args
47
+ a: args,
48
+ k: key
30
49
  };
31
50
  return handler;
32
- }
33
- function defineServerAction(atom, invoke, opts) {
51
+ };
52
+ const defineServerAction = function defineServerAction(atom, invoke, opts) {
34
53
  const captureKey = opts?.captureKey;
35
54
  const stripCallOptions = opts?.stripCallOptions ?? true;
36
55
  const run = ((...allArgs) => {
@@ -43,66 +62,68 @@ function defineServerAction(atom, invoke, opts) {
43
62
  return Promise.resolve(invoke(...args));
44
63
  };
45
64
  const bind = (...args) => {
46
- return bindHandler(captureKey ?? run[ACTION_KEY], (...a) => void set(...a), args);
65
+ const key = captureKey ?? readActionKey(run);
66
+ return bindHandler(key, (...a) => {
67
+ set(...a);
68
+ }, args);
47
69
  };
48
70
  Object.assign(run, {
49
- set,
50
- bind,
51
- with: bind,
71
+ $$atom: 1,
52
72
  atom,
53
- $$atom: 1
73
+ bind,
74
+ set,
75
+ with: bind
54
76
  });
55
77
  Object.defineProperty(run, "result", {
56
78
  enumerable: true,
57
79
  get: () => registry().get(atom)
58
80
  });
59
- if (captureKey) run[ACTION_KEY] = captureKey;
81
+ if (captureKey) writeActionKey(run, captureKey);
60
82
  return run;
61
- }
62
- function defineStreamAction(fn) {
83
+ };
84
+ const defineStreamAction = function defineStreamAction(fn) {
63
85
  const call = ((...allArgs) => {
64
86
  const { args } = splitCallOptions(allArgs);
65
87
  return fn(...args);
66
88
  });
67
- const deny = () => {
68
- throw new Error("oxidejs: stream actions cannot be bound to DOM events");
69
- };
70
89
  Object.assign(call, {
71
- set: call,
72
- bind: deny,
73
- with: deny,
90
+ $$atom: 1,
74
91
  atom: void 0,
75
- $$atom: 1
92
+ bind: denyStreamBind,
93
+ set: call,
94
+ with: denyStreamBind
76
95
  });
77
96
  return call;
78
- }
97
+ };
79
98
  /** Attach an ilha server-island capture key to an action handle. */
80
- function brandServerAction(key, handle) {
81
- handle[ACTION_KEY] = key;
99
+ const brandServerAction = function brandServerAction(key, handle) {
100
+ writeActionKey(handle, key);
82
101
  return handle;
83
- }
102
+ };
84
103
  /** Wrap an RPC caller as an `Atom.fn`-shaped client action handle. */
85
- function wrapClientRpc(rpc) {
104
+ const wrapClientRpc = function wrapClientRpc(rpc) {
86
105
  const invoke = (...args) => Promise.resolve(rpc(...args));
87
- return defineServerAction(Atom.make(Atom.fn((packed) => Effect.tryPromise({
88
- try: () => invoke(...packed),
89
- catch: (error) => error instanceof Error ? error : new Error(String(error))
90
- }))), (...args) => invoke(...args), { stripCallOptions: false });
91
- }
106
+ const atom = Atom.make(Atom.fn((packed) => Effect.tryPromise({
107
+ catch: (error) => error instanceof Error ? error : new Error(String(error)),
108
+ try: () => invoke(...packed)
109
+ })));
110
+ return defineServerAction(atom, (...args) => invoke(...args), { stripCallOptions: false });
111
+ };
92
112
  /**
93
113
  * Wrap a streaming RPC caller so the client handle returns an async generator
94
114
  * (awaiting the underlying client lazily), not `Promise<AsyncGenerator>`.
95
115
  */
96
- function wrapClientStreamRpc(rpc) {
116
+ const wrapClientStreamRpc = function wrapClientStreamRpc(rpc) {
97
117
  return defineStreamAction((...args) => rpc(...args));
98
- }
118
+ };
99
119
  function action(fn) {
100
120
  if (isAsyncGeneratorFunction(fn)) return defineStreamAction(fn);
101
121
  const invoke = (...args) => Promise.resolve(fn(...args));
102
- return defineServerAction(Atom.make(Atom.fn((packed) => Effect.tryPromise({
103
- try: () => invoke(...packed),
104
- catch: (error) => error instanceof Error ? error : new Error(String(error))
105
- }))), invoke);
122
+ const atom = Atom.make(Atom.fn((packed) => Effect.tryPromise({
123
+ catch: (error) => error instanceof Error ? error : new Error(String(error)),
124
+ try: () => invoke(...packed)
125
+ })));
126
+ return defineServerAction(atom, invoke);
106
127
  }
107
128
  //#endregion
108
129
  export { ACTION_CALL, action, brandServerAction, getRequestStore, useCtx, useEnv, useFetchCtx, useRequest, withRequestStore, wrapClientRpc, wrapClientStreamRpc };