@vyr/service-rpc 0.0.19 → 0.0.20
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/package.json +1 -1
- package/src/RpcService.ts +15 -5
package/package.json
CHANGED
package/src/RpcService.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import axios from 'axios'
|
|
2
|
-
import {
|
|
2
|
+
import { Serialization, Listener } from '@vyr/engine'
|
|
3
3
|
import { Service } from '@vyr/service'
|
|
4
4
|
import { path, tokenKey, topic, Confirm, Message } from '@vyr/service-rpc-universal'
|
|
5
5
|
|
|
@@ -55,7 +55,7 @@ class RpcService extends Service {
|
|
|
55
55
|
this.socket = new window.io(location.href, params)
|
|
56
56
|
|
|
57
57
|
this.socket.on(topic, (content: string) => {
|
|
58
|
-
const msg =
|
|
58
|
+
const msg = Serialization.parse(content) as Message
|
|
59
59
|
if (msg.message !== undefined) return config.notify(msg.message)
|
|
60
60
|
this._delayTrigger(msg)
|
|
61
61
|
})
|
|
@@ -106,7 +106,7 @@ class RpcService extends Service {
|
|
|
106
106
|
trigger<T extends Message = Message>(method: string, msg: T) {
|
|
107
107
|
this._listener.trigger(method, msg)
|
|
108
108
|
}
|
|
109
|
-
listen<T extends Message = Message>(method: string, cb: (msg: T) => void) {
|
|
109
|
+
listen<T extends Message = Message>(method: string, cb: (msg: T, ...others: any[]) => void) {
|
|
110
110
|
this._listener.listen(method, cb)
|
|
111
111
|
}
|
|
112
112
|
unlisten<T extends Message = Message>(method: string, cb: (msg: T) => void) {
|
|
@@ -114,8 +114,18 @@ class RpcService extends Service {
|
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
/**向服务端发送数据 */
|
|
117
|
-
send(msg: Message) {
|
|
118
|
-
|
|
117
|
+
send<T extends Message | void = Message>(msg: Message) {
|
|
118
|
+
return new Promise<T>(resolve => {
|
|
119
|
+
this.socket.emit(topic, Serialization.stringify(msg), (content: string) => {
|
|
120
|
+
if (content) {
|
|
121
|
+
const msg = Serialization.parse(content)
|
|
122
|
+
resolve(msg as T)
|
|
123
|
+
} else {
|
|
124
|
+
//@ts-ignore
|
|
125
|
+
resolve()
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
})
|
|
119
129
|
}
|
|
120
130
|
}
|
|
121
131
|
|