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
class TAbstractDataInserter {
constructor ( parameters = {} ) {
const _parameters = {
...{
driver: null
}, ...parameters
}
this._driver = _parameters.driver
this._isProcessing = false
this._queue = []
}
save ( data, parameters, onSuccess, onProgress, onError ) {
if ( !data ) {
onError( 'Data cannot be null or empty, aborting database insert !!!' )
return
}
if ( !parameters ) {
onError( 'Invalid parent id, unable to set children to unknown database node !!!' )
return
}
this._queue.push( {
data,
parameters,
onSuccess,
onProgress,
onError
} )
this._processQueue()
}
_processQueue () {
if ( this._queue.length === 0 || this._isProcessing ) { return }
this._isProcessing = true
const self = this
const dataBloc = this._queue.shift()
const data = dataBloc.data
const parameters = dataBloc.parameters
const onSuccess = dataBloc.onSuccess
const onProgress = dataBloc.onProgress
const onError = dataBloc.onError
self._save(
data,
parameters,
_onSaveSuccess,
_onSaveProgress,
_onSaveError
)
function _onSaveSuccess ( result ) {
onSuccess( result )
self._isProcessing = false
self._processQueue()
}
function _onSaveProgress ( progress ) {
onProgress( progress )
}
function _onSaveError ( error ) {
onError( error )
self._isProcessing = false
self._processQueue()
}
}
_save ( ) {}
}
export { TAbstractDataInserter }