@logicflow/engine 0.1.0 → 0.1.2

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.
@@ -1 +0,0 @@
1
- export * from './browser'
@@ -1,28 +0,0 @@
1
- import { runInNodeContext } from './nodeVm'
2
-
3
- const isInNodeJS = typeof global === 'object' && global.global === global
4
-
5
- const globalScope: any = (() => {
6
- if (typeof self === 'object' && self.self === self) {
7
- return self
8
- }
9
-
10
- if (isInNodeJS) {
11
- return global
12
- }
13
-
14
- if (typeof globalThis === 'object') {
15
- return globalThis
16
- }
17
-
18
- return {
19
- eval: () => undefined,
20
- } as Record<string, unknown>
21
- })()
22
-
23
- const getExpressionResult = async (code: string, context: any) => {
24
- const r = await runInNodeContext(code, context)
25
- return r
26
- }
27
-
28
- export { isInNodeJS, globalScope, getExpressionResult }
@@ -1,14 +0,0 @@
1
- import vm from 'node:vm'
2
- // const vm = require('node:vm');
3
-
4
- export const runInNodeContext = async (
5
- code: string,
6
- globalData: Record<string, unknown> = {},
7
- ): Promise<any> => {
8
- const context = vm.createContext(globalData)
9
- vm.runInContext(code, context)
10
-
11
- console.log('context ===>>>', context)
12
-
13
- return context
14
- }
@@ -1,137 +0,0 @@
1
- import { Engine } from '..'
2
- import { storage } from '../utils'
3
-
4
- export const MAX_RECORDER = 100
5
- export const MAX_INSTANCE = 100
6
- export const LOGICFLOW_ENGINE_INSTANCES = 'LOGICFLOW_ENGINE_INSTANCES'
7
-
8
- export class Recorder implements Recorder.Base {
9
- instanceId: Engine.Key
10
- maxRecorder: number
11
-
12
- constructor({ instanceId }) {
13
- this.instanceId = instanceId
14
- this.maxRecorder = MAX_RECORDER
15
-
16
- const instances = this.getItem(LOGICFLOW_ENGINE_INSTANCES) || []
17
- if (instances.indexOf(instanceId) === -1) {
18
- instances.push(instanceId)
19
- }
20
- if (instances.length > MAX_INSTANCE) {
21
- const clearInstance = instances.shift()
22
- this.clearInstance(clearInstance)
23
- }
24
- this.setItem(LOGICFLOW_ENGINE_INSTANCES, instances)
25
- }
26
-
27
- setMaxRecorderNumber(max: number) {
28
- this.maxRecorder = max
29
- }
30
-
31
- // 将存储 storage 的方法收敛到此处,并在此处做异常处理 - setItem
32
- setItem(key: string | number, value: unknown) {
33
- try {
34
- storage.setItem(key, value)
35
- } catch (error) {
36
- console.error('Ops, something wrong with storage.setItem !!!')
37
- storage.clear()
38
- storage.setItem(key, value)
39
- }
40
- }
41
-
42
- // getItem 方法
43
- getItem(key: string | number) {
44
- return storage.getItem(key)
45
- }
46
-
47
- async getExecutionActions(executionId: Engine.Key) {
48
- return this.getItem(executionId)
49
- }
50
-
51
- async getExecutionList() {
52
- return this.getItem(this.instanceId) || []
53
- }
54
-
55
- private addExecution(executionId: Engine.Key) {
56
- const instanceExecutions = this.getItem(this.instanceId) || []
57
- if (instanceExecutions.length >= this.maxRecorder) {
58
- const toBeRemovedItem = instanceExecutions.shift()
59
- this.popExecution(toBeRemovedItem)
60
- }
61
- instanceExecutions.push(executionId)
62
- this.setItem(this.instanceId, instanceExecutions)
63
- }
64
-
65
- private popExecution(executionId: Engine.Key) {
66
- const instanceData = this.getItem(executionId) || []
67
- instanceData.forEach((actionId) => {
68
- storage.removeItem(actionId)
69
- })
70
- storage.removeItem(executionId)
71
- }
72
-
73
- private pushActionToExecution(executionId: Engine.Key, actionId: Engine.Key) {
74
- const actions = this.getItem(executionId) || []
75
- actions.push(actionId)
76
- this.setItem(executionId, actions)
77
- }
78
-
79
- /**
80
- * @param {Object} action
81
- * {
82
- * actionId: '',
83
- * nodeId: '',
84
- * executionId: '',
85
- * nodeType: '',
86
- * timestamp: '',
87
- * properties: {},
88
- * }
89
- */
90
- async addActionRecord(action: Recorder.Info) {
91
- const { executionId, actionId } = action
92
- const instanceData = await this.getExecutionActions(executionId)
93
-
94
- if (!instanceData) {
95
- this.addExecution(executionId)
96
- }
97
- this.pushActionToExecution(executionId, actionId)
98
- this.setItem(actionId, action)
99
- }
100
-
101
- async getActionRecord(actionId: Engine.Key): Promise<Recorder.Info> {
102
- return this.getItem(actionId)
103
- }
104
-
105
- clear() {
106
- this.clearInstance(this.instanceId)
107
- }
108
-
109
- clearInstance(instanceId: Engine.Key) {
110
- const instanceExecutions = this.getItem(instanceId) || []
111
- // TODO: 完善类型定义
112
- instanceExecutions.forEach((executionId) => {
113
- storage.removeItem(executionId)
114
- const instanceData = this.getItem(executionId) || []
115
- instanceData.forEach((actionId) => {
116
- storage.removeItem(actionId)
117
- })
118
- })
119
-
120
- storage.removeItem(instanceId)
121
- }
122
- }
123
-
124
- export namespace Recorder {
125
- export interface Base {
126
- addActionRecord: (action: Info) => Promise<void>
127
- getActionRecord: (actionId: Engine.Key) => Promise<Info>
128
- getExecutionActions: (executionId: Engine.Key) => Promise<string[]>
129
- clear: () => void
130
- }
131
-
132
- export type Info = {
133
- timestamp: number
134
- } & Engine.NextActionParam
135
- }
136
-
137
- export default Recorder
@@ -1,41 +0,0 @@
1
- // 判断当前环境是否为服务端
2
- // const isServer = typeof window === undefined;
3
-
4
- // const isServer = process.env.BROWSER === true;
5
-
6
- const isInBrowser = typeof window === 'object' && window.window === window
7
-
8
- const isInNodeJS = typeof global === 'object' && global.global === global
9
-
10
- const isInWebWorker =
11
- !isInBrowser && typeof self === 'object' && self.constructor
12
-
13
- const globalScope: any = (() => {
14
- if (isInBrowser) {
15
- return window
16
- }
17
-
18
- if (typeof self === 'object' && self.self === self) {
19
- return self
20
- }
21
-
22
- if (isInNodeJS) {
23
- return global
24
- }
25
-
26
- if (typeof globalThis === 'object') {
27
- return globalThis
28
- }
29
-
30
- return {
31
- eval: () => undefined,
32
- } as Record<string, unknown>
33
- })()
34
-
35
- export {
36
- // 环境相关方法
37
- globalScope,
38
- isInWebWorker,
39
- isInBrowser,
40
- isInNodeJS,
41
- }
package/src/utils/id.ts DELETED
@@ -1,16 +0,0 @@
1
- import { v4 as uuidV4 } from 'uuid'
2
-
3
- export const createExecId = (): string => {
4
- const uuid = uuidV4()
5
- return `exec-${uuid}`
6
- }
7
-
8
- export const createActionId = (): string => {
9
- const uuid = uuidV4()
10
- return `action-${uuid}`
11
- }
12
-
13
- export const createEngineId = (): string => {
14
- const uuid = uuidV4()
15
- return `engine-${uuid}`
16
- }
@@ -1,5 +0,0 @@
1
- import storage from './storage'
2
-
3
- export * from './global'
4
- export * from './id'
5
- export { storage }
@@ -1,55 +0,0 @@
1
- /**
2
- * 存储执行记录
3
- */
4
- import { globalScope } from './global'
5
-
6
- if (!globalScope.sessionStorage) {
7
- const storage = {
8
- data: {} as Record<string, unknown>,
9
-
10
- setItem(key, value) {
11
- storage.data[key] = value
12
- },
13
-
14
- getItem(key) {
15
- return storage.data[key]
16
- },
17
-
18
- removeItem(key) {
19
- delete storage.data[key]
20
- },
21
-
22
- clear() {
23
- storage.data = {}
24
- },
25
- }
26
-
27
- globalScope.sessionStorage = storage
28
- }
29
-
30
- export default {
31
- setItem(key, value) {
32
- if (typeof value === 'object') {
33
- value = JSON.stringify(value)
34
- }
35
-
36
- globalScope.sessionStorage.setItem(key, value)
37
- },
38
-
39
- getItem(key) {
40
- const value = globalScope.sessionStorage.getItem(key)
41
- try {
42
- return JSON.parse(value)
43
- } catch (error) {
44
- return value
45
- }
46
- },
47
-
48
- removeItem(key) {
49
- globalScope.sessionStorage.removeItem(key)
50
- },
51
-
52
- clear() {
53
- globalScope.sessionStorage.clear()
54
- },
55
- }