@signaldb/preact 2.0.0-beta.7 → 2.0.0-beta.9

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 (45) hide show
  1. package/dist/{reactivity-adapters/preact/src/index.d.ts → index.d.ts} +1 -1
  2. package/package.json +5 -1
  3. package/dist/base/core/src/AsyncDataAdapter.d.ts +0 -64
  4. package/dist/base/core/src/AutoFetchDataAdapter.d.ts +0 -112
  5. package/dist/base/core/src/Collection/Cursor.d.ts +0 -113
  6. package/dist/base/core/src/Collection/Observer.d.ts +0 -64
  7. package/dist/base/core/src/Collection/index.d.ts +0 -294
  8. package/dist/base/core/src/Collection/types.d.ts +0 -28
  9. package/dist/base/core/src/DataAdapter.d.ts +0 -35
  10. package/dist/base/core/src/DefaultDataAdapter.d.ts +0 -35
  11. package/dist/base/core/src/WorkerDataAdapter.d.ts +0 -25
  12. package/dist/base/core/src/WorkerDataAdapterHost.d.ts +0 -62
  13. package/dist/base/core/src/createIndex.d.ts +0 -7
  14. package/dist/base/core/src/createIndexProvider.d.ts +0 -8
  15. package/dist/base/core/src/createReactivityAdapter.d.ts +0 -8
  16. package/dist/base/core/src/createStorageAdapter.d.ts +0 -9
  17. package/dist/base/core/src/getIndexInfo.d.ts +0 -39
  18. package/dist/base/core/src/index.d.ts +0 -22
  19. package/dist/base/core/src/types/Dependency.d.ts +0 -4
  20. package/dist/base/core/src/types/IndexProvider.d.ts +0 -26
  21. package/dist/base/core/src/types/Modifier.d.ts +0 -46
  22. package/dist/base/core/src/types/ReactivityAdapter.d.ts +0 -6
  23. package/dist/base/core/src/types/Selector.d.ts +0 -46
  24. package/dist/base/core/src/types/Signal.d.ts +0 -4
  25. package/dist/base/core/src/types/StorageAdapter.d.ts +0 -20
  26. package/dist/base/core/src/utils/EventEmitter.d.ts +0 -71
  27. package/dist/base/core/src/utils/batchOnNextTick.d.ts +0 -16
  28. package/dist/base/core/src/utils/compact.d.ts +0 -9
  29. package/dist/base/core/src/utils/createSignal.d.ts +0 -14
  30. package/dist/base/core/src/utils/deepClone.d.ts +0 -17
  31. package/dist/base/core/src/utils/get.d.ts +0 -9
  32. package/dist/base/core/src/utils/getMatchingKeys.d.ts +0 -19
  33. package/dist/base/core/src/utils/intersection.d.ts +0 -9
  34. package/dist/base/core/src/utils/isEqual.d.ts +0 -14
  35. package/dist/base/core/src/utils/isFieldExpression.d.ts +0 -11
  36. package/dist/base/core/src/utils/match.d.ts +0 -12
  37. package/dist/base/core/src/utils/modify.d.ts +0 -14
  38. package/dist/base/core/src/utils/project.d.ts +0 -15
  39. package/dist/base/core/src/utils/queryId.d.ts +0 -9
  40. package/dist/base/core/src/utils/randomId.d.ts +0 -7
  41. package/dist/base/core/src/utils/reactiveOrAsync.d.ts +0 -59
  42. package/dist/base/core/src/utils/serializeValue.d.ts +0 -12
  43. package/dist/base/core/src/utils/set.d.ts +0 -13
  44. package/dist/base/core/src/utils/sortItems.d.ts +0 -12
  45. package/dist/base/core/src/utils/uniqueBy.d.ts +0 -10
@@ -1,4 +1,4 @@
1
- declare const preactReactivityAdapter: import("@signaldb/core").ReactivityAdapter<{
1
+ declare const preactReactivityAdapter: import('@signaldb/core').ReactivityAdapter<{
2
2
  depend: () => void;
3
3
  notify: () => void;
4
4
  }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@signaldb/preact",
3
- "version": "2.0.0-beta.7",
3
+ "version": "2.0.0-beta.9",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "rimraf dist && vite build",
@@ -53,6 +53,10 @@
53
53
  ],
