@signaldb/svelte 2.0.0-beta.6 → 2.0.0-beta.7

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 (89) hide show
  1. package/dist/base/core/src/AsyncDataAdapter.d.ts +64 -0
  2. package/dist/base/core/src/AsyncDataAdapter.js +450 -0
  3. package/dist/base/core/src/AutoFetchDataAdapter.d.ts +112 -0
  4. package/dist/base/core/src/AutoFetchDataAdapter.js +652 -0
  5. package/dist/base/core/src/Collection/Cursor.d.ts +113 -0
  6. package/dist/base/core/src/Collection/Cursor.js +293 -0
  7. package/dist/base/core/src/Collection/Observer.d.ts +64 -0
  8. package/dist/base/core/src/Collection/Observer.js +181 -0
  9. package/dist/base/core/src/Collection/index.d.ts +294 -0
  10. package/dist/base/core/src/Collection/index.js +580 -0
  11. package/dist/base/core/src/Collection/types.d.ts +28 -0
  12. package/dist/base/core/src/Collection/types.js +2 -0
  13. package/dist/base/core/src/DataAdapter.d.ts +35 -0
  14. package/dist/base/core/src/DataAdapter.js +2 -0
  15. package/dist/base/core/src/DefaultDataAdapter.d.ts +35 -0
  16. package/dist/base/core/src/DefaultDataAdapter.js +398 -0
  17. package/dist/base/core/src/WorkerDataAdapter.d.ts +25 -0
  18. package/dist/base/core/src/WorkerDataAdapter.js +211 -0
  19. package/dist/base/core/src/WorkerDataAdapterHost.d.ts +62 -0
  20. package/dist/base/core/src/WorkerDataAdapterHost.js +409 -0
  21. package/dist/base/core/src/createIndex.d.ts +7 -0
  22. package/dist/base/core/src/createIndex.js +120 -0
  23. package/dist/base/core/src/createIndexProvider.d.ts +8 -0
  24. package/dist/base/core/src/createIndexProvider.js +11 -0
  25. package/dist/base/core/src/createReactivityAdapter.d.ts +8 -0
  26. package/dist/base/core/src/createReactivityAdapter.js +11 -0
  27. package/dist/base/core/src/createStorageAdapter.d.ts +9 -0
  28. package/dist/base/core/src/createStorageAdapter.js +11 -0
  29. package/dist/base/core/src/getIndexInfo.d.ts +39 -0
  30. package/dist/base/core/src/getIndexInfo.js +176 -0
  31. package/dist/base/core/src/index.d.ts +22 -0
  32. package/dist/base/core/src/index.js +39 -0
  33. package/dist/base/core/src/types/Dependency.d.ts +4 -0
  34. package/dist/base/core/src/types/Dependency.js +2 -0
  35. package/dist/base/core/src/types/IndexProvider.d.ts +26 -0
  36. package/dist/base/core/src/types/IndexProvider.js +2 -0
  37. package/dist/base/core/src/types/Modifier.d.ts +46 -0
  38. package/dist/base/core/src/types/Modifier.js +3 -0
  39. package/dist/base/core/src/types/ReactivityAdapter.d.ts +6 -0
  40. package/dist/base/core/src/types/ReactivityAdapter.js +2 -0
  41. package/dist/base/core/src/types/Selector.d.ts +46 -0
  42. package/dist/base/core/src/types/Selector.js +3 -0
  43. package/dist/base/core/src/types/Signal.d.ts +4 -0
  44. package/dist/base/core/src/types/Signal.js +2 -0
  45. package/dist/base/core/src/types/StorageAdapter.d.ts +20 -0
  46. package/dist/base/core/src/types/StorageAdapter.js +2 -0
  47. package/dist/base/core/src/utils/EventEmitter.d.ts +71 -0
  48. package/dist/base/core/src/utils/EventEmitter.js +144 -0
  49. package/dist/base/core/src/utils/batchOnNextTick.d.ts +16 -0
  50. package/dist/base/core/src/utils/batchOnNextTick.js +72 -0
  51. package/dist/base/core/src/utils/compact.d.ts +9 -0
  52. package/dist/base/core/src/utils/compact.js +21 -0
  53. package/dist/base/core/src/utils/createSignal.d.ts +14 -0
  54. package/dist/base/core/src/utils/createSignal.js +38 -0
  55. package/dist/base/core/src/utils/deepClone.d.ts +17 -0
  56. package/dist/base/core/src/utils/deepClone.js +68 -0
  57. package/dist/base/core/src/utils/get.d.ts +9 -0
  58. package/dist/base/core/src/utils/get.js +27 -0
  59. package/dist/base/core/src/utils/getMatchingKeys.d.ts +19 -0
  60. package/dist/base/core/src/utils/getMatchingKeys.js +49 -0
  61. package/dist/base/core/src/utils/intersection.d.ts +9 -0
  62. package/dist/base/core/src/utils/intersection.js +16 -0
  63. package/dist/base/core/src/utils/isEqual.d.ts +14 -0
  64. package/dist/base/core/src/utils/isEqual.js +43 -0
  65. package/dist/base/core/src/utils/isFieldExpression.d.ts +11 -0
  66. package/dist/base/core/src/utils/isFieldExpression.js +52 -0
  67. package/dist/base/core/src/utils/match.d.ts +12 -0
  68. package/dist/base/core/src/utils/match.js +16 -0
  69. package/dist/base/core/src/utils/modify.d.ts +14 -0
  70. package/dist/base/core/src/utils/modify.js +29 -0
  71. package/dist/base/core/src/utils/project.d.ts +15 -0
  72. package/dist/base/core/src/utils/project.js +43 -0
  73. package/dist/base/core/src/utils/queryId.d.ts +9 -0
  74. package/dist/base/core/src/utils/queryId.js +30 -0
  75. package/dist/base/core/src/utils/randomId.d.ts +7 -0
  76. package/dist/base/core/src/utils/randomId.js +12 -0
  77. package/dist/base/core/src/utils/reactiveOrAsync.d.ts +59 -0
  78. package/dist/base/core/src/utils/reactiveOrAsync.js +93 -0
  79. package/dist/base/core/src/utils/serializeValue.d.ts +12 -0
  80. package/dist/base/core/src/utils/serializeValue.js +27 -0
  81. package/dist/base/core/src/utils/set.d.ts +13 -0
  82. package/dist/base/core/src/utils/set.js +43 -0
  83. package/dist/base/core/src/utils/sortItems.d.ts +12 -0
  84. package/dist/base/core/src/utils/sortItems.js +23 -0
  85. package/dist/base/core/src/utils/uniqueBy.d.ts +10 -0
  86. package/dist/base/core/src/utils/uniqueBy.js +19 -0
  87. package/dist/reactivity-adapters/svelte/index.svelte.d.ts +10 -0
  88. package/dist/reactivity-adapters/svelte/index.svelte.js +36 -0
  89. package/package.json +3 -3
