@webreflection/utils 0.2.0 → 0.2.1

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
@@ -9,10 +9,29 @@ A [collection](./src/) of utility functions.
9
9
 
10
10
  ```js
11
11
  // example: a shim based on ArrayBuffer if native is `false`
12
- import { SharedArrayBuffer, native } from 'https://esm.run/@webreflection/utils/shared-array-buffer';
12
+ import { SharedArrayBuffer, native } from '@webreflection/utils/shared-array-buffer';
13
13
 
14
14
  // example: self bound Promise.withResolvers()
15
- import withResolvers from 'https://esm.run/@webreflection/utils/with-resolvers';
15
+ import withResolvers from '@webreflection/utils/with-resolvers';
16
16
 
17
17
  const { promise, resolve, reject } = withResolvers();
18
+
19
+ // example: any object bound method
20
+ import bound from '@webreflection/utils/bound';
21
+
22
+ const { all, resolve } = bound(Promise);
23
+ all([1, 2, 3]);
24
+ resolve(4);
25
+
26
+
27
+ // example: any object bound method is identical
28
+ // particularly useful for listeners identity
29
+ import boundOnce from '@webreflection/utils/bound-once';
30
+
31
+ const { all, resolve } = boundOnce(Promise);
32
+ all([1, 2, 3]);
33
+ resolve(4);
34
+
35
+ boundOnce(Promise).all === all;
36
+ boundOnce(Promise).resolve === resolve;
18
37
  ```
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@webreflection/utils",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  "./bound": {
7
7
  "types": "./types/bound.d.ts",
8
8
  "import": "./src/bound.js"
9
9
  },
10
+ "./bound-once": {
11
+ "types": "./types/bound-once.d.ts",
12
+ "import": "./src/bound-once.js"
13
+ },
10
14
  "./shared-array-buffer": {
11
15
  "types": "./types/shared-array-buffer.d.ts",
12
16
  "import": "./src/shared-array-buffer.js"
@@ -19,6 +23,7 @@
19
23
  },
20
24
  "tests": [
21
25
  "bound",
26
+ "bound-once",
22
27
  "shared-array-buffer",
23
28
  "with-resolvers"
24
29
  ],
@@ -0,0 +1,29 @@
1
+ //@ts-check
2
+
3
+ /**
4
+ * @type {WeakMap<object, Map<string | symbol, (...args: any[]) => unknown>>}
5
+ */
6
+ const methods = new WeakMap;
7
+
8
+ /**
9
+ * @type {ProxyHandler<any>}
10
+ */
11
+ const handler = {
12
+ get(target, prop) {
13
+ const known = /** @type {Map<string | symbol, (...args: any[]) => unknown>} */ (methods.get(target));
14
+ let method = known.get(prop);
15
+ if (!method) known.set(prop, (method = target[prop].bind(target)));
16
+ return method;
17
+ }
18
+ };
19
+
20
+ /**
21
+ * @template {object} T
22
+ * @param {T} target
23
+ * @returns {import('./bound.js').Bound<T>}
24
+ */
25
+ export default target => {
26
+ let known = methods.get(target);
27
+ if (!known) methods.set(target, (known = new Map));
28
+ return new Proxy(target, handler);
29
+ };
@@ -0,0 +1,2 @@
1
+ declare function _default<T extends object>(target: T): import("./bound.js").Bound<T>;
2
+ export default _default;