Skip to content

Commit

Permalink
make sure to init instances extensions can use (ampproject#5161)
Browse files Browse the repository at this point in the history
* make sure to init instances extensions can use

* add comments on why we do this
  • Loading branch information
erwinmombay authored Sep 22, 2016
1 parent dc236f7 commit b7c21b9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
5 changes: 3 additions & 2 deletions 3p/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ import {yieldone} from '../ads/yieldone';
import {zergnet} from '../ads/zergnet';
import {zucks} from '../ads/zucks';

initLogConstructor();

/**
* Whether the embed type may be used with amp-embed tag.
* @const {!Object<string, boolean>}
Expand All @@ -121,6 +119,9 @@ const AMP_EMBED_ALLOWED = {
const data = parseFragment(location.hash);
window.context = data._context;

// This should only be invoked after window.context is set
initLogConstructor();

if (getMode().test || getMode().localDev) {
register('_ping_', _ping_);
}
Expand Down
11 changes: 11 additions & 0 deletions src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,17 @@ let logConstructor = null;

export function initLogConstructor() {
logConstructor = Log;
// Initialize instances for use. If a binary (an extension for example) that
// does not call `initLogConstructor` invokes `dev()` or `user()` earlier
// than the binary that does call `initLogConstructor` (amp.js), the extension
// will throw an error as that extension will never be able to initialize
// the log instances and we also don't want it to call `initLogConstructor`
// either (since that will cause the Log implementation to be bundled into that
// binary). So we must initialize the instances eagerly so that they are
// ready for use (stored globally) after the main binary calls
// `initLogConstructor`.
dev();
user();
}

export function resetLogConstructorForTesting() {
Expand Down
3 changes: 1 addition & 2 deletions src/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from './service/extensions-impl';
import {ampdocServiceFor} from './ampdoc';
import {cssText} from '../build/css';
import {dev, user} from './log';
import {dev, user, initLogConstructor} from './log';
import {fromClassForDoc, getService, getServiceForDoc} from './service';
import {childElementsByTag} from './dom';
import {
Expand Down Expand Up @@ -59,7 +59,6 @@ import {installViewportService} from './service/viewport-impl';
import {installVsyncService} from './service/vsync-impl';
import {installXhrService} from './service/xhr-impl';
import {isExperimentOn, toggleExperiment} from './experiments';
import {initLogConstructor} from './log';
import {platformFor} from './platform';
import {registerElement} from './custom-element';
import {registerExtendedElement} from './extended-element';
Expand Down

0 comments on commit b7c21b9

Please sign in to comment.