Skip to content

Commit

Permalink
Cleanup services module
Browse files Browse the repository at this point in the history
- Don’t call `getServices` with a second parameter (it’s not expecting
it)
- Reorder conditionals to make code clearer
  • Loading branch information
jridgewell committed Dec 30, 2015
1 parent 3a28d82 commit 561d569
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ let ServiceHolderDef;
* @return {*}
*/
export function getService(win, id, opt_factory) {
const services = getServices(win, id);
const services = getServices(win);
let s = services[id];
if (!s || !s.obj) {
if (!s) {
s = services[id] = {};
}
if (!s.obj) {
assert(opt_factory, 'Factory not given and service missing %s', id);
if (!s) {
s = services[id] = {};
}
s.obj = opt_factory(win);
// The service may have been requested already, in which case we have a
// pending promise we need to fulfill.
if (s.resolve) {
s.resolve(s.obj);
}
Expand All @@ -78,18 +80,15 @@ export function getElementService(win, id, providedByElement) {
'but %s is not loaded in the current page. To fix this ' +
'problem load the JavaScript file for %s in this page.',
id, providedByElement, providedByElement, providedByElement);
const services = getServices(win, id);
const services = getServices(win);
const s = services[id];
if (s) {
// If we have a promise we return the promise
if (s.promise) {
return s.promise;
}
// If meanwhile an object was created, we make a promise from it
// If a service was registered with getService, we make a promise from it
// which we will return in future invocations.
if (s.obj) {
return s.promise = Promise.resolve(s.obj);
if (!s.promise) {
s.promise = Promise.resolve(s.obj);
}
return s.promise;
}
// TODO(@cramforce): Add a check that if the element is eventually registered
// that the service is actually provided and this promise resolves.
Expand Down

0 comments on commit 561d569

Please sign in to comment.