@@ -11,15 +11,15 @@ It’s especially useful for smooth communication between different execution co
1111### 🔗 Transparent Remote Function Calls
1212Call remote functions just like local ones:
1313``` js
14- await remote .add (1 , 2 );
14+ await remote .add (1 , 2 )
1515```
1616
1717### 🔄 Function Serialization
1818You can pass functions as arguments and even receive functions as return values:
1919
2020``` js
21- let fn = await remote .getCallback ();
22- await fn (" hello" );
21+ let fn = await remote .getCallback ()
22+ await fn (" hello" )
2323```
2424
2525### 🔌 Pluggable Transport
@@ -52,38 +52,41 @@ Too much boilerplate.
5252### ⚡️ With ` ioRPC ` :
5353#### UI (main window)
5454
55- ``` js
56- import { pair } from " https://unpkg.com/iorpc/index.esm.js" ;
55+ ``` html
56+ <script type =" module" >
57+ import { pair } from " https://unpkg.com/iorpc/index.esm.js"
5758
58- const worker = new Worker (" worker.js" );
59+ const worker = new Worker (" worker.js" )
5960
60- const { local , remote } = pair ({
61- send : msg => worker .postMessage (msg),
62- on : handler => worker .onmessage = e => handler (e .data )
63- });
61+ const {local , remote } = pair ({
62+ send : msg => worker .postMessage (msg),
63+ on : handler => worker .onmessage = e => handler (e .data )
64+ })
6465
65- async function run () {
66- await remote .processData ([1 , 2 , 3 ], progress => {
67- console .log (" Progress:" , progress);
68- });
69- }
66+ async function run () {
67+ await remote .processData ([1 , 2 , 3 ], progress => {
68+ console .log (" Progress:" , progress)
69+ })
70+ }
71+ run ()
72+ </script >
7073```
7174---
7275#### Worker (worker.js)
7376``` js
74- importScripts (" https://unpkg.com/iorpc/index.js" );
77+ importScripts (" https://unpkg.com/iorpc/index.js" )
7578
7679const { local , remote } = iorpc .pair ({
7780 send : msg => postMessage (msg),
7881 on : handler => onmessage = e => handler (e .data )
79- });
82+ })
8083
8184local .processData = async function (data , onProgress ) {
8285 for (let i = 0 ; i < data .length ; i++ ) {
83- await new Promise (r => setTimeout (r, 500 ));
84- await onProgress ((i + 1 ) / data .length );
86+ await new Promise (r => setTimeout (r, 500 ))
87+ await onProgress ((i + 1 ) / data .length )
8588 }
86- };
89+ }
8790```
8891---
8992🚀 Highlights
@@ -169,7 +172,7 @@ const { pair } = require('iorpc')
169172```
170173``` html
171174<script type =" module" >
172- import { pair } from " https://unpkg.com/iorpc/index.esm.js" ;
175+ import { pair } from " https://unpkg.com/iorpc/index.esm.js"
173176 </script >
174177```
175178If export is not specified, it will create a global variable:
@@ -262,7 +265,7 @@ const localApi = {
262265 }
263266}
264267
265- const wss = new WebSocketServer ({ port: 8080 });
268+ const wss = new WebSocketServer ({ port: 8080 })
266269wss .on (' connection' , ws => {
267270 const { remote } = pair ({
268271 send : data => ws .send (JSON .stringify (data)),
@@ -286,7 +289,7 @@ console.log('WebSocket server running on port 8080')
286289// const WebSocket = require('ws')
287290// const { pair } = require('iorpc')
288291import WebSocket from " ws"
289- import { pair } from ' iorpc' ;
292+ import { pair } from ' iorpc'
290293
291294const ws = new WebSocket (' ws://localhost:8080' )
292295
0 commit comments