-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.js
70 lines (51 loc) · 2.15 KB
/
index.js
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
import CompositeClosureHelper from 'paraviewweb/src/Common/Core/CompositeClosureHelper';
// ----------------------------------------------------------------------------
// Global
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Busy Monitor
// ----------------------------------------------------------------------------
function busyMonitor(publicAPI, model) {
model.busyCounter = 0;
const checkNotifyStatus = (delta) => {
const lastCount = model.busyCounter;
model.busyCounter += delta;
if (model.busyCounter <= 0) {
// not busy anymore
publicAPI.fireBusyStatusChanged(false);
model.busyCounter = 0;
} else if (lastCount === 0 && model.busyCounter > 0) {
// busy now, don't notify again with increased count
publicAPI.fireBusyStatusChanged(true);
}
};
const success = (args) => {
checkNotifyStatus(-1);
return Promise.resolve(args);
};
const error = (args) => {
checkNotifyStatus(-1);
return Promise.reject(args);
};
publicAPI.busy = (promise) => {
checkNotifyStatus(1);
return promise.then(success, error);
};
publicAPI.busyWrapFunction = (fn) => (...args) => publicAPI.busy(fn(...args));
publicAPI.isBusy = () => model.busyCounter > 0;
}
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
const DEFAULT_VALUES = {};
// ----------------------------------------------------------------------------
export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);
CompositeClosureHelper.destroy(publicAPI, model);
CompositeClosureHelper.event(publicAPI, model, 'busyStatusChanged');
busyMonitor(publicAPI, model);
}
// ----------------------------------------------------------------------------
export const newInstance = CompositeClosureHelper.newInstance(extend);
// ----------------------------------------------------------------------------
export default { newInstance, extend };