@signaldb/core 2.0.0-beta.17 → 2.0.0-beta.19

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.
@@ -1,90 +1,11 @@
1
- //#region src/utils/reactiveOrAsync.ts
1
+ //#region src/createReactivityAdapter.ts
2
2
  /**
3
- * A generator helper that makes TypeScript infer the “synchronous value type” for maybe-async expressions.
4
- *
5
- * Usage:
6
- * const doc = yield* unwrap(Collection.findOne(...))
7
- * const list = yield* unwrap(Collection.find(...).fetch())
8
- *
9
- * Runtime note:
10
- * This does not “unwrap” Promises by itself. It yields the value/Promise to the runner and returns the
11
- * value that the runner feeds back via `.next(...)`.
12
- * @param value The value (or Promise of a value) to yield to the runner.
13
- * @returns A generator that yields `value` and resolves to the runner-supplied unwrapped `T`.
3
+ * Creates an ReactivityAdapter based on the given definition.
4
+ * @param definition - The definition of the ReactivityAdapter.
5
+ * @returns The created ReactivityAdapter.
14
6
  */
15
- function unwrap(value) {
16
- return (function* () {
17
- return yield value;
18
- })();
19
- }
20
- /**
21
- * Internal: checks for thenables (Promise-like).
22
- * @param value The value to test.
23
- * @returns `true` if `value` looks like a Promise/thenable.
24
- */
25
- function isThenable(value) {
26
- return typeof value === "object" && value !== null && typeof value.then === "function";
27
- }
28
- /**
29
- * Internal runner: executes a generator either synchronously (reactive) or asynchronously (imperative).
30
- *
31
- * - In sync mode, yielding a Promise is a programming error and throws.
32
- * - In async mode, yielded Promises are awaited.
33
- * @param thisArgument The `this` value to bind when invoking `gen`.
34
- * @param mode Execution mode options.
35
- * @param gen The generator workflow to run.
36
- * @returns The workflow result (a plain value in sync mode, or a Promise in async mode).
37
- */
38
- function runReactiveOrAsync(thisArgument, mode, gen) {
39
- const a = !!mode?.async;
40
- const it = gen.call(thisArgument, a);
41
- if (!a) {
42
- let step = it.next();
43
- while (!step.done) {
44
- const y = step.value;
45
- if (isThenable(y)) throw new Error("Promise yielded in sync flow");
46
- step = it.next(y);
47
- }
48
- return step.value;
49
- }
50
- return (async function() {
51
- let step = it.next();
52
- while (!step.done) {
53
- const y = step.value;
54
- const v = isThenable(y) ? await y : y;
55
- step = it.next(v);
56
- }
57
- return step.value;
58
- })();
59
- }
60
- /**
61
- * Factory that turns a generator workflow into a callable method that can run in sync (reactive) or async mode.
62
- *
63
- * Call style:
64
- * fn(a, b) -> sync/reactive return
65
- * await fn(a, b, { async: true }) -> async return
66
- * @param gen Generator workflow. Receives `(a)` which indicates async mode and should `yield`/`yield* unwrap(...)`
67
- * any values that may be Promises.
68
- * @returns A callable method with overloads plus a `.generator` property for composition.
69
- */
70
- function reactiveOrAsync(gen) {
71
- /**
72
- * The generated method wrapper.
73
- * @param allArguments Method arguments, optionally ending with a `ModeOptions` object.
74
- * @returns The workflow result (sync) or a Promise of the result (async).
75
- */
76
- function method(...allArguments) {
77
- const last = allArguments.length > 0 ? allArguments.at(-1) : void 0;
78
- const hasMode = typeof last === "object" && last !== null && "async" in last;
79
- const mode = hasMode ? last : void 0;
80
- const parameters = hasMode ? allArguments.slice(0, -1) : allArguments;
81
- return runReactiveOrAsync(this, mode, function* (a) {
82
- return yield* gen.call(this, a, ...parameters);
83
- });
84
- }
85
- method.generator = gen;
86
- return method;
7
+ function createReactivityAdapter(definition) {
8
+ return definition;
87
9
  }
88
10
  //#endregion
89
- exports.default = reactiveOrAsync;
90
- exports.unwrap = unwrap;
11
+ exports.default = createReactivityAdapter;
package/dist/index31.mjs CHANGED
@@ -1,89 +1,11 @@
1
- //#region src/utils/reactiveOrAsync.ts
1
+ //#region src/createReactivityAdapter.ts
2
2
  /**
3
- * A generator helper that makes TypeScript infer the “synchronous value type” for maybe-async expressions.
4
- *
5
- * Usage:
6
- * const doc = yield* unwrap(Collection.findOne(...))
7
- * const list = yield* unwrap(Collection.find(...).fetch())
8
- *
9
- * Runtime note:
10
- * This does not “unwrap” Promises by itself. It yields the value/Promise to the runner and returns the
11
- * value that the runner feeds back via `.next(...)`.
12
- * @param value The value (or Promise of a value) to yield to the runner.
13
- * @returns A generator that yields `value` and resolves to the runner-supplied unwrapped `T`.
3
+ * Creates an ReactivityAdapter based on the given definition.
4
+ * @param definition - The definition of the ReactivityAdapter.
5
+ * @returns The created ReactivityAdapter.
14
6
  */
15
- function unwrap(value) {
16
- return (function* () {
17
- return yield value;
18
- })();
19
- }
20
- /**
21
- * Internal: checks for thenables (Promise-like).
22
- * @param value The value to test.
23
- * @returns `true` if `value` looks like a Promise/thenable.
24
- */
25
- function isThenable(value) {
26
- return typeof value === "object" && value !== null && typeof value.then === "function";
27
- }
28
- /**
29
- * Internal runner: executes a generator either synchronously (reactive) or asynchronously (imperative).
30
- *
31
- * - In sync mode, yielding a Promise is a programming error and throws.
32
- * - In async mode, yielded Promises are awaited.
33
- * @param thisArgument The `this` value to bind when invoking `gen`.
34
- * @param mode Execution mode options.
35
- * @param gen The generator workflow to run.
36
- * @returns The workflow result (a plain value in sync mode, or a Promise in async mode).
37
- */
38
- function runReactiveOrAsync(thisArgument, mode, gen) {
39
- const a = !!mode?.async;
40
- const it = gen.call(thisArgument, a);
41
- if (!a) {
42
- let step = it.next();
43
- while (!step.done) {
44
- const y = step.value;
45
- if (isThenable(y)) throw new Error("Promise yielded in sync flow");
46
- step = it.next(y);
47
- }
48
- return step.value;
49
- }
50
- return (async function() {
51
- let step = it.next();
52
- while (!step.done) {
53
- const y = step.value;
54
- const v = isThenable(y) ? await y : y;
55
- step = it.next(v);
56
- }
57
- return step.value;
58
- })();
59
- }
60
- /**
61
- * Factory that turns a generator workflow into a callable method that can run in sync (reactive) or async mode.
62
- *
63
- * Call style:
64
- * fn(a, b) -> sync/reactive return
65
- * await fn(a, b, { async: true }) -> async return
66
- * @param gen Generator workflow. Receives `(a)` which indicates async mode and should `yield`/`yield* unwrap(...)`
67
- * any values that may be Promises.
68
- * @returns A callable method with overloads plus a `.generator` property for composition.
69
- */
70
- function reactiveOrAsync(gen) {
71
- /**
72
- * The generated method wrapper.
73
- * @param allArguments Method arguments, optionally ending with a `ModeOptions` object.
74
- * @returns The workflow result (sync) or a Promise of the result (async).
75
- */
76
- function method(...allArguments) {
77
- const last = allArguments.length > 0 ? allArguments.at(-1) : void 0;
78
- const hasMode = typeof last === "object" && last !== null && "async" in last;
79
- const mode = hasMode ? last : void 0;
80
- const parameters = hasMode ? allArguments.slice(0, -1) : allArguments;
81
- return runReactiveOrAsync(this, mode, function* (a) {
82
- return yield* gen.call(this, a, ...parameters);
83
- });
84
- }
85
- method.generator = gen;
86
- return method;
7
+ function createReactivityAdapter(definition) {
8
+ return definition;
87
9
  }
88
10
  //#endregion
89
- export { reactiveOrAsync as default, unwrap };
11
+ export { createReactivityAdapter as default };