@vyr/service-rpc-universal 0.0.1

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.
Files changed (51) hide show
  1. package/package.json +15 -0
  2. package/src/IMessage.ts +14 -0
  3. package/src/Message.ts +17 -0
  4. package/src/Operation.ts +113 -0
  5. package/src/Scheme.ts +20 -0
  6. package/src/UseRecord.ts +50 -0
  7. package/src/User.ts +21 -0
  8. package/src/VirtualNode.ts +107 -0
  9. package/src/asset/add.ts +35 -0
  10. package/src/asset/close.ts +35 -0
  11. package/src/asset/copy.ts +23 -0
  12. package/src/asset/create.ts +39 -0
  13. package/src/asset/cut.ts +23 -0
  14. package/src/asset/delete.ts +28 -0
  15. package/src/asset/dragdrap.ts +36 -0
  16. package/src/asset/index.ts +13 -0
  17. package/src/asset/initialize.ts +24 -0
  18. package/src/asset/open.ts +35 -0
  19. package/src/asset/remove.ts +29 -0
  20. package/src/asset/rename.ts +21 -0
  21. package/src/asset/select.ts +19 -0
  22. package/src/asset/update.ts +39 -0
  23. package/src/dep/add.ts +22 -0
  24. package/src/dep/create.ts +23 -0
  25. package/src/dep/delete.ts +23 -0
  26. package/src/dep/index.ts +5 -0
  27. package/src/dep/remove.ts +23 -0
  28. package/src/dep/update.ts +22 -0
  29. package/src/index.ts +14 -0
  30. package/src/rpc/confirm.ts +32 -0
  31. package/src/rpc/connection.ts +20 -0
  32. package/src/rpc/disconnect.ts +20 -0
  33. package/src/rpc/getUser.ts +37 -0
  34. package/src/rpc/index.ts +9 -0
  35. package/src/rpc/login.ts +35 -0
  36. package/src/rpc/logout.ts +20 -0
  37. package/src/rpc/redo.ts +18 -0
  38. package/src/rpc/undo.ts +18 -0
  39. package/src/scene/add.ts +35 -0
  40. package/src/scene/create.ts +28 -0
  41. package/src/scene/delete.ts +27 -0
  42. package/src/scene/download.ts +20 -0
  43. package/src/scene/dragdrap.ts +36 -0
  44. package/src/scene/index.ts +10 -0
  45. package/src/scene/initialize.ts +36 -0
  46. package/src/scene/record.ts +22 -0
  47. package/src/scene/remove.ts +28 -0
  48. package/src/scene/switch.ts +37 -0
  49. package/src/scene/update.ts +34 -0
  50. package/src/sidebar/index.ts +1 -0
  51. package/src/sidebar/switch.ts +35 -0
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@vyr/service-rpc-universal",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "main": "./src/index.ts",
6
+ "author": "",
7
+ "license": "MIT",
8
+ "dependencies": {
9
+ "@vyr/engine": "0.0.1"
10
+ },
11
+ "files": [
12
+ "package.json",
13
+ "src/"
14
+ ]
15
+ }
@@ -0,0 +1,14 @@
1
+ /** 通信消息 */
2
+ class IMessage {
3
+ message?: string
4
+ method!: string
5
+ params!: any
6
+
7
+ constructor(message?: string) {
8
+ this.message = message
9
+ }
10
+ }
11
+
12
+ export {
13
+ IMessage
14
+ }
package/src/Message.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { IMessage } from './IMessage'
2
+ import * as rpc from './rpc'
3
+ import * as sidebar from './sidebar'
4
+ import * as asset from './asset'
5
+ import * as scene from './scene'
6
+ import * as dep from './dep'
7
+
8
+ /** 通信消息 */
9
+ class Message extends IMessage {
10
+ static rpc = rpc
11
+ static sidebar = sidebar
12
+ static asset = asset
13
+ static scene = scene
14
+ static dep = dep
15
+ }
16
+
17
+ export { Message }
@@ -0,0 +1,113 @@
1
+ import { Generate } from "@vyr/engine"
2
+
3
+ const privateState = {
4
+ assetHistroryCollection: new Map<string, { undo: string[], redo: string[] }>(),
5
+ executeCollection: new Map<string, Operation[]>(),
6
+ inverse: (operation: Operation) => {
7
+ const Constructor = operation.constructor as typeof Operation
8
+ //@ts-ignore
9
+ const inverseOperation = new Constructor({ ...operation, inverse: !operation.inverse })
10
+ return inverseOperation
11
+ }
12
+ }
13
+
14
+ abstract class Operation {
15
+ /**撤销上一次的操作 */
16
+ static undo(trigger: string) {
17
+ const executeCollection = privateState.executeCollection.get(trigger)
18
+ if (executeCollection === undefined || executeCollection.length === 0) return
19
+ const operation = executeCollection[executeCollection.length - 1]
20
+ const current = operation.inverse ? operation.undo : operation;
21
+ if (current === null) return;
22
+ for (const asset of current.assets) {
23
+ const assetHistrory = privateState.assetHistroryCollection.get(asset)
24
+ if (assetHistrory === undefined || assetHistrory.undo.length === 0) return
25
+ const endTrigger = assetHistrory.undo[assetHistrory.undo.length - 1]
26
+ if (endTrigger !== trigger) return
27
+ }
28
+ const inverseOperation = privateState.inverse(current)
29
+ this.execute(inverseOperation)
30
+ }
31
+ /**重做上一次的操作 */
32
+ static redo(trigger: string) {
33
+ const executeCollection = privateState.executeCollection.get(trigger)
34
+ if (executeCollection === undefined || executeCollection.length === 0) return
35
+ const operation = executeCollection[executeCollection.length - 1]
36
+ const current = operation.inverse ? operation : operation.redo;
37
+ if (current === null) return;
38
+ for (const asset of current.assets) {
39
+ const assetHistrory = privateState.assetHistroryCollection.get(asset)
40
+ if (assetHistrory === undefined || assetHistrory.redo.length === 0) return
41
+ const endTrigger = assetHistrory.redo[assetHistrory.redo.length - 1]
42
+ if (endTrigger !== trigger) return
43
+ }
44
+ const inverseOperation = privateState.inverse(current)
45
+ this.execute(inverseOperation)
46
+ }
47
+ /**执行一次操作 */
48
+ static execute(operation: Operation) {
49
+ //将操作添加到对应的执行集合中
50
+ let executeCollection = privateState.executeCollection.get(operation.trigger)
51
+ if (executeCollection === undefined) {
52
+ executeCollection = []
53
+ privateState.executeCollection.set(operation.trigger, executeCollection)
54
+ }
55
+
56
+ //为操作绑定撤销、重做链接
57
+ const bind = executeCollection[executeCollection.length - 1]
58
+ if (bind !== undefined) {
59
+ if (operation.inverse) {
60
+ operation.redo = bind.inverse ? bind : bind.redo
61
+ } else {
62
+ operation.undo = bind.inverse ? bind.undo : bind
63
+ }
64
+ }
65
+
66
+ operation.throwIfOperationUnavailable()
67
+ operation.execute()
68
+
69
+ //将操作添加到对应的目标集合中
70
+ for (const asset of operation.assets) {
71
+ let assetHistrory = privateState.assetHistroryCollection.get(asset)
72
+ if (assetHistrory === undefined) {
73
+ assetHistrory = { undo: [], redo: [] }
74
+ privateState.assetHistroryCollection.set(asset, assetHistrory)
75
+ }
76
+ if (operation.inverse) {
77
+ assetHistrory.redo.push(operation.trigger)
78
+ if (assetHistrory.undo[assetHistrory.undo.length - 1] === operation.trigger) assetHistrory.undo.pop()
79
+ } else {
80
+ assetHistrory.undo.push(operation.trigger)
81
+ if (assetHistrory.redo[assetHistrory.redo.length - 1] === operation.trigger) assetHistrory.redo.pop()
82
+ }
83
+ }
84
+
85
+ executeCollection.push(operation)
86
+ }
87
+
88
+ readonly mark: string
89
+ /**操作涉及的资产 */
90
+ readonly assets: string[]
91
+ /**操作的触发者 */
92
+ readonly trigger: string
93
+ /**是否为逆操作 */
94
+ inverse: boolean
95
+ undo: Operation | null
96
+ redo: Operation | null
97
+
98
+ constructor(operation: Partial<Operation> = {}) {
99
+ this.mark = operation.mark ?? Generate.uuid()
100
+ this.assets = operation.assets ?? []
101
+ this.trigger = operation.trigger ?? ''
102
+ this.inverse = operation.inverse ?? false
103
+ this.undo = operation.undo ?? null
104
+ this.redo = operation.redo ?? null
105
+ }
106
+
107
+ abstract execute(): void
108
+
109
+ /**若操作不可用则抛出错误 */
110
+ abstract throwIfOperationUnavailable(): void
111
+ }
112
+
113
+ export { Operation }
package/src/Scheme.ts ADDED
@@ -0,0 +1,20 @@
1
+ declare global {
2
+ interface VyrSchemeDefine {
3
+ [k: string]: string
4
+ cache: string
5
+ project: string
6
+ static: string
7
+ scene: string
8
+ entry: string
9
+ }
10
+ }
11
+
12
+ const defineScheme: VyrSchemeDefine = {
13
+ cache: '/cache',
14
+ project: '/project',
15
+ static: '/static',
16
+ scene: '/scene',
17
+ entry: '/entry',
18
+ }
19
+
20
+ export { defineScheme }
@@ -0,0 +1,50 @@
1
+ import { ArrayUtils, DeserializationObject } from "@vyr/engine"
2
+
3
+ class UseRecord {
4
+ record: { [uuid: string]: string[] } = {}
5
+
6
+ addUser(url: string, client: string) {
7
+ if (this.record[url] === undefined) this.record[url] = []
8
+ ArrayUtils.insert(this.record[url], client)
9
+ }
10
+
11
+ removeUser(url: string, client: string) {
12
+ const record = this.record[url]
13
+ if (Array.isArray(record) === false) return
14
+ ArrayUtils.remove(record, client)
15
+ }
16
+
17
+ hasUser(url: string, client: string) {
18
+ if (this.record[url] === undefined) return false
19
+ return this.record[url].indexOf(client) > -1 ? true : false
20
+ }
21
+
22
+ clearUser(client: string) {
23
+ const urls = Object.keys(this.record)
24
+ for (const url of urls) {
25
+ this.removeUser(url, client)
26
+ }
27
+ }
28
+
29
+ clear(url: string) {
30
+ delete this.record[url]
31
+ }
32
+
33
+ copy(useRecord: DeserializationObject<UseRecord>) {
34
+ this.record = useRecord.record
35
+ }
36
+
37
+ isOperable(url: string, client: string) {
38
+ const record = this.record[url]
39
+ if (Array.isArray(record) === false || record.length === 0) return true
40
+ return record.indexOf(client) === 0 ? true : false
41
+ }
42
+
43
+ isDelete(url: string, client: string) {
44
+ const record = this.record[url]
45
+ if (Array.isArray(record) === false || record.length === 0) return true
46
+ return record.length === 1 && record.indexOf(client) === 0 ? true : false
47
+ }
48
+ }
49
+
50
+ export { UseRecord }
package/src/User.ts ADDED
@@ -0,0 +1,21 @@
1
+ class User {
2
+ name = ''
3
+ password = ''
4
+ token = ''
5
+ role = ''
6
+
7
+ constructor(user: Partial<User> = {}) {
8
+ this.name = user.name ?? ''
9
+ this.password = user.password ?? ''
10
+ this.token = user.token ?? ''
11
+ this.role = user.role ?? ''
12
+ }
13
+
14
+ clone() {
15
+ const user = new User(this)
16
+ user.password = ''
17
+ return user
18
+ }
19
+ }
20
+
21
+ export { User }
@@ -0,0 +1,107 @@
1
+ import { ArrayUtils, Descriptor, DeserializationObject } from "@vyr/engine"
2
+
3
+ /**虚拟资产,包含目录或文件的基本信息 */
4
+ class VirtualNode extends Descriptor {
5
+ static type = 'VirtualNode'
6
+ /**虚拟资产的类型是否为文件 */
7
+ readonly fileType: boolean
8
+ /**虚拟资产的方案 */
9
+ readonly scheme: string
10
+ /**虚拟资产是否可见 */
11
+ readonly visible: boolean
12
+ /**虚拟资产的请求地址 */
13
+ readonly url: string
14
+ /**虚拟资产的父节点 */
15
+ readonly parent: string
16
+ /**虚拟资产的用户队列,按顺序存储用户的表示 */
17
+ readonly userQueue: string[]
18
+ /**虚拟资产的所有子路径 */
19
+ declare children: VirtualNode[]
20
+ /**虚拟资产的后缀 */
21
+ readonly suffix: string
22
+ /**虚拟资产的类别
23
+ * - ts 脚本
24
+ * - material 材质
25
+ * - geometry 几何体
26
+ * - texture 纹理
27
+ * - image 图片
28
+ * - video 视频
29
+ * - audio 音频
30
+ * - other 其他(未被识别的文件)
31
+ */
32
+ readonly category: string
33
+ /**虚拟资产的名称 */
34
+ declare name: string
35
+
36
+ constructor(meta: Partial<DeserializationObject<VirtualNode>>) {
37
+ super({ ...meta, children: meta.children })
38
+ this.fileType = meta.fileType ?? false
39
+ this.scheme = meta.scheme ?? ''
40
+ this.visible = meta.visible ?? true
41
+ this.url = meta.url ?? this.uuid
42
+ this.parent = meta.parent ?? ''
43
+ this.userQueue = meta.userQueue ?? []
44
+ this.suffix = meta.suffix ?? ''
45
+ this.category = meta.category ?? ''
46
+ }
47
+
48
+ sort() {
49
+ const dirs = []
50
+ const files = []
51
+ for (const sub of this.children) sub.fileType ? files.push(sub) : dirs.push(sub)
52
+ dirs.sort((a, b) => a.url.localeCompare(b.url))
53
+ files.sort((a, b) => a.url.localeCompare(b.url))
54
+ this.clear()
55
+ this.add(...dirs, ...files)
56
+ }
57
+
58
+ addUser(user: string) {
59
+ ArrayUtils.insert(this.userQueue, user)
60
+ }
61
+
62
+ removeUser(user: string) {
63
+ ArrayUtils.remove(this.userQueue, user)
64
+ }
65
+
66
+ hasUser(user: string) {
67
+ return this.userQueue.indexOf(user) > -1 ? true : false
68
+ }
69
+
70
+ clearUser(user: string) {
71
+ this.traverse<VirtualNode>((sub) => sub.removeUser(user))
72
+ }
73
+
74
+ getUserCount() {
75
+ let count = 0
76
+ this.traverse<VirtualNode>((sub) => { count += sub.userQueue.length })
77
+ return count
78
+ }
79
+
80
+ getUniqueName(name: string, suffix: string) {
81
+ let count = 0
82
+ let curName = name
83
+
84
+ const subs: VirtualNode[] = [...this.children]
85
+ while (subs.length) {
86
+ const sub = subs.pop() as VirtualNode
87
+ if (sub.suffix !== suffix) continue
88
+ const i = sub.name.indexOf(curName)
89
+ if (i === 0) {
90
+ count++
91
+ curName = name + count
92
+ subs.length = 0
93
+ subs.push(...this.children)
94
+ }
95
+ }
96
+
97
+ return curName
98
+ }
99
+
100
+ isEditable(user: string) {
101
+ if (this.userQueue.length === 0) return true
102
+ return this.userQueue.indexOf(user) === 0 ? true : false
103
+ }
104
+ }
105
+ Descriptor.register(VirtualNode)
106
+
107
+ export { VirtualNode }
@@ -0,0 +1,35 @@
1
+ import { Descriptor, DeserializationObject } from '@vyr/engine'
2
+ import { IMessage } from '../IMessage'
3
+
4
+ const Method = '/cli/asset.add'
5
+
6
+ interface Params {
7
+ url: string
8
+ parent: string
9
+ node: DeserializationObject<Descriptor>[]
10
+ /**将新增节点插入到此节点之前 */
11
+ next: string
12
+ }
13
+
14
+ class RequestMessage extends IMessage {
15
+ method = Method
16
+ params: Params
17
+
18
+ constructor(url: Params['url'], parent: Params['parent'], node: Params['node'], next: Params['next'] = '') {
19
+ super()
20
+ this.params = { url, parent, node, next }
21
+ }
22
+ }
23
+
24
+ class NoticeMessage extends IMessage {
25
+ method = Method
26
+ params: Params
27
+
28
+ constructor(url: Params['url'], parent: Params['parent'], node: Params['node'], next: Params['next'] = '') {
29
+ super()
30
+ this.params = { url, parent, node, next }
31
+ }
32
+ }
33
+
34
+
35
+ export { Method, RequestMessage, NoticeMessage }
@@ -0,0 +1,35 @@
1
+ import { IMessage } from '../IMessage'
2
+
3
+ interface RequestParams {
4
+ url: string
5
+ }
6
+
7
+ const Method = '/cli/asset.close'
8
+
9
+ class RequestMessage extends IMessage {
10
+ method = Method
11
+ params: RequestParams
12
+
13
+ constructor(url: RequestParams['url']) {
14
+ super()
15
+ this.params = { url }
16
+ }
17
+ }
18
+
19
+ interface NoticeParams {
20
+ url: string
21
+ client: string
22
+ }
23
+
24
+ class NoticeMessage extends IMessage {
25
+ method = Method
26
+ params: NoticeParams
27
+
28
+ constructor(url: NoticeParams['url'], client: NoticeParams['client']) {
29
+ super()
30
+ this.params = { url, client }
31
+ }
32
+ }
33
+
34
+
35
+ export { Method, RequestMessage, NoticeMessage }
@@ -0,0 +1,23 @@
1
+ import { IMessage } from '../IMessage'
2
+
3
+ const Method = '/cli/asset.copy'
4
+
5
+ interface RequestParams {
6
+ /**需要复制的文件路径 */
7
+ urls: string[]
8
+ /**目标路径 */
9
+ target: string
10
+ }
11
+
12
+ class RequestMessage extends IMessage {
13
+ method = Method
14
+ params: RequestParams
15
+
16
+ constructor(urls: RequestParams['urls'], target: RequestParams['target']) {
17
+ super()
18
+ this.params = { urls, target }
19
+ }
20
+ }
21
+
22
+
23
+ export { Method, RequestMessage }
@@ -0,0 +1,39 @@
1
+ import { IMessage } from '../IMessage'
2
+
3
+ const Method = '/cli/asset.create'
4
+
5
+ interface RequestParams {
6
+ fileType: boolean
7
+ name: string
8
+ suffix: string
9
+ parent: string
10
+ content: string
11
+ }
12
+
13
+ class RequestMessage extends IMessage {
14
+ method = Method
15
+ params: RequestParams
16
+
17
+ constructor(fileType: boolean, name: string, suffix: string, parent: string, content: string) {
18
+ super()
19
+ this.params = { fileType, name, suffix, parent, content }
20
+ }
21
+ }
22
+
23
+ interface NoticeParams {
24
+ node: string
25
+ parent: string
26
+ }
27
+
28
+ class NoticeMessage extends IMessage {
29
+ method = Method
30
+ params: NoticeParams
31
+
32
+ constructor(node: NoticeParams['node'], parent: NoticeParams['parent']) {
33
+ super()
34
+ this.params = { node, parent }
35
+ }
36
+ }
37
+
38
+
39
+ export { Method, RequestMessage, NoticeMessage }
@@ -0,0 +1,23 @@
1
+ import { IMessage } from '../IMessage'
2
+
3
+ const Method = '/cli/asset.cut'
4
+
5
+ interface RequestParams {
6
+ /**需要剪切的文件路径 */
7
+ urls: string[]
8
+ /**目标路径 */
9
+ target: string
10
+ }
11
+
12
+ class RequestMessage extends IMessage {
13
+ method = Method
14
+ params: RequestParams
15
+
16
+ constructor(urls: RequestParams['urls'], target: RequestParams['target']) {
17
+ super()
18
+ this.params = { urls, target }
19
+ }
20
+ }
21
+
22
+
23
+ export { Method, RequestMessage }
@@ -0,0 +1,28 @@
1
+ import { IMessage } from '../IMessage'
2
+
3
+ const Method = '/cli/asset.delete'
4
+
5
+ interface Params {
6
+ urls: string[]
7
+ }
8
+
9
+ class RequestMessage extends IMessage {
10
+ method = Method
11
+ params: Params
12
+ constructor(urls: Params['urls']) {
13
+ super()
14
+ this.params = { urls }
15
+ }
16
+ }
17
+
18
+ class NoticeMessage extends IMessage {
19
+ method = Method
20
+ params: Params
21
+ constructor(urls: Params['urls']) {
22
+ super()
23
+ this.params = { urls }
24
+ }
25
+ }
26
+
27
+
28
+ export { Method, RequestMessage, NoticeMessage }
@@ -0,0 +1,36 @@
1
+ import { DraggableEndType, DraggableData } from '@vyr/declare'
2
+ import { IMessage } from '../IMessage'
3
+
4
+ const Method = '/cli/asset.dragdrap'
5
+
6
+ interface Params {
7
+ url: string
8
+ type: DraggableEndType,
9
+ dragData: DraggableData<{ uuids: string[] }>,
10
+ targetData: DraggableData<{ uuid: string }>,
11
+ }
12
+
13
+ class RequestMessage extends IMessage {
14
+ method = Method
15
+ params: Params
16
+ constructor(url: Params['url'], type: Params['type'], dragData: Params['dragData'], targetData: Params['targetData']) {
17
+ super()
18
+ this.params = { url, type, dragData, targetData }
19
+ }
20
+ }
21
+
22
+ interface NoticeParams extends Params {
23
+ next: string
24
+ }
25
+
26
+ class NoticeMessage extends IMessage {
27
+ method = Method
28
+ params: NoticeParams
29
+ constructor(url: NoticeParams['url'], type: NoticeParams['type'], dragData: NoticeParams['dragData'], targetData: NoticeParams['targetData'], next: NoticeParams['next'] = '') {
30
+ super()
31
+ this.params = { url, type, dragData, targetData, next }
32
+ }
33
+ }
34
+
35
+
36
+ export { Method, RequestMessage, NoticeMessage }
@@ -0,0 +1,13 @@
1
+ export * as initialize from './initialize'
2
+ export * as create from './create'
3
+ export * as delete from './delete'
4
+ export * as add from './add'
5
+ export * as remove from './remove'
6
+ export * as rename from './rename'
7
+ export * as select from './select'
8
+ export * as open from './open'
9
+ export * as close from './close'
10
+ export * as copy from './copy'
11
+ export * as cut from './cut'
12
+ export * as update from './update'
13
+ export * as dragdrap from './dragdrap'
@@ -0,0 +1,24 @@
1
+ import { DeserializationObject } from '@vyr/engine'
2
+ import { IMessage } from '../IMessage'
3
+ import { VirtualNode } from '../VirtualNode'
4
+
5
+ const Method = '/cli/asset.initialize'
6
+
7
+ class RequestMessage extends IMessage {
8
+ method = Method
9
+ constructor() {
10
+ super()
11
+ }
12
+ }
13
+
14
+ class NoticeMessage extends IMessage {
15
+ method = Method
16
+ params
17
+ constructor(root: DeserializationObject<VirtualNode>) {
18
+ super()
19
+ this.params = { root }
20
+ }
21
+ }
22
+
23
+
24
+ export { Method, RequestMessage, NoticeMessage }
@@ -0,0 +1,35 @@
1
+ import { IMessage } from '../IMessage'
2
+
3
+ const Method = '/cli/asset.open'
4
+
5
+ interface RequestParams {
6
+ url: string
7
+ }
8
+
9
+ class RequestMessage extends IMessage {
10
+ method = Method
11
+ params: RequestParams
12
+
13
+ constructor(url: RequestParams['url']) {
14
+ super()
15
+ this.params = { url }
16
+ }
17
+ }
18
+
19
+ interface NoticeParams {
20
+ url: string
21
+ client: string
22
+ }
23
+
24
+ class NoticeMessage extends IMessage {
25
+ method = Method
26
+ params: NoticeParams
27
+
28
+ constructor(url: NoticeParams['url'], client: NoticeParams['client']) {
29
+ super()
30
+ this.params = { url, client }
31
+ }
32
+ }
33
+
34
+
35
+ export { Method, RequestMessage, NoticeMessage }