@vyr/service-asset 0.0.33 → 0.0.35

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@vyr/service-asset",
3
- "version": "0.0.33",
3
+ "version": "0.0.35",
4
4
  "description": "",
5
5
  "main": "./src/index.ts",
6
6
  "author": "",
@@ -1,9 +1,10 @@
1
+ import z from 'zod'
1
2
  import { Service } from '@vyr/service'
2
3
  import { DraggableData, DraggableEndType, } from '@vyr/declare'
3
4
  import { runtime } from '@vyr/runtime'
4
- import { Descriptor, DeserializationObject } from '@vyr/engine'
5
- import { RpcService, Message, VirtualNode, request } from '@vyr/service-rpc'
6
- import { AssetMethodProvider } from "./AssetMethodProvider"
5
+ import { AsyncTask, Descriptor, DeserializationObject } from '@vyr/engine'
6
+ import { GatewayService, api, VirtualNode, request } from '@vyr/service-gateway'
7
+ import { bindExecutor } from './executor'
7
8
 
8
9
  class AssetService extends Service {
9
10
  static key = 'asset'
@@ -23,7 +24,7 @@ class AssetService extends Service {
23
24
  window.URL.revokeObjectURL(url)
24
25
  }
25
26
 
26
- private _provider = new AssetMethodProvider(this)
27
+ readonly initializeTask = new AsyncTask()
27
28
  readonly root
28
29
 
29
30
  constructor(name: string, root: VirtualNode) {
@@ -33,82 +34,191 @@ class AssetService extends Service {
33
34
  }
34
35
 
35
36
  async ready() {
36
- this._provider.listen()
37
+ const gatewayService = runtime.get<GatewayService>('gateway')
38
+ bindExecutor(gatewayService)
37
39
  }
38
40
 
39
41
  async start() {
40
- await this._provider.ready.done()
42
+ await this.initializeTask.done()
41
43
  }
42
44
 
43
- select(path: string) {
44
- const rpcService = runtime.get<RpcService>('rpc')
45
- rpcService.send(new Message.asset.select.RequestMessage(path))
45
+ async open(url: string) {
46
+ const data: z.infer<typeof api.asset.open.requestSchema> = {
47
+ url
48
+ }
49
+ await request({
50
+ url: api.asset.open.path,
51
+ method: api.asset.open.method,
52
+ data
53
+ })
46
54
  }
47
55
 
48
- open(path: string) {
49
- const rpcService = runtime.get<RpcService>('rpc')
50
- rpcService.send(new Message.asset.open.RequestMessage(path))
51
- }
52
-
53
- close(path: string) {
54
- const rpcService = runtime.get<RpcService>('rpc')
55
- rpcService.send(new Message.asset.close.RequestMessage(path))
56
+ async close(url: string) {
57
+ const data: z.infer<typeof api.asset.close.requestSchema> = {
58
+ url
59
+ }
60
+ await request({
61
+ url: api.asset.close.path,
62
+ method: api.asset.close.method,
63
+ data
64
+ })
56
65
  }
57
66
 
58
67
  /**新增文件 */
59
- create(fileType: boolean, name: string, suffix: string, parent: string, content: string) {
60
- const rpcService = runtime.get<RpcService>('rpc')
61
- rpcService.send(new Message.asset.create.RequestMessage(fileType, name, suffix, parent, content))
68
+ async create(fileType: boolean, name: string, suffix: string, parent: string, content: string) {
69
+ const data: z.infer<typeof api.asset.create.requestSchema> = {
70
+ fileType,
71
+ name,
72
+ suffix,
73
+ parent,
74
+ content
75
+ }
76
+ await request({
77
+ url: api.asset.create.path,
78
+ method: api.asset.create.method,
79
+ data
80
+ })
62
81
  }
63
82
 
64
83
  /**删除文件 */
65
- delete(path: string[] | string) {
66
- const requestData = Array.isArray(path) ? path : [path]
67
- if (requestData.length === 0) return
68
- const rpcService = runtime.get<RpcService>('rpc')
69
- rpcService.send(new Message.asset.delete.RequestMessage(requestData))
84
+ async delete(path: string[] | string) {
85
+ const urls = Array.isArray(path) ? path : [path]
86
+ if (urls.length === 0) return
87
+
88
+ const data: z.infer<typeof api.asset.delete.requestSchema> = {
89
+ urls
90
+ }
91
+ await request({
92
+ url: api.asset.delete.path,
93
+ method: api.asset.delete.method,
94
+ data
95
+ })
96
+ }
97
+
98
+ async add(url: string, node: DeserializationObject<Descriptor>[], parent: string) {
99
+
100
+ const data: z.infer<typeof api.asset.add.requestSchema> = {
101
+ url,
102
+ parent,
103
+ node,
104
+ next: ''
105
+ }
106
+ await request({
107
+ url: api.asset.add.path,
108
+ method: api.asset.add.method,
109
+ data
110
+ })
111
+ }
112
+
113
+ async remove(url: string, uuids: string[]) {
114
+
115
+ const data: z.infer<typeof api.asset.remove.requestSchema> = {
116
+ url,
117
+ uuids
118
+ }
119
+ await request({
120
+ url: api.asset.remove.path,
121
+ method: api.asset.remove.method,
122
+ data
123
+ })
124
+ }
125
+
126
+ async dragdrap(url: string, type: DraggableEndType, dragData: DraggableData<{ uuids: string[] }>, targetData: DraggableData<{ uuid: string }>) {
127
+
128
+ const data: z.infer<typeof api.asset.dragdrap.requestSchema> = {
129
+ url,
130
+ type,
131
+ dragData,
132
+ targetData,
133
+ next: ""
134
+ }
135
+ await request({
136
+ url: api.asset.dragdrap.path,
137
+ method: api.asset.dragdrap.method,
138
+ data
139
+ })
140
+ }
141
+
142
+ async rename(url: string, newName: string) {
143
+
144
+ const data: z.infer<typeof api.asset.rename.requestSchema> = {
145
+ url,
146
+ newName
147
+ }
148
+ await request({
149
+ url: api.asset.rename.path,
150
+ method: api.asset.rename.method,
151
+ data
152
+ })
70
153
  }
71
154
 
72
- add(path: string, node: DeserializationObject<Descriptor>[], parent: string) {
73
- const rpcService = runtime.get<RpcService>('rpc')
74
- rpcService.send(new Message.asset.add.RequestMessage(path, parent, node))
75
- }
155
+ /**剪切文件,将文件移动到指定目录 */
156
+ async cut(urls: string[], target: string) {
157
+ if (urls.length === 0) return
76
158
 
77
- remove(path: string, uuids: string[]) {
78
- const rpcService = runtime.get<RpcService>('rpc')
79
- rpcService.send(new Message.asset.remove.RequestMessage(path, uuids))
159
+ const data: z.infer<typeof api.asset.cut.requestSchema> = {
160
+ urls,
161
+ target
162
+ }
163
+ await request({
164
+ url: api.asset.cut.path,
165
+ method: api.asset.cut.method,
166
+ data
167
+ })
80
168
  }
81
169
 
82
- dragdrap(path: string, type: DraggableEndType, dragData: DraggableData<{ uuids: string[] }>, targetData: DraggableData<{ uuid: string }>) {
83
- const rpcService = runtime.get<RpcService>('rpc')
84
- rpcService.send(new Message.asset.dragdrap.RequestMessage(path, type, dragData, targetData))
170
+ /**复制文件 */
171
+ async copy(urls: string[], target: string) {
172
+ if (urls.length === 0) return
173
+
174
+ const data: z.infer<typeof api.asset.copy.requestSchema> = {
175
+ urls,
176
+ target
177
+ }
178
+ await request({
179
+ url: api.asset.copy.path,
180
+ method: api.asset.copy.method,
181
+ data
182
+ })
183
+ }
184
+ async update(url: string, node: DeserializationObject<Descriptor>) {
185
+
186
+ const data: z.infer<typeof api.asset.update.requestSchema> = {
187
+ url,
188
+ node
189
+ }
190
+ await request({
191
+ url: api.asset.update.path,
192
+ method: api.asset.update.method,
193
+ data
194
+ })
85
195
  }
86
196
 
87
- rename(path: string, newName: string) {
88
- const rpcService = runtime.get<RpcService>('rpc')
89
- rpcService.send(new Message.asset.rename.RequestMessage(newName, path))
197
+ async read(url: string) {
198
+ const res = await request.get<string>(url, { responseType: 'text' })
199
+ return res.data
90
200
  }
91
201
 
92
- /**剪切文件,将文件移动到指定目录 */
93
- cut(path: string[], target: string) {
94
- if (path.length === 0) return
95
- const rpcService = runtime.get<RpcService>('rpc')
96
- rpcService.send(new Message.asset.cut.RequestMessage(path, target))
97
- }
202
+ async download(id: string, filename: string) {
98
203
 
99
- /**复制文件 */
100
- copy(path: string[], target: string) {
101
- if (path.length === 0) return
102
- const rpcService = runtime.get<RpcService>('rpc')
103
- rpcService.send(new Message.asset.copy.RequestMessage(path, target))
104
- }
105
- update(path: string, content: DeserializationObject<Descriptor>) {
106
- const rpcService = runtime.get<RpcService>('rpc')
107
- rpcService.send(new Message.asset.update.RequestMessage(path, content))
204
+ const params: z.infer<typeof api.asset.download.requestSchema> = {
205
+ id
206
+ }
207
+ const res = await request({
208
+ url: api.asset.download.path,
209
+ method: api.asset.download.method,
210
+ params,
211
+ responseType: 'blob'
212
+ })
213
+ AssetService.save(filename, res.data)
108
214
  }
109
215
 
110
- async read(url: string) {
111
- const res = await request.get<string>(url, { responseType: 'text' })
216
+ async upload(formData: FormData) {
217
+ const res = await request({
218
+ url: api.asset.upload.path,
219
+ method: api.asset.upload.method,
220
+ data: formData,
221
+ })
112
222
  return res.data
113
223
  }
114
224
  }
@@ -0,0 +1,140 @@
1
+ import z from 'zod'
2
+ import { ArrayUtils, Descriptor, DeserializationObject } from "@vyr/engine"
3
+ import { DataService, runtime } from "@vyr/runtime"
4
+ import { api, request, GatewayService, VirtualNode } from "@vyr/service-gateway"
5
+ import { AssetService } from "../AssetService"
6
+
7
+ const _clear = (items: Descriptor[], item: VirtualNode) => {
8
+ if (items.length === 0) return
9
+ for (const stickupItem of items) {
10
+ if (stickupItem.uuid !== item.uuid) continue
11
+ ArrayUtils.remove(items, stickupItem)
12
+ return
13
+ }
14
+ }
15
+
16
+ const connection = async (data: z.infer<typeof api.system.connection.noticeSchema>) => {
17
+ const { data: result } = await request.post<z.infer<typeof api.asset.initialize.responseSchema>>(api.asset.initialize.path, {})
18
+
19
+ const service = runtime.get<AssetService>('asset')
20
+ service.root.clear()
21
+ for (const sub of result.root.children) service.root.add(new VirtualNode(sub))
22
+
23
+ const gatewayService = runtime.get<GatewayService>('gateway')
24
+ gatewayService.trigger(api.asset.initialize.path, result)
25
+ service.initializeTask.run()
26
+ }
27
+
28
+ const disconnect = (data: z.infer<typeof api.system.disconnect.noticeSchema>) => {
29
+ const service = runtime.get<AssetService>('asset')
30
+ service.root.clearUser(data.client)
31
+ }
32
+
33
+ const open = (data: z.infer<typeof api.asset.open.noticeSchema>) => {
34
+ const asset = VirtualNode.get<VirtualNode>(data.url)
35
+ if (asset) asset.addUser(data.client)
36
+ }
37
+
38
+ const close = (data: z.infer<typeof api.asset.close.noticeSchema>) => {
39
+ const asset = VirtualNode.get<VirtualNode>(data.url)
40
+ if (asset) asset.removeUser(data.client)
41
+ }
42
+
43
+ const create = (data: z.infer<typeof api.asset.create.noticeSchema>) => {
44
+ const asset = VirtualNode.get<VirtualNode>(data.parent)
45
+ if (asset === null) return
46
+ asset.add(new VirtualNode(VirtualNode.deserialization(data.asset)))
47
+ asset.sort()
48
+ }
49
+
50
+ const _delete = (data: z.infer<typeof api.asset.delete.noticeSchema>) => {
51
+ const dataService = runtime.get<DataService>('data')
52
+ for (const path of data.urls) {
53
+ const node = VirtualNode.get<VirtualNode>(path)
54
+ if (node === null) return
55
+ const ancestor = node.traceAncestor<VirtualNode>(false)
56
+ if (ancestor.parent === null) continue
57
+
58
+ _clear(dataService.footer.status.stickupItems, node)
59
+ ancestor.parent.remove(node)
60
+ }
61
+ }
62
+
63
+ const add = (data: z.infer<typeof api.asset.add.noticeSchema>) => {
64
+ const parent = Descriptor.get<Descriptor>(data.parent)
65
+ if (parent === null) return
66
+
67
+ if (data.next) {
68
+ for (const item of data.node) {
69
+ const node = Descriptor.create(item)
70
+ parent.insertBefore(node.uuid, data.next)
71
+ }
72
+ } else {
73
+ for (const item of data.node) {
74
+ const node = Descriptor.create(item)
75
+ parent.add(node)
76
+ }
77
+ }
78
+ }
79
+
80
+ const remove = (data: z.infer<typeof api.asset.remove.noticeSchema>) => {
81
+ const dataService = runtime.get<DataService>('data')
82
+
83
+ for (const uuid of data.uuids) {
84
+ const target = Descriptor.get<Descriptor>(uuid)
85
+ if (target === null) continue
86
+ const ancestor = target.traceAncestor(false)
87
+ if (ancestor.parent === null) continue
88
+ ancestor.parent.remove(target)
89
+
90
+ ArrayUtils.removeByKey(dataService.sidebar.selectAll, 'uuid', target.uuid)
91
+ ArrayUtils.removeByKey(dataService.sidebar.status.stickupItems, 'uuid', target.uuid)
92
+ }
93
+ }
94
+
95
+ const update = (data: z.infer<typeof api.asset.update.noticeSchema>) => {
96
+ const node = data.node as DeserializationObject<Descriptor>
97
+ const descriptor = Descriptor.get<Descriptor>(data.node.uuid)
98
+ if (descriptor) descriptor.syncWith(node)
99
+ }
100
+
101
+ const dragdrap = (data: z.infer<typeof api.asset.dragdrap.noticeSchema>) => {
102
+ const service = runtime.get<AssetService>('asset')
103
+ const dataService = runtime.get<DataService>('data')
104
+ if (data.url !== dataService.sidebar.url) return
105
+
106
+ const method = data.next ? 'insertBefore' : data.type
107
+ const target = data.next || data.targetData.data.uuid
108
+
109
+ for (const uuid of data.dragData.data.uuids) {
110
+ //@ts-ignore
111
+ service.root[method](uuid, target)
112
+ }
113
+ }
114
+
115
+ const bindExecutor = (gatewayService: GatewayService) => {
116
+ gatewayService.listen(api.system.connection.path, connection)
117
+ gatewayService.listen(api.system.disconnect.path, disconnect)
118
+ gatewayService.listen(api.asset.create.path, create)
119
+ gatewayService.listen(api.asset.delete.path, _delete)
120
+ gatewayService.listen(api.asset.add.path, add)
121
+ gatewayService.listen(api.asset.remove.path, remove)
122
+ gatewayService.listen(api.asset.open.path, open)
123
+ gatewayService.listen(api.asset.close.path, close)
124
+ gatewayService.listen(api.asset.update.path, update)
125
+ gatewayService.listen(api.asset.dragdrap.path, dragdrap)
126
+ }
127
+
128
+ export {
129
+ bindExecutor,
130
+ connection,
131
+ disconnect,
132
+ open,
133
+ close,
134
+ create,
135
+ _delete as delete,
136
+ add,
137
+ remove,
138
+ update,
139
+ dragdrap,
140
+ }
@@ -1,135 +0,0 @@
1
- import { ArrayUtils, AsyncTask, Descriptor } from "@vyr/engine"
2
- import { DataService, runtime, SidebarNavigator } from "@vyr/runtime"
3
- import { Message, RpcService, VirtualNode } from '@vyr/service-rpc'
4
- import { AssetService } from "./AssetService"
5
-
6
- class AssetMethodProvider {
7
- readonly ready = new AsyncTask()
8
- readonly service
9
-
10
- constructor(service: AssetService) {
11
- this.service = service
12
- }
13
-
14
- private _clear(items: Descriptor[], item: VirtualNode) {
15
- if (items.length === 0) return
16
- for (const stickupItem of items) {
17
- if (stickupItem.uuid !== item.uuid) continue
18
- ArrayUtils.remove(items, stickupItem)
19
- return
20
- }
21
- }
22
-
23
- listen() {
24
- const rpcService = runtime.get<RpcService>('rpc')
25
- rpcService.listen(Message.rpc.connection.Method, this._connection)
26
- rpcService.listen(Message.rpc.disconnect.Method, this._disconnect)
27
- rpcService.listen(Message.asset.initialize.Method, this._initialize)
28
- rpcService.listen(Message.asset.create.Method, this._create)
29
- rpcService.listen(Message.asset.delete.Method, this._delete)
30
- rpcService.listen(Message.asset.add.Method, this._add)
31
- rpcService.listen(Message.asset.remove.Method, this._remove)
32
- rpcService.listen(Message.asset.open.Method, this._open)
33
- rpcService.listen(Message.asset.close.Method, this._close)
34
- rpcService.listen(Message.asset.update.Method, this._update)
35
- rpcService.listen(Message.asset.dragdrap.Method, this._dragdrap)
36
- }
37
-
38
- _connection = async (msg: InstanceType<typeof Message['rpc']['connection']['NoticeMessage']>) => {
39
- const rpcService = runtime.get<RpcService>('rpc')
40
- rpcService.send(new Message.asset.initialize.RequestMessage())
41
- }
42
-
43
- _disconnect = (msg: InstanceType<typeof Message['rpc']['disconnect']['NoticeMessage']>) => {
44
- this.service.root.clearUser(msg.params.client)
45
- }
46
-
47
- _initialize = (msg: InstanceType<typeof Message['asset']['initialize']['NoticeMessage']>) => {
48
- this.service.root.clear()
49
- for (const sub of msg.params.root.children) this.service.root.add(new VirtualNode(sub))
50
- this.ready.run()
51
- }
52
-
53
- _open = (msg: InstanceType<typeof Message['asset']['open']['NoticeMessage']>) => {
54
- const asset = VirtualNode.get<VirtualNode>(msg.params.url)
55
- if (asset) asset.addUser(msg.params.client)
56
- }
57
-
58
- _close = (msg: InstanceType<typeof Message['asset']['close']['NoticeMessage']>) => {
59
- const asset = VirtualNode.get<VirtualNode>(msg.params.url)
60
- if (asset) asset.removeUser(msg.params.client)
61
- }
62
-
63
- _create = (msg: InstanceType<typeof Message['asset']['create']['NoticeMessage']>) => {
64
- const asset = VirtualNode.get<VirtualNode>(msg.params.parent)
65
- if (asset === null) return
66
- asset.add(new VirtualNode(VirtualNode.deserialization(msg.params.node)))
67
- asset.sort()
68
- }
69
-
70
- _delete = (msg: InstanceType<typeof Message['asset']['delete']['NoticeMessage']>) => {
71
- const dataService = runtime.get<DataService>('data')
72
- for (const path of msg.params.urls) {
73
- const node = VirtualNode.get<VirtualNode>(path)
74
- if (node === null) return
75
- const ancestor = node.traceAncestor<VirtualNode>(false)
76
- if (ancestor.parent === null) continue
77
-
78
- this._clear(dataService.footer.status.stickupItems, node)
79
- ancestor.parent.remove(node)
80
- }
81
- }
82
-
83
- _add = (msg: InstanceType<typeof Message['asset']['add']['NoticeMessage']>) => {
84
- const parent = Descriptor.get<Descriptor>(msg.params.parent)
85
- if (parent === null) return
86
-
87
- if (msg.params.next) {
88
- for (const item of msg.params.node) {
89
- const node = Descriptor.create(item)
90
- parent.insertBefore(node.uuid, msg.params.next)
91
- }
92
- } else {
93
- for (const item of msg.params.node) {
94
- const node = Descriptor.create(item)
95
- parent.add(node)
96
- }
97
- }
98
- }
99
-
100
- _remove = (msg: InstanceType<typeof Message['asset']['remove']['NoticeMessage']>) => {
101
- const dataService = runtime.get<DataService>('data')
102
- const navigator = dataService.sidebar.get<SidebarNavigator>(AssetService.key)
103
-
104
- for (const uuid of msg.params.uuids) {
105
- const target = Descriptor.get<Descriptor>(uuid)
106
- if (target === null) continue
107
- const ancestor = target.traceAncestor(false)
108
- if (ancestor.parent === null) continue
109
- ancestor.parent.remove(target)
110
-
111
- ArrayUtils.removeByKey(navigator.selectAll, 'uuid', target.uuid)
112
- ArrayUtils.removeByKey(navigator.status.stickupItems, 'uuid', target.uuid)
113
- }
114
- }
115
-
116
- _update = (msg: InstanceType<typeof Message['asset']['update']['NoticeMessage']>) => {
117
- const descriptor = Descriptor.get<Descriptor>(msg.params.content.uuid)
118
- if (descriptor) descriptor.syncWith(msg.params.content)
119
- }
120
-
121
- _dragdrap = (msg: InstanceType<typeof Message['asset']['dragdrap']['NoticeMessage']>) => {
122
- const dataService = runtime.get<DataService>('data')
123
- const navigator = dataService.sidebar.get<SidebarNavigator>(AssetService.key)
124
- if (msg.params.url !== navigator.url) return
125
-
126
- const method = msg.params.next ? 'insertBefore' : msg.params.type
127
- const target = msg.params.next || msg.params.targetData.data.uuid
128
-
129
- for (const uuid of msg.params.dragData.data.uuids) {
130
- this.service.root[method](uuid, target)
131
- }
132
- }
133
- }
134
-
135
- export { AssetMethodProvider }