1
1
import { md5Hash , newObjectId } from './cryptoUtils' ;
2
+ import { KeyPromiseQueue } from './KeyPromiseQueue' ;
2
3
import { logger } from './logger' ;
3
4
import rest from './rest' ;
4
5
import Auth from './Auth' ;
5
6
6
7
const PUSH_STATUS_COLLECTION = '_PushStatus' ;
7
8
const JOB_STATUS_COLLECTION = '_JobStatus' ;
8
9
10
+ const pushPromiseQueue = new KeyPromiseQueue ( ) ;
11
+ const jobPromiseQueue = new KeyPromiseQueue ( ) ;
12
+
9
13
const incrementOp = function ( object = { } , key , amount = 1 ) {
10
14
if ( ! object [ key ] ) {
11
15
object [ key ] = { __op : 'Increment' , amount : amount } ;
@@ -28,22 +32,14 @@ export function flatten(array) {
28
32
}
29
33
30
34
function statusHandler ( className , database ) {
31
- let lastPromise = Promise . resolve ( ) ;
32
-
33
35
function create ( object ) {
34
- lastPromise = lastPromise . then ( ( ) => {
35
- return database . create ( className , object ) . then ( ( ) => {
36
- return Promise . resolve ( object ) ;
37
- } ) ;
36
+ return database . create ( className , object ) . then ( ( ) => {
37
+ return Promise . resolve ( object ) ;
38
38
} ) ;
39
- return lastPromise ;
40
39
}
41
40
42
41
function update ( where , object ) {
43
- lastPromise = lastPromise . then ( ( ) => {
44
- return database . update ( className , where , object ) ;
45
- } ) ;
46
- return lastPromise ;
42
+ return jobPromiseQueue . enqueue ( where . objectId , ( ) => database . update ( className , where , object ) ) ;
47
43
}
48
44
49
45
return Object . freeze ( {
@@ -53,29 +49,21 @@ function statusHandler(className, database) {
53
49
}
54
50
55
51
function restStatusHandler ( className , config ) {
56
- let lastPromise = Promise . resolve ( ) ;
57
52
const auth = Auth . master ( config ) ;
58
53
function create ( object ) {
59
- lastPromise = lastPromise . then ( ( ) => {
60
- return rest . create ( config , auth , className , object ) . then ( ( { response } ) => {
61
- // merge the objects
62
- return Promise . resolve ( Object . assign ( { } , object , response ) ) ;
63
- } ) ;
54
+ return rest . create ( config , auth , className , object ) . then ( ( { response } ) => {
55
+ return { ...object , ...response } ;
64
56
} ) ;
65
- return lastPromise ;
66
57
}
67
58
68
59
function update ( where , object ) {
69
- // TODO: when we have updateWhere, use that for proper interfacing
70
- lastPromise = lastPromise . then ( ( ) => {
71
- return rest
60
+ return pushPromiseQueue . enqueue ( where . objectId , ( ) =>
61
+ rest
72
62
. update ( config , auth , className , { objectId : where . objectId } , object )
73
63
. then ( ( { response } ) => {
74
- // merge the objects
75
- return Promise . resolve ( Object . assign ( { } , object , response ) ) ;
76
- } ) ;
77
- } ) ;
78
- return lastPromise ;
64
+ return { ...object , ...response } ;
65
+ } )
66
+ ) ;
79
67
}
80
68
81
69
return Object . freeze ( {
0 commit comments