@webreflection/bindings 0.0.0 → 0.0.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 +9 -7
- package/package.json +13 -3
- package/src/message-port.js +1 -1
package/README.md
CHANGED
|
@@ -5,24 +5,26 @@ A (soon to be) collection of bindings for various scenarios.
|
|
|
5
5
|
### Example
|
|
6
6
|
|
|
7
7
|
```js
|
|
8
|
-
import bindings from '@webreflection/bindings/port';
|
|
8
|
+
import bindings from '@webreflection/bindings/message-port';
|
|
9
9
|
|
|
10
10
|
const { port1, port2 } = new MessageChannel;
|
|
11
11
|
|
|
12
12
|
// expose bindings the remote can invoke
|
|
13
13
|
// the `remote` itself can invoke (asynchronously)
|
|
14
14
|
// bindings exposed via the other port
|
|
15
|
-
const remote =
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
15
|
+
const remote = /** @type {{sum: (a:number, b:number) => Promise<number>}} */(
|
|
16
|
+
bindings(port1, {
|
|
17
|
+
log(...values) {
|
|
18
|
+
console.log(...values);
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
|
+
);
|
|
20
22
|
|
|
21
23
|
// assuming this is an iframe
|
|
22
24
|
parent.postMessage(null, '*', [port2]);
|
|
23
25
|
|
|
24
26
|
// later on ...
|
|
25
|
-
const value = await remote.
|
|
27
|
+
const value = await remote.sum(1, 2);
|
|
26
28
|
```
|
|
27
29
|
|
|
28
30
|
The remote counterpart can use or expose bindings in a similar way, this module just orchestrate a reliable way to invoke exposed APIs via `postMessage` dances.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webreflection/bindings",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A (soon to be) collection of bindings for various scenarios.",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./message-port": "./src/message-port.js"
|
|
7
7
|
},
|
|
@@ -17,5 +17,15 @@
|
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@webreflection/utils": "^0.3.6",
|
|
19
19
|
"next-resolver": "^0.2.0"
|
|
20
|
-
}
|
|
20
|
+
},
|
|
21
|
+
"main": "index.js",
|
|
22
|
+
"devDependencies": {},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/WebReflection/bindings.git"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/WebReflection/bindings/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/WebReflection/bindings#readme"
|
|
21
31
|
}
|
package/src/message-port.js
CHANGED
|
@@ -7,7 +7,7 @@ const [next, resolve] = nextResolver();
|
|
|
7
7
|
* Creates a proxy that can be used to call methods remotely via the port.
|
|
8
8
|
* @param {MessagePort} port - The port to use for communication.
|
|
9
9
|
* @param {Record<string, (...args: any[]) => Promise<any>>} bindings - The bindings to use for the methods.
|
|
10
|
-
* @returns {
|
|
10
|
+
* @returns {unknown}
|
|
11
11
|
*/
|
|
12
12
|
export default (port, bindings) => {
|
|
13
13
|
port.onmessage = async ({ data: [exec, id, name, args] }) => {
|