Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup services module #1273

Merged
merged 1 commit into from
Jan 5, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Web S=should probably have an assert here that s.obj is actually an object.

}
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