15
15
* limitations under the License.
16
16
*/
17
17
18
- import { Deferred , validateArgCount , validateCallback } from '@firebase/util' ;
18
+ import { validateArgCount , validateCallback , Compat } from '@firebase/util' ;
19
19
20
- import {
21
- Repo ,
22
- repoOnDisconnectCancel ,
23
- repoOnDisconnectSet ,
24
- repoOnDisconnectSetWithPriority ,
25
- repoOnDisconnectUpdate
26
- } from '../core/Repo' ;
27
20
import { Indexable } from '../core/util/misc' ;
28
- import { Path } from '../core/util/Path' ;
29
21
import { warn } from '../core/util/util' ;
30
- import {
31
- validateWritablePath ,
32
- validateFirebaseDataArg ,
33
- validatePriority ,
34
- validateFirebaseMergeDataArg
35
- } from '../core/util/validation' ;
22
+ import { OnDisconnect as ExpOnDisconnect } from '../exp/OnDisconnect' ;
36
23
37
- export class OnDisconnect {
38
- constructor ( private repo_ : Repo , private path_ : Path ) { }
24
+ export class OnDisconnect implements Compat < ExpOnDisconnect > {
25
+ constructor ( readonly _delegate : ExpOnDisconnect ) { }
39
26
40
27
cancel ( onComplete ?: ( a : Error | null ) => void ) : Promise < void > {
41
28
validateArgCount ( 'OnDisconnect.cancel' , 0 , 1 , arguments . length ) ;
42
29
validateCallback ( 'OnDisconnect.cancel' , 1 , onComplete , true ) ;
43
- const deferred = new Deferred < void > ( ) ;
44
- repoOnDisconnectCancel (
45
- this . repo_ ,
46
- this . path_ ,
47
- deferred . wrapCallback ( onComplete )
48
- ) ;
49
- return deferred . promise ;
30
+ const result = this . _delegate . cancel ( ) ;
31
+ if ( onComplete ) {
32
+ result . then (
33
+ ( ) => onComplete ( null ) ,
34
+ error => onComplete ( error )
35
+ ) ;
36
+ }
37
+ return result ;
50
38
}
51
39
52
40
remove ( onComplete ?: ( a : Error | null ) => void ) : Promise < void > {
53
41
validateArgCount ( 'OnDisconnect.remove' , 0 , 1 , arguments . length ) ;
54
- validateWritablePath ( 'OnDisconnect.remove' , this . path_ ) ;
55
42
validateCallback ( 'OnDisconnect.remove' , 1 , onComplete , true ) ;
56
- const deferred = new Deferred < void > ( ) ;
57
- repoOnDisconnectSet (
58
- this . repo_ ,
59
- this . path_ ,
60
- null ,
61
- deferred . wrapCallback ( onComplete )
62
- ) ;
63
- return deferred . promise ;
43
+ const result = this . _delegate . remove ( ) ;
44
+ if ( onComplete ) {
45
+ result . then (
46
+ ( ) => onComplete ( null ) ,
47
+ error => onComplete ( error )
48
+ ) ;
49
+ }
50
+ return result ;
64
51
}
65
52
66
53
set ( value : unknown , onComplete ?: ( a : Error | null ) => void ) : Promise < void > {
67
54
validateArgCount ( 'OnDisconnect.set' , 1 , 2 , arguments . length ) ;
68
- validateWritablePath ( 'OnDisconnect.set' , this . path_ ) ;
69
- validateFirebaseDataArg ( 'OnDisconnect.set' , 1 , value , this . path_ , false ) ;
70
55
validateCallback ( 'OnDisconnect.set' , 2 , onComplete , true ) ;
71
- const deferred = new Deferred < void > ( ) ;
72
- repoOnDisconnectSet (
73
- this . repo_ ,
74
- this . path_ ,
75
- value ,
76
- deferred . wrapCallback ( onComplete )
77
- ) ;
78
- return deferred . promise ;
56
+ const result = this . _delegate . set ( value ) ;
57
+ if ( onComplete ) {
58
+ result . then (
59
+ ( ) => onComplete ( null ) ,
60
+ error => onComplete ( error )
61
+ ) ;
62
+ }
63
+ return result ;
79
64
}
80
65
81
66
setWithPriority (
@@ -84,34 +69,22 @@ export class OnDisconnect {
84
69
onComplete ?: ( a : Error | null ) => void
85
70
) : Promise < void > {
86
71
validateArgCount ( 'OnDisconnect.setWithPriority' , 2 , 3 , arguments . length ) ;
87
- validateWritablePath ( 'OnDisconnect.setWithPriority' , this . path_ ) ;
88
- validateFirebaseDataArg (
89
- 'OnDisconnect.setWithPriority' ,
90
- 1 ,
91
- value ,
92
- this . path_ ,
93
- false
94
- ) ;
95
- validatePriority ( 'OnDisconnect.setWithPriority' , 2 , priority , false ) ;
96
72
validateCallback ( 'OnDisconnect.setWithPriority' , 3 , onComplete , true ) ;
97
-
98
- const deferred = new Deferred < void > ( ) ;
99
- repoOnDisconnectSetWithPriority (
100
- this . repo_ ,
101
- this . path_ ,
102
- value ,
103
- priority ,
104
- deferred . wrapCallback ( onComplete )
105
- ) ;
106
- return deferred . promise ;
73
+ const result = this . _delegate . setWithPriority ( value , priority ) ;
74
+ if ( onComplete ) {
75
+ result . then (
76
+ ( ) => onComplete ( null ) ,
77
+ error => onComplete ( error )
78
+ ) ;
79
+ }
80
+ return result ;
107
81
}
108
82
109
83
update (
110
84
objectToMerge : Indexable ,
111
85
onComplete ?: ( a : Error | null ) => void
112
86
) : Promise < void > {
113
87
validateArgCount ( 'OnDisconnect.update' , 1 , 2 , arguments . length ) ;
114
- validateWritablePath ( 'OnDisconnect.update' , this . path_ ) ;
115
88
if ( Array . isArray ( objectToMerge ) ) {
116
89
const newObjectToMerge : { [ k : string ] : unknown } = { } ;
117
90
for ( let i = 0 ; i < objectToMerge . length ; ++ i ) {
@@ -123,21 +96,14 @@ export class OnDisconnect {
123
96
'existing data, or an Object with integer keys if you really do want to only update some of the children.'
124
97
) ;
125
98
}
126
- validateFirebaseMergeDataArg (
127
- 'OnDisconnect.update' ,
128
- 1 ,
129
- objectToMerge ,
130
- this . path_ ,
131
- false
132
- ) ;
133
99
validateCallback ( 'OnDisconnect.update' , 2 , onComplete , true ) ;
134
- const deferred = new Deferred < void > ( ) ;
135
- repoOnDisconnectUpdate (
136
- this . repo_ ,
137
- this . path_ ,
138
- objectToMerge ,
139
- deferred . wrapCallback ( onComplete )
140
- ) ;
141
- return deferred . promise ;
100
+ const result = this . _delegate . update ( objectToMerge ) ;
101
+ if ( onComplete ) {
102
+ result . then (
103
+ ( ) => onComplete ( null ) ,
104
+ error => onComplete ( error )
105
+ ) ;
106
+ }
107
+ return result ;
142
108
}
143
109
}
0 commit comments