@signaldb/svelte 2.0.0-beta.21 → 2.0.0-beta.22
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/package.json +3 -3
- package/dist/base/core/src/AsyncDataAdapter.d.ts +0 -112
- package/dist/base/core/src/AsyncDataAdapter.js +0 -549
- package/dist/base/core/src/AutoFetchDataAdapter.d.ts +0 -112
- package/dist/base/core/src/AutoFetchDataAdapter.js +0 -612
- package/dist/base/core/src/Collection/Cursor.d.ts +0 -165
- package/dist/base/core/src/Collection/Cursor.js +0 -352
- package/dist/base/core/src/Collection/Observer.d.ts +0 -95
- package/dist/base/core/src/Collection/Observer.js +0 -227
- package/dist/base/core/src/Collection/index.d.ts +0 -341
- package/dist/base/core/src/Collection/index.js +0 -761
- package/dist/base/core/src/Collection/types.d.ts +0 -28
- package/dist/base/core/src/Collection/types.js +0 -2
- package/dist/base/core/src/DataAdapter.d.ts +0 -54
- package/dist/base/core/src/DataAdapter.js +0 -2
- package/dist/base/core/src/DefaultDataAdapter.d.ts +0 -35
- package/dist/base/core/src/DefaultDataAdapter.js +0 -408
- package/dist/base/core/src/WorkerDataAdapter.d.ts +0 -79
- package/dist/base/core/src/WorkerDataAdapter.js +0 -749
- package/dist/base/core/src/WorkerDataAdapterHost.d.ts +0 -64
- package/dist/base/core/src/WorkerDataAdapterHost.js +0 -430
- package/dist/base/core/src/createIndex.d.ts +0 -7
- package/dist/base/core/src/createIndex.js +0 -120
- package/dist/base/core/src/createIndexProvider.d.ts +0 -8
- package/dist/base/core/src/createIndexProvider.js +0 -11
- package/dist/base/core/src/createReactivityAdapter.d.ts +0 -8
- package/dist/base/core/src/createReactivityAdapter.js +0 -11
- package/dist/base/core/src/createStorageAdapter.d.ts +0 -9
- package/dist/base/core/src/createStorageAdapter.js +0 -11
- package/dist/base/core/src/getIndexInfo.d.ts +0 -39
- package/dist/base/core/src/getIndexInfo.js +0 -188
- package/dist/base/core/src/index.d.ts +0 -26
- package/dist/base/core/src/index.js +0 -39
- package/dist/base/core/src/types/Dependency.d.ts +0 -4
- package/dist/base/core/src/types/Dependency.js +0 -2
- package/dist/base/core/src/types/IndexProvider.d.ts +0 -26
- package/dist/base/core/src/types/IndexProvider.js +0 -2
- package/dist/base/core/src/types/Modifier.d.ts +0 -46
- package/dist/base/core/src/types/Modifier.js +0 -3
- package/dist/base/core/src/types/ReactivityAdapter.d.ts +0 -6
- package/dist/base/core/src/types/ReactivityAdapter.js +0 -2
- package/dist/base/core/src/types/Selector.d.ts +0 -46
- package/dist/base/core/src/types/Selector.js +0 -3
- package/dist/base/core/src/types/Signal.d.ts +0 -4
- package/dist/base/core/src/types/Signal.js +0 -2
- package/dist/base/core/src/types/StorageAdapter.d.ts +0 -28
- package/dist/base/core/src/types/StorageAdapter.js +0 -2
- package/dist/base/core/src/utils/EventEmitter.d.ts +0 -71
- package/dist/base/core/src/utils/EventEmitter.js +0 -144
- package/dist/base/core/src/utils/applyQueryOptions.d.ts +0 -15
- package/dist/base/core/src/utils/applyQueryOptions.js +0 -36
- package/dist/base/core/src/utils/batchOnNextTick.d.ts +0 -16
- package/dist/base/core/src/utils/batchOnNextTick.js +0 -72
- package/dist/base/core/src/utils/compact.d.ts +0 -9
- package/dist/base/core/src/utils/compact.js +0 -21
- package/dist/base/core/src/utils/createSignal.d.ts +0 -14
- package/dist/base/core/src/utils/createSignal.js +0 -38
- package/dist/base/core/src/utils/deepClone.d.ts +0 -17
- package/dist/base/core/src/utils/deepClone.js +0 -68
- package/dist/base/core/src/utils/get.d.ts +0 -9
- package/dist/base/core/src/utils/get.js +0 -27
- package/dist/base/core/src/utils/getMatchingKeys.d.ts +0 -19
- package/dist/base/core/src/utils/getMatchingKeys.js +0 -49
- package/dist/base/core/src/utils/idIndexQuery.d.ts +0 -20
- package/dist/base/core/src/utils/idIndexQuery.js +0 -49
- package/dist/base/core/src/utils/incrementalQueryUpdate.d.ts +0 -62
- package/dist/base/core/src/utils/incrementalQueryUpdate.js +0 -197
- package/dist/base/core/src/utils/intersection.d.ts +0 -9
- package/dist/base/core/src/utils/intersection.js +0 -16
- package/dist/base/core/src/utils/isEqual.d.ts +0 -14
- package/dist/base/core/src/utils/isEqual.js +0 -43
- package/dist/base/core/src/utils/isFieldExpression.d.ts +0 -11
- package/dist/base/core/src/utils/isFieldExpression.js +0 -52
- package/dist/base/core/src/utils/match.d.ts +0 -12
- package/dist/base/core/src/utils/match.js +0 -16
- package/dist/base/core/src/utils/modify.d.ts +0 -14
- package/dist/base/core/src/utils/modify.js +0 -29
- package/dist/base/core/src/utils/project.d.ts +0 -15
- package/dist/base/core/src/utils/project.js +0 -43
- package/dist/base/core/src/utils/projectItems.d.ts +0 -12
- package/dist/base/core/src/utils/projectItems.js +0 -25
- package/dist/base/core/src/utils/queryDelta.d.ts +0 -83
- package/dist/base/core/src/utils/queryDelta.js +0 -231
- package/dist/base/core/src/utils/queryId.d.ts +0 -9
- package/dist/base/core/src/utils/queryId.js +0 -61
- package/dist/base/core/src/utils/randomId.d.ts +0 -7
- package/dist/base/core/src/utils/randomId.js +0 -12
- package/dist/base/core/src/utils/reactiveOrAsync.d.ts +0 -59
- package/dist/base/core/src/utils/reactiveOrAsync.js +0 -93
- package/dist/base/core/src/utils/serializeValue.d.ts +0 -12
- package/dist/base/core/src/utils/serializeValue.js +0 -27
- package/dist/base/core/src/utils/set.d.ts +0 -13
- package/dist/base/core/src/utils/set.js +0 -43
- package/dist/base/core/src/utils/sortItems.d.ts +0 -12
- package/dist/base/core/src/utils/sortItems.js +0 -23
- package/dist/base/core/src/utils/storageIndexQuery.d.ts +0 -20
- package/dist/base/core/src/utils/storageIndexQuery.js +0 -75
- package/dist/base/core/src/utils/uniqueBy.d.ts +0 -10
- package/dist/base/core/src/utils/uniqueBy.js +0 -19
- package/dist/index.cjs +0 -38
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs +0 -38
- package/dist/index.mjs.map +0 -1
- package/dist/reactivity-adapters/svelte/index.svelte.d.ts +0 -10
- package/dist/reactivity-adapters/svelte/index.svelte.js +0 -36
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
import type ReactivityAdapter from '../types/ReactivityAdapter';
|
|
2
|
-
import type { QueryDelta } from '../utils/queryDelta';
|
|
3
|
-
import type { BaseItem, FindOptions, Transform } from './types';
|
|
4
|
-
import type { ObserveCallbacks } from './Observer';
|
|
5
|
-
/**
|
|
6
|
-
* Checks if the current scope is reactive, considering the provided reactivity adapter.
|
|
7
|
-
* @param reactivity - The reactivity adapter or a boolean indicating whether reactivity is enabled.
|
|
8
|
-
* @returns A boolean indicating if the current scope is reactive.
|
|
9
|
-
*/
|
|
10
|
-
export declare function isInReactiveScope(reactivity: ReactivityAdapter | undefined | false): boolean;
|
|
11
|
-
/**
|
|
12
|
-
* Reports whether the query a cursor stands for has produced an outcome yet.
|
|
13
|
-
* Supplied by the collection, which owns the query registration; a cursor
|
|
14
|
-
* built without one simply never reports itself as loading.
|
|
15
|
-
*/
|
|
16
|
-
export interface QueryStateAccessor {
|
|
17
|
-
/**
|
|
18
|
-
* Whether the query has settled — completed or failed — at least once
|
|
19
|
-
* since it was registered.
|
|
20
|
-
*/
|
|
21
|
-
hasSettled: () => boolean;
|
|
22
|
-
/**
|
|
23
|
-
* Subscribes to the query settling. Returns a cleanup function.
|
|
24
|
-
*/
|
|
25
|
-
onSettled: (callback: () => void) => () => void;
|
|
26
|
-
}
|
|
27
|
-
export interface CursorOptions<T extends BaseItem, U = T, Async extends boolean = false> extends FindOptions<T, Async> {
|
|
28
|
-
transform?: Transform<T, U>;
|
|
29
|
-
bindEvents?: (requery: () => void, applyDelta: (delta: QueryDelta<T>) => void) => () => void;
|
|
30
|
-
queryState?: QueryStateAccessor;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Represents a cursor for querying and observing a filtered, sorted, and transformed
|
|
34
|
-
* subset of items from a collection. Supports reactivity and field tracking.
|
|
35
|
-
* @template T - The type of the items in the collection.
|
|
36
|
-
* @template U - The transformed item type after applying transform (default is T).
|
|
37
|
-
*/
|
|
38
|
-
export default class Cursor<T extends BaseItem, U = T, Async extends boolean = false> {
|
|
39
|
-
private observer;
|
|
40
|
-
private getItems;
|
|
41
|
-
private options;
|
|
42
|
-
private onCleanupCallbacks;
|
|
43
|
-
/**
|
|
44
|
-
* Creates a new instance of the `Cursor` class.
|
|
45
|
-
* Provides utilities for querying, observing, and transforming items from a collection.
|
|
46
|
-
* @template T - The type of the items in the collection.
|
|
47
|
-
* @template U - The transformed item type after applying transformations (default is T).
|
|
48
|
-
* @param getItems - A function that retrieves the filtered list of items.
|
|
49
|
-
* @param options - Optional configuration for the cursor.
|
|
50
|
-
* @param options.transform - A transformation function to apply to each item when retrieving them.
|
|
51
|
-
* @param options.bindEvents - A function to bind reactivity events for the cursor, which should return a cleanup function.
|
|
52
|
-
* @param options.fields - A projection object defining which fields of the item should be included or excluded.
|
|
53
|
-
* @param options.sort - A sort specifier to determine the order of the items.
|
|
54
|
-
* @param options.skip - The number of items to skip from the beginning of the result set.
|
|
55
|
-
* @param options.limit - The maximum number of items to return in the result set.
|
|
56
|
-
* @param options.reactive - A reactivity adapter to enable observing changes in the cursor's result set.
|
|
57
|
-
* @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
|
|
58
|
-
* @param options.transformAll - A function that will be able to solve the n+1 problem
|
|
59
|
-
*/
|
|
60
|
-
constructor(getItems: Async extends true ? () => Promise<T[]> : () => T[], options?: CursorOptions<T, U, Async>);
|
|
61
|
-
private addGetters;
|
|
62
|
-
private transform;
|
|
63
|
-
private depend;
|
|
64
|
-
private ensureObserver;
|
|
65
|
-
private observeRawChanges;
|
|
66
|
-
/**
|
|
67
|
-
* Cleans up all resources associated with the cursor, such as reactive bindings
|
|
68
|
-
* and event listeners. This method should be called when the cursor is no longer needed
|
|
69
|
-
* to prevent memory leaks.
|
|
70
|
-
*/
|
|
71
|
-
cleanup(): void;
|
|
72
|
-
/**
|
|
73
|
-
* Registers a cleanup callback to be executed when the `cleanup` method is called.
|
|
74
|
-
* Useful for managing resources and ensuring proper cleanup of bindings or listeners.
|
|
75
|
-
* @param callback - A function to be executed during cleanup.
|
|
76
|
-
*/
|
|
77
|
-
onCleanup(callback: () => void): void;
|
|
78
|
-
/**
|
|
79
|
-
* Iterates over each item in the cursor's result set, applying the provided callback
|
|
80
|
-
* function to each transformed item.
|
|
81
|
-
* ⚡️ this function is reactive!
|
|
82
|
-
* @param callback - A function to execute for each item in the result set.
|
|
83
|
-
* @param callback.item - The transformed item.
|
|
84
|
-
* @returns A promise that resolves when all items have been processed, or void if not in async mode.
|
|
85
|
-
*/
|
|
86
|
-
forEach(callback: (item: U) => void): Async extends true ? Promise<void> : void;
|
|
87
|
-
/**
|
|
88
|
-
* Creates a new array populated with the results of applying the provided callback
|
|
89
|
-
* function to each transformed item in the cursor's result set.
|
|
90
|
-
* ⚡️ this function is reactive!
|
|
91
|
-
* @template V - The type of the items in the resulting array.
|
|
92
|
-
* @param callback - A function to execute for each item in the result set.
|
|
93
|
-
* @param callback.item - The transformed item.
|
|
94
|
-
* @returns An array of results after applying the callback to each item.
|
|
95
|
-
*/
|
|
96
|
-
map<V>(callback: (item: U) => V): Async extends true ? Promise<V[]> : V[];
|
|
97
|
-
/**
|
|
98
|
-
* Fetches all transformed items from the cursor's result set as an array.
|
|
99
|
-
* Automatically applies filtering, sorting, and limiting as per the cursor's options.
|
|
100
|
-
* ⚡️ this function is reactive!
|
|
101
|
-
* @returns An array of transformed items in the result set.
|
|
102
|
-
*/
|
|
103
|
-
fetch(): Async extends true ? Promise<U[]> : U[];
|
|
104
|
-
/**
|
|
105
|
-
* Counts the total number of items in the cursor's result set after applying
|
|
106
|
-
* filtering and other criteria.
|
|
107
|
-
* ⚡️ this function is reactive!
|
|
108
|
-
* @returns The total number of items in the result set.
|
|
109
|
-
*/
|
|
110
|
-
count(): Async extends true ? Promise<number> : number;
|
|
111
|
-
/**
|
|
112
|
-
* Whether this cursor's query has yet to deliver a first result.
|
|
113
|
-
* ⚡️ this function is reactive!
|
|
114
|
-
*
|
|
115
|
-
* An asynchronous data adapter answers a newly registered query only after a
|
|
116
|
-
* round trip, and serves a neutral empty result until it does — which a
|
|
117
|
-
* consumer cannot otherwise tell apart from "there is nothing to show". This
|
|
118
|
-
* is that distinction, per query rather than per collection, so one screen
|
|
119
|
-
* waiting on its own data says nothing about any other query.
|
|
120
|
-
*
|
|
121
|
-
* Follows the usual `isLoading`/`isFetching` split: it reports "no result
|
|
122
|
-
* yet", not "an execution is in flight". A write that re-runs an
|
|
123
|
-
* already-settled query drives it through `'active'` again while this stays
|
|
124
|
-
* `false`, so a list does not fall back to a loading state every time one of
|
|
125
|
-
* its rows changes.
|
|
126
|
-
*
|
|
127
|
-
* A query that fails counts as settled — the `query.error` event on the
|
|
128
|
-
* collection is what surfaces the failure, and a loading state that never
|
|
129
|
-
* ends is the worse answer. Reading this registers the query if nothing else
|
|
130
|
-
* has, so it cannot wait on something nobody asked for. It is always `false`
|
|
131
|
-
* for an `{ async: true }` cursor, whose `fetch()` awaits the real result
|
|
132
|
-
* anyway, and for a data adapter that answers synchronously.
|
|
133
|
-
* @returns A boolean indicating whether the first result is still pending.
|
|
134
|
-
*/
|
|
135
|
-
isLoading(): boolean;
|
|
136
|
-
/**
|
|
137
|
-
* Observes changes to the cursor's result set and triggers the specified callbacks
|
|
138
|
-
* when items are added, removed, or updated. Supports reactivity and transformation.
|
|
139
|
-
* @param callbacks - An object containing the callback functions to handle different change events.
|
|
140
|
-
* @param callbacks.added - Triggered when an item is added to the result set.
|
|
141
|
-
* @param callbacks.removed - Triggered when an item is removed from the result set.
|
|
142
|
-
* @param callbacks.changed - Triggered when an item in the result set is modified.
|
|
143
|
-
* @param callbacks.addedBefore - Triggered when an item is added before another item in the result set.
|
|
144
|
-
* @param callbacks.movedBefore - Triggered when an item is moved before another item in the result set.
|
|
145
|
-
* @param callbacks.changedField - Triggered when a specific field of an item changes.
|
|
146
|
-
* @param skipInitial - A boolean indicating whether to skip the initial notification of the current result set.
|
|
147
|
-
* @returns A function to stop observing changes.
|
|
148
|
-
*/
|
|
149
|
-
observeChanges(callbacks: ObserveCallbacks<T>, skipInitial?: boolean): () => void;
|
|
150
|
-
/**
|
|
151
|
-
* Forces the cursor to re-evaluate its result set by re-fetching items
|
|
152
|
-
* from the collection. This is useful when the underlying data or query
|
|
153
|
-
* criteria have changed, and you want to ensure the cursor reflects the latest state.
|
|
154
|
-
*/
|
|
155
|
-
requery(): void;
|
|
156
|
-
/**
|
|
157
|
-
* Brings the cursor up to date from a description of what changed, rather than by re-running the
|
|
158
|
-
* query and comparing the result with the previous one.
|
|
159
|
-
*
|
|
160
|
-
* Falls back to `requery` when the delta does not fit the result the cursor currently holds, so
|
|
161
|
-
* a caller never has to decide which of the two is safe.
|
|
162
|
-
* @param delta - The change to apply.
|
|
163
|
-
*/
|
|
164
|
-
applyDelta(delta: QueryDelta<T>): void;
|
|
165
|
-
}
|
|
@@ -1,352 +0,0 @@
|
|
|
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.isInReactiveScope = isInReactiveScope;
|
|
7
|
-
const Observer_1 = __importDefault(require("./Observer"));
|
|
8
|
-
/**
|
|
9
|
-
* Checks if the current scope is reactive, considering the provided reactivity adapter.
|
|
10
|
-
* @param reactivity - The reactivity adapter or a boolean indicating whether reactivity is enabled.
|
|
11
|
-
* @returns A boolean indicating if the current scope is reactive.
|
|
12
|
-
*/
|
|
13
|
-
function isInReactiveScope(reactivity) {
|
|
14
|
-
if (!reactivity)
|
|
15
|
-
return false; // if reactivity is disabled we don't need to check
|
|
16
|
-
if (!reactivity.isInScope)
|
|
17
|
-
return true; // if reactivity is enabled and no isInScope method is provided we assume it is in scope
|
|
18
|
-
return reactivity.isInScope(); // if reactivity is enabled and isInScope method is provided we check if it is in scope
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Represents a cursor for querying and observing a filtered, sorted, and transformed
|
|
22
|
-
* subset of items from a collection. Supports reactivity and field tracking.
|
|
23
|
-
* @template T - The type of the items in the collection.
|
|
24
|
-
* @template U - The transformed item type after applying transform (default is T).
|
|
25
|
-
*/
|
|
26
|
-
class Cursor {
|
|
27
|
-
observer;
|
|
28
|
-
getItems;
|
|
29
|
-
options;
|
|
30
|
-
onCleanupCallbacks = [];
|
|
31
|
-
/**
|
|
32
|
-
* Creates a new instance of the `Cursor` class.
|
|
33
|
-
* Provides utilities for querying, observing, and transforming items from a collection.
|
|
34
|
-
* @template T - The type of the items in the collection.
|
|
35
|
-
* @template U - The transformed item type after applying transformations (default is T).
|
|
36
|
-
* @param getItems - A function that retrieves the filtered list of items.
|
|
37
|
-
* @param options - Optional configuration for the cursor.
|
|
38
|
-
* @param options.transform - A transformation function to apply to each item when retrieving them.
|
|
39
|
-
* @param options.bindEvents - A function to bind reactivity events for the cursor, which should return a cleanup function.
|
|
40
|
-
* @param options.fields - A projection object defining which fields of the item should be included or excluded.
|
|
41
|
-
* @param options.sort - A sort specifier to determine the order of the items.
|
|
42
|
-
* @param options.skip - The number of items to skip from the beginning of the result set.
|
|
43
|
-
* @param options.limit - The maximum number of items to return in the result set.
|
|
44
|
-
* @param options.reactive - A reactivity adapter to enable observing changes in the cursor's result set.
|
|
45
|
-
* @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
|
|
46
|
-
* @param options.transformAll - A function that will be able to solve the n+1 problem
|
|
47
|
-
*/
|
|
48
|
-
constructor(getItems, options) {
|
|
49
|
-
this.getItems = getItems;
|
|
50
|
-
this.options = options || {};
|
|
51
|
-
}
|
|
52
|
-
addGetters(item) {
|
|
53
|
-
if (!isInReactiveScope(this.options.reactive))
|
|
54
|
-
return item;
|
|
55
|
-
const depend = this.depend.bind(this);
|
|
56
|
-
return Object.entries(item).reduce((memo, [key, value]) => {
|
|
57
|
-
Object.defineProperty(memo, key, {
|
|
58
|
-
get() {
|
|
59
|
-
depend({
|
|
60
|
-
changedField: notify => (changedItem, changedFieldName) => {
|
|
61
|
-
if (changedFieldName !== key || changedItem.id !== item.id)
|
|
62
|
-
return;
|
|
63
|
-
notify();
|
|
64
|
-
},
|
|
65
|
-
});
|
|
66
|
-
return value;
|
|
67
|
-
},
|
|
68
|
-
enumerable: true,
|
|
69
|
-
configurable: true,
|
|
70
|
-
});
|
|
71
|
-
return memo;
|
|
72
|
-
}, {});
|
|
73
|
-
}
|
|
74
|
-
transform(rawItem) {
|
|
75
|
-
const item = this.options.fieldTracking
|
|
76
|
-
? this.addGetters(rawItem)
|
|
77
|
-
: rawItem;
|
|
78
|
-
if (!this.options.transform)
|
|
79
|
-
return item;
|
|
80
|
-
return this.options.transform(item);
|
|
81
|
-
}
|
|
82
|
-
depend(changeEvents, bindExtraNotifier) {
|
|
83
|
-
if (this.options?.async)
|
|
84
|
-
return;
|
|
85
|
-
if (!isInReactiveScope(this.options.reactive)) {
|
|
86
|
-
// eslint-disable-next-line no-console
|
|
87
|
-
console.warn('Cursor.depend() called outside of a reactive scope without async option; consider using { async: true } or wrapping in a reactive scope');
|
|
88
|
-
}
|
|
89
|
-
if (!this.options.reactive)
|
|
90
|
-
return;
|
|
91
|
-
const signal = this.options.reactive.create();
|
|
92
|
-
signal.depend();
|
|
93
|
-
const notify = () => signal.notify();
|
|
94
|
-
/**
|
|
95
|
-
* Builds a notifier function for the specified event.
|
|
96
|
-
* @template Event - The type of the event.
|
|
97
|
-
* @param event - The event for which to build the notifier.
|
|
98
|
-
* @returns A function that handles the event and triggers the appropriate notifications.
|
|
99
|
-
*/
|
|
100
|
-
function buildNotifier(event) {
|
|
101
|
-
const eventHandler = changeEvents[event];
|
|
102
|
-
return (...args) => {
|
|
103
|
-
// if the event is just turned on with true, we can notify directly
|
|
104
|
-
if (eventHandler === true) {
|
|
105
|
-
notify();
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
// if the event is something else than true or a function, we don't care about it
|
|
109
|
-
if (typeof eventHandler !== 'function')
|
|
110
|
-
return;
|
|
111
|
-
// if the event is a function, we call it with the notify function
|
|
112
|
-
eventHandler(notify)(...args);
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
const stop = this.observeRawChanges({
|
|
116
|
-
added: buildNotifier('added'),
|
|
117
|
-
addedBefore: buildNotifier('addedBefore'),
|
|
118
|
-
changed: buildNotifier('changed'),
|
|
119
|
-
changedField: buildNotifier('changedField'),
|
|
120
|
-
movedBefore: buildNotifier('movedBefore'),
|
|
121
|
-
removed: buildNotifier('removed'),
|
|
122
|
-
}, true);
|
|
123
|
-
if (this.options.reactive.onDispose) {
|
|
124
|
-
this.options.reactive.onDispose(() => stop(), signal);
|
|
125
|
-
}
|
|
126
|
-
this.onCleanup(stop);
|
|
127
|
-
// A notifier that isn't driven by the result set itself — `isLoading()`
|
|
128
|
-
// needs to re-run its scope when the query settles, which is precisely
|
|
129
|
-
// the moment the result set may *not* have changed (an empty query
|
|
130
|
-
// completing produces no diff, so the observer above stays silent).
|
|
131
|
-
if (!bindExtraNotifier)
|
|
132
|
-
return;
|
|
133
|
-
const stopExtraNotifier = bindExtraNotifier(notify);
|
|
134
|
-
if (this.options.reactive.onDispose) {
|
|
135
|
-
this.options.reactive.onDispose(() => stopExtraNotifier(), signal);
|
|
136
|
-
}
|
|
137
|
-
this.onCleanup(stopExtraNotifier);
|
|
138
|
-
}
|
|
139
|
-
ensureObserver() {
|
|
140
|
-
if (!this.observer) {
|
|
141
|
-
const observer = new Observer_1.default(() => {
|
|
142
|
-
const requery = () => {
|
|
143
|
-
observer.runChecks(this.getItems);
|
|
144
|
-
};
|
|
145
|
-
const applyDelta = (delta) => {
|
|
146
|
-
observer.applyDelta(delta, this.getItems);
|
|
147
|
-
};
|
|
148
|
-
const cleanup = this.options.bindEvents
|
|
149
|
-
&& this.options.bindEvents(requery, applyDelta);
|
|
150
|
-
return () => {
|
|
151
|
-
if (cleanup)
|
|
152
|
-
cleanup();
|
|
153
|
-
};
|
|
154
|
-
});
|
|
155
|
-
this.onCleanup(() => observer.stop());
|
|
156
|
-
this.observer = observer;
|
|
157
|
-
}
|
|
158
|
-
return this.observer;
|
|
159
|
-
}
|
|
160
|
-
observeRawChanges(callbacks, skipInitial = false) {
|
|
161
|
-
const observer = this.ensureObserver();
|
|
162
|
-
observer.addCallbacks(callbacks, skipInitial);
|
|
163
|
-
observer.runChecks(this.getItems);
|
|
164
|
-
return () => {
|
|
165
|
-
observer.removeCallbacks(callbacks);
|
|
166
|
-
if (!observer.isEmpty())
|
|
167
|
-
return;
|
|
168
|
-
// remove observer if it's empty
|
|
169
|
-
observer.stop();
|
|
170
|
-
this.observer = undefined;
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Cleans up all resources associated with the cursor, such as reactive bindings
|
|
175
|
-
* and event listeners. This method should be called when the cursor is no longer needed
|
|
176
|
-
* to prevent memory leaks.
|
|
177
|
-
*/
|
|
178
|
-
cleanup() {
|
|
179
|
-
this.onCleanupCallbacks.forEach((callback) => {
|
|
180
|
-
callback();
|
|
181
|
-
});
|
|
182
|
-
this.onCleanupCallbacks = [];
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Registers a cleanup callback to be executed when the `cleanup` method is called.
|
|
186
|
-
* Useful for managing resources and ensuring proper cleanup of bindings or listeners.
|
|
187
|
-
* @param callback - A function to be executed during cleanup.
|
|
188
|
-
*/
|
|
189
|
-
onCleanup(callback) {
|
|
190
|
-
this.onCleanupCallbacks.push(callback);
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Iterates over each item in the cursor's result set, applying the provided callback
|
|
194
|
-
* function to each transformed item.
|
|
195
|
-
* ⚡️ this function is reactive!
|
|
196
|
-
* @param callback - A function to execute for each item in the result set.
|
|
197
|
-
* @param callback.item - The transformed item.
|
|
198
|
-
* @returns A promise that resolves when all items have been processed, or void if not in async mode.
|
|
199
|
-
*/
|
|
200
|
-
forEach(callback) {
|
|
201
|
-
this.depend({
|
|
202
|
-
addedBefore: true,
|
|
203
|
-
removed: true,
|
|
204
|
-
movedBefore: true,
|
|
205
|
-
...this.options.fieldTracking ? {} : { changed: true },
|
|
206
|
-
});
|
|
207
|
-
const executeForEach = (items) => {
|
|
208
|
-
items.forEach((item) => {
|
|
209
|
-
callback(this.transform(item));
|
|
210
|
-
});
|
|
211
|
-
};
|
|
212
|
-
const result = this.getItems();
|
|
213
|
-
if (result instanceof Promise) {
|
|
214
|
-
return result.then(executeForEach);
|
|
215
|
-
}
|
|
216
|
-
else {
|
|
217
|
-
executeForEach(result);
|
|
218
|
-
return undefined;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Creates a new array populated with the results of applying the provided callback
|
|
223
|
-
* function to each transformed item in the cursor's result set.
|
|
224
|
-
* ⚡️ this function is reactive!
|
|
225
|
-
* @template V - The type of the items in the resulting array.
|
|
226
|
-
* @param callback - A function to execute for each item in the result set.
|
|
227
|
-
* @param callback.item - The transformed item.
|
|
228
|
-
* @returns An array of results after applying the callback to each item.
|
|
229
|
-
*/
|
|
230
|
-
map(callback) {
|
|
231
|
-
const results = [];
|
|
232
|
-
const maybePromise = this.forEach((item) => {
|
|
233
|
-
results.push(callback(item));
|
|
234
|
-
});
|
|
235
|
-
if (maybePromise instanceof Promise) {
|
|
236
|
-
return maybePromise.then(() => results);
|
|
237
|
-
}
|
|
238
|
-
return results;
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Fetches all transformed items from the cursor's result set as an array.
|
|
242
|
-
* Automatically applies filtering, sorting, and limiting as per the cursor's options.
|
|
243
|
-
* ⚡️ this function is reactive!
|
|
244
|
-
* @returns An array of transformed items in the result set.
|
|
245
|
-
*/
|
|
246
|
-
fetch() {
|
|
247
|
-
return this.map(item => item);
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Counts the total number of items in the cursor's result set after applying
|
|
251
|
-
* filtering and other criteria.
|
|
252
|
-
* ⚡️ this function is reactive!
|
|
253
|
-
* @returns The total number of items in the result set.
|
|
254
|
-
*/
|
|
255
|
-
count() {
|
|
256
|
-
this.depend({
|
|
257
|
-
added: true,
|
|
258
|
-
removed: true,
|
|
259
|
-
});
|
|
260
|
-
const maybePromise = this.getItems();
|
|
261
|
-
return (maybePromise instanceof Promise
|
|
262
|
-
? maybePromise.then(items => items.length)
|
|
263
|
-
: maybePromise.length);
|
|
264
|
-
}
|
|
265
|
-
/**
|
|
266
|
-
* Whether this cursor's query has yet to deliver a first result.
|
|
267
|
-
* ⚡️ this function is reactive!
|
|
268
|
-
*
|
|
269
|
-
* An asynchronous data adapter answers a newly registered query only after a
|
|
270
|
-
* round trip, and serves a neutral empty result until it does — which a
|
|
271
|
-
* consumer cannot otherwise tell apart from "there is nothing to show". This
|
|
272
|
-
* is that distinction, per query rather than per collection, so one screen
|
|
273
|
-
* waiting on its own data says nothing about any other query.
|
|
274
|
-
*
|
|
275
|
-
* Follows the usual `isLoading`/`isFetching` split: it reports "no result
|
|
276
|
-
* yet", not "an execution is in flight". A write that re-runs an
|
|
277
|
-
* already-settled query drives it through `'active'` again while this stays
|
|
278
|
-
* `false`, so a list does not fall back to a loading state every time one of
|
|
279
|
-
* its rows changes.
|
|
280
|
-
*
|
|
281
|
-
* A query that fails counts as settled — the `query.error` event on the
|
|
282
|
-
* collection is what surfaces the failure, and a loading state that never
|
|
283
|
-
* ends is the worse answer. Reading this registers the query if nothing else
|
|
284
|
-
* has, so it cannot wait on something nobody asked for. It is always `false`
|
|
285
|
-
* for an `{ async: true }` cursor, whose `fetch()` awaits the real result
|
|
286
|
-
* anyway, and for a data adapter that answers synchronously.
|
|
287
|
-
* @returns A boolean indicating whether the first result is still pending.
|
|
288
|
-
*/
|
|
289
|
-
isLoading() {
|
|
290
|
-
const queryState = this.options.queryState;
|
|
291
|
-
if (!queryState || this.options.async)
|
|
292
|
-
return false;
|
|
293
|
-
this.depend({}, notify => queryState.onSettled(notify));
|
|
294
|
-
return !queryState.hasSettled();
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Observes changes to the cursor's result set and triggers the specified callbacks
|
|
298
|
-
* when items are added, removed, or updated. Supports reactivity and transformation.
|
|
299
|
-
* @param callbacks - An object containing the callback functions to handle different change events.
|
|
300
|
-
* @param callbacks.added - Triggered when an item is added to the result set.
|
|
301
|
-
* @param callbacks.removed - Triggered when an item is removed from the result set.
|
|
302
|
-
* @param callbacks.changed - Triggered when an item in the result set is modified.
|
|
303
|
-
* @param callbacks.addedBefore - Triggered when an item is added before another item in the result set.
|
|
304
|
-
* @param callbacks.movedBefore - Triggered when an item is moved before another item in the result set.
|
|
305
|
-
* @param callbacks.changedField - Triggered when a specific field of an item changes.
|
|
306
|
-
* @param skipInitial - A boolean indicating whether to skip the initial notification of the current result set.
|
|
307
|
-
* @returns A function to stop observing changes.
|
|
308
|
-
*/
|
|
309
|
-
observeChanges(callbacks, skipInitial = false) {
|
|
310
|
-
return this.observeRawChanges(Object
|
|
311
|
-
.entries(callbacks)
|
|
312
|
-
.reduce((memo, [callbackName, callback]) => {
|
|
313
|
-
if (!callback)
|
|
314
|
-
return memo;
|
|
315
|
-
return {
|
|
316
|
-
...memo,
|
|
317
|
-
[callbackName]: (item, before) => {
|
|
318
|
-
const transformedValue = this.transform(item);
|
|
319
|
-
const hasBeforeParameter = before !== undefined;
|
|
320
|
-
const transformedBeforeValue = hasBeforeParameter && before
|
|
321
|
-
? this.transform(before)
|
|
322
|
-
: null;
|
|
323
|
-
return callback(transformedValue, ...hasBeforeParameter ? [transformedBeforeValue] : []);
|
|
324
|
-
},
|
|
325
|
-
};
|
|
326
|
-
}, {}), skipInitial);
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* Forces the cursor to re-evaluate its result set by re-fetching items
|
|
330
|
-
* from the collection. This is useful when the underlying data or query
|
|
331
|
-
* criteria have changed, and you want to ensure the cursor reflects the latest state.
|
|
332
|
-
*/
|
|
333
|
-
requery() {
|
|
334
|
-
if (!this.observer)
|
|
335
|
-
return;
|
|
336
|
-
this.observer.runChecks(this.getItems);
|
|
337
|
-
}
|
|
338
|
-
/**
|
|
339
|
-
* Brings the cursor up to date from a description of what changed, rather than by re-running the
|
|
340
|
-
* query and comparing the result with the previous one.
|
|
341
|
-
*
|
|
342
|
-
* Falls back to `requery` when the delta does not fit the result the cursor currently holds, so
|
|
343
|
-
* a caller never has to decide which of the two is safe.
|
|
344
|
-
* @param delta - The change to apply.
|
|
345
|
-
*/
|
|
346
|
-
applyDelta(delta) {
|
|
347
|
-
if (!this.observer)
|
|
348
|
-
return;
|
|
349
|
-
this.observer.applyDelta(delta, this.getItems);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
exports.default = Cursor;
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import type { QueryDelta } from '../utils/queryDelta';
|
|
2
|
-
type AddedCallback<T> = (item: T) => void;
|
|
3
|
-
type AddedBeforeCallback<T> = (item: T, before: T) => void;
|
|
4
|
-
type ChangedCallback<T> = (item: T) => void;
|
|
5
|
-
type ChangedFieldCallback<T> = <Field extends keyof T>(item: T, field: Field, oldValue: T[Field], newValue: T[Field]) => void;
|
|
6
|
-
type MovedBeforeCallback<T> = (item: T, before: T) => void;
|
|
7
|
-
type RemovedCallback<T> = (item: T) => void;
|
|
8
|
-
export interface ObserveCallbacks<T> {
|
|
9
|
-
added?: AddedCallback<T>;
|
|
10
|
-
addedBefore?: AddedBeforeCallback<T>;
|
|
11
|
-
changed?: ChangedCallback<T>;
|
|
12
|
-
changedField?: ChangedFieldCallback<T>;
|
|
13
|
-
movedBefore?: MovedBeforeCallback<T>;
|
|
14
|
-
removed?: RemovedCallback<T>;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Represents an observer that tracks changes in a collection of items and triggers
|
|
18
|
-
* callbacks for various events such as addition, removal, and modification of items.
|
|
19
|
-
* @template T - The type of the items being observed, which must include an `id` field.
|
|
20
|
-
*/
|
|
21
|
-
export default class Observer<T extends {
|
|
22
|
-
id: any;
|
|
23
|
-
}> {
|
|
24
|
-
private previousItems;
|
|
25
|
-
private callbacks;
|
|
26
|
-
private unbindEvents;
|
|
27
|
-
/**
|
|
28
|
-
* Creates a new instance of the `Observer` class.
|
|
29
|
-
* Sets up event bindings and initializes the callbacks for tracking changes in a collection.
|
|
30
|
-
* @param bindEvents - A function to bind external events to the observer. Must return a cleanup function to unbind those events.
|
|
31
|
-
*/
|
|
32
|
-
constructor(bindEvents: () => () => void);
|
|
33
|
-
private call;
|
|
34
|
-
private hasCallbacks;
|
|
35
|
-
/**
|
|
36
|
-
* Determines if the observer has no active callbacks registered for any events.
|
|
37
|
-
* @returns A boolean indicating whether the observer is empty (i.e., no callbacks are registered).
|
|
38
|
-
*/
|
|
39
|
-
isEmpty(): boolean;
|
|
40
|
-
/**
|
|
41
|
-
* Compares the previous state of items with the new state and triggers the appropriate callbacks
|
|
42
|
-
* for events such as added, removed, changed, or moved items.
|
|
43
|
-
* @param getItems - A function that returns a promise resolving to the new items or the items themselves.
|
|
44
|
-
*/
|
|
45
|
-
runChecks(getItems: () => Promise<T[]> | T[]): void;
|
|
46
|
-
/**
|
|
47
|
-
* Brings the observer up to date from a description of what changed, instead of from the new
|
|
48
|
-
* result.
|
|
49
|
-
*
|
|
50
|
-
* `runChecks` has to rediscover the change by comparing the whole new result against the whole
|
|
51
|
-
* old one — a cost proportional to the result, paid on every write, to find out that one row
|
|
52
|
-
* moved. When the change is already known it can simply be reported, and the cost becomes
|
|
53
|
-
* proportional to the change.
|
|
54
|
-
*
|
|
55
|
-
* The delta must have been computed against exactly the result this observer holds. If it was
|
|
56
|
-
* not, this reports nothing, falls back to `runChecks`, and returns `false`.
|
|
57
|
-
*
|
|
58
|
-
* Note that the reported moves are minimal, where a comparison reports every item whose
|
|
59
|
-
* neighbour changed. Applying them yields the same order either way — there are simply fewer of
|
|
60
|
-
* them.
|
|
61
|
-
* @param delta - The change to report.
|
|
62
|
-
* @param getItems - Used to fall back to a comparison when the delta cannot be applied.
|
|
63
|
-
* @returns Whether the delta was applied.
|
|
64
|
-
*/
|
|
65
|
-
applyDelta(delta: QueryDelta<T>, getItems: () => Promise<T[]> | T[]): boolean;
|
|
66
|
-
/**
|
|
67
|
-
* Reports a delta and adopts the result it produces.
|
|
68
|
-
*
|
|
69
|
-
* The single place the callbacks are fired from, whether the change arrived as a delta or was
|
|
70
|
-
* found by comparing two results — so the two can never disagree about what a consumer is told.
|
|
71
|
-
* @param delta - The change to report.
|
|
72
|
-
* @param nextItems - The result the delta produces.
|
|
73
|
-
*/
|
|
74
|
-
private emitDelta;
|
|
75
|
-
private finishCheck;
|
|
76
|
-
private checkItems;
|
|
77
|
-
private stopped;
|
|
78
|
-
/**
|
|
79
|
-
* Stops the observer by unbinding all events and cleaning up resources.
|
|
80
|
-
* Safe to call multiple times - will only unbind once.
|
|
81
|
-
*/
|
|
82
|
-
stop(): void;
|
|
83
|
-
/**
|
|
84
|
-
* Registers callbacks for specific events to observe changes in the collection.
|
|
85
|
-
* @param callbacks - An object containing the callbacks for various events (e.g., 'added', 'removed').
|
|
86
|
-
* @param skipInitial - A boolean indicating whether to skip invoking the callbacks for the initial state of the collection.
|
|
87
|
-
*/
|
|
88
|
-
addCallbacks(callbacks: ObserveCallbacks<T>, skipInitial?: boolean): void;
|
|
89
|
-
/**
|
|
90
|
-
* Removes the specified callbacks for specific events, unregistering them from the observer.
|
|
91
|
-
* @param callbacks - An object containing the callbacks to be removed for various events.
|
|
92
|
-
*/
|
|
93
|
-
removeCallbacks(callbacks: ObserveCallbacks<T>): void;
|
|
94
|
-
}
|
|
95
|
-
export {};
|