@soffinal/stream 0.1.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.
package/dist/map.js ADDED
@@ -0,0 +1,99 @@
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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"map.js","sourceRoot":"","sources":["../src/map.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,OAAO,GAAgB,SAAQ,UAAU,CAAC,GAAe;IAmD7D;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,YAAY,OAAgC;QAC1C,KAAK,CAAC,OAAO,CAAC,CAAC;QAEf,MAAM,IAAI,GAAG,IAAI,CAAC;QAElB,IAAI,CAAC,GAAG,GAAG,IAAI,KAAK,CAClB,CAAC,GAAQ,EAAE,KAAY,EAAE,EAAE;YACzB,IAAI,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,IAAI,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,KAAK,KAAK;gBACxG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CAAC,aAAa,GAAG,IAAI,MAAM,EAAE,CAAC;gBAC3D,OAAQ,IAAI,CAAC,aAAqB,CAAC,IAAI,CAAC,CAAC;YAC3C,CAAC;SACF,CACK,CAAC;QAET,IAAI,CAAC,KAAK,GAAG,IAAI,KAAK,CACpB,GAAG,EAAE;YACH,IAAI,IAAI,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC;gBAClB,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBAC1C,IAAI,CAAC,YAAY,EAAE,IAAI,EAAE,CAAC;YAC5B,CAAC;QACH,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,YAAY;oBAAE,IAAI,CAAC,YAAY,GAAG,IAAI,MAAM,EAAE,CAAC;gBACzD,OAAQ,IAAI,CAAC,YAAoB,CAAC,IAAI,CAAC,CAAC;YAC1C,CAAC;SACF,CACK,CAAC;IACX,CAAC;CACF"}
package/dist/set.d.ts ADDED
@@ -0,0 +1,92 @@
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 declare class Set<VALUE> extends globalThis.Set<VALUE> {
27
+ protected _addStream?: Stream<VALUE>;
28
+ protected _deleteStream?: Stream<VALUE>;
29
+ protected _clearStream?: Stream<void>;
30
+ /**
31
+ * Adds a value to the set and emits the value to listeners.
32
+ * Only emits if the value is actually added (not a duplicate).
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const tags = new Set<string>();
37
+ * tags.add.listen(tag => console.log('Added:', tag));
38
+ *
39
+ * tags.add('javascript'); // Added: javascript
40
+ * tags.add('javascript'); // No emission (duplicate)
41
+ * ```
42
+ */
43
+ add: ((value: VALUE) => this) & Stream<VALUE>;
44
+ /**
45
+ * Deletes a value from the set and emits the value to listeners.
46
+ * Only emits if the value was actually deleted (existed in set).
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * const items = new Set(['a', 'b', 'c']);
51
+ * items.delete.listen(item => console.log('Removed:', item));
52
+ *
53
+ * items.delete('b'); // Removed: b
54
+ * items.delete('x'); // No emission (didn't exist)
55
+ * ```
56
+ */
57
+ delete: ((value: VALUE) => boolean) & Stream<VALUE>;
58
+ /**
59
+ * Clears all values from the set and emits to listeners.
60
+ * Only emits if the set was not already empty.
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * const cache = new Set([1, 2, 3]);
65
+ * cache.clear.listen(() => console.log('Cache cleared'));
66
+ *
67
+ * cache.clear(); // Cache cleared
68
+ * cache.clear(); // No emission (already empty)
69
+ * ```
70
+ */
71
+ clear: (() => void) & Stream<void>;
72
+ /**
73
+ * Creates a new reactive Set.
74
+ *
75
+ * @param values - Optional iterable of initial values
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * // Empty set
80
+ * const tags = new Set<string>();
81
+ *
82
+ * // With initial values
83
+ * const colors = new Set(['red', 'green', 'blue']);
84
+ *
85
+ * // Listen to changes
86
+ * colors.add.listen(color => updateUI(color));
87
+ * colors.delete.listen(color => removeFromUI(color));
88
+ * ```
89
+ */
90
+ constructor(values?: Iterable<VALUE>);
91
+ }
92
+ //# sourceMappingURL=set.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set.d.ts","sourceRoot":"","sources":["../src/set.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,GAAG,CAAC,KAAK,CAAE,SAAQ,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC;IACnD,SAAS,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;IACrC,SAAS,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;IACxC,SAAS,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IAEtC;;;;;;;;;;;;OAYG;IACK,GAAG,EAAE,CAAC,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAEtD;;;;;;;;;;;;OAYG;IACK,MAAM,EAAE,CAAC,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAE5D;;;;;;;;;;;;OAYG;IACK,KAAK,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAE3C;;;;;;;;;;;;;;;;;OAiBG;gBACS,MAAM,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC;CAqDrC"}
package/dist/set.js ADDED
@@ -0,0 +1,94 @@
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
@@ -0,0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CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YAClD,IAAI,CAAC,aAAa,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;YAChC,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,aAAa;oBAAE,IAAI,CAAC,aAAa,GAAG,IAAI,MAAM,EAAS,CAAC;gBAClE,OAAQ,IAAI,CAAC,aAAqB,CAAC,IAAI,CAAC,CAAC;YAC3C,CAAC;SACF,CACK,CAAC;QAET,IAAI,CAAC,KAAK,GAAG,IAAI,KAAK,CACpB,GAAG,EAAE;YACH,IAAI,IAAI,CAAC,IAAI,GAAG,CAAC,EAAE,CAAC;gBAClB,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBAC1C,IAAI,CAAC,YAAY,EAAE,IAAI,EAAE,CAAC;YAC5B,CAAC;QACH,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,YAAY;oBAAE,IAAI,CAAC,YAAY,GAAG,IAAI,MAAM,EAAQ,CAAC;gBAC/D,OAAQ,IAAI,CAAC,YAAoB,CAAC,IAAI,CAAC,CAAC;YAC1C,CAAC;SACF,CACK,CAAC;IACX,CAAC;CACF"}
@@ -0,0 +1,79 @@
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 declare class State<VALUE> extends Stream<VALUE> {
23
+ protected _value: VALUE;
24
+ /**
25
+ * Creates a new State with an initial value.
26
+ *
27
+ * @param initialValue - The initial state value
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const count = new State(0);
32
+ * const theme = new State<'light' | 'dark'>('light');
33
+ * const user = new State<User | null>(null);
34
+ * ```
35
+ */
36
+ constructor(initialValue: VALUE);
37
+ /**
38
+ * Updates the state with one or more values sequentially.
39
+ * Each value triggers listeners and updates the current state.
40
+ *
41
+ * @param values - Values to set as state
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * const state = new State(0);
46
+ * state.listen(v => console.log(v));
47
+ *
48
+ * state.push(1, 2, 3); // Logs: 1, 2, 3
49
+ * console.log(state.value); // 3
50
+ * ```
51
+ */
52
+ push(...values: VALUE[]): void;
53
+ /**
54
+ * Gets the current state value.
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * const state = new State('hello');
59
+ * console.log(state.value); // 'hello'
60
+ * ```
61
+ */
62
+ get value(): VALUE;
63
+ /**
64
+ * Sets the current state value and notifies all listeners.
65
+ *
66
+ * @param value - The new state value
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * const state = new State(0);
71
+ * state.listen(v => console.log('New value:', v));
72
+ *
73
+ * state.value = 42; // New value: 42
74
+ * state.value = 100; // New value: 100
75
+ * ```
76
+ */
77
+ set value(value: VALUE);
78
+ }
79
+ //# sourceMappingURL=state.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,KAAK,CAAC,KAAK,CAAE,SAAQ,MAAM,CAAC,KAAK,CAAC;IAC7C,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC;IAExB;;;;;;;;;;;OAWG;gBACS,YAAY,EAAE,KAAK;IAI/B;;;;;;;;;;;;;;OAcG;IACM,IAAI,CAAC,GAAG,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI;IAKvC;;;;;;;;OAQG;IACH,IAAI,KAAK,UAER;IACD;;;;;;;;;;;;;OAaG;IACH,IAAI,KAAK,CAAC,KAAK,OAAA,EAGd;CACF"}
package/dist/state.js ADDED
@@ -0,0 +1,90 @@
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
@@ -0,0 +1 @@
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"}