@soffinal/stream 0.1.0 → 0.1.3

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/dist/list.js DELETED
@@ -1,200 +0,0 @@
1
- import { Stream } from "./stream";
2
- /**
3
- * A reactive List that provides array-like functionality with stream-based mutation events.
4
- * Emits events when items are inserted, deleted, or the list is cleared.
5
- * Supports negative indexing with modulo wrapping.
6
- * @template VALUE - The type of values stored in the list
7
- *
8
- * @example
9
- * ```typescript
10
- * const todos = new List<string>();
11
- *
12
- * // Listen to insertions
13
- * todos.insert.listen(([index, item]) => {
14
- * console.log(`Added "${item}" at index ${index}`);
15
- * });
16
- *
17
- * // Listen to deletions
18
- * todos.delete.listen(([index, item]) => {
19
- * console.log(`Removed "${item}" from index ${index}`);
20
- * });
21
- *
22
- * todos.insert(0, "Buy milk"); //Added "Buy milk" at index 0
23
- * todos.insert(1, "Walk dog"); //Added "Walk dog" at index 1
24
- * todos.insert(-1, "kechma haja"); //Added "kechma haja" at index 2
25
- * todos[0] = "Buy organic milk"; // Added "Buy organic milk" at index 0
26
- * ```
27
- */
28
- export class List {
29
- /**
30
- * Creates a new reactive List.
31
- *
32
- * @param items - Optional iterable of initial items
33
- *
34
- * @example
35
- * ```typescript
36
- * // Empty list
37
- * const list = new List<number>();
38
- *
39
- * // With initial items
40
- * const todos = new List(['Buy milk', 'Walk dog']);
41
- *
42
- * // Listen to changes
43
- * todos.insert.listen(([index, item]) => updateUI(index, item));
44
- * todos.delete.listen(([index, item]) => removeFromUI(index));
45
- *
46
- * // Index access with modulo wrapping
47
- * console.log(todos[0]); // 'Buy milk'
48
- * console.log(todos[-1]); // 'Walk dog' (last item)
49
- * ```
50
- */
51
- constructor(items) {
52
- this._items = [];
53
- if (items)
54
- this._items = [...items];
55
- const self = this;
56
- function normalizeIndex(index, length) {
57
- if (length === 0)
58
- return 0;
59
- return index < 0 ? ((index % length) + length) % length : index % length;
60
- }
61
- this.insert = new Proxy((index, value) => {
62
- const actualIndex = index < 0 ? Math.max(0, self._items.length + index + 1) : Math.min(index, self._items.length);
63
- self._items.splice(actualIndex, 0, value);
64
- self._insertStream?.push([actualIndex, value]);
65
- return proxy;
66
- }, {
67
- get(target, prop) {
68
- if (prop in target)
69
- return target[prop];
70
- if (!self._insertStream)
71
- self._insertStream = new Stream();
72
- return self._insertStream[prop];
73
- },
74
- });
75
- this.delete = new Proxy((index) => {
76
- if (index < 0 || index >= self._items.length)
77
- return undefined;
78
- const value = self._items.splice(index, 1)[0];
79
- self._deleteStream?.push([index, value]);
80
- return value;
81
- }, {
82
- get(target, prop) {
83
- if (prop in target)
84
- return target[prop];
85
- if (!self._deleteStream)
86
- self._deleteStream = new Stream();
87
- return self._deleteStream[prop];
88
- },
89
- });
90
- this.clear = new Proxy(() => {
91
- if (self._items.length > 0) {
92
- self._items.length = 0;
93
- self._clearStream?.push();
94
- }
95
- }, {
96
- get(target, prop) {
97
- if (prop in target)
98
- return target[prop];
99
- if (!self._clearStream)
100
- self._clearStream = new Stream();
101
- return self._clearStream[prop];
102
- },
103
- });
104
- const proxy = new Proxy(this, {
105
- get(target, prop) {
106
- if (typeof prop === "string" && /^-?\d+$/.test(prop)) {
107
- const index = parseInt(prop);
108
- if (target._items.length === 0)
109
- return undefined;
110
- const actualIndex = normalizeIndex(index, target._items.length);
111
- return target._items[actualIndex];
112
- }
113
- return target[prop];
114
- },
115
- set(target, prop, value) {
116
- if (typeof prop === "string" && /^-?\d+$/.test(prop)) {
117
- const index = parseInt(prop);
118
- if (target._items.length === 0) {
119
- // Empty array: any index mutation adds first element at index 0
120
- target._items.push(value);
121
- target._insertStream?.push([0, value]);
122
- return true;
123
- }
124
- const actualIndex = normalizeIndex(index, target._items.length);
125
- const oldValue = target._items[actualIndex];
126
- if (oldValue !== value) {
127
- target._items[actualIndex] = value;
128
- target._insertStream?.push([actualIndex, value]);
129
- }
130
- return true;
131
- }
132
- target[prop] = value;
133
- return true;
134
- },
135
- });
136
- return proxy;
137
- }
138
- /**
139
- * Gets the value at the specified index without modulo wrapping.
140
- *
141
- * @param index - The index to access
142
- * @returns The value at the index or undefined
143
- *
144
- * @example
145
- * ```typescript
146
- * const list = new List([10, 20, 30]);
147
- * console.log(list.get(1)); // 20
148
- * console.log(list.get(5)); // undefined
149
- * ```
150
- */
151
- get(index) {
152
- return this._items[index];
153
- }
154
- /**
155
- * Gets the current length of the list.
156
- *
157
- * @example
158
- * ```typescript
159
- * const list = new List([1, 2, 3]);
160
- * console.log(list.length); // 3
161
- *
162
- * list.insert(0, 0);
163
- * console.log(list.length); // 4
164
- * ```
165
- */
166
- get length() {
167
- return this._items.length;
168
- }
169
- /**
170
- * Returns an iterator for the list values.
171
- *
172
- * @example
173
- * ```typescript
174
- * const list = new List([1, 2, 3]);
175
- * for (const value of list.values()) {
176
- * console.log(value); // 1, 2, 3
177
- * }
178
- * ```
179
- */
180
- values() {
181
- return this._items[Symbol.iterator]();
182
- }
183
- /**
184
- * Makes the list iterable.
185
- *
186
- * @example
187
- * ```typescript
188
- * const list = new List(['a', 'b', 'c']);
189
- * for (const item of list) {
190
- * console.log(item); // 'a', 'b', 'c'
191
- * }
192
- *
193
- * const array = [...list]; // ['a', 'b', 'c']
194
- * ```
195
- */
196
- [Symbol.iterator]() {
197
- return this._items[Symbol.iterator]();
198
- }
199
- }
200
- //# sourceMappingURL=list.js.map
package/dist/list.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"list.js","sourceRoot":"","sources":["../src/list.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,OAAO,IAAI;IAqDf;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,YAAY,KAAuB;QA1E3B,WAAM,GAAY,EAAE,CAAC;QA2E3B,IAAI,KAAK;YAAE,IAAI,CAAC,MAAM,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC;QA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C,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC,CAAC;oBACnD,CAAC;oBACD,OAAO,IAAI,CAAC;gBACd,CAAC;gBACA,MAAc,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;gBAC9B,OAAO,IAAI,CAAC;YACd,CAAC;SACF,CAAC,CAAC;QAEH,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;;;;;;;;;;;OAYG;IACH,GAAG,CAAC,KAAa;QACf,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC5B,CAAC;IAED;;;;;;;;;;;OAWG;IACH,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC;IAC5B,CAAC;IACD;;;;;;;;;;OAUG;IACH,MAAM;QACJ,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;IACxC,CAAC;IACD;;;;;;;;;;;;OAYG;IACH,CAAC,MAAM,CAAC,QAAQ,CAAC;QACf,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;IACxC,CAAC;CACF"}
package/dist/map.js DELETED
@@ -1,99 +0,0 @@
1
- import { Stream } from "./stream";
2
- /**
3
- * A reactive Map that extends the native Map with stream-based mutation events.
4
- * Emits events when entries are set, deleted, or the map is cleared.
5
- *
6
- * @template KEY - The type of keys in the map
7
- * @template VALUE - The type of values in the map
8
- *
9
- * @example
10
- * ```typescript
11
- * const cache = new Map<string, any>();
12
- *
13
- * // Listen to cache updates
14
- * cache.set.listen(([key, value]) => {
15
- * console.log(`Cache updated: ${key} = ${value}`);
16
- * });
17
- *
18
- * // Listen to cache evictions
19
- * cache.delete.listen(([key, value]) => {
20
- * console.log(`Cache evicted: ${key}`);
21
- * });
22
- *
23
- * cache.set('user:123', { name: 'John' });
24
- * cache.delete('user:123');
25
- * ```
26
- */
27
- export class Map extends globalThis.Map {
28
- /**
29
- * Creates a new reactive Map.
30
- *
31
- * @param entries - Optional iterable of initial key-value pairs
32
- *
33
- * @example
34
- * ```typescript
35
- * // Empty map
36
- * const cache = new Map<string, any>();
37
- *
38
- * // With initial entries
39
- * const config = new Map([
40
- * ['theme', 'dark'],
41
- * ['lang', 'en']
42
- * ]);
43
- *
44
- * // Listen to changes
45
- * config.set.listen(([key, value]) => saveConfig(key, value));
46
- * config.delete.listen(([key]) => removeConfig(key));
47
- * ```
48
- */
49
- constructor(entries) {
50
- super(entries);
51
- const self = this;
52
- this.set = new Proxy((key, value) => {
53
- if (globalThis.Map.prototype.has.call(self, key) && globalThis.Map.prototype.get.call(self, key) === value)
54
- return self;
55
- globalThis.Map.prototype.set.call(self, key, value);
56
- self._setStream?.push([key, value]);
57
- return self;
58
- }, {
59
- get(target, prop) {
60
- if (prop in target)
61
- return target[prop];
62
- if (!self._setStream)
63
- self._setStream = new Stream();
64
- return self._setStream[prop];
65
- },
66
- });
67
- this.delete = new Proxy((key) => {
68
- if (!globalThis.Map.prototype.has.call(self, key))
69
- return false;
70
- const value = globalThis.Map.prototype.get.call(self, key);
71
- globalThis.Map.prototype.delete.call(self, key);
72
- self._deleteStream?.push([key, value]);
73
- return true;
74
- }, {
75
- get(target, prop) {
76
- if (prop in target)
77
- return target[prop];
78
- if (!self._deleteStream)
79
- self._deleteStream = new Stream();
80
- return self._deleteStream[prop];
81
- },
82
- });
83
- this.clear = new Proxy(() => {
84
- if (self.size > 0) {
85
- globalThis.Map.prototype.clear.call(self);
86
- self._clearStream?.push();
87
- }
88
- }, {
89
- get(target, prop) {
90
- if (prop in target)
91
- return target[prop];
92
- if (!self._clearStream)
93
- self._clearStream = new Stream();
94
- return self._clearStream[prop];
95
- },
96
- });
97
- }
98
- }
99
- //# sourceMappingURL=map.js.map
package/dist/map.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"map.js","sourceRoot":"","sources":["../src/map.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;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js DELETED
@@ -1,94 +0,0 @@
1
- import { Stream } from "./stream";
2
- /**
3
- * A reactive Set that extends the native Set with stream-based mutation events.
4
- * Emits events when items are added, deleted, or the set is cleared.
5
- *
6
- * @template VALUE - The type of values stored in the set
7
- *
8
- * @example
9
- * ```typescript
10
- * const activeUsers = new Set<string>();
11
- *
12
- * // Listen to additions
13
- * activeUsers.add.listen(userId => {
14
- * console.log(`User ${userId} came online`);
15
- * });
16
- *
17
- * // Listen to deletions
18
- * activeUsers.delete.listen(userId => {
19
- * console.log(`User ${userId} went offline`);
20
- * });
21
- *
22
- * activeUsers.add('alice'); // User alice came online
23
- * activeUsers.delete('alice'); // User alice went offline
24
- * ```
25
- */
26
- export class Set extends globalThis.Set {
27
- /**
28
- * Creates a new reactive Set.
29
- *
30
- * @param values - Optional iterable of initial values
31
- *
32
- * @example
33
- * ```typescript
34
- * // Empty set
35
- * const tags = new Set<string>();
36
- *
37
- * // With initial values
38
- * const colors = new Set(['red', 'green', 'blue']);
39
- *
40
- * // Listen to changes
41
- * colors.add.listen(color => updateUI(color));
42
- * colors.delete.listen(color => removeFromUI(color));
43
- * ```
44
- */
45
- constructor(values) {
46
- super(values);
47
- const self = this;
48
- this.add = new Proxy((value) => {
49
- if (globalThis.Set.prototype.has.call(self, value))
50
- return self;
51
- globalThis.Set.prototype.add.call(self, value);
52
- self._addStream?.push(value);
53
- return self;
54
- }, {
55
- get(target, prop) {
56
- if (prop in target)
57
- return target[prop];
58
- if (!self._addStream)
59
- self._addStream = new Stream();
60
- return self._addStream[prop];
61
- },
62
- });
63
- this.delete = new Proxy((value) => {
64
- if (!globalThis.Set.prototype.has.call(self, value))
65
- return false;
66
- globalThis.Set.prototype.delete.call(self, value);
67
- self._deleteStream?.push(value);
68
- return true;
69
- }, {
70
- get(target, prop) {
71
- if (prop in target)
72
- return target[prop];
73
- if (!self._deleteStream)
74
- self._deleteStream = new Stream();
75
- return self._deleteStream[prop];
76
- },
77
- });
78
- this.clear = new Proxy(() => {
79
- if (self.size > 0) {
80
- globalThis.Set.prototype.clear.call(self);
81
- self._clearStream?.push();
82
- }
83
- }, {
84
- get(target, prop) {
85
- if (prop in target)
86
- return target[prop];
87
- if (!self._clearStream)
88
- self._clearStream = new Stream();
89
- return self._clearStream[prop];
90
- },
91
- });
92
- }
93
- }
94
- //# sourceMappingURL=set.js.map
package/dist/set.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"set.js","sourceRoot":"","sources":["../src/set.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,GAAW,SAAQ,UAAU,CAAC,GAAU;IAkDnD;;;;;;;;;;;;;;;;;OAiBG;IACH,YAAY,MAAwB;QAClC,KAAK,CAAC,MAAM,CAAC,CAAC;QAEd,MAAM,IAAI,GAAG,IAAI,CAAC;QAElB,IAAI,CAAC,GAAG,GAAG,IAAI,KAAK,CAClB,CAAC,KAAY,EAAE,EAAE;YACf,IAAI,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC;gBAAE,OAAO,IAAI,CAAC;YAChE,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YAC/C,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;YAC7B,OAAO,IAAI,CAAC;QACd,CAAC,EACD;YACE,GAAG,CAAC,MAAM,EAAE,IAAI;gBACd,IAAI,IAAI,IAAI,MAAM;oBAAE,OAAQ,MAAc,CAAC,IAAI,CAAC,CAAC;gBACjD,IAAI,CAAC,IAAI,CAAC,UAAU;oBAAE,IAAI,CAAC,UAAU,GAAG,IAAI,MAAM,EAAS,CAAC;gBAC5D,OAAQ,IAAI,CAAC,UAAkB,CAAC,IAAI,CAAC,CAAC;YACxC,C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AC,CAAC;YAC1C,CAAC;SACF,CACK,CAAC;IACX,CAAC;CACF"}
package/dist/state.js DELETED
@@ -1,90 +0,0 @@
1
- import { Stream } from "./stream";
2
- /**
3
- * A reactive state container that extends Stream to provide stateful value management.
4
- *
5
- * @template VALUE - The type of the state value
6
- *
7
- * @example
8
- * ```typescript
9
- * // Basic state
10
- * const counter = new State(0);
11
- * counter.listen(value => console.log('Counter:', value));
12
- * counter.value = 5; // Counter: 5
13
- *
14
- * // Complex state
15
- * interface User { id: string; name: string; }
16
- * const user = new State<User | null>(null);
17
- *
18
- * user.listen(u => console.log('User:', u?.name || 'None'));
19
- * user.value = { id: '1', name: 'Alice' }; // User: Alice
20
- * ```
21
- */
22
- export class State extends Stream {
23
- /**
24
- * Creates a new State with an initial value.
25
- *
26
- * @param initialValue - The initial state value
27
- *
28
- * @example
29
- * ```typescript
30
- * const count = new State(0);
31
- * const theme = new State<'light' | 'dark'>('light');
32
- * const user = new State<User | null>(null);
33
- * ```
34
- */
35
- constructor(initialValue) {
36
- super();
37
- this._value = initialValue;
38
- }
39
- /**
40
- * Updates the state with one or more values sequentially.
41
- * Each value triggers listeners and updates the current state.
42
- *
43
- * @param values - Values to set as state
44
- *
45
- * @example
46
- * ```typescript
47
- * const state = new State(0);
48
- * state.listen(v => console.log(v));
49
- *
50
- * state.push(1, 2, 3); // Logs: 1, 2, 3
51
- * console.log(state.value); // 3
52
- * ```
53
- */
54
- push(...values) {
55
- for (const value of values) {
56
- this.value = value;
57
- }
58
- }
59
- /**
60
- * Gets the current state value.
61
- *
62
- * @example
63
- * ```typescript
64
- * const state = new State('hello');
65
- * console.log(state.value); // 'hello'
66
- * ```
67
- */
68
- get value() {
69
- return this._value;
70
- }
71
- /**
72
- * Sets the current state value and notifies all listeners.
73
- *
74
- * @param value - The new state value
75
- *
76
- * @example
77
- * ```typescript
78
- * const state = new State(0);
79
- * state.listen(v => console.log('New value:', v));
80
- *
81
- * state.value = 42; // New value: 42
82
- * state.value = 100; // New value: 100
83
- * ```
84
- */
85
- set value(value) {
86
- this._value = value;
87
- super.push(value);
88
- }
89
- }
90
- //# sourceMappingURL=state.js.map
package/dist/state.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"state.js","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,OAAO,KAAa,SAAQ,MAAa;IAG7C;;;;;;;;;;;OAWG;IACH,YAAY,YAAmB;QAC7B,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,MAAM,GAAG,YAAY,CAAC;IAC7B,CAAC;IACD;;;;;;;;;;;;;;OAcG;IACM,IAAI,CAAC,GAAG,MAAe;QAC9B,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACrB,CAAC;IACH,CAAC;IACD;;;;;;;;OAQG;IACH,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IACD;;;;;;;;;;;;;OAaG;IACH,IAAI,KAAK,CAAC,KAAK;QACb,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACpB,CAAC;CACF"}