@webreflection/utils 0.2.4 → 0.2.6

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,10 @@ resolve(4);
34
34
 
35
35
  boundOnce(Promise).all === all;
36
36
  boundOnce(Promise).resolve === resolve;
37
+
38
+ // example: do not import or export same module
39
+
40
+ import sticky from '@webreflection/utils/sticky-module';
41
+
42
+ export default sticky('@module/name', { always: 'same' });
37
43
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webreflection/utils",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
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-module": {
25
+ "types": "./types/sticky-module.d.ts",
26
+ "import": "./src/sticky-module.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-module",
34
39
  "with-resolvers"
35
40
  ],
36
41
  "scripts": {
package/src/bound-once.js CHANGED
@@ -4,9 +4,9 @@ const $ = Symbol.for('@webreflection/utils/bound-once');
4
4
 
5
5
  // @ts-ignore
6
6
  const methods = globalThis[$] || Object.defineProperty(
7
- globalThis,
8
- $,
9
- { value: new WeakMap },
7
+ globalThis,
8
+ $,
9
+ { value: new WeakMap },
10
10
  )[$];
11
11
 
12
12
  /**
@@ -27,7 +27,6 @@ const handler = {
27
27
  * @returns {import('./bound.js').Bound<T>}
28
28
  */
29
29
  export default target => {
30
- let known = methods.get(target);
31
- if (!known) methods.set(target, (known = new Map));
30
+ if (!methods.has(target)) methods.set(target, new Map);
32
31
  return new Proxy(target, handler);
33
32
  };
@@ -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;