di.js icon indicating copy to clipboard operation
di.js copied to clipboard

Mock providers not cached

Open OliverJAsh opened this issue 11 years ago • 3 comments

var di = require('di');

function Bar() {}

di.annotate(Foo, new di.Inject(Bar));
function Foo() {}

di.annotate(BarMock, new di.Provide(Bar));
function BarMock() {
    console.log('initialise BarMock');
}

var injector = new di.Injector([BarMock]);

injector.get(Foo);
injector.get(BarMock);

I expect that BarMock should only be initialised once, as the second time I call injector.getPromise, BarMock has already been initialised – the subsequent request should fetch the cached version.

I am seeing:

$ node test
initialise BarMock
initialise BarMock

I expect:

$ node test
initialise BarMock

OliverJAsh avatar Nov 05 '14 19:11 OliverJAsh

@vojtajina Really keen to fix this. Any pointers?

OliverJAsh avatar Nov 12 '14 15:11 OliverJAsh

Maybe related to this todo in injector.js ?

// TODO(vojta): handle these cases: // 1/ // - requested as promise (delayed) // - requested again as promise (before the previous gets resolved) -> cache the promise

i have the same problem without using Provide

machard avatar Dec 10 '14 22:12 machard

i've confirmed with a quick modification that it is due to this unhandled use case. I'll try to make a pull request including tests quickly

machard avatar Dec 10 '14 23:12 machard