Skip to content

Commit be189f9

Browse files
docs: update readme
1 parent 9cda5d6 commit be189f9

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

readme.md

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@
22

33
[![Build Status](https://github.com/iceprosurface/remote-async/actions/workflows/merge.yml/badge.svg)](https://github.com/iceprosurface/remote-async/actions/workflows/merge.yml) [![codecov](https://codecov.io/gh/iceprosurface/remote-async/branch/master/graph/badge.svg)](https://codecov.io/gh/iceprosurface/remote-async)
44

5-
## install
5+
## Installation
66

77
```
8-
# npm
8+
# Using npm
99
npm i remote-async
10-
# yarn
10+
# Using yarn
1111
yarn add remote-async
12+
# Using pnpm
13+
pnpm add remote-async
1214
```
1315

14-
## why
16+
## Why use remote-async?
1517

16-
This is a lib for using promise under the protocol like socket(just like a normal request under http).
18+
This library allows the use of promises over protocols like sockets (similar to a standard HTTP request).
1719

18-
It has been specified a simple format between service and use a system like Subscribers - Publishers to get update.
20+
It utilizes a simple format for communication between services and employs a Subscribers-Publishers system for updates.
1921

20-
The server can be use under any protocol like socket, web worker event etc.
22+
The server can operate over any protocol, such as sockets, web workers, events, etc.
2123

2224

23-
## how to use
25+
## Usage
2426

2527
### iframe event
2628

@@ -32,7 +34,7 @@ const iframe = document.getElementById('iframe')
3234
const ORIGIN = 'some-origin'
3335
window.addEventListener('message', (event) => {
3436
if ((event.origin || event.originalEvent.origin) !== ORIGIN) {
35-
// for security, host must match
37+
// For security reasons, the host must match.
3638
return;
3739
}
3840
if (event.data.type === 'promise') {
@@ -44,17 +46,16 @@ ServerClient.registerSender((data) => {
4446
})
4547

4648
ServerClient.listen('anything', (data, resolve, reject) => {
47-
// do anything
49+
// Perform any action.
4850
})
4951
ServerClient.registerPromise('do-something')
5052
```
5153

52-
The script should be same as previous code in iframe.After this, protocol is completely duplex communication under
53-
`postMessage`.
54+
The script should be identical to the previous code within the iframe. After this, the protocol facilitates full duplex communication via postMessage
5455

55-
### figma plugin
56+
### Figma plugin
5657

57-
Figma plugin usage was similar to iframe event usage.
58+
Usage within a Figma plugin is similar to that of iframe events.
5859

5960
```ts
6061
// message front
@@ -83,7 +84,7 @@ ServerClient.registerPromise('do-something');
8384
```
8485

8586
```ts
86-
// field `main` in manifest.json
87+
// Field `main` in manifest.json
8788
export const serverMain = new Server();
8889
serverMain.registerSender((data) => {
8990
figma.ui.postMessage({
@@ -97,15 +98,15 @@ figma.ui.onmessage = (message) => {
9798
serverMain.receiveData(message.data);
9899
}
99100
};
100-
// listen event from front
101+
// Listen for events from the front.
101102
serverMain.listen('do something', async (data, resolve, reject) => {
102-
// do something
103+
// Perform any action.
103104
});
104105
```
105106

106107
### qiankun
107108

108-
qiankun did not have any events for communication, but wo can use `onGlobalStateChange` and `setGlobalState` to simulate event.
109+
qiankun does not have native events for communication, but we can use onGlobalStateChange and setGlobalState to simulate events.
109110

110111
```javascript
111112
// main/index.js
@@ -117,26 +118,26 @@ const { onGlobalStateChange, setGlobalState } = initGlobalState({
117118

118119
const serverMain = new Server();
119120
serverMain.listen('do-something', (d, resolve, reject) => {
120-
// if client send data { a: 1}, send resolve({ b:2 })
121+
// If client sends data { a: 1}, send resolve({ b: 2 })
121122
if (d.a === 1) {
122123
resolve({ b: 2 });
123124
} else {
124125
reject({ c: 1 });
125126
}
126127
});
127128
serverMain.registerSender((data) => {
128-
// define where to send data to client
129+
// Define where to send data to the client.
129130
setGlobalState({ asyncData: data });
130131
});
131132

132133
onGlobalStateChange((value, prev) => {
133-
// define where to receiveData from client
134+
// Define where to receive data from the client.
134135
serverMain.receiveData(value.asyncData);
135136
});
136137
```
137138

138139
```javascript
139-
// single spa
140+
// Single spa
140141
export const ServerClient = new Server();
141142
function storeTest(props) {
142143
if (props.onGlobalStateChange) {
@@ -159,13 +160,13 @@ function storeTest(props) {
159160
},
160161
});
161162
}
162-
// to use in any vue component
163+
// To use in any Vue component
163164
export default {
164165
methods: {
165166
async click() {
166-
// just like axios
167+
// Similar to using axios
167168
const data = await ServerClient.registerPromise('do-something', { a: 1 });
168-
// should be {b: 2}
169+
// Should return {b: 2}
169170
console.log(data);
170171
},
171172
}
@@ -175,7 +176,7 @@ export default {
175176
### socket
176177

177178
```javascript
178-
// server
179+
// Server
179180
var io = require('socket.io')(http);
180181
var { Server } = require('remote-async');
181182
const serverMain = new Server();
@@ -193,7 +194,7 @@ io.on('connection', (socket) => {
193194
```
194195

195196
```javascript
196-
// client
197+
// Client
197198
import { Server } from 'remote-async';
198199
const serverClient = new Server();
199200
const socket = io();
@@ -202,12 +203,12 @@ socket.on('connection', (socket) => {
202203
serverClient.receiveData(data);
203204
});
204205
serverClient.registerSender((data) => socket.emit('async-data', data));
205-
// do a promise
206+
// Execute a promise.
206207
serverClient.registerPromise('do-something', {a: 1})
207208
.then((d) => {
208-
// it will return here
209+
// It will return here.
209210
console.log(d);
210-
// {b: 2}
211+
// Should be {b: 2}.
211212
})
212213
socket.on('disconnect', () => {
213214
console.log('user disconnected');
@@ -220,7 +221,7 @@ socket.on('connection', (socket) => {
220221

221222
### constructor(setting: { timeout: number })
222223

223-
`timeout` default timeout setting
224+
`timeout`: Default timeout setting.
224225

225226
### Server.receiveData(data: RemoteData)
226227

@@ -229,18 +230,18 @@ Receive a remote data from another server.
229230

230231
### Server.listen(target: string, callback: RemoteCallBack)
231232

232-
Add a listener on `target` path, which will call `callback` when `receiveData` was called by any conditions.
233+
Add a listener on `target` path, which will call `callback` when `receiveData` is invoked under any circumstances.
233234

234235
### Server.off(target: string, callback?: any)
235236

236-
Remove listener on target. This will remove all callbacks when callback params was not set.
237+
Remove listener on target. This will remove all callbacks when the callback parameter is not set.
237238

238239
### Server.registerPromise(target: string, data: any, option: { timeout: number })
239240

240-
Register a promise and pass to anther server which listen `target` path.
241+
Registers a promise and sends it to another server listening on the `target` path.
241242

242243

243244
### Server.registerSender(sender: (data: RemoteData) => void)
244245

245-
Define the server sender how to pass a promise to another server
246+
Defines how the server sender should pass a promise to another server.
246247

0 commit comments

Comments
 (0)