@webreflection/utils 0.2.5 → 0.2.7

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 CHANGED
@@ -34,4 +34,9 @@ resolve(4);
34
34
 
35
35
  boundOnce(Promise).all === all;
36
36
  boundOnce(Promise).resolve === resolve;
37
+
38
+ // example: always retrieve the first time data/module
39
+ import sticky from '@webreflection/utils/sticky';
40
+
41
+ export default sticky('@module/name', { always: 'same' });
37
42
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webreflection/utils",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "type": "module",
5
5
  "types": {
6
6
  "./bound-once": "./types/bound-once.d.ts",
@@ -21,6 +21,10 @@
21
21
  "types": "./types/shared-array-buffer.d.ts",
22
22
  "import": "./src/shared-array-buffer.js"
23
23
  },
24
+ "./sticky": {
25
+ "types": "./types/sticky.d.ts",
26
+ "import": "./src/sticky.js"
27
+ },
24
28
  "./with-resolvers": {
25
29
  "types": "./types/with-resolvers.d.ts",
26
30
  "import": "./src/with-resolvers.js"
@@ -31,6 +35,7 @@
31
35
  "bound-once",
32
36
  "bound",
33
37
  "shared-array-buffer",
38
+ "sticky",
34
39
  "with-resolvers"
35
40
  ],
36
41
  "scripts": {
package/src/sticky.js ADDED
@@ -0,0 +1,27 @@
1
+ //@ts-check
2
+
3
+ const { defineProperty } = Object;
4
+ const { for: symbolFor } = Symbol;
5
+
6
+ /**
7
+ * Allow leaking a module globally to help avoid conflicting exports
8
+ * if the module might have been re-bundled in other projects.
9
+ * @template T
10
+ * @param {string} name the module name to save or retrieve
11
+ * @param {T} value the module as value to save if not known
12
+ * @param {globalThis} [global=globalThis] the reference where modules are saved where `globalThis` is the default
13
+ * @returns {[T, boolean]} the passed `value` or the previous one as first entry, a boolean indicating if it was known or not
14
+ */
15
+ export default (name, value, global = globalThis) => {
16
+ /** @type {symbol} */
17
+ const symbol = symbolFor(name);
18
+ const known = symbol in global;
19
+ return [
20
+ known ?
21
+ // @ts-ignore
22
+ global[symbol] :
23
+ // @ts-ignore
24
+ defineProperty(global, symbol, { value })[symbol],
25
+ known,
26
+ ];
27
+ };
@@ -0,0 +1,2 @@
1
+ declare function _default<T>(name: string, value: T, global?: typeof globalThis): [T, boolean];
2
+ export default _default;