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
|
import { FilterModel } from '../core/filter/FilterModel';
import { ErrorResult, Exception, OperationParameters, OperationReference, Result, ResultParameters } from '../model/idea/idea';
import { action, computed, observable } from "mobx";
import { Gateway } from '../manager/Gateway';
export abstract class BaseOperation {
private _interactionTimeoutId: number = 0;
private static _currentOperations: Map<number, PollPromise> = new Map<number, PollPromise>();
//public InteractionTimeout: EventDelegate<InteractionTimeoutEventArgs> = new EventDelegate<InteractionTimeoutEventArgs>();
@observable public Error: string = "";
@observable public OverridingFilters: FilterModel[] = [];
//@observable
@observable public Result?: Result = undefined;
@observable public ComputationStarted: boolean = false;
public OperationReference?: OperationReference = undefined;
private static _nextId = 0;
public RequestSalt: string = "";
public Id: number;
constructor() {
this.Id = BaseOperation._nextId++;
}
@computed
public get FilterString(): string {
return "";
}
@action
public SetResult(result: Result): void {
this.Result = result;
}
public async Update(): Promise<void> {
try {
if (BaseOperation._currentOperations.has(this.Id)) {
BaseOperation._currentOperations.get(this.Id)!.Cancel();
if (this.OperationReference) {
Gateway.Instance.PauseOperation(this.OperationReference.toJSON());
}
}
const operationParameters = this.CreateOperationParameters();
if (this.Result) {
this.Result.progress = 0;
} // bcz: used to set Result to undefined, but that causes the display to blink
this.Error = "";
const salt = Math.random().toString();
this.RequestSalt = salt;
if (!operationParameters) {
this.ComputationStarted = false;
return;
}
this.ComputationStarted = true;
//let start = performance.now();
const promise = Gateway.Instance.StartOperation(operationParameters.toJSON());
promise.catch(err => {
action(() => {
this.Error = err;
console.error(err);
});
});
const operationReference = await promise;
if (operationReference) {
this.OperationReference = operationReference;
const resultParameters = new ResultParameters();
resultParameters.operationReference = operationReference;
const pollPromise = new PollPromise(salt, operationReference);
BaseOperation._currentOperations.set(this.Id, pollPromise);
pollPromise.Start(async () => {
const result = await Gateway.Instance.GetResult(resultParameters.toJSON());
if (result instanceof ErrorResult) {
throw new Error((result).message);
}
if (this.RequestSalt === pollPromise.RequestSalt) {
if (result && (!this.Result || this.Result.progress !== result.progress)) {
/*if (operationViewModel.Result !== null && operationViewModel.Result !== undefined) {
let t1 = performance.now();
console.log((t1 - start) + " milliseconds.");
start = performance.now();
}*/
this.SetResult(result);
}
if (!result || result.progress! < 1) {
return true;
}
}
return false;
}, 100).catch((err: Error) => action(() => {
this.Error = err.message;
console.error(err.message);
})()
);
}
}
catch (err) {
console.error(err as Exception);
// ErrorDialog.Instance.HandleError(err, operationViewModel);
}
}
public CreateOperationParameters(): OperationParameters | undefined { return undefined; }
private interactionTimeout() {
// clearTimeout(this._interactionTimeoutId);
// this.InteractionTimeout.Fire(new InteractionTimeoutEventArgs(this.TypedViewModel, InteractionTimeoutType.Timeout));
}
}
export class PollPromise {
public RequestSalt: string;
public OperationReference: OperationReference;
private _notCanceled: boolean = true;
private _poll: undefined | (() => Promise<boolean>);
private _delay: number = 0;
public constructor(requestKey: string, operationReference: OperationReference) {
this.RequestSalt = requestKey;
this.OperationReference = operationReference;
}
public Cancel(): void {
this._notCanceled = false;
}
public Start(poll: () => Promise<boolean>, delay: number): Promise<void> {
this._poll = poll;
this._delay = delay;
return this.pollRecursive();
}
private pollRecursive = (): Promise<void> => {
return Promise.resolve().then(this._poll).then((flag) => {
this._notCanceled && flag && new Promise((res) => (setTimeout(res, this._delay)))
.then(this.pollRecursive);
});
}
}
export class InteractionTimeoutEventArgs {
constructor(public Sender: object, public Type: InteractionTimeoutType) {
}
}
export enum InteractionTimeoutType {
Reset = 0,
Timeout = 1
}
|