This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
/
application.ts
226 lines (212 loc) · 6.5 KB
/
application.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import {MachineConfig, Machine, StateMachine, assign} from 'xstate';
import {
FINAL,
MachineFactory,
CreateChannelEvent,
OpenChannelEvent,
CreateChannel,
JoinChannel,
ConcludeChannel,
IStore,
SendStates,
ChannelUpdated,
ChannelStoreEntry
} from '@statechannels/wallet-protocols';
import {State, getChannelId} from '@statechannels/nitro-protocol';
import {sendDisplayMessage, dispatchChannelUpdatedMessage} from '../messaging';
// Events
type OpenEvent = CreateChannelEvent | OpenChannelEvent;
interface PlayerStateUpdate {
type: 'PLAYER_STATE_UPDATE';
state: State;
}
interface PlayerRequestConclude {
type: 'PLAYER_REQUEST_CONCLUDE';
channelId: string;
}
export type ApplicationWorkflowEvent =
| PlayerRequestConclude
| PlayerStateUpdate
| SendStates
| OpenEvent;
// Context
interface ApplicationContext {
channelId?: string;
}
// States
const initializing = {on: {CREATE_CHANNEL: 'create', OPEN_CHANNEL: 'join'}};
const join = {
entry: ['assignChannelId', 'displayUi'],
invoke: {
id: 'joinMachine',
src: 'invokeJoinMachine',
data: (context, event) => event,
onDone: {target: 'running', actions: ['hideUi']},
autoForward: true
},
on: {
SendStates: {actions: ['updateStore']},
CHANNEL_UPDATED: {actions: ['sendChannelUpdatedNotification']}
}
};
const create = {
entry: ['displayUi'],
invoke: {
id: 'createMachine',
src: 'invokeCreateMachine',
data: (context, event) => event,
onDone: {target: 'running', actions: ['hideUi', 'assignChannelId']},
autoForward: true
},
on: {
SendStates: {actions: ['updateStore']},
CHANNEL_UPDATED: {actions: ['sendChannelUpdatedNotification']}
}
};
const running = {
on: {
PLAYER_STATE_UPDATE: {target: 'running', actions: ['sendToOpponent']},
CHANNEL_UPDATED: {target: 'running', actions: 'sendChannelUpdatedNotification'},
SendStates: [
{cond: 'channelOpen', target: 'running', actions: ['updateStore', 'sendChannelUpdated']},
{cond: 'channelClosing', target: 'closing', actions: ['updateStore', 'sendChannelUpdated']}
],
PLAYER_REQUEST_CONCLUDE: {target: 'closing'}
}
};
const closing = {
entry: ['displayUi'],
exit: ['hideUi'],
invoke: {
id: 'closingMachine',
src: 'invokeClosingMachine',
data: (context, event) => context,
autoForward: true
},
on: {
SendStates: {actions: ['updateStore']},
CHANNEL_UPDATED: [
{cond: 'channelClosed', target: 'done', actions: ['sendChannelUpdatedNotification']},
{target: 'closing', actions: ['sendChannelUpdatedNotification']}
]
}
};
const done = {type: FINAL};
export const config: MachineConfig<any, any, any> = {
initial: 'initializing',
states: {initializing, join, create, running, closing, done}
};
export const applicationWorkflow: MachineFactory<ApplicationContext, any> = (
store: IStore,
context: ApplicationContext
) => {
// Always use an empty context instead of undefined
if (!context) {
context = {};
}
const invokeJoinMachine = (
context,
event: OpenChannelEvent
): StateMachine<any, any, any, any> => {
return JoinChannel.machine(store, event);
};
const invokeCreateMachine = (
context,
event: CreateChannelEvent
): StateMachine<any, any, any, any> => {
return CreateChannel.machine(store);
};
const updateStore = (context, event: SendStates) => {
store.receiveStates(
// TODO: The outcome can get removed when going over the wire if it's empty
// For now we just add it back here
event.signedStates.map(ss => ({state: {outcome: [], ...ss.state}, signatures: ss.signatures}))
);
};
const sendToOpponent = (context, event: PlayerStateUpdate) => {
store.sendState(event.state);
};
const sendChannelUpdated = (context, event: ChannelUpdated) => {
if (event.entry.states.length > 0) {
const channelId = getChannelId(event.entry.states[0].state.channel);
// TODO: We should filter by context.channelId but that is not being set currently
dispatchChannelUpdatedMessage(channelId, new ChannelStoreEntry(event.entry));
}
};
const displayUi = (context, event) => {
sendDisplayMessage('Show');
};
const hideUi = (context, event) => {
sendDisplayMessage('Hide');
};
const invokeClosingMachine = (context: ApplicationContext) => {
if (!context.channelId) {
throw new Error('No channel id');
}
return ConcludeChannel.machine(store, {channelId: context.channelId});
};
const channelOpen = (context: ApplicationContext, event: SendStates): boolean => {
return !channelClosing(context, event);
};
const channelClosing = (context: ApplicationContext, event: SendStates): boolean => {
return event.signedStates
.filter(ss => context.channelId === getChannelId(ss.state.channel))
.some(ss => ss.state.isFinal);
};
const channelClosed = (context: ApplicationContext, event: ChannelUpdated): boolean => {
if (!context || !context.channelId) {
return false;
}
const channelStoreEntry = new ChannelStoreEntry(event.entry);
return channelStoreEntry.hasSupportedState && channelStoreEntry.latestSupportedState.isFinal;
};
const assignChannelId = assign(
(
context: ApplicationContext,
event: any // TODO Proper typing
) => {
if (!context.channelId) {
if (event.type === 'PLAYER_STATE_UPDATE') {
return {channelId: getChannelId(event.state.channel)};
} else if (event.type === 'OPEN_CHANNEL') {
return {channelId: getChannelId(event.signedState.state.channel)};
} else if (event.type === 'done.invoke.createMachine') {
return event.data;
}
return {};
}
return {};
}
);
const guards = {channelOpen, channelClosing, channelClosed};
const services = {invokeClosingMachine, invokeCreateMachine, invokeJoinMachine};
const actions = {
sendToOpponent,
updateStore,
sendChannelUpdated,
assignChannelId,
displayUi,
hideUi
};
const options = {
services,
actions,
guards
};
return Machine(config).withConfig(options, context);
};
const mockServices = {
invokeCreateMachine: () => {},
invokeJoinMachine: () => {},
invokeClosingMachine: () => {}
};
const mockActions = {
sendToOpponent: () => {},
updateStore: () => {},
sendChannelUpdatedNotification: () => {},
hideUi: () => {},
displayUi: () => {},
assignChannelId: () => {}
};
const mockGuards = {channelOpen: () => true, channelClosing: () => true};
export const mockOptions = {services: mockServices, actions: mockActions, guards: mockGuards};