@signaldb/core 2.0.0-beta.13 → 2.0.0-beta.14

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.
Files changed (82) hide show
  1. package/dist/.vite/manifest.json +72 -46
  2. package/dist/AsyncDataAdapter.d.ts +11 -2
  3. package/dist/Collection/Cursor.d.ts +11 -1
  4. package/dist/Collection/Observer.d.ts +31 -0
  5. package/dist/DataAdapter.d.ts +15 -2
  6. package/dist/WorkerDataAdapter.d.ts +25 -2
  7. package/dist/WorkerDataAdapterHost.d.ts +2 -0
  8. package/dist/index.cjs.js +15 -15
  9. package/dist/index.d.ts +2 -0
  10. package/dist/index.mjs +15 -15
  11. package/dist/index10.cjs.js +7 -19
  12. package/dist/index10.mjs +7 -19
  13. package/dist/index11.cjs.js +19 -41
  14. package/dist/index11.mjs +19 -41
  15. package/dist/index12.cjs.js +41 -18
  16. package/dist/index12.mjs +41 -18
  17. package/dist/index13.cjs.js +18 -41
  18. package/dist/index13.mjs +18 -41
  19. package/dist/index14.cjs.js +39 -80
  20. package/dist/index14.mjs +39 -80
  21. package/dist/index15.cjs.js +82 -11
  22. package/dist/index15.mjs +82 -11
  23. package/dist/index16.cjs.js +11 -132
  24. package/dist/index16.mjs +11 -132
  25. package/dist/index17.cjs.js +127 -38
  26. package/dist/index17.mjs +127 -38
  27. package/dist/index18.cjs.js +43 -11
  28. package/dist/index18.mjs +43 -11
  29. package/dist/index19.cjs.js +10 -18
  30. package/dist/index19.mjs +11 -19
  31. package/dist/index20.cjs.js +19 -33
  32. package/dist/index20.mjs +19 -33
  33. package/dist/index21.cjs.js +33 -31
  34. package/dist/index21.mjs +33 -31
  35. package/dist/index22.cjs.js +31 -21
  36. package/dist/index22.mjs +31 -21
  37. package/dist/index23.cjs.js +16 -14
  38. package/dist/index23.mjs +16 -14
  39. package/dist/index24.cjs.js +15 -338
  40. package/dist/index24.mjs +15 -338
  41. package/dist/index25.cjs.js +122 -554
  42. package/dist/index25.mjs +121 -554
  43. package/dist/index26.cjs.js +34 -7
  44. package/dist/index26.mjs +34 -7
  45. package/dist/index27.cjs.js +339 -7
  46. package/dist/index27.mjs +339 -7
  47. package/dist/index28.cjs.js +550 -83
  48. package/dist/index28.mjs +550 -82
  49. package/dist/index29.cjs.js +8 -422
  50. package/dist/index29.mjs +8 -422
  51. package/dist/index30.cjs.js +7 -68
  52. package/dist/index30.mjs +7 -68
  53. package/dist/index31.cjs.js +86 -28
  54. package/dist/index31.mjs +85 -28
  55. package/dist/index32.cjs.js +446 -278
  56. package/dist/index32.mjs +446 -278
  57. package/dist/index33.cjs.js +68 -17
  58. package/dist/index33.mjs +68 -17
  59. package/dist/index34.cjs.js +460 -304
  60. package/dist/index34.mjs +460 -304
  61. package/dist/index35.cjs.js +14 -532
  62. package/dist/index35.mjs +14 -532
  63. package/dist/index36.cjs.js +389 -0
  64. package/dist/index36.mjs +389 -0
  65. package/dist/index37.cjs.js +539 -0
  66. package/dist/index37.mjs +539 -0
  67. package/dist/index4.cjs.js +199 -140
  68. package/dist/index4.mjs +195 -140
  69. package/dist/index5.cjs.js +152 -253
  70. package/dist/index5.mjs +152 -253
  71. package/dist/index6.cjs.js +267 -89
  72. package/dist/index6.mjs +267 -89
  73. package/dist/index7.cjs.js +121 -29
  74. package/dist/index7.mjs +121 -29
  75. package/dist/index8.cjs.js +29 -8
  76. package/dist/index8.mjs +29 -8
  77. package/dist/index9.cjs.js +8 -7
  78. package/dist/index9.mjs +8 -7
  79. package/dist/utils/incrementalQueryUpdate.d.ts +60 -0
  80. package/dist/utils/projectItems.d.ts +12 -0
  81. package/dist/utils/queryDelta.d.ts +83 -0
  82. package/package.json +1 -1