@@ -0,0 +1,12 @@
1
+ import type Selector from '../types/Selector';
2
+ type BaseItem = Record<string, any>;
3
+ /**
4
+ * Tests whether a given item matches a specified selector.
5
+ * Uses the `mingo` library to evaluate the query.
6
+ * @template T - The type of the item being tested.
7
+ * @param item - The item to test against the selector.
8
+ * @param selector - The query selector used to match the item.
9
+ * @returns A boolean indicating whether the item matches the selector.
10
+ */
11
+ export default function match<T extends BaseItem = BaseItem>(item: T, selector: Selector<T>): boolean;
12
+ export {};
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = match;
4
+ const mingo_1 = require("mingo");
5
+ /**
6
+ * Tests whether a given item matches a specified selector.
7
+ * Uses the `mingo` library to evaluate the query.
8
+ * @template T - The type of the item being tested.
9
+ * @param item - The item to test against the selector.
10
+ * @param selector - The query selector used to match the item.
11
+ * @returns A boolean indicating whether the item matches the selector.
12
+ */
13
+ function match(item, selector) {
14
+ const query = new mingo_1.Query(selector);
15
+ return query.test(item);
16
+ }
@@ -0,0 +1,14 @@
1
+ import type Modifier from '../types/Modifier';
2
+ /**
3
+ * Applies a modifier to an object and returns a new modified object.
4
+ * @template T - The type of the object to be modified.
5
+ * @param item - The object to be modified.
6
+ * @param modifier - The modifier to apply. This can be any transformation logic.
7
+ * @returns - Returns a new object with the modifications applied.
8
+ * @example
9
+ * const item = { a: 1, b: 2 }
10
+ * const modifier = { $set: { b: 3, c: 4 } }
11
+ * const result = modify(item, modifier)
12
+ * // result: { a: 1, b: 3, c: 4 }
13
+ */
14
+ export default function modify<T extends Record<string, any>>(item: T, modifier: Modifier): T;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.default = modify;
7
+ const mingo_1 = require("mingo");
8
+ const deepClone_1 = __importDefault(require("./deepClone"));
9
+ /**
10
+ * Applies a modifier to an object and returns a new modified object.
11
+ * @template T - The type of the object to be modified.
12
+ * @param item - The object to be modified.
13
+ * @param modifier - The modifier to apply. This can be any transformation logic.
14
+ * @returns - Returns a new object with the modifications applied.
15
+ * @example
16
+ * const item = { a: 1, b: 2 }
17
+ * const modifier = { $set: { b: 3, c: 4 } }
18
+ * const result = modify(item, modifier)
19
+ * // result: { a: 1, b: 3, c: 4 }
20
+ */
21
+ function modify(item, modifier) {
22
+ const hasOperators = Object.keys(modifier).some(key => key.startsWith('$'));
23
+ if (!hasOperators)
24
+ return modifier;
25
+ const clonedItem = (0, deepClone_1.default)(item);
26
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
27
+ (0, mingo_1.update)(clonedItem, modifier);
28
+ return clonedItem;
29
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Projects the fields of an object based on a specified fields configuration.
3
+ * Supports inclusion (`1`) and exclusion (`0`) of specific fields. Creates a new object
4
+ * with the desired fields included or excluded, based on the configuration.
5
+ * @template T - The type of the object being projected.
6
+ * @param item - The original object to project fields from.
7
+ * @param fields - An object defining the fields to include (`1`) or exclude (`0`).
8
+ * - Keys are the field names, and values are either `1` (include) or `0` (exclude).
9
+ * @returns A new object with the specified fields included or excluded.
10
+ * - If all fields are set to `0`, the excluded fields are removed from the result.
11
+ * - If fields are set to `1`, only the included fields are retained.
12
+ */
13
+ export default function project<T extends Record<string, any>>(item: T, fields: {
14
+ [P in keyof T]?: 0 | 1;
15
+ } & Record<string, 0 | 1>): T;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.default = project;
7
+ const get_1 = __importDefault(require("./get"));
8
+ const set_1 = __importDefault(require("./set"));
9
+ /**
10
+ * Projects the fields of an object based on a specified fields configuration.
11
+ * Supports inclusion (`1`) and exclusion (`0`) of specific fields. Creates a new object
12
+ * with the desired fields included or excluded, based on the configuration.
13
+ * @template T - The type of the object being projected.
14
+ * @param item - The original object to project fields from.
15
+ * @param fields - An object defining the fields to include (`1`) or exclude (`0`).
16
+ * - Keys are the field names, and values are either `1` (include) or `0` (exclude).
17
+ * @returns A new object with the specified fields included or excluded.
18
+ * - If all fields are set to `0`, the excluded fields are removed from the result.
19
+ * - If fields are set to `1`, only the included fields are retained.
20
+ */
21
+ function project(item, fields) {
22
+ const allFieldsDeactivated = Object.values(fields).every(value => value === 0);
23
+ if (allFieldsDeactivated) {
24
+ const result = { ...item };
25
+ Object.keys(fields).forEach((key) => {
26
+ const fieldValue = (0, get_1.default)(item, key);
27
+ if (fieldValue === undefined)
28
+ return;
29
+ (0, set_1.default)(result, key, undefined, true);
30
+ });
31
+ return result;
32
+ }
33
+ const result = {};
34
+ Object.entries(fields).forEach(([key, value]) => {
35
+ const fieldValue = (0, get_1.default)(item, key);
36
+ if (fieldValue === undefined)
37
+ return;
38
+ if (fieldValue == null && value !== 1)
39
+ return;
40
+ (0, set_1.default)(result, key, value === 1 ? fieldValue : undefined);
41
+ });
42
+ return result;
43
+ }
@@ -0,0 +1,9 @@
1
+ import type { QueryOptions } from '../DataAdapter';
2
+ import type Selector from '../types/Selector';
3
+ /**
4
+ * Generates a unique identifier for a query based on its selector and options.
5
+ * @param selector - The selector object.
6
+ * @param options - The query options object (optional).
7
+ * @returns A unique identifier string for the query.
8
+ */
9
+ export default function queryId(selector: Selector<any>, options?: QueryOptions<any>): string;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = queryId;
4
+ /**
5
+ * Returns true when `options` is effectively "empty" for the purpose of queryId generation.
6
+ * Treats `undefined`/`null` as empty, and only considers plain objects with zero own enumerable keys as empty.
7
+ * Arrays and non-object values are not considered empty.
8
+ * @param options - Query options to test.
9
+ * @returns `true` if `options` is `null`/`undefined` or a plain object with no own keys; otherwise `false`.
10
+ */
11
+ function isEmptyOptions(options) {
12
+ if (options == null)
13
+ return true;
14
+ if (typeof options !== 'object')
15
+ return false;
16
+ if (Array.isArray(options))
17
+ return false;
18
+ return Object.keys(options).length === 0;
19
+ }
20
+ /**
21
+ * Generates a unique identifier for a query based on its selector and options.
22
+ * @param selector - The selector object.
23
+ * @param options - The query options object (optional).
24
+ * @returns A unique identifier string for the query.
25
+ */
26
+ function queryId(selector, options) {
27
+ const selectorId = JSON.stringify(selector);
28
+ const optionsId = isEmptyOptions(options) ? -1 : JSON.stringify(options);
29
+ return `${selectorId}:${optionsId}`;
30
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * creates a random id
3
+ * @returns a random string of 16 characters
4
+ * @example
5
+ * randomId() // '1234567890abcdef'
6
+ */
7
+ export default function randomId(): string;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = randomId;
4
+ /**
5
+ * creates a random id
6
+ * @returns a random string of 16 characters
7
+ * @example
8
+ * randomId() // '1234567890abcdef'
9
+ */
10
+ function randomId() {
11
+ return Array.from({ length: 16 }, () => 'abcdefghijklmnopqrstuvwxyz0123456789'[Math.floor(Math.random() * 36)]).join('');
12
+ }
@@ -0,0 +1,59 @@
1
+ export type MaybePromise<T> = T | Promise<T>;
2
+ /**
3
+ * Options that control execution mode (and potential future mode-specific behavior).
4
+ * Keep this minimal; you can extend it later (e.g. signal, timeoutMs, debugLabel).
5
+ */
6
+ export type ModeOptions = {
7
+ async?: boolean;
8
+ };
9
+ /**
10
+ * A generator helper that makes TypeScript infer the “synchronous value type” for maybe-async expressions.
11
+ *
12
+ * Usage:
13
+ * const doc = yield* unwrap(Collection.findOne(...))
14
+ * const list = yield* unwrap(Collection.find(...).fetch())
15
+ *
16
+ * Runtime note:
17
+ * This does not “unwrap” Promises by itself. It yields the value/Promise to the runner and returns the
18
+ * value that the runner feeds back via `.next(...)`.
19
+ * @param value The value (or Promise of a value) to yield to the runner.
20
+ * @returns A generator that yields `value` and resolves to the runner-supplied unwrapped `T`.
21
+ */
22
+ export declare function unwrap<T>(value: MaybePromise<T>): Generator<MaybePromise<T>, T, T>;
23
+ /**
24
+ * Generator shape used by the factory.
25
+ *
26
+ * `TThis` is the type of `this` inside the generator.
27
+ * `Args` are the method parameters (excluding the mode flag).
28
+ * `TReturn` is the final return value of the workflow.
29
+ * `TNext` is the type that is yielded/awaited and fed back via `.next(...)`.
30
+ *
31
+ * Note:
32
+ * - For best inference at yield sites, prefer `yield* unwrap(expr)` for maybe-async expressions.
33
+ */
34
+ export type ReactiveOrAsyncGen<TThis, Arguments extends any[], TReturn, TNext> = (this: TThis, a: boolean, ...args: Arguments) => Generator<MaybePromise<TNext>, TReturn, TNext>;
35
+ /**
36
+ * The method type produced from the generator signature.
37
+ * Adds overloads so that `{ async: true }` yields a `Promise<...>` return type.
38
+ */
39
+ export type ReactiveOrAsyncMethod<TThis, P extends any[], R, N> = {
40
+ (this: TThis, ...args: P): R;
41
+ (this: TThis, ...args: [...P, ModeOptions?]): MaybePromise<R>;
42
+ (this: TThis, ...args: [...P, {
43
+ async: true;
44
+ }]): Promise<R>;
45
+ } & {
46
+ /** Exposes the underlying generator for composition via `yield* method.generator.call(this, a, ...)` */
47
+ generator: (this: TThis, a: boolean, ...args: P) => Generator<MaybePromise<N>, R, N>;
48
+ };
49
+ /**
50
+ * Factory that turns a generator workflow into a callable method that can run in sync (reactive) or async mode.
51
+ *
52
+ * Call style:
53
+ * fn(a, b) -> sync/reactive return
54
+ * await fn(a, b, { async: true }) -> async return
55
+ * @param gen Generator workflow. Receives `(a)` which indicates async mode and should `yield`/`yield* unwrap(...)`
56
+ * any values that may be Promises.
57
+ * @returns A callable method with overloads plus a `.generator` property for composition.
58
+ */
59
+ export default function reactiveOrAsync<TThis, P extends any[], R, N>(gen: (this: TThis, a: boolean, ...args: P) => Generator<MaybePromise<N>, R, N>): ReactiveOrAsyncMethod<TThis, P, R, N>;
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unwrap = unwrap;
4
+ exports.default = reactiveOrAsync;
5
+ /**
6
+ * A generator helper that makes TypeScript infer the “synchronous value type” for maybe-async expressions.
7
+ *
8
+ * Usage:
9
+ * const doc = yield* unwrap(Collection.findOne(...))
10
+ * const list = yield* unwrap(Collection.find(...).fetch())
11
+ *
12
+ * Runtime note:
13
+ * This does not “unwrap” Promises by itself. It yields the value/Promise to the runner and returns the
14
+ * value that the runner feeds back via `.next(...)`.
15
+ * @param value The value (or Promise of a value) to yield to the runner.
16
+ * @returns A generator that yields `value` and resolves to the runner-supplied unwrapped `T`.
17
+ */
18
+ function unwrap(value) {
19
+ return (function* () {
20
+ return yield value;
21
+ })();
22
+ }
23
+ /**
24
+ * Internal: checks for thenables (Promise-like).
25
+ * @param value The value to test.
26
+ * @returns `true` if `value` looks like a Promise/thenable.
27
+ */
28
+ function isThenable(value) {
29
+ return typeof value === 'object' && value !== null && typeof value.then === 'function';
30
+ }
31
+ /**
32
+ * Internal runner: executes a generator either synchronously (reactive) or asynchronously (imperative).
33
+ *
34
+ * - In sync mode, yielding a Promise is a programming error and throws.
35
+ * - In async mode, yielded Promises are awaited.
36
+ * @param thisArgument The `this` value to bind when invoking `gen`.
37
+ * @param mode Execution mode options.
38
+ * @param gen The generator workflow to run.
39
+ * @returns The workflow result (a plain value in sync mode, or a Promise in async mode).
40
+ */
41
+ function runReactiveOrAsync(thisArgument, mode, gen) {
42
+ const a = !!mode?.async;
43
+ const it = gen.call(thisArgument, a);
44
+ if (!a) {
45
+ let step = it.next();
46
+ while (!step.done) {
47
+ const y = step.value;
48
+ if (isThenable(y))
49
+ throw new Error('Promise yielded in sync flow');
50
+ step = it.next(y);
51
+ }
52
+ return step.value;
53
+ }
54
+ return (async function () {
55
+ let step = it.next();
56
+ while (!step.done) {
57
+ const y = step.value;
58
+ const v = isThenable(y) ? await y : y;
59
+ step = it.next(v);
60
+ }
61
+ return step.value;
62
+ })();
63
+ }
64
+ /**
65
+ * Factory that turns a generator workflow into a callable method that can run in sync (reactive) or async mode.
66
+ *
67
+ * Call style:
68
+ * fn(a, b) -> sync/reactive return
69
+ * await fn(a, b, { async: true }) -> async return
70
+ * @param gen Generator workflow. Receives `(a)` which indicates async mode and should `yield`/`yield* unwrap(...)`
71
+ * any values that may be Promises.
72
+ * @returns A callable method with overloads plus a `.generator` property for composition.
73
+ */
74
+ function reactiveOrAsync(gen) {
75
+ /**
76
+ * The generated method wrapper.
77
+ * @param allArguments Method arguments, optionally ending with a `ModeOptions` object.
78
+ * @returns The workflow result (sync) or a Promise of the result (async).
79
+ */
80
+ function method(...allArguments) {
81
+ const last = allArguments.length > 0 ? allArguments.at(-1) : undefined;
82
+ const hasMode = typeof last === 'object'
83
+ && last !== null
84
+ && 'async' in (last);
85
+ const mode = hasMode ? last : undefined;
86
+ const parameters = (hasMode ? allArguments.slice(0, -1) : allArguments);
87
+ return runReactiveOrAsync(this, mode, function* (a) {
88
+ return yield* gen.call(this, a, ...parameters);
89
+ });
90
+ }
91
+ method.generator = gen;
92
+ return method;
93
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Serializes a value into a string representation.
3
+ * Handles various types, including strings, numbers, booleans, dates, and objects.
4
+ * Falls back to JSON stringification for unsupported types.
5
+ * @param value - The value to serialize.
6
+ * - Strings are returned as-is.
7
+ * - Numbers and booleans are converted to their string representation.
8
+ * - Dates are converted to ISO string format.
9
+ * - Other values are stringified using `JSON.stringify`.
10
+ * @returns A string representation of the value.
11
+ */
12
+ export default function serializeValue(value: any): string | null;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = serializeValue;
4
+ /**
5
+ * Serializes a value into a string representation.
6
+ * Handles various types, including strings, numbers, booleans, dates, and objects.
7
+ * Falls back to JSON stringification for unsupported types.
8
+ * @param value - The value to serialize.
9
+ * - Strings are returned as-is.
10
+ * - Numbers and booleans are converted to their string representation.
11
+ * - Dates are converted to ISO string format.
12
+ * - Other values are stringified using `JSON.stringify`.
13
+ * @returns A string representation of the value.
14
+ */
15
+ function serializeValue(value) {
16
+ if (value == null)
17
+ return null;
18
+ if (typeof value === 'string')
19
+ return value;
20
+ if (typeof value === 'number')
21
+ return value.toString();
22
+ if (typeof value === 'boolean')
23
+ return value.toString();
24
+ if (value instanceof Date)
25
+ return value.toISOString();
26
+ return JSON.stringify(value);
27
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Sets a value at a specified path within an object. Creates nested structures
3
+ * (arrays or objects) as needed to set the value at the correct location. Supports
4
+ * deleting the key if the value is `undefined` and the `deleteIfUndefined` flag is set to `true`.
5
+ * @template T - The type of the object to modify.
6
+ * @template K - The type of the value to set.
7
+ * @param object - The object to modify. The object is mutated directly.
8
+ * @param path - The path (dot or bracket notation) where the value should be set.
9
+ * @param value - The value to set at the specified path.
10
+ * @param deleteIfUndefined - A boolean indicating whether to delete the key if the value is `undefined` (default: `false`).
11
+ * @returns The modified object.
12
+ */
13
+ export default function set<T extends object, K>(object: T, path: string, value: K, deleteIfUndefined?: boolean): T;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = set;
4
+ /**
5
+ * Sets a value at a specified path within an object. Creates nested structures
6
+ * (arrays or objects) as needed to set the value at the correct location. Supports
7
+ * deleting the key if the value is `undefined` and the `deleteIfUndefined` flag is set to `true`.
8
+ * @template T - The type of the object to modify.
9
+ * @template K - The type of the value to set.
10
+ * @param object - The object to modify. The object is mutated directly.
11
+ * @param path - The path (dot or bracket notation) where the value should be set.
12
+ * @param value - The value to set at the specified path.
13
+ * @param deleteIfUndefined - A boolean indicating whether to delete the key if the value is `undefined` (default: `false`).
14
+ * @returns The modified object.
15
+ */
16
+ function set(object, path, value, deleteIfUndefined = false) {
17
+ if (object == null)
18
+ return object;
19
+ const segments = path.split(/[.[\]]/g);
20
+ if (segments[0] === '')
21
+ segments.shift();
22
+ if (segments.at(-1) === '')
23
+ segments.pop();
24
+ const apply = (node) => {
25
+ if (segments.length > 1) {
26
+ const key = segments.shift();
27
+ const nextIsNumber = !Number.isNaN(Number.parseInt(segments[0], 10));
28
+ if (node[key] === undefined) {
29
+ node[key] = nextIsNumber ? [] : {};
30
+ }
31
+ apply(node[key]);
32
+ }
33
+ else {
34
+ if (deleteIfUndefined && value === undefined) {
35
+ delete node[segments[0]];
36
+ return;
37
+ }
38
+ node[segments[0]] = value;
39
+ }
40
+ };
41
+ apply(object);
42
+ return object;
43
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Sorts an array of items based on multiple fields and their specified sort order.
3
+ * Uses the `fast-sort` library for efficient sorting.
4
+ * @template T - The type of the items in the array.
5
+ * @param items - The array of items to be sorted.
6
+ * @param sortFields - An object defining the sort order for each field.
7
+ * - Keys are the field names, and values are either `1` (ascending) or `-1` (descending).
8
+ * @returns A new array of items sorted based on the specified fields and their order.
9
+ */
10
+ export default function sortItems<T extends Record<string, any>>(items: T[], sortFields: {
11
+ [P in keyof T]?: -1 | 1;
12
+ } & Record<string, -1 | 1>): T[];
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.default = sortItems;
7
+ const fast_sort_1 = require("fast-sort");
8
+ const get_1 = __importDefault(require("./get"));
9
+ /**
10
+ * Sorts an array of items based on multiple fields and their specified sort order.
11
+ * Uses the `fast-sort` library for efficient sorting.
12
+ * @template T - The type of the items in the array.
13
+ * @param items - The array of items to be sorted.
14
+ * @param sortFields - An object defining the sort order for each field.
15
+ * - Keys are the field names, and values are either `1` (ascending) or `-1` (descending).
16
+ * @returns A new array of items sorted based on the specified fields and their order.
17
+ */
18
+ function sortItems(items, sortFields) {
19
+ return (0, fast_sort_1.sort)(items).by(Object.entries(sortFields).map(([key, value]) => {
20
+ const order = value === 1 ? 'asc' : 'desc';
21
+ return { [order]: (i) => (0, get_1.default)(i, key) };
22
+ }));
23
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Filters an array to ensure unique values based on a specified key or transformation function.
3
+ * @template T - The type of the elements in the array.
4
+ * @param array - The array to filter for unique values.
5
+ * @param fn - A key or transformation function to determine uniqueness.
6
+ * - If a key is provided, it will use the corresponding property of each element for uniqueness.
7
+ * - If a function is provided, it will use the return value of the function applied to each element for uniqueness.
8
+ * @returns A new array containing only unique elements based on the specified key or transformation.
9
+ */
10
+ export default function uniqueBy<T>(array: T[], fn: keyof T | ((item: T) => any)): T[];
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = uniqueBy;
4
+ /**
5
+ * Filters an array to ensure unique values based on a specified key or transformation function.
6
+ * @template T - The type of the elements in the array.
7
+ * @param array - The array to filter for unique values.
8
+ * @param fn - A key or transformation function to determine uniqueness.
9
+ * - If a key is provided, it will use the corresponding property of each element for uniqueness.
10
+ * - If a function is provided, it will use the return value of the function applied to each element for uniqueness.
11
+ * @returns A new array containing only unique elements based on the specified key or transformation.
12
+ */
13
+ function uniqueBy(array, fn) {
14
+ const set = new Set();
15
+ return array.filter((element) => {
16
+ const value = typeof fn === 'function' ? fn(element) : element[fn];
17
+ return !set.has(value) && set.add(value);
18
+ });
19
+ }
@@ -0,0 +1,10 @@
1
+ export declare class SvelteDependency {
2
+ #private;
3
+ constructor();
4
+ depend(): void;
5
+ notify(): void;
6
+ onDispose(callback: () => void): void;
7
+ dispose(): void;
8
+ }
9
+ declare const svelteReactivityAdapter: import("@signaldb/core").ReactivityAdapter<SvelteDependency>;
10
+ export default svelteReactivityAdapter;
@@ -0,0 +1,36 @@
1
+ import { createSubscriber } from 'svelte/reactivity';
2
+ import { createReactivityAdapter } from '@signaldb/core';
3
+ export class SvelteDependency {
4
+ #subscribe;
5
+ #update;
6
+ #onDisposeCallbacks;
7
+ constructor() {
8
+ this.#onDisposeCallbacks = [];
9
+ this.#subscribe = createSubscriber((update) => {
10
+ this.#update = update;
11
+ return () => this.dispose();
12
+ });
13
+ }
14
+ depend() {
15
+ this.#subscribe();
16
+ }
17
+ notify() {
18
+ // The #update can potentially be undefined because it only becomes
19
+ // available after #subscribe is called for the first time within a scope.
20
+ this.#update?.();
21
+ }
22
+ onDispose(callback) {
23
+ this.#onDisposeCallbacks.push(callback);
24
+ }
25
+ dispose() {
26
+ this.#onDisposeCallbacks.forEach(callback => callback());
27
+ }
28
+ }
29
+ const svelteReactivityAdapter = createReactivityAdapter({
30
+ create: () => new SvelteDependency(),
31
+ isInScope: () => $effect.tracking(),
32
+ onDispose(callback, svelteDependency) {
33
+ svelteDependency.onDispose(callback);
34
+ },
35
+ });
36
+ export default svelteReactivityAdapter;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@signaldb/svelte",
3
3
  "type": "module",
4
- "version": "2.0.0-beta.6",
4
+ "version": "2.0.0-beta.7",
5
5
  "scripts": {
6
6
  "build": "tsc -d --noEmit false",
7
7
  "analyze-bundle": "bundle-analyzer ./dist --upload-token=$BUNDLE_ANALYZER_UPLOAD_TOKEN --bundle-name=@signaldb/svelte",
@@ -45,7 +45,7 @@
45
45
  "svelte": "^5.7.0"
46
46
  },
47
47
  "devDependencies": {
48
- "@sveltejs/vite-plugin-svelte": "6.2.4",
49
- "svelte": "5.49.1"
48
+ "@sveltejs/vite-plugin-svelte": "7.1.2",
49
+ "svelte": "5.56.1"
50
50
  }
51
51
  }