@signaldb/core 1.5.3 → 1.6.0

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 (64) hide show
  1. package/dist/.vite/manifest.json +28 -27
  2. package/dist/Collection/createIndex.d.ts +1 -1
  3. package/dist/Collection/index.d.ts +1 -0
  4. package/dist/index.cjs.js +14 -12
  5. package/dist/index.cjs10.js +27 -10
  6. package/dist/index.cjs11.js +10 -3
  7. package/dist/index.cjs12.js +3 -136
  8. package/dist/index.cjs13.js +131 -61
  9. package/dist/index.cjs14.js +72 -5
  10. package/dist/index.cjs15.js +8 -41
  11. package/dist/index.cjs16.js +25 -11
  12. package/dist/index.cjs17.js +143 -23
  13. package/dist/index.cjs18.js +5 -262
  14. package/dist/index.cjs19.js +36 -63
  15. package/dist/index.cjs2.js +210 -731
  16. package/dist/index.cjs20.js +13 -85
  17. package/dist/index.cjs21.js +24 -8
  18. package/dist/index.cjs22.js +67 -24
  19. package/dist/index.cjs23.js +72 -131
  20. package/dist/index.cjs24.js +16 -5
  21. package/dist/index.cjs25.js +22 -11
  22. package/dist/index.cjs26.js +7 -27
  23. package/dist/index.cjs27.js +5 -27
  24. package/dist/index.cjs28.js +27 -7
  25. package/dist/index.cjs3.js +757 -136
  26. package/dist/index.cjs4.js +165 -62
  27. package/dist/index.cjs5.js +67 -3
  28. package/dist/index.cjs6.js +2 -2
  29. package/dist/index.cjs7.js +2 -2
  30. package/dist/index.cjs8.js +2 -2
  31. package/dist/index.cjs9.js +3 -27
  32. package/dist/index.d.ts +1 -0
  33. package/dist/index.mjs +14 -12
  34. package/dist/index10.mjs +27 -10
  35. package/dist/index11.mjs +10 -3
  36. package/dist/index12.mjs +3 -136
  37. package/dist/index13.mjs +131 -60
  38. package/dist/index14.mjs +71 -5
  39. package/dist/index15.mjs +8 -40
  40. package/dist/index16.mjs +25 -11
  41. package/dist/index17.mjs +143 -23
  42. package/dist/index18.mjs +5 -261
  43. package/dist/index19.mjs +36 -63
  44. package/dist/index2.mjs +210 -732
  45. package/dist/index20.mjs +13 -84
  46. package/dist/index21.mjs +24 -8
  47. package/dist/index22.mjs +66 -24
  48. package/dist/index23.mjs +71 -131
  49. package/dist/index24.mjs +16 -5
  50. package/dist/index25.mjs +22 -11
  51. package/dist/index26.mjs +7 -27
  52. package/dist/index27.mjs +5 -27
  53. package/dist/index28.mjs +27 -7
  54. package/dist/index3.mjs +757 -136
  55. package/dist/index4.mjs +165 -61
  56. package/dist/index5.mjs +66 -3
  57. package/dist/index6.mjs +2 -2
  58. package/dist/index7.mjs +2 -2
  59. package/dist/index8.mjs +2 -2
  60. package/dist/index9.mjs +3 -27
  61. package/dist/types/IndexProvider.d.ts +2 -0
  62. package/dist/utils/getMatchingKeys.d.ts +2 -2
  63. package/dist/utils/serializeValue.d.ts +1 -1
  64. package/package.json +1 -1