54
54
  "peerDependencies": {
55
55
  "@preact/signals-core": "1.x.x",
56
+ "@signaldb/core": "2.x"
57
+ },
58
+ "devDependencies": {
59
+ "@preact/signals-core": "^1.14.4",
56
60
  "@signaldb/core": "1.x || ^2.0.0-0"
57
61
  }
58
62
  }
@@ -1,64 +0,0 @@
1
- import type { BaseItem } from './Collection';
2
- import type Collection from './Collection';
3
- import type DataAdapter from './DataAdapter';
4
- import type { CollectionBackend } from './DataAdapter';
5
- import type StorageAdapter from './types/StorageAdapter';
6
- export interface AsyncDataAdapterOptions {
7
- /** Factory to obtain a StorageAdapter per collection name */
8
- storage: (name: string) => StorageAdapter<any, any>;
9
- /** Optional logical id (handy if you run multiple adapters side-by-side) */
10
- id?: string;
11
- /** Optional error hook (mirrors WorkerDataAdapterHost) */
12
- onError?: (error: Error) => void;
13
- }
14
- /**
15
- * AsyncDataAdapter
16
- * Combines WorkerDataAdapter + WorkerDataAdapterHost into a single, transport-free adapter.
17
- * - Keeps the DataAdapter/CollectionBackend surface identical to the Worker version.
18
- * - Executes queries and mutations directly against the provided StorageAdapter.
19
- * - Preserves index-aware query optimization and push-style query updates to listeners.
20
- */
21
- export default class AsyncDataAdapter implements DataAdapter {
22
- private options;
23
- private id;
24
- private onError;
25
- private storageAdapters;
26
- private storageAdapterReady;
27
- private collectionIndices;
28
- private queries;
29
- constructor(options: AsyncDataAdapterOptions);
30
- createCollectionBackend<T extends BaseItem<I>, I = any, E extends BaseItem = T, U = E>(collection: Collection<T, I, E, U>, indices: string[]): CollectionBackend<T, I>;
31
- private setupStorage;
32
- private ensureStorageAdapter;
33
- /**
34
- * Compute and publish the result for a specific query
35
- * @param collectionName - name of the collection
36
- * @param selector - query selector
37
- * @param options - query options
38
- */
39
- private fulfillQuery;
40
- /**
41
- * Notify listeners about state changes and keep the cache updated
42
- * @param collectionName - name of the collection
43
- * @param qid - query id
44
- * @param state - new state
45
- * @param error - error if state is 'error', null otherwise
46
- */
47
- private publishState;
48
- private publishResult;
49
- private getIndexInfo;
50
- private queryItems;
51
- private executeQuery;
52
- /**
53
- * After mutations, recompute and push updates for affected active queries
54
- * @param collectionName - name of the collection
55
- * @param affectedItems - item states before and/or after the mutation
56
- */
57
- private checkQueryUpdates;
58
- private insert;
59
- private updateOne;
60
- private updateMany;
61
- private replaceOne;
62
- private removeOne;
63
- private removeMany;
64
- }
@@ -1,112 +0,0 @@
1
- import type { BaseItem } from './Collection';
2
- import type Collection from './Collection';
3
- import type DataAdapter from './DataAdapter';
4
- import type { CollectionBackend } from './DataAdapter';
5
- import type StorageAdapter from './types/StorageAdapter';
6
- import type Selector from './types/Selector';
7
- /**
8
- * AutoFetchDataAdapterOptions
9
- *
10
- * Merges the core options required to talk to a per-collection StorageAdapter with
11
- * the auto-fetch behavior inspired by AutoFetchCollection.
12
- */
13
- export interface AutoFetchDataAdapterOptions {
14
- /** Factory to obtain a StorageAdapter per collection name */
15
- storage?: (name: string) => StorageAdapter<any, any>;
16
- /** Optional logical id (handy if you run multiple adapters side-by-side) */
17
- id?: string;
18
- /** Optional error hook */
19
- onError?: (error: Error) => void;
20
- /**
21
- * Fetch hook: given a selector, retrieve items from a remote source.
22
- * Must resolve to an object with an `items` array. Items MUST include an `id`.
23
- */
24
- fetchQueryItems: (collectionName: string, selector: Selector<BaseItem>) => Promise<BaseItem[] | undefined>;
25
- /**
26
- * Optional: called once at adapter construction to subscribe to remote changes.
27
- * When invoked, call the provided callback whenever the remote source changed
28
- * and all active queries should be re-fetched.
29
- */
30
- registerRemoteChange?: (onChange: () => Promise<void>) => Promise<void>;
31
- /**
32
- * Merge strategy for ingesting freshly fetched items with existing ones.
33
- * Default is shallow spread (right wins).
34
- */
35
- mergeItems?: <T extends BaseItem<any>>(a: T, b: T) => T;
36
- /**
37
- * Delay (ms) before purging auto-fetched items for a query after the query
38
- * becomes inactive. Set to 0 to purge immediately. Default: 10s.
39
- */
40
- purgeDelay?: number;
41
- }
42
- /**
43
- * AutoFetchDataAdapter
44
- *
45
- * A DataAdapter that:
46
- * - Mirrors the CollectionBackend surface (CRUD + query registry + lifecycle)
47
- * - Executes queries against a provided StorageAdapter (local cache)
48
- * - On first registration of a selector, auto-fetches from a remote source and
49
- * ingests the result into storage (upsert), then pushes query result updates
50
- * - Optionally purges auto-fetched items for a selector once no observers remain
51
- * - Can subscribe to remote change notifications to re-fetch active selectors
52
- *
53
- * IMPORTANT: Purging only ever deletes items that were introduced via the
54
- * auto-fetch path and are no longer referenced by any active selector. Items
55
- * inserted through CRUD calls are never purged.
56
- */
57
- export default class AutoFetchDataAdapter implements DataAdapter {
58
- private options;
59
- private id;
60
- private onError;
61
- private fetchQueryItems;
62
- private mergeItems;
63
- private purgeDelay;
64
- private storageAdapters;
65
- private storageAdapterReady;
66
- private collectionIndices;
67
- private queries;
68
- private activeObservers;
69
- private observerTimeouts;
70
- private selectorIds;
71
- private idRefCounts;
72
- private autoloadIds;
73
- constructor(options: AutoFetchDataAdapterOptions);
74
- createCollectionBackend<T extends BaseItem<I>, I = any, E extends BaseItem = T, U = E>(collection: Collection<T, I, E, U>, indices: string[]): CollectionBackend<T, I>;
75
- private forceRefetchAll;
76
- private fetchAndIngest;
77
- private purgeSelector;
78
- private setupStorage;
79
- private ensureStorageAdapter;
80
- private publishForSelector;
81
- private publishState;
82
- private publishResult;
83
- private fulfillQuery;
84
- private getIndexInfo;
85
- private queryItems;
86
- private executeQuery;
87
- private checkQueryUpdates;
88
- private insert;
89
- private updateOne;
90
- private updateMany;
91
- private replaceOne;
92
- private removeOne;
93
- private removeMany;
94
- private upsertMerged;
95
- }
96
- /**
97
- * Usage (example):
98
- *
99
- * const adapter = new AutoFetchDataAdapter({
100
- * storage: (name) => new IndexedDBStorage(name),
101
- * fetchQueryItems: async (collectionName, selector) => {
102
- * const res = await fetch(`/api/${collectionName}?q=${encodeURIComponent(JSON.stringify(selector||{}))}`)
103
- * const items = await res.json()
104
- * return { items }
105
- * },
106
- * registerRemoteChange: (onChange) => subscribeToWS(onChange),
107
- * mergeItems: (a, b) => ({ ...a, ...b }),
108
- * purgeDelay: 10_000,
109
- * })
110
- *
111
- * const backend = adapter.createCollectionBackend(myCollection, ['status', 'projectId'])
112
- */
@@ -1,113 +0,0 @@
1
- import type ReactivityAdapter from '../types/ReactivityAdapter';
2
- import type { BaseItem, FindOptions, Transform } from './types';
3
- import type { ObserveCallbacks } from './Observer';
4
- /**
5
- * Checks if the current scope is reactive, considering the provided reactivity adapter.
6
- * @param reactivity - The reactivity adapter or a boolean indicating whether reactivity is enabled.
7
- * @returns A boolean indicating if the current scope is reactive.
8
- */
9
- export declare function isInReactiveScope(reactivity: ReactivityAdapter | undefined | false): boolean;
10
- export interface CursorOptions<T extends BaseItem, U = T, Async extends boolean = false> extends FindOptions<T, Async> {
11
- transform?: Transform<T, U>;
12
- bindEvents?: (requery: () => void) => () => void;
13
- }
14
- /**
15
- * Represents a cursor for querying and observing a filtered, sorted, and transformed
16
- * subset of items from a collection. Supports reactivity and field tracking.
17
- * @template T - The type of the items in the collection.
18
- * @template U - The transformed item type after applying transform (default is T).
19
- */
20
- export default class Cursor<T extends BaseItem, U = T, Async extends boolean = false> {
21
- private observer;
22
- private getItems;
23
- private options;
24
- private onCleanupCallbacks;
25
- /**
26
- * Creates a new instance of the `Cursor` class.
27
- * Provides utilities for querying, observing, and transforming items from a collection.
28
- * @template T - The type of the items in the collection.
29
- * @template U - The transformed item type after applying transformations (default is T).
30
- * @param getItems - A function that retrieves the filtered list of items.
31
- * @param options - Optional configuration for the cursor.
32
- * @param options.transform - A transformation function to apply to each item when retrieving them.
33
- * @param options.bindEvents - A function to bind reactivity events for the cursor, which should return a cleanup function.
34
- * @param options.fields - A projection object defining which fields of the item should be included or excluded.
35
- * @param options.sort - A sort specifier to determine the order of the items.
36
- * @param options.skip - The number of items to skip from the beginning of the result set.
37
- * @param options.limit - The maximum number of items to return in the result set.
38
- * @param options.reactive - A reactivity adapter to enable observing changes in the cursor's result set.
39
- * @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
40
- * @param options.transformAll - A function that will be able to solve the n+1 problem
41
- */
42
- constructor(getItems: Async extends true ? () => Promise<T[]> : () => T[], options?: CursorOptions<T, U, Async>);
43
- private addGetters;
44
- private transform;
45
- private depend;
46
- private ensureObserver;
47
- private observeRawChanges;
48
- /**
49
- * Cleans up all resources associated with the cursor, such as reactive bindings
50
- * and event listeners. This method should be called when the cursor is no longer needed
51
- * to prevent memory leaks.
52
- */
53
- cleanup(): void;
54
- /**
55
- * Registers a cleanup callback to be executed when the `cleanup` method is called.
56
- * Useful for managing resources and ensuring proper cleanup of bindings or listeners.
57
- * @param callback - A function to be executed during cleanup.
58
- */
59
- onCleanup(callback: () => void): void;
60
- /**
61
- * Iterates over each item in the cursor's result set, applying the provided callback
62
- * function to each transformed item.
63
- * ⚡️ this function is reactive!
64
- * @param callback - A function to execute for each item in the result set.
65
- * @param callback.item - The transformed item.
66
- * @returns A promise that resolves when all items have been processed, or void if not in async mode.
67
- */
68
- forEach(callback: (item: U) => void): Async extends true ? Promise<void> : void;
69
- /**
70
- * Creates a new array populated with the results of applying the provided callback
71
- * function to each transformed item in the cursor's result set.
72
- * ⚡️ this function is reactive!
73
- * @template V - The type of the items in the resulting array.
74
- * @param callback - A function to execute for each item in the result set.
75
- * @param callback.item - The transformed item.
76
- * @returns An array of results after applying the callback to each item.
77
- */
78
- map<V>(callback: (item: U) => V): Async extends true ? Promise<V[]> : V[];
79
- /**
80
- * Fetches all transformed items from the cursor's result set as an array.
81
- * Automatically applies filtering, sorting, and limiting as per the cursor's options.
82
- * ⚡️ this function is reactive!
83
- * @returns An array of transformed items in the result set.
84
- */
85
- fetch(): Async extends true ? Promise<U[]> : U[];
86
- /**
87
- * Counts the total number of items in the cursor's result set after applying
88
- * filtering and other criteria.
89
- * ⚡️ this function is reactive!
90
- * @returns The total number of items in the result set.
91
- */
92
- count(): Async extends true ? Promise<number> : number;
93
- /**
94
- * Observes changes to the cursor's result set and triggers the specified callbacks
95
- * when items are added, removed, or updated. Supports reactivity and transformation.
96
- * @param callbacks - An object containing the callback functions to handle different change events.
97
- * @param callbacks.added - Triggered when an item is added to the result set.
98
- * @param callbacks.removed - Triggered when an item is removed from the result set.
99
- * @param callbacks.changed - Triggered when an item in the result set is modified.
100
- * @param callbacks.addedBefore - Triggered when an item is added before another item in the result set.
101
- * @param callbacks.movedBefore - Triggered when an item is moved before another item in the result set.
102
- * @param callbacks.changedField - Triggered when a specific field of an item changes.
103
- * @param skipInitial - A boolean indicating whether to skip the initial notification of the current result set.
104
- * @returns A function to stop observing changes.
105
- */
106
- observeChanges(callbacks: ObserveCallbacks<T>, skipInitial?: boolean): () => void;
107
- /**
108
- * Forces the cursor to re-evaluate its result set by re-fetching items
109
- * from the collection. This is useful when the underlying data or query
110
- * criteria have changed, and you want to ensure the cursor reflects the latest state.
111
- */
112
- requery(): void;
113
- }
@@ -1,64 +0,0 @@
1
- type AddedCallback<T> = (item: T) => void;
2
- type AddedBeforeCallback<T> = (item: T, before: T) => void;
3
- type ChangedCallback<T> = (item: T) => void;
4
- type ChangedFieldCallback<T> = <Field extends keyof T>(item: T, field: Field, oldValue: T[Field], newValue: T[Field]) => void;
5
- type MovedBeforeCallback<T> = (item: T, before: T) => void;
6
- type RemovedCallback<T> = (item: T) => void;
7
- export interface ObserveCallbacks<T> {
8
- added?: AddedCallback<T>;
9
- addedBefore?: AddedBeforeCallback<T>;
10
- changed?: ChangedCallback<T>;
11
- changedField?: ChangedFieldCallback<T>;
12
- movedBefore?: MovedBeforeCallback<T>;
13
- removed?: RemovedCallback<T>;
14
- }
15
- /**
16
- * Represents an observer that tracks changes in a collection of items and triggers
17
- * callbacks for various events such as addition, removal, and modification of items.
18
- * @template T - The type of the items being observed, which must include an `id` field.
19
- */
20
- export default class Observer<T extends {
21
- id: any;
22
- }> {
23
- private previousItems;
24
- private callbacks;
25
- private unbindEvents;
26
- /**
27
- * Creates a new instance of the `Observer` class.
28
- * Sets up event bindings and initializes the callbacks for tracking changes in a collection.
29
- * @param bindEvents - A function to bind external events to the observer. Must return a cleanup function to unbind those events.
30
- */
31
- constructor(bindEvents: () => () => void);
32
- private call;
33
- private hasCallbacks;
34
- /**
35
- * Determines if the observer has no active callbacks registered for any events.
36
- * @returns A boolean indicating whether the observer is empty (i.e., no callbacks are registered).
37
- */
38
- isEmpty(): boolean;
39
- /**
40
- * Compares the previous state of items with the new state and triggers the appropriate callbacks
41
- * for events such as added, removed, changed, or moved items.
42
- * @param getItems - A function that returns a promise resolving to the new items or the items themselves.
43
- */
44
- runChecks(getItems: () => Promise<T[]> | T[]): void;
45
- private checkItems;
46
- private stopped;
47
- /**
48
- * Stops the observer by unbinding all events and cleaning up resources.
49
- * Safe to call multiple times - will only unbind once.
50
- */
51
- stop(): void;
52
- /**
53
- * Registers callbacks for specific events to observe changes in the collection.
54
- * @param callbacks - An object containing the callbacks for various events (e.g., 'added', 'removed').
55
- * @param skipInitial - A boolean indicating whether to skip invoking the callbacks for the initial state of the collection.
56
- */
57
- addCallbacks(callbacks: ObserveCallbacks<T>, skipInitial?: boolean): void;
58
- /**
59
- * Removes the specified callbacks for specific events, unregistering them from the observer.
60
- * @param callbacks - An object containing the callbacks to be removed for various events.
61
- */
62
- removeCallbacks(callbacks: ObserveCallbacks<T>): void;
63
- }
64
- export {};