1
1
var sjcl_ss = cordova . require ( 'cordova-plugin-secure-storage.sjcl_ss' ) ;
2
2
var ESP6Promise = cordova . require ( 'cordova-plugin-secure-storage.es6-promise' ) . Promise ;
3
3
4
+ var _rejectOnTimeout = function ( callbackError ) {
5
+ return setTimeout ( function ( ) {
6
+ if ( 'function' === typeof callbackError ) {
7
+ callbackError ( {
8
+ code : 'timeout' ,
9
+ error : 'The request took too long'
10
+ } ) ;
11
+ }
12
+ } , SecureStorage . Timeout ) ;
13
+ } ;
14
+
4
15
var SecureStorageiOS = function ( success , error , service ) {
5
16
var _success = function ( ) { } ;
6
17
var _service = service ;
@@ -23,20 +34,38 @@ SecureStorageiOS.prototype = {
23
34
var defer = new ESP6Promise . defer ( ) ;
24
35
var self = this ;
25
36
var _key = key ;
37
+ var _resolved = false ;
38
+ var _rejectTimeout = null ;
26
39
27
40
var _success = function ( value ) {
28
- if ( 'function' === typeof success ) {
29
- success ( value ) ;
30
- } ;
41
+ if ( ! _resolved ) {
42
+ _resolved = true ;
43
+
44
+ if ( _rejectTimeout ) {
45
+ clearTimeout ( _rejectTimeout ) ;
46
+ }
31
47
32
- defer . resolve ( value ) ;
48
+ if ( 'function' === typeof success ) {
49
+ success ( value ) ;
50
+ } ;
51
+
52
+ defer . resolve ( value ) ;
53
+ }
33
54
} ;
34
55
var _error = function ( msg ) {
35
- if ( 'function' === typeof error ) {
36
- error ( msg ) ;
37
- }
56
+ if ( ! _resolved ) {
57
+ _resolved = true ;
38
58
39
- defer . reject ( msg ) ;
59
+ if ( _rejectTimeout ) {
60
+ clearTimeout ( _rejectTimeout ) ;
61
+ }
62
+
63
+ if ( 'function' === typeof error ) {
64
+ error ( msg ) ;
65
+ }
66
+
67
+ defer . reject ( msg ) ;
68
+ }
40
69
} ;
41
70
42
71
if ( 'string' === typeof success ) {
@@ -45,6 +74,8 @@ SecureStorageiOS.prototype = {
45
74
46
75
cordova . exec ( _success , _error , "SecureStorage" , "get" , [ self . service , _key ] ) ;
47
76
77
+ _rejectTimeout = _rejectOnTimeout ( _error ) ;
78
+
48
79
return defer . promise ;
49
80
} ,
50
81
@@ -53,20 +84,39 @@ SecureStorageiOS.prototype = {
53
84
var self = this ;
54
85
var _key = key ;
55
86
var _value = value ;
87
+ var _resolved = false ;
88
+ var _rejectTimeout = null ;
56
89
57
90
var _success = function ( value ) {
58
- if ( ! ! success && 'function' === typeof success ) {
59
- success ( value ) ;
60
- } ;
91
+ if ( ! _resolved ) {
92
+ _resolved = true ;
61
93
62
- defer . resolve ( value ) ;
94
+ if ( _rejectTimeout ) {
95
+ clearTimeout ( _rejectTimeout ) ;
96
+ }
97
+
98
+ if ( ! ! success && 'function' === typeof success ) {
99
+ success ( value ) ;
100
+ } ;
101
+
102
+ defer . resolve ( value ) ;
103
+ }
63
104
} ;
105
+
64
106
var _error = function ( msg ) {
65
- if ( ! ! error && 'function' === typeof error ) {
66
- error ( msg ) ;
67
- }
107
+ if ( ! _resolved ) {
108
+ _resolved = true ;
109
+
110
+ if ( _rejectTimeout ) {
111
+ clearTimeout ( _rejectTimeout ) ;
112
+ }
113
+
114
+ if ( ! ! error && 'function' === typeof error ) {
115
+ error ( msg ) ;
116
+ }
68
117
69
- defer . reject ( msg ) ;
118
+ defer . reject ( msg ) ;
119
+ }
70
120
} ;
71
121
72
122
if ( ! ! success && 'string' === typeof success ) {
@@ -78,27 +128,47 @@ SecureStorageiOS.prototype = {
78
128
79
129
cordova . exec ( _success , _error , "SecureStorage" , "set" , [ self . service , _key , _value ] ) ;
80
130
131
+ _rejectTimeout = _rejectOnTimeout ( _error ) ;
132
+
81
133
return defer . promise ;
82
134
} ,
83
135
84
136
remove : function ( success , error , key ) {
85
137
var defer = new ESP6Promise . defer ( ) ;
86
138
var self = this ;
87
139
var _key = key ;
140
+ var _resolved = false ;
141
+ var _rejectTimeout = null ;
88
142
89
143
var _success = function ( value ) {
90
- if ( 'function' === typeof success ) {
91
- success ( value ) ;
92
- } ;
144
+ if ( ! _resolved ) {
145
+ _resolved = true ;
146
+
147
+ if ( _rejectTimeout ) {
148
+ clearTimeout ( _rejectTimeout ) ;
149
+ }
93
150
94
- defer . resolve ( value ) ;
151
+ if ( 'function' === typeof success ) {
152
+ success ( value ) ;
153
+ } ;
154
+
155
+ defer . resolve ( value ) ;
156
+ }
95
157
} ;
96
158
var _error = function ( msg ) {
97
- if ( 'function' === typeof error ) {
98
- error ( msg ) ;
99
- }
159
+ if ( ! _resolved ) {
160
+ _resolved = true ;
100
161
101
- defer . reject ( msg ) ;
162
+ if ( _rejectTimeout ) {
163
+ clearTimeout ( _rejectTimeout ) ;
164
+ }
165
+
166
+ if ( 'function' === typeof error ) {
167
+ error ( msg ) ;
168
+ }
169
+
170
+ defer . reject ( msg ) ;
171
+ }
102
172
} ;
103
173
104
174
if ( 'string' === typeof success ) {
@@ -107,6 +177,8 @@ SecureStorageiOS.prototype = {
107
177
108
178
cordova . exec ( _success , _error , "SecureStorage" , "remove" , [ self . service , _key ] ) ;
109
179
180
+ _rejectTimeout = _rejectOnTimeout ( _error ) ;
181
+
110
182
return defer . promise ;
111
183
}
112
184
} ;
@@ -144,20 +216,39 @@ SecureStorageAndroid.prototype = {
144
216
var defer = new ESP6Promise . defer ( ) ;
145
217
var self = this ;
146
218
var _key = key ;
219
+ var _resolved = false ;
220
+ var _rejectTimeout = null ;
147
221
148
222
var _success = function ( value ) {
149
- if ( 'function' === typeof success ) {
150
- success ( value ) ;
151
- } ;
223
+ if ( ! _resolved ) {
224
+ _resolved = true ;
225
+
226
+ if ( _rejectTimeout ) {
227
+ clearTimeout ( _rejectTimeout ) ;
228
+ }
152
229
153
- defer . resolve ( value ) ;
230
+ if ( 'function' === typeof success ) {
231
+ success ( value ) ;
232
+ } ;
233
+
234
+ defer . resolve ( value ) ;
235
+ }
154
236
} ;
237
+
155
238
var _error = function ( msg ) {
156
- if ( 'function' === typeof error ) {
157
- error ( msg ) ;
158
- }
239
+ if ( ! _resolved ) {
240
+ _resolved = true ;
241
+
242
+ if ( _rejectTimeout ) {
243
+ clearTimeout ( _rejectTimeout ) ;
244
+ }
159
245
160
- defer . reject ( msg ) ;
246
+ if ( 'function' === typeof error ) {
247
+ error ( msg ) ;
248
+ }
249
+
250
+ defer . reject ( msg ) ;
251
+ }
161
252
} ;
162
253
163
254
if ( 'string' === typeof success ) {
@@ -166,6 +257,8 @@ SecureStorageAndroid.prototype = {
166
257
167
258
cordova . exec ( _success , _error , "SecureStorage" , "get" , [ self . service , _key ] ) ;
168
259
260
+ _rejectTimeout = _rejectOnTimeout ( _error ) ;
261
+
169
262
return defer . promise ;
170
263
} ,
171
264
@@ -174,20 +267,38 @@ SecureStorageAndroid.prototype = {
174
267
var self = this ;
175
268
var _key = key ;
176
269
var _value = value ;
270
+ var _resolved = false ;
271
+ var _rejectTimeout = null ;
177
272
178
273
var _success = function ( value ) {
179
- if ( ! ! success && 'function' === typeof success ) {
180
- success ( value ) ;
181
- } ;
274
+ if ( ! _resolved ) {
275
+ _resolved = true ;
276
+
277
+ if ( _rejectTimeout ) {
278
+ clearTimeout ( _rejectTimeout ) ;
279
+ }
182
280
183
- defer . resolve ( value ) ;
281
+ if ( ! ! success && 'function' === typeof success ) {
282
+ success ( value ) ;
283
+ } ;
284
+
285
+ defer . resolve ( value ) ;
286
+ }
184
287
} ;
185
288
var _error = function ( msg ) {
186
- if ( ! ! error && 'function' === typeof error ) {
187
- error ( msg ) ;
188
- }
289
+ if ( ! _resolved ) {
290
+ _resolved = true ;
189
291
190
- defer . reject ( msg ) ;
292
+ if ( _rejectTimeout ) {
293
+ clearTimeout ( _rejectTimeout ) ;
294
+ }
295
+
296
+ if ( ! ! error && 'function' === typeof error ) {
297
+ error ( msg ) ;
298
+ }
299
+
300
+ defer . reject ( msg ) ;
301
+ }
191
302
} ;
192
303
193
304
if ( ! ! success && 'string' === typeof success ) {
@@ -199,27 +310,47 @@ SecureStorageAndroid.prototype = {
199
310
200
311
cordova . exec ( _success , _error , "SecureStorage" , "set" , [ self . service , _key , _value ] ) ;
201
312
313
+ _rejectTimeout = _rejectOnTimeout ( _error ) ;
314
+
202
315
return defer . promise ;
203
316
} ,
204
317
205
318
remove : function ( success , error , key ) {
206
319
var defer = new ESP6Promise . defer ( ) ;
207
320
var self = this ;
208
321
var _key = key ;
322
+ var _resolved = false ;
323
+ var _rejectTimeout = null ;
209
324
210
325
var _success = function ( value ) {
211
- if ( 'function' === typeof success ) {
212
- success ( value ) ;
213
- } ;
326
+ if ( ! _resolved ) {
327
+ _resolved = true ;
214
328
215
- defer . resolve ( value ) ;
329
+ if ( _rejectTimeout ) {
330
+ clearTimeout ( _rejectTimeout ) ;
331
+ }
332
+
333
+ if ( 'function' === typeof success ) {
334
+ success ( value ) ;
335
+ } ;
336
+
337
+ defer . resolve ( value ) ;
338
+ }
216
339
} ;
217
340
var _error = function ( msg ) {
218
- if ( 'function' === typeof error ) {
219
- error ( msg ) ;
220
- }
341
+ if ( ! _resolved ) {
342
+ _resolved = true ;
343
+
344
+ if ( _rejectTimeout ) {
345
+ clearTimeout ( _rejectTimeout ) ;
346
+ }
347
+
348
+ if ( 'function' === typeof error ) {
349
+ error ( msg ) ;
350
+ }
221
351
222
- defer . reject ( msg ) ;
352
+ defer . reject ( msg ) ;
353
+ }
223
354
} ;
224
355
225
356
if ( 'string' === typeof success ) {
@@ -228,6 +359,8 @@ SecureStorageAndroid.prototype = {
228
359
229
360
cordova . exec ( _success , _error , "SecureStorage" , "remove" , [ self . service , _key ] ) ;
230
361
362
+ _rejectTimeout = _rejectOnTimeout ( _error ) ;
363
+
231
364
return defer . promise ;
232
365
}
233
366
} ;
@@ -340,6 +473,8 @@ switch(cordova.platformId) {
340
473
SecureStorage = null ;
341
474
}
342
475
476
+ SecureStorage . Timeout = 3000 ;
477
+
343
478
if ( ! cordova . plugins ) {
344
479
cordova . plugins = { } ;
345
480
}
0 commit comments