@@ -1,32 +1,90 @@
1
- const require_match = require("./index18.cjs.js");
2
- const require_project = require("./index21.cjs.js");
3
- const require_sortItems = require("./index23.cjs.js");
4
- //#region src/utils/applyQueryOptions.ts
1
+ //#region src/utils/reactiveOrAsync.ts
5
2
  /**
6
- * Filters, sorts, paginates and projects a plain in-memory array the same way
7
- * DefaultDataAdapter and WorkerDataAdapterHost apply a selector/QueryOptions
8
- * pair to their stored items. Used to re-derive a query's result locally after
9
- * a write, without asking the backing store again.
10
- * @template T - The type of the items.
11
- * @param items - The items to filter, sort, paginate and project.
12
- * @param selector - The selector to match items against.
13
- * @param options - Sort, skip, limit and field projection options.
14
- * @returns The resulting items.
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`.
15
14
  */
16
- function applyQueryOptions(items, selector, options) {
17
- const matched = selector == null ? [] : items.filter((item) => require_match.default(item, selector));
18
- const { sort, skip, limit, fields } = options || {};
19
- const sorted = sort ? require_sortItems.default(matched, sort) : matched;
20
- const skipped = skip ? sorted.slice(skip) : sorted;
21
- const limited = limit ? skipped.slice(0, limit) : skipped;
22
- const idExcluded = fields && fields.id === 0;
23
- return limited.map((item) => {
24
- if (!fields) return item;
25
- return {
26
- ...idExcluded ? {} : { id: item.id },
27
- ...require_project.default(item, fields)
28
- };
29
- });
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;
30
87
  }
31
88
  //#endregion
32
- exports.default = applyQueryOptions;
89
+ exports.default = reactiveOrAsync;
90
+ exports.unwrap = unwrap;
package/dist/index31.mjs CHANGED
@@ -1,32 +1,89 @@
1
- import match from "./index18.mjs";
2
- import project from "./index21.mjs";
3
- import sortItems from "./index23.mjs";
4
- //#region src/utils/applyQueryOptions.ts
1
+ //#region src/utils/reactiveOrAsync.ts
5
2
  /**
6
- * Filters, sorts, paginates and projects a plain in-memory array the same way
7
- * DefaultDataAdapter and WorkerDataAdapterHost apply a selector/QueryOptions
8
- * pair to their stored items. Used to re-derive a query's result locally after
9
- * a write, without asking the backing store again.
10
- * @template T - The type of the items.
11
- * @param items - The items to filter, sort, paginate and project.
12
- * @param selector - The selector to match items against.
13
- * @param options - Sort, skip, limit and field projection options.
14
- * @returns The resulting items.
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`.
15
14
  */
16
- function applyQueryOptions(items, selector, options) {
17
- const matched = selector == null ? [] : items.filter((item) => match(item, selector));
18
- const { sort, skip, limit, fields } = options || {};
19
- const sorted = sort ? sortItems(matched, sort) : matched;
20
- const skipped = skip ? sorted.slice(skip) : sorted;
21
- const limited = limit ? skipped.slice(0, limit) : skipped;
22
- const idExcluded = fields && fields.id === 0;
23
- return limited.map((item) => {
24
- if (!fields) return item;
25
- return {
26
- ...idExcluded ? {} : { id: item.id },
27
- ...project(item, fields)
28
- };
29
- });
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;
30
87
  }
31
88
  //#endregion
32
- export { applyQueryOptions as default };
89
+ export { reactiveOrAsync as default, unwrap };