@webreflection/utils 0.2.7 → 0.2.8
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/README.md +3 -1
- package/package.json +1 -1
- package/src/bound-once.js +5 -7
- package/src/sticky.js +5 -8
package/README.md
CHANGED
|
@@ -38,5 +38,7 @@ boundOnce(Promise).resolve === resolve;
|
|
|
38
38
|
// example: always retrieve the first time data/module
|
|
39
39
|
import sticky from '@webreflection/utils/sticky';
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
const [module, known] = sticky('@module/name', { always: 'same' });
|
|
42
|
+
|
|
43
|
+
export default module;
|
|
42
44
|
```
|
package/package.json
CHANGED
package/src/bound-once.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
//@ts-check
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
import sticky from './sticky.js';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
{ value: new WeakMap },
|
|
10
|
-
)[$];
|
|
5
|
+
const [methods] = sticky(
|
|
6
|
+
'@webreflection/utils/bound-once',
|
|
7
|
+
/** @type {WeakMap<object, Map<string | symbol, (...args: any[]) => unknown>>} */ (new WeakMap),
|
|
8
|
+
);
|
|
11
9
|
|
|
12
10
|
/**
|
|
13
11
|
* @type {ProxyHandler<any>}
|
package/src/sticky.js
CHANGED
|
@@ -13,15 +13,12 @@ const { for: symbolFor } = Symbol;
|
|
|
13
13
|
* @returns {[T, boolean]} the passed `value` or the previous one as first entry, a boolean indicating if it was known or not
|
|
14
14
|
*/
|
|
15
15
|
export default (name, value, global = globalThis) => {
|
|
16
|
-
/** @type {symbol} */
|
|
17
16
|
const symbol = symbolFor(name);
|
|
18
|
-
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
const known = global[symbol];
|
|
19
19
|
return [
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
// @ts-ignore
|
|
24
|
-
defineProperty(global, symbol, { value })[symbol],
|
|
25
|
-
known,
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
known ?? defineProperty(global, symbol, { value })[symbol],
|
|
22
|
+
!!known,
|
|
26
23
|
];
|
|
27
24
|
};
|