forked from mebjas/html5-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state-manager.ts
193 lines (163 loc) · 5.57 KB
/
state-manager.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
/**
* @fileoverview
* State handler.
*
* @author mebjas <[email protected]>
*/
/** Different states of scanner */
export enum Html5QrcodeScannerState {
// Invalid internal state, do not set to this state.
UNKNOWN = 0,
// Indicates the scanning is not running or user is using file based
// scanning.
NOT_STARTED = 1,
// Camera scan is running.
SCANNING,
// Camera scan is paused but camera is running.
PAUSED,
}
/** Transaction for state transition. */
export interface StateManagerTransaction {
/**
* Executes the current transaction.
*/
execute(): void;
/**
* Cancels the current transaction.
*/
cancel(): void;
}
/** Manager class for states. */
export interface StateManager {
/**
* Start a transition to a new state. No other transitions will be allowed
* till this one is executed.
*
* @param newState new state to transition to.
*
* @returns transaction of type {@interface StateManagerTransaction}.
*
* @throws error if the new state is not a valid transition from current
* state.
*/
startTransition(newState: Html5QrcodeScannerState): StateManagerTransaction;
/**
* Directly execute a transition.
*
* @param newState new state to transition to.
*
* @throws error if the new state is not a valid transition from current
* state.
*/
directTransition(newState: Html5QrcodeScannerState): void;
/**
* Get current state.
*/
getState(): Html5QrcodeScannerState;
}
/**
* Implementation of {@interface StateManager} and
* {@interface StateManagerTransaction}.
*/
class StateManagerImpl implements StateManager, StateManagerTransaction {
private state: Html5QrcodeScannerState = Html5QrcodeScannerState.NOT_STARTED;
private onGoingTransactionNewState: Html5QrcodeScannerState
= Html5QrcodeScannerState.UNKNOWN;
public directTransition(newState: Html5QrcodeScannerState) {
this.failIfTransitionOngoing();
this.validateTransition(newState);
this.state = newState;
}
public startTransition(newState: Html5QrcodeScannerState): StateManagerTransaction {
this.failIfTransitionOngoing();
this.validateTransition(newState);
this.onGoingTransactionNewState = newState;
return this;
}
public execute() {
if (this.onGoingTransactionNewState
=== Html5QrcodeScannerState.UNKNOWN) {
throw "Transaction is already cancelled, cannot execute().";
}
const tempNewState = this.onGoingTransactionNewState;
this.onGoingTransactionNewState = Html5QrcodeScannerState.UNKNOWN;
this.directTransition(tempNewState);
}
public cancel() {
if (this.onGoingTransactionNewState
=== Html5QrcodeScannerState.UNKNOWN) {
throw "Transaction is already cancelled, cannot cancel().";
}
this.onGoingTransactionNewState = Html5QrcodeScannerState.UNKNOWN;
}
public getState(): Html5QrcodeScannerState {
return this.state;
}
//#region private methods
private failIfTransitionOngoing() {
if (this.onGoingTransactionNewState
!== Html5QrcodeScannerState.UNKNOWN) {
throw "Cannot transition to a new state, already under transition";
}
}
private validateTransition(newState: Html5QrcodeScannerState) {
switch(this.state) {
case Html5QrcodeScannerState.UNKNOWN:
throw "Transition from unknown is not allowed";
case Html5QrcodeScannerState.NOT_STARTED:
this.failIfNewStateIs(newState, [Html5QrcodeScannerState.PAUSED]);
break;
case Html5QrcodeScannerState.SCANNING:
// Both state transitions legal from here.
break;
case Html5QrcodeScannerState.PAUSED:
// Both state transitions legal from here.
break;
}
}
private failIfNewStateIs(
newState: Html5QrcodeScannerState,
disallowedStatesToTransition: Array<Html5QrcodeScannerState>) {
for (const disallowedState of disallowedStatesToTransition) {
if (newState === disallowedState) {
throw `Cannot transition from ${this.state} to ${newState}`;
}
}
}
//#endregion
}
export class StateManagerProxy {
private stateManager: StateManager;
constructor(stateManager: StateManager) {
this.stateManager = stateManager;
}
startTransition(newState: Html5QrcodeScannerState): StateManagerTransaction {
return this.stateManager.startTransition(newState);
}
directTransition(newState: Html5QrcodeScannerState) {
this.stateManager.directTransition(newState);
}
getState(): Html5QrcodeScannerState {
return this.stateManager.getState();
}
canScanFile(): boolean {
return this.stateManager.getState() === Html5QrcodeScannerState.NOT_STARTED;
}
isScanning(): boolean {
return this.stateManager.getState() !== Html5QrcodeScannerState.NOT_STARTED;
}
isStrictlyScanning(): boolean {
return this.stateManager.getState() === Html5QrcodeScannerState.SCANNING;
}
isPaused(): boolean {
return this.stateManager.getState() === Html5QrcodeScannerState.PAUSED;
}
}
/**
* Factory for creating instance of {@class StateManagerProxy}.
*/
export class StateManagerFactory {
public static create(): StateManagerProxy {
return new StateManagerProxy(new StateManagerImpl());
}
}