@vyr/service-rpc-universal 0.0.12 → 0.0.14

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 CHANGED
@@ -1 +1 @@
1
- {"name":"@vyr/service-rpc-universal","version":"0.0.12","description":"","main":"./src/index.ts","author":"","license":"MIT","dependencies":{"@vyr/engine":"0.0.12"},"files":["package.json","src/"]}
1
+ {"name":"@vyr/service-rpc-universal","version":"0.0.14","description":"","main":"./src/index.ts","author":"","license":"MIT","dependencies":{"@vyr/engine":"0.0.14"},"files":["package.json","src/"]}
@@ -0,0 +1,122 @@
1
+ import axios from 'axios'
2
+ import { Descriptor, Listener } from '@vyr/engine'
3
+ import { Service } from '@vyr/service'
4
+ import { path, tokenKey, topic, Confirm, Message } from '@vyr/service-rpc-universal'
5
+
6
+ interface RpcServiceConfig {
7
+ token: string
8
+ notify: (msg: string) => void
9
+ confirm: (options: Partial<Confirm>, value?: string) => Promise<Confirm>
10
+ }
11
+
12
+ const _logout = () => {
13
+ window.localStorage.removeItem(tokenKey)
14
+ const location = new URL(window.location.href)
15
+ location.pathname = '/login.html'
16
+ window.location.href = location.href
17
+ }
18
+
19
+ class RpcService extends Service {
20
+
21
+ static async login(name: string, password: string) {
22
+ const data = new Message.rpc.login.RequestMessage(name, password)
23
+ const res = await axios.post<InstanceType<typeof Message['rpc']['login']['NoticeMessage']>>(Message.rpc.login.Method, data)
24
+ window.localStorage.setItem(tokenKey, res.data.params.token)
25
+
26
+ const location = new URL(window.location.href)
27
+ location.pathname = '/'
28
+ window.location.href = location.href
29
+ }
30
+
31
+ static async getUser() {
32
+ const data = new Message.rpc.getUser.RequestMessage()
33
+ const res = await axios.get<InstanceType<typeof Message['rpc']['getUser']['NoticeMessage']>>(Message.rpc.getUser.Method, data)
34
+ return res.data.params.user
35
+ }
36
+
37
+ static getToken() {
38
+ return window.localStorage.getItem(tokenKey) ?? ''
39
+ }
40
+ static setToken(toekn: string) {
41
+ window.localStorage.setItem(tokenKey, toekn)
42
+ }
43
+
44
+ private _listener = new Listener<{ [k: string]: (msg: Message) => void }>()
45
+ private confirm
46
+ private socket: any
47
+ private needTrigger = false
48
+ private msgQueue: Message[] = []
49
+
50
+ constructor(name: string, config: RpcServiceConfig) {
51
+ super(name)
52
+ this.confirm = config.confirm
53
+ const params = { path, extraHeaders: { authorization: 'Bearer ' + config.token }, autoConnect: false }
54
+ //@ts-ignore
55
+ this.socket = new window.io(location.href, params)
56
+
57
+ this.socket.on(topic, (content: string) => {
58
+ const msg = Descriptor.deserialization<Message>(content)
59
+ if (msg.message !== undefined) return config.notify(msg.message)
60
+ this._delayTrigger(msg)
61
+ })
62
+ this.socket.on('disconnect', () => {
63
+ this.trigger(Message.rpc.disconnect.Method, new Message.rpc.disconnect.NoticeMessage(this.client))
64
+ })
65
+
66
+ this.listen(Message.rpc.confirm.Method, this._onConfirm)
67
+ }
68
+
69
+ private _onConfirm = async (msg: InstanceType<typeof Message['rpc']['confirm']['NoticeMessage']>) => {
70
+ const result = await this.confirm(msg.params.config)
71
+ msg.params.config.value = result.value
72
+ msg.params.config.success = result.success
73
+ this.send(msg)
74
+ }
75
+
76
+ private _delayTrigger(msg: Message) {
77
+ this.msgQueue.push(msg)
78
+ if (this.needTrigger === true) return
79
+ this.needTrigger = true
80
+ setTimeout(() => this._executeMsgQueue(), 160)
81
+ }
82
+ private _executeMsgQueue() {
83
+ this.needTrigger = false
84
+ for (const msg of this.msgQueue) {
85
+ this.trigger(msg.method, msg)
86
+ }
87
+ this.msgQueue.length = 0
88
+ }
89
+
90
+ get client(): string {
91
+ return this.socket.id
92
+ }
93
+
94
+ get url(): string {
95
+ return this.socket.uri
96
+ }
97
+
98
+ async ready() {
99
+ this.listen(Message.rpc.logout.Method, _logout)
100
+ }
101
+
102
+ async start() {
103
+ await this.socket.connect()
104
+ }
105
+
106
+ trigger<T extends Message = Message>(method: string, msg: T) {
107
+ this._listener.trigger(method, msg)
108
+ }
109
+ listen<T extends Message = Message>(method: string, cb: (msg: T) => void) {
110
+ this._listener.listen(method, cb)
111
+ }
112
+ unlisten<T extends Message = Message>(method: string, cb: (msg: T) => void) {
113
+ this._listener.unlisten(method, cb)
114
+ }
115
+
116
+ /**向服务端发送数据 */
117
+ send(msg: Message) {
118
+ this.socket.emit(topic, Descriptor.serialization(msg))
119
+ }
120
+ }
121
+
122
+ export { RpcService }
@@ -0,0 +1,10 @@
1
+ import { Locale } from "@vyr/locale";
2
+ import { zhCnLanguageProvider, ZhCNLanguageProvider } from "./LanguageProvider";
3
+
4
+ Locale.register(zhCnLanguageProvider)
5
+
6
+ const language = Locale.getLanguage<ZhCNLanguageProvider>(zhCnLanguageProvider.name)
7
+
8
+ export {
9
+ language
10
+ }
@@ -0,0 +1,15 @@
1
+ import { LanguageProvider } from '@vyr/locale'
2
+
3
+ interface ZhCNLanguageProvider extends LanguageProvider {
4
+
5
+ }
6
+
7
+ const zhCnLanguageProvider: ZhCNLanguageProvider = {
8
+ id: 'zh_CN',
9
+ name: '@vyr/service-rpc',
10
+ }
11
+
12
+ export {
13
+ ZhCNLanguageProvider,
14
+ zhCnLanguageProvider,
15
+ }
@@ -0,0 +1,2 @@
1
+ export * from './LanguageProvider'
2
+ export * from './Language'