@oscarpalmer/atoms 0.49.0 → 0.51.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 (47) hide show
  1. package/dist/js/array.js +60 -10
  2. package/dist/js/array.mjs +60 -10
  3. package/dist/js/clone.js +70 -0
  4. package/dist/js/clone.mjs +59 -0
  5. package/dist/js/element/focusable.js +2 -4
  6. package/dist/js/element/focusable.mjs +2 -4
  7. package/dist/js/element/index.js +4 -6
  8. package/dist/js/element/index.mjs +4 -6
  9. package/dist/js/emitter.js +102 -0
  10. package/dist/js/emitter.mjs +102 -0
  11. package/dist/js/equal.js +99 -0
  12. package/dist/js/equal.mjs +91 -0
  13. package/dist/js/index.js +452 -191
  14. package/dist/js/index.mjs +3 -0
  15. package/dist/js/log.js +4 -3
  16. package/dist/js/log.mjs +4 -3
  17. package/dist/js/math.js +9 -1
  18. package/dist/js/math.mjs +9 -1
  19. package/dist/js/string.js +1 -1
  20. package/dist/js/string.mjs +1 -1
  21. package/dist/js/timer.js +3 -1
  22. package/dist/js/timer.mjs +3 -1
  23. package/dist/js/value.js +11 -59
  24. package/dist/js/value.mjs +11 -59
  25. package/package.json +13 -8
  26. package/src/js/array.ts +144 -14
  27. package/src/js/clone.ts +88 -0
  28. package/src/js/element/focusable.ts +2 -8
  29. package/src/js/element/index.ts +2 -6
  30. package/src/js/emitter.ts +164 -0
  31. package/src/js/equal.ts +140 -0
  32. package/src/js/index.ts +3 -0
  33. package/src/js/log.ts +10 -10
  34. package/src/js/math.ts +14 -1
  35. package/src/js/number.ts +2 -2
  36. package/src/js/string.ts +4 -1
  37. package/src/js/timer.ts +14 -13
  38. package/src/js/value.ts +10 -84
  39. package/types/array.d.ts +18 -0
  40. package/types/clone.d.ts +2 -0
  41. package/types/emitter.d.ts +23 -0
  42. package/types/equal.d.ts +4 -0
  43. package/types/index.d.ts +3 -0
  44. package/types/log.d.ts +6 -6
  45. package/types/math.d.ts +4 -0
  46. package/types/string.d.ts +3 -0
  47. package/types/value.d.ts +0 -4
package/dist/js/array.js CHANGED
@@ -13,6 +13,9 @@ function chunk(array, size) {
13
13
  }
14
14
  return chunks;
15
15
  }
16
+ var comparison = function(first, second) {
17
+ return [first, second].every((value) => ["bigint", "boolean", "date", "number"].includes(typeof value)) ? Number(first) - Number(second) : String(first).localeCompare(String(second));
18
+ };
16
19
  function exists(array, value, key) {
17
20
  return findValue("index", array, value, key) > -1;
18
21
  }
@@ -28,12 +31,11 @@ var findValue = function(type, array, value, key) {
28
31
  return type === "index" ? array.indexOf(value) : array.find((item) => item === value);
29
32
  }
30
33
  if (callbacks.bool != null) {
31
- const index2 = array.findIndex(callbacks.bool);
32
- return type === "index" ? index2 : index2 > -1 ? array[index2] : undefined;
34
+ const index = array.findIndex(callbacks.bool);
35
+ return type === "index" ? index : index > -1 ? array[index] : undefined;
33
36
  }
34
37
  const { length } = array;
