This repository was archived by the owner on Feb 26, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +17
-5
lines changed Expand file tree Collapse file tree 2 files changed +17
-5
lines changed Original file line number Diff line number Diff line change @@ -218,12 +218,9 @@ class Injector {
218
218
219
219
resolving . push ( token ) ;
220
220
221
- // TODO(vojta): handle these cases :
221
+ // TODO(vojta): handle this case :
222
222
// 1/
223
223
// - requested as promise (delayed)
224
- // - requested again as promise (before the previous gets resolved) -> cache the promise
225
- // 2/
226
- // - requested as promise (delayed)
227
224
// - requested again sync (before the previous gets resolved)
228
225
// -> error, but let it go inside to throw where exactly is the async provider
229
226
var delayingInstantiation = wantPromise && provider . params . some ( ( param ) => ! param . isPromise ) ;
@@ -243,7 +240,9 @@ class Injector {
243
240
resolving . pop ( ) ;
244
241
245
242
// Once all dependencies (promises) are resolved, instantiate.
246
- return Promise . all ( args ) . then ( function ( args ) {
243
+ instance = Promise . all ( args ) . then ( function ( args ) {
244
+ injector . _cache . delete ( token ) ;
245
+
247
246
try {
248
247
instance = provider . create ( args ) ;
249
248
} catch ( e ) {
@@ -264,6 +263,8 @@ class Injector {
264
263
// a promise or not.
265
264
return instance ;
266
265
} ) ;
266
+ this . _cache . set ( token , instance ) ;
267
+ return instance ;
267
268
}
268
269
269
270
try {
Original file line number Diff line number Diff line change @@ -97,6 +97,17 @@ describe('async', function() {
97
97
expect ( controller . promise ) . toBePromiseLike ( ) ;
98
98
} ) ;
99
99
100
+ it ( 'should not instantiate multiple times in case of delayed instantiating' , function ( done ) {
101
+ var injector = new Injector ( [ fetchUsers ] ) ;
102
+
103
+ Promise . all ( [
104
+ injector . getPromise ( UserController ) ,
105
+ injector . getPromise ( UserController )
106
+ ] ) . then ( function ( result ) {
107
+ expect ( result [ 0 ] ) . toBe ( result [ 1 ] ) ;
108
+ done ( ) ;
109
+ } ) ;
110
+ } ) ;
100
111
101
112
// regression
102
113
it ( 'should not cache TransientScope' , function ( done ) {
You can’t perform that action at this time.
0 commit comments