This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
/
index.ts
179 lines (150 loc) · 5.33 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { apiClients, automation } from '@cortezaproject/corteza-js'
import { ActionContext, StoreOptions } from 'vuex'
import { promptDefinitions } from '../../components/prompts'
interface Options {
api: apiClients.Automation;
ws: WebSocket;
watchInterval: number;
webapp: string;
}
interface State {
loading: boolean;
prompts: Array<automation.Prompt>;
/**
* Is prompt component active (modal open)?
* prompt = modal is open, show this/current prompt
* true = modal is open, show list of pending prompts
* false = modal is closed
*/
active: automation.Prompt | boolean;
}
interface ResumePayload {
prompt: automation.Prompt;
input: automation.Vars;
}
async function loadPrompts (api: apiClients.Automation): Promise<Array<automation.Prompt>> {
return api.sessionListPrompts().then(({ set } = {}) => {
if (!Array.isArray(set)) {
return []
}
return set.map((p: automation.Prompt) => new automation.Prompt(p))
})
}
async function resumeState (api: apiClients.Automation, { sessionID, stateID }: automation.Prompt, input: automation.Vars): Promise<unknown> {
return api.sessionResumeState({ sessionID, stateID, input })
}
async function cancelState (api: apiClients.Automation, { sessionID, stateID }: automation.Prompt): Promise<unknown> {
return api.sessionCancel({ sessionID, stateID })
}
function onlyFresh (existing: Array<automation.Prompt>, fresh: Array<automation.Prompt>): Array<automation.Prompt> {
const index = existing.map(({ stateID }) => stateID)
return fresh.filter(({ stateID = undefined }) => stateID && !index.includes(stateID))
}
export default function ({ api, webapp }: Options): StoreOptions<State> {
return {
strict: true,
state: {
loading: false,
active: false,
prompts: [],
},
getters: {
all ({ prompts }: State): Array<automation.Prompt> {
return prompts
},
isLoading ({ loading }: State): boolean {
return loading
},
isActive ({ active }: State): boolean {
return active !== false
},
current ({ active }: State): automation.Prompt | undefined {
if (typeof active === 'boolean') {
return undefined
} else {
return active
}
},
},
actions: {
async activate ({ commit }: ActionContext<State, State>, m?: true | automation.Prompt): Promise<void> {
commit('active', m ?? true)
},
async deactivate ({ commit }: ActionContext<State, State>): Promise<void> {
commit('active', false)
},
// fetch calls automation API endpoint and collects all states
async update ({ commit, state }: ActionContext<State, State>): Promise<void> {
return loadPrompts(api)
.then(pp => {
if (pp.length === 0) {
// purge all
commit('clear')
return
}
// filter fresh prompts before committing
// to minimize store traffic & state changes
const fresh = onlyFresh(state.prompts, pp)
if (fresh.length > 0) {
commit('update', fresh)
}
})
},
new ({ commit }: ActionContext<State, State>, prompt: automation.Prompt): void {
commit('update', [prompt])
},
async resume ({ commit }: ActionContext<State, State>, { prompt, input }: ResumePayload): Promise<void> {
commit('loading')
return resumeState(api, prompt, input)
.then(() => commit('remove', prompt))
.catch(() => commit('remove', prompt))
.finally(() => commit('loading', false))
},
/**
* Used when prompt is handled in this session and we need to
* send the cancellation state back to the API
*/
async cancel ({ commit }: ActionContext<State, State>, prompt: automation.Prompt): Promise<void> {
commit('loading')
return cancelState(api, prompt)
.then(() => commit('remove', prompt))
.finally(() => commit('loading', false))
},
/**
* Used when prompt is handled in another session
*/
clear ({ commit }: ActionContext<State, State>, prompt: automation.Prompt): void {
commit('remove', prompt)
},
},
mutations: {
loading (state: State, flag = true): void {
state.loading = flag
},
active (state: State, m: boolean | automation.Prompt): void {
state.active = m
},
clear (state: State): void {
state.prompts = []
},
update (state: State, pp: Array<automation.Prompt>): void {
// // Check if prompt should be run only in certain webapps
state.prompts.push(...pp.filter(({ ref }) => {
return promptDefinitions.some(p => {
return p.ref === ref && (!p.meta.webapps || p.meta.webapps.includes(webapp))
})
}))
},
remove (state: State, prompt: automation.Prompt): void {
state.prompts = state.prompts.filter(({ stateID }) => stateID !== prompt.stateID)
if (typeof state.active === 'object' && state.active.stateID === prompt.stateID) {
// When removed prompt is also active,
// reset active value.
//
// Set it to true but only if there are more prompts pending
state.active = state.prompts.length > 0
}
},
},
}
}