35
- let index = 0;
36
- for (;index < length; index += 1) {
38
+ for (let index = 0;index < length; index += 1) {
37
39
  const item = array[index];
38
40
  if (callbacks.key?.(item) === value) {
39
41
  return type === "index" ? index : item;
@@ -56,8 +58,7 @@ var findValues = function(type, array, value, key) {
56
58
  const hasCallback = typeof callbacks?.key === "function";
57
59
  const result = [];
58
60
  const values = hasCallback ? [] : result;
59
- let index = 0;
60
- for (;index < length; index += 1) {
61
+ for (let index = 0;index < length; index += 1) {
61
62
  const item = array[index];
62
63
  const itemValue = hasCallback ? callbacks.key?.(item) : item;
63
64
  if (type === "all" && itemValue === value || type === "unique" && values.indexOf(itemValue) === -1) {
@@ -84,6 +85,18 @@ var getCallbacks = function(bool, key) {
84
85
  key: (value) => value?.[key]
85
86
  };
86
87
  };
88
+ var getSortedValue = function(map, value, callback) {
89
+ if (!map.has(value)) {
90
+ map.set(value, new Map);
91
+ }
92
+ const stored = map.get(value);
93
+ if (stored?.has(callback)) {
94
+ return stored.get(callback);
95
+ }
96
+ const result = callback?.(value) ?? value;
97
+ stored?.set(callback, result);
98
+ return result;
99
+ };
87
100
  function groupBy(array, key) {
88
101
  const callbacks = getCallbacks(undefined, key);
89
102
  if (callbacks?.key == null) {
@@ -91,8 +104,7 @@ function groupBy(array, key) {
91
104
  }
92
105
  const grouped = {};
93
106
  const { length } = array;
94
- let index = 0;
95
- for (;index < length; index += 1) {
107
+ for (let index = 0;index < length; index += 1) {
96
108
  const item = array[index];
97
109
  const value = callbacks.key(item);
98
110
  if (value in grouped) {
@@ -112,9 +124,8 @@ function insert(array, index, values) {
112
124
  var insertValues = function(type, array, values, start, deleteCount) {
113
125
  const chunked = chunk(values).reverse();
114
126
  const { length } = chunked;
115
- let index = 0;
116
127
  let returned;
117
- for (;index < length; index += 1) {
128
+ for (let index = 0;index < length; index += 1) {
118
129
  const result = array.splice(start, index === 0 ? deleteCount : 0, ...chunked[index]);
119
130
  if (returned == null) {
120
131
  returned = result;
@@ -125,6 +136,44 @@ var insertValues = function(type, array, values, start, deleteCount) {
125
136
  function push(array, values) {
126
137
  return insertValues("push", array, values, array.length, 0);
127
138
  }
139
+ function sort(array, first, second) {
140
+ if (first == null || typeof first === "boolean") {
141
+ return first === true ? array.sort((first2, second2) => second2 - first2) : array.sort();
142
+ }
143
+ const direction = second === true ? "desc" : "asc";
144
+ const keys = (Array.isArray(first) ? first : [first]).map((key) => {
145
+ if (typeof key === "object") {
146
+ return "value" in key ? {
147
+ direction: key.direction,
148
+ callback: getCallbacks(null, key.value)?.key
149
+ } : null;
150
+ }
151
+ return {
152
+ direction,
153
+ callback: getCallbacks(null, key)?.key
154
+ };
155
+ }).filter((key) => typeof key?.callback === "function");
156
+ const { length } = keys;
157
+ if (length === 0) {
158
+ return second === true ? array.sort((first2, second2) => second2 - first2) : array.sort();
159
+ }
160
+ const store = new Map;
161
+ const sorted = array.sort((first2, second2) => {
162
+ for (let index = 0;index < length; index += 1) {
163
+ const { callback, direction: direction2 } = keys[index];
164
+ if (callback == null) {
165
+ continue;
166
+ }
167
+ const compared = comparison(getSortedValue(store, first2, callback), getSortedValue(store, second2, callback)) * (direction2 === "asc" ? 1 : -1);
168
+ if (compared !== 0) {
169
+ return compared;
170
+ }
171
+ }
172
+ return 0;
173
+ });
174
+ store.clear();
175
+ return sorted;
176
+ }
128
177
  function splice(array, start, amountOrValues, values) {
129
178
  const amoutOrValuesIsArray = Array.isArray(amountOrValues);
130
179
  return insertValues("splice", array, amoutOrValuesIsArray ? amountOrValues : values ?? [], start, amoutOrValuesIsArray ? array.length : typeof amountOrValues === "number" && amountOrValues > 0 ? amountOrValues : 0);
@@ -135,6 +184,7 @@ function unique(array, key) {
135
184
  export {
136
185
  unique,
137
186
  splice,
187
+ sort,
138
188
  push,
139
189
  insert,
140
190
  indexOf,
package/dist/js/array.mjs CHANGED
@@ -13,6 +13,9 @@ function chunk(array, size) {
13
13
  }
14
14
  return chunks;
15
15
  }
16
+ var comparison = function(first, second) {
17
+ return [first, second].every((value) => ["bigint", "boolean", "date", "number"].includes(typeof value)) ? Number(first) - Number(second) : String(first).localeCompare(String(second));
18
+ };
16
19
  function exists(array, value, key) {
17
20
  return findValue("index", array, value, key) > -1;
18
21
  }
@@ -28,12 +31,11 @@ var findValue = function(type, array, value, key) {
28
31
  return type === "index" ? array.indexOf(value) : array.find((item) => item === value);
29
32
  }
30
33
  if (callbacks.bool != null) {
31
- const index2 = array.findIndex(callbacks.bool);
32
- return type === "index" ? index2 : index2 > -1 ? array[index2] : undefined;
34
+ const index = array.findIndex(callbacks.bool);
35
+ return type === "index" ? index : index > -1 ? array[index] : undefined;
33
36
  }
34
37
  const { length } = array;
35
- let index = 0;
36
- for (;index < length; index += 1) {
38
+ for (let index = 0;index < length; index += 1) {
37
39
  const item = array[index];
38
40
  if (callbacks.key?.(item) === value) {
39
41
  return type === "index" ? index : item;
@@ -56,8 +58,7 @@ var findValues = function(type, array, value, key) {
56
58
  const hasCallback = typeof callbacks?.key === "function";
57
59
  const result = [];
58
60
  const values = hasCallback ? [] : result;
59
- let index = 0;
60
- for (;index < length; index += 1) {
61
+ for (let index = 0;index < length; index += 1) {
61
62
  const item = array[index];
62
63
  const itemValue = hasCallback ? callbacks.key?.(item) : item;
63
64
  if (type === "all" && itemValue === value || type === "unique" && values.indexOf(itemValue) === -1) {
@@ -84,6 +85,18 @@ var getCallbacks = function(bool, key) {
84
85
  key: (value) => value?.[key]
85
86
  };
86
87
  };
88
+ var getSortedValue = function(map, value, callback) {
89
+ if (!map.has(value)) {
90
+ map.set(value, new Map);
91
+ }
92
+ const stored = map.get(value);
93
+ if (stored?.has(callback)) {
94
+ return stored.get(callback);
95
+ }
96
+ const result = callback?.(value) ?? value;
97
+ stored?.set(callback, result);
98
+ return result;
99
+ };
87
100
  function groupBy(array, key) {
88
101
  const callbacks = getCallbacks(undefined, key);
89
102
  if (callbacks?.key == null) {
@@ -91,8 +104,7 @@ function groupBy(array, key) {
91
104
  }
92
105
  const grouped = {};
93
106
  const { length } = array;
94
- let index = 0;
95
- for (;index < length; index += 1) {
107
+ for (let index = 0;index < length; index += 1) {
96
108
  const item = array[index];
97
109
  const value = callbacks.key(item);
98
110
  if (value in grouped) {
@@ -112,9 +124,8 @@ function insert(array, index, values) {
112
124
  var insertValues = function(type, array, values, start, deleteCount) {
113
125
  const chunked = chunk(values).reverse();
114
126
  const { length } = chunked;
115
- let index = 0;
116
127
  let returned;
117
- for (;index < length; index += 1) {
128
+ for (let index = 0;index < length; index += 1) {
118
129
  const result = array.splice(start, index === 0 ? deleteCount : 0, ...chunked[index]);
119
130
  if (returned == null) {
120
131
  returned = result;
@@ -125,6 +136,44 @@ var insertValues = function(type, array, values, start, deleteCount) {
125
136
  function push(array, values) {
126
137
  return insertValues("push", array, values, array.length, 0);
127
138
  }
139
+ function sort(array, first, second) {
140
+ if (first == null || typeof first === "boolean") {
141
+ return first === true ? array.sort((first2, second2) => second2 - first2) : array.sort();
142
+ }
143
+ const direction = second === true ? "desc" : "asc";
144
+ const keys = (Array.isArray(first) ? first : [first]).map((key) => {
145
+ if (typeof key === "object") {
146
+ return "value" in key ? {
147
+ direction: key.direction,
148
+ callback: getCallbacks(null, key.value)?.key
149
+ } : null;
150
+ }
151
+ return {
152
+ direction,
153
+ callback: getCallbacks(null, key)?.key
154
+ };
155
+ }).filter((key) => typeof key?.callback === "function");
156
+ const { length } = keys;
157
+ if (length === 0) {
158
+ return second === true ? array.sort((first2, second2) => second2 - first2) : array.sort();
159
+ }
160
+ const store = new Map;
161
+ const sorted = array.sort((first2, second2) => {
162
+ for (let index = 0;index < length; index += 1) {
163
+ const { callback, direction: direction2 } = keys[index];
164
+ if (callback == null) {
165
+ continue;
166
+ }
167
+ const compared = comparison(getSortedValue(store, first2, callback), getSortedValue(store, second2, callback)) * (direction2 === "asc" ? 1 : -1);
168
+ if (compared !== 0) {
169
+ return compared;
170
+ }
171
+ }
172
+ return 0;
173
+ });
174
+ store.clear();
175
+ return sorted;
176
+ }
128
177
  function splice(array, start, amountOrValues, values) {
129
178
  const amoutOrValuesIsArray = Array.isArray(amountOrValues);
130
179
  return insertValues("splice", array, amoutOrValuesIsArray ? amountOrValues : values ?? [], start, amoutOrValuesIsArray ? array.length : typeof amountOrValues === "number" && amountOrValues > 0 ? amountOrValues : 0);
@@ -135,6 +184,7 @@ function unique(array, key) {
135
184
  export {
136
185
  unique,
137
186
  splice,
187
+ sort,
138
188
  push,
139
189
  insert,
140
190
  indexOf,
@@ -0,0 +1,70 @@
1
+ // src/js/is.ts
2
+ function isArrayOrPlainObject(value) {
3
+ return Array.isArray(value) || isPlainObject(value);
4
+ }
5
+ function isPlainObject(value) {
6
+ if (typeof value !== "object" || value === null) {
7
+ return false;
8
+ }
9
+ const prototype = Object.getPrototypeOf(value);
10
+ return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value);
11
+ }
12
+
13
+ // src/js/clone.ts
14
+ function clone(value) {
15
+ switch (true) {
16
+ case value == null:
17
+ return value;
18
+ case typeof value === "bigint":
19
+ return BigInt(value);
20
+ case typeof value === "boolean":
21
+ return Boolean(value);
22
+ case typeof value === "function":
23
+ return;
24
+ case typeof value === "number":
25
+ return Number(value);
26
+ case typeof value === "string":
27
+ return String(value);
28
+ case typeof value === "symbol":
29
+ return Symbol(value.description);
30
+ case value instanceof ArrayBuffer:
31
+ return cloneArrayBuffer(value);
32
+ case value instanceof DataView:
33
+ return cloneDataView(value);
34
+ case value instanceof Node:
35
+ return value.cloneNode(true);
36
+ case value instanceof RegExp:
37
+ return cloneRegularExpression(value);
38
+ case isArrayOrPlainObject(value):
39
+ return cloneNested(value);
40
+ default:
41
+ return structuredClone(value);
42
+ }
43
+ }
44
+ var cloneArrayBuffer = function(value) {
45
+ const cloned = new ArrayBuffer(value.byteLength);
46
+ new Uint8Array(cloned).set(new Uint8Array(value));
47
+ return cloned;
48
+ };
49
+ var cloneDataView = function(value) {
50
+ const buffer = cloneArrayBuffer(value.buffer);
51
+ return new DataView(buffer, value.byteOffset, value.byteLength);
52
+ };
53
+ var cloneNested = function(value) {
54
+ const cloned = Array.isArray(value) ? [] : {};
55
+ const keys = Object.keys(value);
56
+ const { length } = keys;
57
+ for (let index = 0;index < length; index += 1) {
58
+ const key = keys[index];
59
+ cloned[key] = clone(value[key]);
60
+ }
61
+ return cloned;
62
+ };
63
+ var cloneRegularExpression = function(value) {
64
+ const cloned = new RegExp(value.source, value.flags);
65
+ cloned.lastIndex = value.lastIndex;
66
+ return cloned;
67
+ };
68
+ export {
69
+ clone
70
+ };
@@ -0,0 +1,59 @@
1
+ // src/js/clone.ts
2
+ import {isArrayOrPlainObject} from "./is";
3
+ function clone(value) {
4
+ switch (true) {
5
+ case value == null:
6
+ return value;
7
+ case typeof value === "bigint":
8
+ return BigInt(value);
9
+ case typeof value === "boolean":
10
+ return Boolean(value);
11
+ case typeof value === "function":
12
+ return;
13
+ case typeof value === "number":
14
+ return Number(value);
15
+ case typeof value === "string":
16
+ return String(value);
17
+ case typeof value === "symbol":
18
+ return Symbol(value.description);
19
+ case value instanceof ArrayBuffer:
20
+ return cloneArrayBuffer(value);
21
+ case value instanceof DataView:
22
+ return cloneDataView(value);
23
+ case value instanceof Node:
24
+ return value.cloneNode(true);
25
+ case value instanceof RegExp:
26
+ return cloneRegularExpression(value);
27
+ case isArrayOrPlainObject(value):
28
+ return cloneNested(value);
29
+ default:
30
+ return structuredClone(value);
31
+ }
32
+ }
33
+ var cloneArrayBuffer = function(value) {
34
+ const cloned = new ArrayBuffer(value.byteLength);
35
+ new Uint8Array(cloned).set(new Uint8Array(value));
36
+ return cloned;
37
+ };
38
+ var cloneDataView = function(value) {
39
+ const buffer = cloneArrayBuffer(value.buffer);
40
+ return new DataView(buffer, value.byteOffset, value.byteLength);
41
+ };
42
+ var cloneNested = function(value) {
43
+ const cloned = Array.isArray(value) ? [] : {};
44
+ const keys = Object.keys(value);
45
+ const { length } = keys;
46
+ for (let index = 0;index < length; index += 1) {
47
+ const key = keys[index];
48
+ cloned[key] = clone(value[key]);
49
+ }
50
+ return cloned;
51
+ };
52
+ var cloneRegularExpression = function(value) {
53
+ const cloned = new RegExp(value.source, value.flags);
54
+ cloned.lastIndex = value.lastIndex;
55
+ return cloned;
56
+ };
57
+ export {
58
+ clone
59
+ };
@@ -32,8 +32,7 @@ var getValidElements = function(parent, filters, tabbable) {
32
32
  const indiced = [];
33
33
  const zeroed = [];
34
34
  const { length } = items;
35
- let index = 0;
36
- for (;index < length; index += 1) {
35
+ for (let index = 0;index < length; index += 1) {
37
36
  const item = items[index];
38
37
  if (item.tabIndex === 0) {
39
38
  zeroed.push(item.element);
@@ -61,8 +60,7 @@ var isDisabledFromFieldset = function(element) {
61
60
  if (parent instanceof HTMLFieldSetElement && parent.disabled) {
62
61
  const children = Array.from(parent.children);
63
62
  const { length } = children;
64
- let index = 0;
65
- for (;index < length; index += 1) {
63
+ for (let index = 0;index < length; index += 1) {
66
64
  const child = children[index];
67
65
  if (child instanceof HTMLLegendElement) {
68
66
  return parent.matches("fieldset[disabled] *") ? true : !child.contains(element);
@@ -32,8 +32,7 @@ var getValidElements = function(parent, filters, tabbable) {
32
32
  const indiced = [];
33
33
  const zeroed = [];
34
34
  const { length } = items;
35
- let index = 0;
36
- for (;index < length; index += 1) {
35
+ for (let index = 0;index < length; index += 1) {
37
36
  const item = items[index];
38
37
  if (item.tabIndex === 0) {
39
38
  zeroed.push(item.element);
@@ -61,8 +60,7 @@ var isDisabledFromFieldset = function(element) {
61
60
  if (parent instanceof HTMLFieldSetElement && parent.disabled) {
62
61
  const children = Array.from(parent.children);
63
62
  const { length } = children;
64
- let index = 0;
65
- for (;index < length; index += 1) {
63
+ for (let index = 0;index < length; index += 1) {
66
64
  const child = children[index];
67
65
  if (child instanceof HTMLLegendElement) {
68
66
  return parent.matches("fieldset[disabled] *") ? true : !child.contains(element);
@@ -33,9 +33,8 @@ var findElementOrElements = function(selector, context, single) {
33
33
  const result = [];
34
34
  if (typeof selector === "string") {
35
35
  const { length: length2 } = contexts;
36
- let index2 = 0;
37
- for (;index2 < length2; index2 += 1) {
38
- const value = callback.call(contexts[index2], selector);
36
+ for (let index = 0;index < length2; index += 1) {
37
+ const value = callback.call(contexts[index], selector);
39
38
  if (single) {
40
39
  if (value == null) {
41
40
  continue;
@@ -44,12 +43,11 @@ var findElementOrElements = function(selector, context, single) {
44
43
  }
45
44
  result.push(...Array.from(value));
46
45
  }
47
- return single ? undefined : result.filter((value, index3, array) => array.indexOf(value) === index3);
46
+ return single ? undefined : result.filter((value, index, array) => array.indexOf(value) === index);
48
47
  }
49
48
  const nodes = Array.isArray(selector) ? selector : selector instanceof NodeList ? Array.from(selector) : [selector];
50
49
  const { length } = nodes;
51
- let index = 0;
52
- for (;index < length; index += 1) {
50
+ for (let index = 0;index < length; index += 1) {
53
51
  const node = nodes[index];
54
52
  const element = node instanceof Document ? node.body : node instanceof Element ? node : undefined;
55
53
  if (element != null && (context == null || contexts.length === 0 || contexts.some((context2) => context2 === element || context2.contains(element))) && !result.includes(element)) {
@@ -9,9 +9,8 @@ var findElementOrElements = function(selector, context, single) {
9
9
  const result = [];
10
10
  if (typeof selector === "string") {
11
11
  const { length: length2 } = contexts;
12
- let index2 = 0;
13
- for (;index2 < length2; index2 += 1) {
14
- const value = callback.call(contexts[index2], selector);
12
+ for (let index = 0;index < length2; index += 1) {
13
+ const value = callback.call(contexts[index], selector);
15
14
  if (single) {
16
15
  if (value == null) {
17
16
  continue;
@@ -20,12 +19,11 @@ var findElementOrElements = function(selector, context, single) {
20
19
  }
21
20
  result.push(...Array.from(value));
22
21
  }
23
- return single ? undefined : result.filter((value, index3, array) => array.indexOf(value) === index3);
22
+ return single ? undefined : result.filter((value, index, array) => array.indexOf(value) === index);
24
23
  }
25
24
  const nodes = Array.isArray(selector) ? selector : selector instanceof NodeList ? Array.from(selector) : [selector];
26
25
  const { length } = nodes;
27
- let index = 0;
28
- for (;index < length; index += 1) {
26
+ for (let index = 0;index < length; index += 1) {
29
27
  const node = nodes[index];
30
28
  const element = node instanceof Document ? node.body : node instanceof Element ? node : undefined;
31
29
  if (element != null && (context == null || contexts.length === 0 || contexts.some((context2) => context2 === element || context2.contains(element))) && !result.includes(element)) {
@@ -0,0 +1,102 @@
1
+ // src/js/emitter.ts
2
+ var createObserable = function(emitter, observers) {
3
+ const instance = Object.create({
4
+ subscribe(first, second, third) {
5
+ return createSubscription(emitter, observers, getObserver(first, second, third));
6
+ }
7
+ });
8
+ return instance;
9
+ };
10
+ var createSubscription = function(emitter, observers, observer) {
11
+ let closed = false;
12
+ const instance = Object.create({
13
+ unsubscribe() {
14
+ if (!closed) {
15
+ closed = true;
16
+ observers.delete(instance);
17
+ }
18
+ }
19
+ });
20
+ Object.defineProperty(instance, "closed", {
21
+ get() {
22
+ return closed || !emitter.active;
23
+ }
24
+ });
25
+ observers.set(instance, observer);
26
+ observer.next?.(emitter.value);
27
+ return instance;
28
+ };
29
+ var getObserver = function(first, second, third) {
30
+ let observer;
31
+ if (typeof first === "object") {
32
+ observer = first;
33
+ } else {
34
+ observer = {
35
+ error: second,
36
+ next: first,
37
+ complete: third
38
+ };
39
+ }
40
+ return observer;
41
+ };
42
+ function emitter(value) {
43
+ let active = true;
44
+ let stored = value;
45
+ function finish(emit) {
46
+ if (active) {
47
+ active = false;
48
+ for (const [subscription, observer] of observers) {
49
+ if (emit) {
50
+ observer.complete?.();
51
+ }
52
+ subscription.unsubscribe();
53
+ }
54
+ }
55
+ }
56
+ const observers = new Map;
57
+ const instance = Object.create({
58
+ destroy() {
59
+ finish(false);
60
+ },
61
+ emit(value2) {
62
+ if (active) {
63
+ stored = value2;
64
+ for (const [, observer] of observers) {
65
+ observer.next?.(value2);
66
+ }
67
+ }
68
+ },
69
+ error(error) {
70
+ if (active) {
71
+ for (const [, observer] of observers) {
72
+ observer.error?.(error);
73
+ }
74
+ }
75
+ },
76
+ finish() {
77
+ finish(true);
78
+ }
79
+ });
80
+ const observable = createObserable(instance, observers);
81
+ Object.defineProperties(instance, {
82
+ active: {
83
+ get() {
84
+ return active;
85
+ }
86
+ },
87
+ observable: {
88
+ get() {
89
+ return observable;
90
+ }
91
+ },
92
+ value: {
93
+ get() {
94
+ return stored;
95
+ }
96
+ }
97
+ });
98
+ return instance;
99
+ }
100
+ export {
101
+ emitter
102
+ };
@@ -0,0 +1,102 @@
1
+ // src/js/emitter.ts
2
+ var createObserable = function(emitter, observers) {
3
+ const instance = Object.create({
4
+ subscribe(first, second, third) {
5
+ return createSubscription(emitter, observers, getObserver(first, second, third));
6
+ }
7
+ });
8
+ return instance;
9
+ };
10
+ var createSubscription = function(emitter, observers, observer) {
11
+ let closed = false;
12
+ const instance = Object.create({
13
+ unsubscribe() {
14
+ if (!closed) {
15
+ closed = true;
16
+ observers.delete(instance);
17
+ }
18
+ }
19
+ });
20
+ Object.defineProperty(instance, "closed", {
21
+ get() {
22
+ return closed || !emitter.active;
23
+ }
24
+ });
25
+ observers.set(instance, observer);
26
+ observer.next?.(emitter.value);
27
+ return instance;
28
+ };
29
+ var getObserver = function(first, second, third) {
30
+ let observer;
31
+ if (typeof first === "object") {
32
+ observer = first;
33
+ } else {
34
+ observer = {
35
+ error: second,
36
+ next: first,
37
+ complete: third
38
+ };
39
+ }
40
+ return observer;
41
+ };
42
+ function emitter(value) {
43
+ let active = true;
44
+ let stored = value;
45
+ function finish(emit) {
46
+ if (active) {
47
+ active = false;
48
+ for (const [subscription, observer] of observers) {
49
+ if (emit) {
50
+ observer.complete?.();
51
+ }
52
+ subscription.unsubscribe();
53
+ }
54
+ }
55
+ }
56
+ const observers = new Map;
57
+ const instance = Object.create({
58
+ destroy() {
59
+ finish(false);
60
+ },
61
+ emit(value2) {
62
+ if (active) {
63
+ stored = value2;
64
+ for (const [, observer] of observers) {
65
+ observer.next?.(value2);
66
+ }
67
+ }
68
+ },
69
+ error(error) {
70
+ if (active) {
71
+ for (const [, observer] of observers) {
72
+ observer.error?.(error);
73
+ }
74
+ }
75
+ },
76
+ finish() {
77
+ finish(true);
78
+ }
79
+ });
80
+ const observable = createObserable(instance, observers);
81
+ Object.defineProperties(instance, {
82
+ active: {
83
+ get() {
84
+ return active;
85
+ }
86
+ },
87
+ observable: {
88
+ get() {
89
+ return observable;
90
+ }
91
+ },
92
+ value: {
93
+ get() {
94
+ return stored;
95
+ }
96
+ }
97
+ });
98
+ return instance;
99
+ }
100
+ export {
101
+ emitter
102
+ };