package/dist/index13.mjs CHANGED
@@ -1,68 +1,139 @@
1
- import createIndexProvider from "./index5.mjs";
2
- import get from "./index25.mjs";
3
- import getMatchingKeys from "./index26.mjs";
4
- import serializeValue from "./index16.mjs";
5
- function createExternalIndex(field, index) {
6
- return createIndexProvider({
7
- query(selector) {
8
- const keys = getMatchingKeys(field, selector);
9
- if (keys.include == null && keys.exclude == null)
10
- return { matched: false };
11
- let includedPositions = [];
12
- if (keys.include == null) {
13
- for (const set of index.values()) {
14
- for (const pos of set) {
15
- includedPositions.push(pos);
16
- }
17
- }
18
- } else {
19
- for (const key of keys.include) {
20
- const posSet = index.get(key);
21
- if (posSet) {
22
- for (const pos of posSet) {
23
- includedPositions.push(pos);
24
- }
25
- }
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+ class EventEmitter {
5
+ constructor() {
6
+ __publicField(this, "_maxListeners", 100);
7
+ /**
8
+ * We store a set of the listeners for each event.
9
+ */
10
+ __publicField(this, "_listenerStore", /* @__PURE__ */ new Map());
11
+ }
12
+ setMaxListeners(max) {
13
+ this._maxListeners = max;
14
+ return this;
15
+ }
16
+ /**
17
+ * Subscribe to an event with a listener function.
18
+ * @param eventName The event name (key of E).
19
+ * @param listener A function that receives the emitted arguments.
20
+ * @returns The emitter instance (for chaining).
21
+ */
22
+ on(eventName, listener) {
23
+ let listenersSet = this._listenerStore.get(eventName);
24
+ if (!listenersSet) {
25
+ listenersSet = /* @__PURE__ */ new Set();
26
+ this._listenerStore.set(eventName, listenersSet);
27
+ }
28
+ listenersSet.add(listener);
29
+ if (listenersSet.size > this._maxListeners) {
30
+ console.warn(`Possible EventEmitter memory leak detected. ${listenersSet.size} ${String(eventName)} listeners added. Use emitter.setMaxListeners() to increase limit.`);
31
+ }
32
+ return this;
33
+ }
34
+ /**
35
+ * Subscribe to an event with a listener function.
36
+ * @param eventName The event name (key of E).
37
+ * @param listener A function that receives the emitted arguments.
38
+ * @returns The emitter instance (for chaining).
39
+ */
40
+ addListener(eventName, listener) {
41
+ return this.on(eventName, listener);
42
+ }
43
+ /**
44
+ * Subscribe to an event, handling it only once. Automatically removes
45
+ * the listener after it fires the first time.
46
+ * @param eventName The event name (key of E).
47
+ * @param listener A function that receives the emitted arguments.
48
+ * @returns The emitter instance (for chaining).
49
+ */
50
+ once(eventName, listener) {
51
+ const onceWrapper = (...args) => {
52
+ listener(...args);
53
+ this.off(eventName, onceWrapper);
54
+ };
55
+ return this.on(eventName, onceWrapper);
56
+ }
57
+ /**
58
+ * Unsubscribe a previously subscribed listener.
59
+ * @param eventName The event name (key of E).
60
+ * @param listener The original function passed to `on` or `once`.
61
+ * @returns The emitter instance (for chaining).
62
+ */
63
+ off(eventName, listener) {
64
+ const listenersSet = this._listenerStore.get(eventName);
65
+ if (!listenersSet)
66
+ return this;
67
+ listenersSet.delete(listener);
68
+ if (listenersSet.size === 0) {
69
+ this._listenerStore.delete(eventName);
70
+ }
71
+ return this;
72
+ }
73
+ /**
74
+ * Unsubscribe a previously subscribed listener.
75
+ * @param eventName The event name (key of E).
76
+ * @param listener The original function passed to `on` or `once`.
77
+ * @returns The emitter instance (for chaining).
78
+ */
79
+ removeListener(eventName, listener) {
80
+ return this.off(eventName, listener);
81
+ }
82
+ /**
83
+ * Emit (dispatch) an event with a variable number of arguments.
84
+ * @param eventName The event name (key of E).
85
+ * @param args The arguments to pass to subscribed listeners.
86
+ */
87
+ emit(eventName, ...args) {
88
+ this.listeners(eventName).forEach((listener) => {
89
+ listener(...args);
90
+ });
91
+ }
92
+ /**
93
+ * Returns the array of listener functions currently registered for a given event.
94
+ * @param eventName The event name (key of E).
95
+ * @returns An array of listener functions.
96
+ */
97
+ listeners(eventName) {
98
+ const listenersSet = this._listenerStore.get(eventName);
99
+ if (!listenersSet)
100
+ return [];
101
+ return [...listenersSet.values()];
102
+ }
103
+ /**
104
+ * Returns the number of listeners for a given event.
105
+ * @param eventName The event name (key of E).
106
+ * @returns The number of listeners.
107
+ */
108
+ listenerCount(eventName) {
109
+ const listenersSet = this._listenerStore.get(eventName);
110
+ return listenersSet ? listenersSet.size : 0;
111
+ }
112
+ /**
113
+ * Removes all listeners for a given event, or all events if none is specified.
114
+ * @param eventName Optional. If omitted, clears all events’ listeners.
115
+ * @returns The emitter instance (for chaining).
116
+ */
117
+ removeAllListeners(eventName) {
118
+ if (eventName === void 0) {
119
+ for (const [eventName_, listenersSet] of this._listenerStore.entries()) {
120
+ for (const listener of listenersSet.values()) {
121
+ this.off(eventName_, listener);
26
122
  }
27
123
  }
28
- if (keys.exclude != null) {
29
- const excludeSet = /* @__PURE__ */ new Set();
30
- for (const key of keys.exclude) {
31
- const posSet = index.get(key);
32
- if (posSet) {
33
- for (const pos of posSet) {
34
- excludeSet.add(pos);
35
- }
36
- }
124
+ this._listenerStore.clear();
125
+ } else {
126
+ const listenersSet = this._listenerStore.get(eventName);
127
+ if (listenersSet) {
128
+ for (const listener of listenersSet.values()) {
129
+ this.off(eventName, listener);
37
130
  }
38
- includedPositions = includedPositions.filter((pos) => !excludeSet.has(pos));
131
+ this._listenerStore.delete(eventName);
39
132
  }
40
- return {
41
- matched: true,
42
- positions: includedPositions,
43
- fields: [field]
44
- };
45
- },
46
- rebuild() {
47
- }
48
- });
49
- }
50
- function createIndex(field) {
51
- const index = /* @__PURE__ */ new Map();
52
- return {
53
- ...createExternalIndex(field, index),
54
- rebuild(items) {
55
- index.clear();
56
- items.forEach((item, i) => {
57
- const value = serializeValue(get(item, field));
58
- const current = index.get(value) || /* @__PURE__ */ new Set();
59
- current.add(i);
60
- index.set(value, current);
61
- });
62
133
  }
63
- };
134
+ return this;
135
+ }
64
136
  }
65
137
  export {
66
- createExternalIndex,
67
- createIndex as default
138
+ EventEmitter as default
68
139
  };
package/dist/index14.mjs CHANGED
@@ -1,8 +1,74 @@
1
- import { Query } from "mingo";
2
- function match(item, selector) {
3
- const query = new Query(selector);
4
- return query.test(item);
1
+ import createIndexProvider from "./index6.mjs";
2
+ import get from "./index24.mjs";
3
+ import getMatchingKeys from "./index28.mjs";
4
+ import serializeValue from "./index20.mjs";
5
+ function createExternalIndex(field, index) {
6
+ return createIndexProvider({
7
+ query(selector) {
8
+ if (!Object.hasOwnProperty.call(selector, field)) {
9
+ return { matched: false };
10
+ }
11
+ const fieldSelector = selector[field];
12
+ const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
13
+ const keys = filteresForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, selector);
14
+ if (keys.include == null && keys.exclude == null)
15
+ return { matched: false };
16
+ let includedPositions = [];
17
+ if (keys.include == null) {
18
+ for (const set of index.values()) {
19
+ for (const pos of set) {
20
+ includedPositions.push(pos);
21
+ }
22
+ }
23
+ } else {
24
+ for (const key of keys.include) {
25
+ const posSet = index.get(key);
26
+ if (posSet) {
27
+ for (const pos of posSet) {
28
+ includedPositions.push(pos);
29
+ }
30
+ }
31
+ }
32
+ }
33
+ if (keys.exclude != null) {
34
+ const excludeSet = /* @__PURE__ */ new Set();
35
+ for (const key of keys.exclude) {
36
+ const posSet = index.get(key);
37
+ if (posSet) {
38
+ for (const pos of posSet) {
39
+ excludeSet.add(pos);
40
+ }
41
+ }
42
+ }
43
+ includedPositions = includedPositions.filter((pos) => !excludeSet.has(pos));
44
+ }
45
+ return {
46
+ matched: true,
47
+ positions: includedPositions,
48
+ fields: [field],
49
+ keepSelector: filteresForNull
50
+ };
51
+ },
52
+ rebuild() {
53
+ }
54
+ });
55
+ }
56
+ function createIndex(field) {
57
+ const index = /* @__PURE__ */ new Map();
58
+ return {
59
+ ...createExternalIndex(field, index),
60
+ rebuild(items) {
61
+ index.clear();
62
+ items.forEach((item, i) => {
63
+ const value = serializeValue(get(item, field));
64
+ const current = index.get(value) || /* @__PURE__ */ new Set();
65
+ current.add(i);
66
+ index.set(value, current);
67
+ });
68
+ }
69
+ };
5
70
  }
6
71
  export {
7
- match as default
72
+ createExternalIndex,
73
+ createIndex as default
8
74
  };
package/dist/index15.mjs CHANGED
@@ -1,43 +1,11 @@
1
- function clone(value) {
2
- if (typeof value === "function")
3
- throw new Error("Cloning functions is not supported");
4
- if (value === null || typeof value !== "object")
5
- return value;
6
- if (value instanceof Date)
7
- return new Date(value);
8
- if (Array.isArray(value))
9
- return value.map((item) => clone(item));
10
- if (value instanceof Map) {
11
- const result2 = /* @__PURE__ */ new Map();
12
- value.forEach((currentValue, key) => {
13
- result2.set(key, clone(currentValue));
14
- });
15
- return result2;
16
- }
17
- if (value instanceof Set) {
18
- const result2 = /* @__PURE__ */ new Set();
19
- value.forEach((currentValue) => {
20
- result2.add(clone(currentValue));
21
- });
22
- return result2;
23
- }
24
- if (value instanceof RegExp)
25
- return new RegExp(value);
26
- const result = {};
27
- for (const key in value) {
28
- if (Object.hasOwnProperty.call(value, key)) {
29
- result[key] = clone(value[key]);
30
- }
31
- }
32
- return result;
33
- }
34
- function deepClone(object) {
35
- if (typeof structuredClone === "function")
36
- return structuredClone(object);
37
- /* istanbul ignore next -- @preserve */
38
- return clone(object);
1
+ import { sort } from "fast-sort";
2
+ import get from "./index24.mjs";
3
+ function sortItems(items, sortFields) {
4
+ return sort(items).by(Object.entries(sortFields).map(([key, value]) => {
5
+ const order = value === 1 ? "asc" : "desc";
6
+ return { [order]: (i) => get(i, key) };
7
+ }));
39
8
  }
40
9
  export {
41
- clone,
42
- deepClone as default
10
+ sortItems as default
43
11
  };
package/dist/index16.mjs CHANGED
@@ -1,14 +1,28 @@
1
- function serializeValue(value) {
2
- if (typeof value === "string")
3
- return value;
4
- if (typeof value === "number")
5
- return value.toString();
6
- if (typeof value === "boolean")
7
- return value.toString();
8
- if (value instanceof Date)
9
- return value.toISOString();
10
- return JSON.stringify(value);
1
+ import get from "./index24.mjs";
2
+ import set from "./index25.mjs";
3
+ function project(item, fields) {
4
+ const allFieldsDeactivated = Object.values(fields).every((value) => value === 0);
5
+ if (allFieldsDeactivated) {
6
+ const result2 = { ...item };
7
+ Object.keys(fields).forEach((key) => {
8
+ const fieldValue = get(item, key);
9
+ if (fieldValue === void 0)
10
+ return;
11
+ set(result2, key, void 0, true);
12
+ });
13
+ return result2;
14
+ }
15
+ const result = {};
16
+ Object.entries(fields).forEach(([key, value]) => {
17
+ const fieldValue = get(item, key);
18
+ if (fieldValue === void 0)
19
+ return;
20
+ if (fieldValue == null && value !== 1)
21
+ return;
22
+ set(result, key, value === 1 ? fieldValue : void 0);
23
+ });
24
+ return result;
11
25
  }
12
26
  export {
13
- serializeValue as default
27
+ project as default
14
28
  };
package/dist/index17.mjs CHANGED
@@ -1,27 +1,147 @@
1
- function createSignal(reactivityAdapter, initialValue, isEqual = Object.is) {
2
- let value = initialValue;
3
- const dependency = reactivityAdapter == null ? void 0 : reactivityAdapter.create();
4
- const isInReactiveScope = () => {
5
- if (!(reactivityAdapter == null ? void 0 : reactivityAdapter.isInScope))
6
- return true;
7
- return reactivityAdapter.isInScope();
8
- };
9
- const signal = {
10
- get() {
11
- if (dependency && isInReactiveScope())
12
- dependency.depend();
13
- return value;
14
- },
15
- set(newValue) {
16
- if (isEqual(value, newValue))
17
- return;
18
- value = newValue;
19
- if (dependency)
20
- dependency.notify();
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+ import isEqual from "./index10.mjs";
5
+ import uniqueBy from "./index26.mjs";
6
+ class Observer {
7
+ /**
8
+ * Creates a new instance of the `Observer` class.
9
+ * Sets up event bindings and initializes the callbacks for tracking changes in a collection.
10
+ * @param bindEvents - A function to bind external events to the observer. Must return a cleanup function to unbind those events.
11
+ */
12
+ constructor(bindEvents) {
13
+ __publicField(this, "previousItems", []);
14
+ __publicField(this, "callbacks");
15
+ __publicField(this, "unbindEvents");
16
+ this.callbacks = {
17
+ added: [],
18
+ addedBefore: [],
19
+ changed: [],
20
+ changedField: [],
21
+ movedBefore: [],
22
+ removed: []
23
+ };
24
+ this.unbindEvents = bindEvents();
25
+ }
26
+ call(event, ...args) {
27
+ this.callbacks[event].forEach(({ callback, options }) => {
28
+ if (!options.skipInitial || !options.isInitial) {
29
+ callback(...args);
30
+ }
31
+ });
32
+ }
33
+ hasCallbacks(events) {
34
+ return events.some((event) => this.callbacks[event].length > 0);
35
+ }
36
+ /**
37
+ * Determines if the observer has no active callbacks registered for any events.
38
+ * @returns A boolean indicating whether the observer is empty (i.e., no callbacks are registered).
39
+ */
40
+ isEmpty() {
41
+ return !this.hasCallbacks([
42
+ "added",
43
+ "addedBefore",
44
+ "changed",
45
+ "changedField",
46
+ "movedBefore",
47
+ "removed"
48
+ ]);
49
+ }
50
+ /**
51
+ * Compares the previous state of items with the new state and triggers the appropriate callbacks
52
+ * for events such as added, removed, changed, or moved items.
53
+ * @param newItems - The new list of items to compare against the previous state.
54
+ */
55
+ runChecks(newItems) {
56
+ const oldItemsMap = new Map(this.previousItems.map((item, index) => [
57
+ item.id,
58
+ { item, index, beforeItem: this.previousItems[index + 1] || null }
59
+ ]));
60
+ const newItemsMap = new Map(newItems.map((item, index) => [
61
+ item.id,
62
+ { item, index, beforeItem: newItems[index + 1] || null }
63
+ ]));
64
+ if (this.hasCallbacks(["changed", "changedField", "movedBefore", "removed"])) {
65
+ oldItemsMap.forEach(({ item: oldItem, index, beforeItem: oldBeforeItem }) => {
66
+ var _a;
67
+ const newItem = newItemsMap.get(oldItem.id);
68
+ if (newItem) {
69
+ if (this.hasCallbacks(["changed", "changedField"]) && !isEqual(newItem.item, oldItem)) {
70
+ this.call("changed", newItem.item);
71
+ if (this.hasCallbacks(["changedField"])) {
72
+ const keys = uniqueBy([
73
+ ...Object.keys(newItem.item),
74
+ ...Object.keys(oldItem)
75
+ ], (value) => value);
76
+ keys.forEach((key) => {
77
+ if (isEqual(newItem.item[key], oldItem[key]))
78
+ return;
79
+ this.call("changedField", newItem.item, key, oldItem[key], newItem.item[key]);
80
+ });
81
+ }
82
+ }
83
+ if (newItem.index !== index && ((_a = newItem.beforeItem) == null ? void 0 : _a.id) !== (oldBeforeItem == null ? void 0 : oldBeforeItem.id)) {
84
+ this.call("movedBefore", newItem.item, newItem.beforeItem);
85
+ }
86
+ } else {
87
+ this.call("removed", oldItem);
88
+ }
89
+ });
21
90
  }
22
- };
23
- return signal;
91
+ if (this.hasCallbacks(["added", "addedBefore"])) {
92
+ newItems.forEach((newItem, index) => {
93
+ const oldItem = oldItemsMap.get(newItem.id);
94
+ if (oldItem)
95
+ return;
96
+ this.call("added", newItem);
97
+ this.call("addedBefore", newItem, newItems[index + 1] || null);
98
+ });
99
+ }
100
+ this.previousItems = newItems;
101
+ Object.keys(this.callbacks).forEach((key) => {
102
+ const event = key;
103
+ const callbacks = this.callbacks[event];
104
+ this.callbacks[event] = callbacks.map((callback) => ({
105
+ ...callback,
106
+ options: {
107
+ ...callback.options,
108
+ isInitial: false
109
+ }
110
+ }));
111
+ });
112
+ }
113
+ /**
114
+ * Stops the observer by unbinding all events and cleaning up resources.
115
+ */
116
+ stop() {
117
+ this.unbindEvents();
118
+ }
119
+ /**
120
+ * Registers callbacks for specific events to observe changes in the collection.
121
+ * @param callbacks - An object containing the callbacks for various events (e.g., 'added', 'removed').
122
+ * @param skipInitial - A boolean indicating whether to skip invoking the callbacks for the initial state of the collection.
123
+ */
124
+ addCallbacks(callbacks, skipInitial = false) {
125
+ Object.keys(callbacks).forEach((key) => {
126
+ const typedKey = key;
127
+ this.callbacks[typedKey].push({
128
+ callback: callbacks[typedKey],
129
+ options: { skipInitial, isInitial: true }
130
+ });
131
+ });
132
+ }
133
+ /**
134
+ * Removes the specified callbacks for specific events, unregistering them from the observer.
135
+ * @param callbacks - An object containing the callbacks to be removed for various events.
136
+ */
137
+ removeCallbacks(callbacks) {
138
+ Object.keys(callbacks).forEach((key) => {
139
+ const typedKey = key;
140
+ const index = this.callbacks[typedKey].findIndex(({ callback }) => callback === callbacks[typedKey]);
141
+ this.callbacks[typedKey].splice(index, 1);
142
+ });
143
+ }
24
144
  }
25
145
  export {
26
- createSignal as default
146
+ Observer as default
27
147
  };