@vyr/engine 0.0.20 → 0.0.22

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,12 +1,12 @@
1
1
  {
2
2
  "name": "@vyr/engine",
3
- "version": "0.0.20",
3
+ "version": "0.0.22",
4
4
  "description": "",
5
5
  "main": "./src/index.ts",
6
6
  "author": "",
7
7
  "license": "MIT",
8
8
  "dependencies": {
9
- "@vyr/locale": "0.0.20",
9
+ "@vyr/locale": "0.0.22",
10
10
  "tinycolor2": "1.6.0"
11
11
  },
12
12
  "devDependencies": {
@@ -1,8 +1,9 @@
1
1
  import { language } from "../locale"
2
2
  import { Category } from "../Category"
3
3
  import { AsyncTask } from "../AsyncTask"
4
- import { Descriptor, ServiceSchedulerDescriptor } from "../descriptor"
4
+ import { DatasetDescriptor, Descriptor, ServiceSchedulerDescriptor } from "../descriptor"
5
5
  import { InteractionDescriptor, Scriptable, ScriptableArgs } from "../interaction"
6
+ import { compile } from "../utils/compile"
6
7
  import { AssetGraph } from "./AssetGraph"
7
8
  import { Engine } from "../Engine"
8
9
 
@@ -33,6 +34,7 @@ const privateState = {
33
34
  }
34
35
 
35
36
  class Asset {
37
+ static virtual = 'virtual'
36
38
  static get baseUrl() {
37
39
  if (privateState.baseUrl === null) {
38
40
  try {
@@ -60,7 +62,7 @@ class Asset {
60
62
  }
61
63
 
62
64
  static createVirtualUrl(fileName: string) {
63
- return `/virtual:/${fileName}`
65
+ return `/${this.virtual}:/${fileName}`
64
66
  }
65
67
 
66
68
  static provider(url: string, provider: DescriptorProvider | ScriptableProvider) {
@@ -191,8 +193,8 @@ class Asset {
191
193
 
192
194
  const queue: Promise<void>[] = []
193
195
  for (const dataset of dependencide.dataset) {
194
- const trigger = Asset.get<Descriptor>(dataset)
195
- queue.push(graphics.invoke('compile', trigger))
196
+ const trigger = Asset.get<DatasetDescriptor>(dataset)
197
+ compile(trigger, graphics)
196
198
  }
197
199
 
198
200
  await Promise.all(queue)
@@ -17,7 +17,6 @@ interface ZhCNLanguageProvider extends LanguageProvider {
17
17
 
18
18
  '/virtual:/preset/net/http.ts': string
19
19
  '/virtual:/preset/dataset/update.ts': string
20
- '/virtual:/preset/dataset/compile.ts': string
21
20
  '/virtual:/preset/scheduler/switch.ts': string
22
21
  '/virtual:/preset/graphics/invoke.ts': string
23
22
  }
@@ -41,7 +40,6 @@ const zhCnLanguageProvider: ZhCNLanguageProvider = {
41
40
 
42
41
  '/virtual:/preset/net/http.ts': '网络请求',
43
42
  '/virtual:/preset/dataset/update.ts': '更新数据集',
44
- '/virtual:/preset/dataset/compile.ts': '组合数据集',
45
43
  '/virtual:/preset/scheduler/switch.ts': '切换场景',
46
44
  '/virtual:/preset/graphics/invoke.ts': '调用交互',
47
45
  }
@@ -1,2 +1 @@
1
- export * as update from './update'
2
- export * as compile from './compile'
1
+ export * as update from './update'
@@ -1,62 +1,19 @@
1
1
  import { language } from "../../../locale";
2
2
  import { Asset } from "../../../asset";
3
- import { HttpType } from "../../../utils";
3
+ import { http } from "../../../utils";
4
4
  import { Graphics } from "../../../graphics";
5
5
  import { Scriptable, InteractionNode, ScriptableArgs } from "../../../interaction";
6
6
 
7
7
  const scriptable = Asset.createVirtualUrl('preset/net/http.ts')
8
8
  class ExecuteScriptable extends Scriptable {
9
9
 
10
- joinUrl(url: string, requestData: any) {
11
- const urlObj = new URL(Asset.joinUrl(url));
12
-
13
- // 直接将requestData添加到urlObj的searchParams中
14
- Object.entries(requestData).forEach(([key, value]) => {
15
- if (value !== null && value !== undefined) {
16
- urlObj.searchParams.set(key, String(value));
17
- }
18
- });
19
-
20
- return urlObj.toString()
21
- }
22
-
23
10
  async execute(graphics: Graphics, args: ScriptableArgs) {
24
11
  const { input, result } = args
25
12
  let url = this.getInputValue('url', input)
26
13
  const type = this.getInputValue('type', input)
27
14
  const data = this.getInputValue('data', input)
28
15
 
29
- const requestData: { [k: string]: any } = {}
30
- const keys: string[] = []
31
- if (data.trim().length === 0) {
32
- keys.push(...result.keys())
33
- } else {
34
- keys.push(...data.split(','))
35
- }
36
-
37
- for (const key of keys) {
38
- const value = result.get(key)
39
- Object.assign(requestData, value ?? {})
40
- }
41
-
42
- url = url.replace(/\${(\w+)}/g, (match: any, key: string) => {
43
- return requestData.hasOwnProperty(key) ? requestData[key] : match;
44
- })
45
-
46
- const config = {
47
- method: type,
48
- headers: {
49
- 'Content-Type': 'application/json',
50
- },
51
- }
52
- let res
53
- if (type === HttpType.GET) {
54
- res = await fetch(this.joinUrl(url, requestData), config)
55
- } else {
56
- res = await fetch(url, { ...config, body: JSON.stringify(requestData) })
57
- }
58
-
59
- const json = await res.json()
16
+ const json = await http(url, type, data, result)
60
17
 
61
18
  return json
62
19
  }
@@ -1,3 +1,2 @@
1
- export * as dataset from './dataset'
2
1
  export * as scheduler from './scheduler'
3
2
  export * as graphics from './graphics'
@@ -1,6 +1,6 @@
1
1
  import { InteractionDescriptor, InteractionNode, InteractionProperty } from "../interaction"
2
2
  import { Descriptor, DynamicDescriptor, PrefabInstanceDescriptor, HTMLDescriptor, StyleDescriptor, DatasetDescriptor, DivDescriptor } from "../descriptor"
3
- import { AssetGraph, Port } from "../asset"
3
+ import { Asset, AssetGraph, Port } from "../asset"
4
4
  import { Category } from "../Category"
5
5
 
6
6
  const interactionExecutor = (interaction: InteractionProperty, path: string, port: Port) => {
@@ -30,7 +30,9 @@ const DescriptorProvider = (descriptor: Descriptor, port: Port) => {
30
30
  const interaction = descriptor.interactions[i]
31
31
  if (interaction.url) {
32
32
  interactionExecutor(interaction, `interactions[${i}]`, port)
33
- port.edges.push({ key: `interactions[${i}].url`, category: Category.interaction, asset: interaction.url })
33
+ if (interaction.url.startsWith(`/${Asset.virtual}:/`) === false) {
34
+ port.edges.push({ key: `interactions[${i}].url`, category: Category.interaction, asset: interaction.url })
35
+ }
34
36
  }
35
37
  }
36
38
  }
@@ -0,0 +1,50 @@
1
+ import { Graphics } from "../graphics/Graphics";
2
+ import { DatasetDescriptor, HttpDataConfig } from "../descriptor/DatasetDescriptor";
3
+ import { http } from "./http";
4
+
5
+ const transformData = (data: any, config: HttpDataConfig) => {
6
+ return data
7
+ }
8
+
9
+ const buildData = async (config: HttpDataConfig, trigger: DatasetDescriptor, graphics: Graphics) => {
10
+ const data = await http(config.url, config.type, '', new Map())
11
+ return transformData(data, config)
12
+ }
13
+
14
+ const compile = async (descriptor: DatasetDescriptor, graphics: Graphics) => {
15
+ if (descriptor.dataConfigs.length > 0) {
16
+ const dataConfigs = descriptor.dataConfigs as unknown as HttpDataConfig[]
17
+
18
+ let mergeData: any[] | any
19
+ if (dataConfigs.length === 1) {
20
+ const config = dataConfigs[0]
21
+ const finalData = await buildData(config, descriptor, graphics)
22
+ mergeData = config.key ? { [config.key]: finalData } : finalData
23
+ } else {
24
+ const keys = new Set<string>()
25
+ const queue: Promise<any>[] = []
26
+ for (const config of dataConfigs) {
27
+ keys.add(config.key)
28
+
29
+ const task = buildData(config, descriptor, graphics).then(finalData => {
30
+ const first = keys.values().next().value
31
+ if (keys.size === 1 && (first === '' || first === undefined)) {
32
+ if (!mergeData) mergeData = []
33
+ mergeData.push(finalData)
34
+ } else {
35
+ if (!mergeData) mergeData = {}
36
+ mergeData[config.key] ? Object.assign(mergeData[config.key], finalData) : mergeData[config.key] = finalData
37
+ }
38
+ })
39
+ queue.push(task)
40
+ }
41
+ await Promise.all(queue)
42
+ }
43
+
44
+ descriptor.setData(mergeData)
45
+ }
46
+ }
47
+
48
+ export {
49
+ compile
50
+ }
@@ -0,0 +1,56 @@
1
+ import { ScriptableArgs } from "../interaction";
2
+ import { Asset } from "../asset";
3
+ import { HttpType } from "./constants";
4
+
5
+ const joinUrl = (url: string, requestData: any) => {
6
+ const urlObj = new URL(Asset.joinUrl(url));
7
+
8
+ // 直接将requestData添加到urlObj的searchParams中
9
+ Object.entries(requestData).forEach(([key, value]) => {
10
+ if (value !== null && value !== undefined) {
11
+ urlObj.searchParams.set(key, String(value));
12
+ }
13
+ });
14
+
15
+ return urlObj.toString()
16
+ }
17
+
18
+ const http = async (url: string, type: string, data: string, result: ScriptableArgs['result']) => {
19
+ const requestData: { [k: string]: any } = {}
20
+ const keys: string[] = []
21
+ if (data.trim().length === 0) {
22
+ keys.push(...result.keys())
23
+ } else {
24
+ keys.push(...data.split(','))
25
+ }
26
+
27
+ for (const key of keys) {
28
+ const value = result.get(key)
29
+ Object.assign(requestData, value ?? {})
30
+ }
31
+
32
+ url = url.replace(/\${(\w+)}/g, (match: any, key: string) => {
33
+ return requestData.hasOwnProperty(key) ? requestData[key] : match;
34
+ })
35
+
36
+ const config = {
37
+ method: type,
38
+ headers: {
39
+ 'Content-Type': 'application/json',
40
+ },
41
+ }
42
+ let res
43
+ if (type === HttpType.GET) {
44
+ res = await fetch(joinUrl(url, requestData), config)
45
+ } else {
46
+ res = await fetch(url, { ...config, body: JSON.stringify(requestData) })
47
+ }
48
+
49
+ const json = await res.json()
50
+
51
+ return json
52
+ }
53
+
54
+ export {
55
+ http
56
+ }
@@ -1,2 +1,4 @@
1
1
  export * from './constants'
2
+ export * from './http'
3
+ export * from './compile'
2
4
  export * from './AssetProvider'
@@ -1,99 +0,0 @@
1
- import { language } from "../../../locale";
2
- import { Asset } from "../../../asset";
3
- import { InteractionNode, Scriptable, ScriptableArgs } from "../../../interaction";
4
- import { DatasetDescriptor, Descriptor, HttpDataConfig } from "../../../descriptor";
5
- import { Graphics } from "../../../graphics";
6
- import { ExecuteScriptable as HttpScriptable } from "../net/http";
7
-
8
- const scriptable = Asset.createVirtualUrl('preset/dataset/compile.ts')
9
- class ExecuteScriptable extends Scriptable {
10
-
11
- async getData(config: HttpDataConfig, http: HttpScriptable, trigger: Descriptor, graphics: Graphics) {
12
- const args: ScriptableArgs = {
13
- input: {
14
- previous: '',
15
- user: {
16
- type: { value: config.type },
17
- url: { value: config.url },
18
- data: { value: '' },
19
- },
20
- default: {}
21
- },
22
- result: new Map(),
23
- trigger: trigger
24
- }
25
-
26
- const data = await http.execute(graphics, args)
27
-
28
- return this.transformData(data, config)
29
- }
30
-
31
- transformData(data: any, config: HttpDataConfig) {
32
- return data
33
- }
34
-
35
- async execute(graphics: Graphics, args: ScriptableArgs) {
36
- const { trigger } = args
37
-
38
- if (trigger instanceof DatasetDescriptor && trigger.dataConfigs.length > 0) {
39
- const dataConfigs = trigger.dataConfigs as HttpDataConfig[]
40
- const http = new HttpScriptable(Asset.createVirtualUrl('preset/dataset/compile/http.ts'))
41
-
42
- let mergeData: any[] | any
43
- if (dataConfigs.length === 1) {
44
- const config = dataConfigs[0]
45
- const finalData = await this.getData(config, http, trigger, graphics)
46
- mergeData = config.key ? { [config.key]: finalData } : finalData
47
- } else {
48
- const keys = new Set<string>()
49
- const queue: Promise<any>[] = []
50
- for (const config of dataConfigs) {
51
- keys.add(config.key)
52
-
53
- const task = this.getData(config, http, trigger, graphics).then(finalData => {
54
- const first = keys.values().next().value
55
- if (keys.size === 1 && (first === '' || first === undefined)) {
56
- if (!mergeData) mergeData = []
57
- mergeData.push(finalData)
58
- } else {
59
- if (!mergeData) mergeData = {}
60
- mergeData[config.key] ? Object.assign(mergeData[config.key], finalData) : mergeData[config.key] = finalData
61
- }
62
- })
63
- queue.push(task)
64
- }
65
- await Promise.all(queue)
66
- }
67
-
68
- trigger.setData(mergeData)
69
- }
70
- }
71
- }
72
- Asset.provider(scriptable, async () => ({ default: ExecuteScriptable }))
73
-
74
- const createExecuteInput = (args: any = {}) => {
75
- const input = {
76
- }
77
-
78
- return input
79
- }
80
-
81
- const createExecuteNode = (options: { id: string; position?: any; input?: any }) => {
82
- const executeNode: InteractionNode = {
83
- id: options.id,
84
- label: language.get(scriptable as any),
85
- position: { x: options?.position?.x ?? 20, y: options?.position?.y ?? 20 },
86
- interaction: 'Execute',
87
- url: scriptable,
88
- input: createExecuteInput(options.input)
89
- }
90
- return executeNode
91
- }
92
-
93
- export {
94
- scriptable,
95
- createExecuteInput,
96
- createExecuteNode,
97
- }
98
-
99
-
@@ -1,27 +0,0 @@
1
- import { Asset } from "../../../asset";
2
- import { InteractionDescriptor } from "../../../interaction";
3
- import { dataset } from "../../execute"
4
-
5
- const url = Asset.createVirtualUrl('preset/dataset/compile.interaction.json')
6
- const nodes = {
7
- compile: 'compile'
8
- }
9
- let _currentInteraction: InteractionDescriptor | null = null
10
- const interactionProvider = async () => {
11
- if (_currentInteraction === null) {
12
- _currentInteraction = new InteractionDescriptor()
13
- const compileNode = dataset.compile.createExecuteNode({ id: nodes.compile })
14
- _currentInteraction.addNode([compileNode])
15
- _currentInteraction.roots.push(compileNode.id)
16
- }
17
- return _currentInteraction
18
- }
19
-
20
- Asset.provider(url, interactionProvider)
21
-
22
- export {
23
- url,
24
- nodes,
25
- }
26
-
27
-
@@ -1 +0,0 @@
1
- export * as compile from "./compile"