* Sets up initialization callbacks and top level promise so that * all internal methods can wait on an internal initialize promise. * * This must be called from constructor _before_ initialize is called.
()
| 128 | * This must be called from constructor _before_ initialize is called. |
| 129 | */ |
| 130 | setupInitialization () { |
| 131 | const finishInitialization = (result) => { |
| 132 | this.isInitialized = result |
| 133 | |
| 134 | // We only want to resolve / reject the init promise once during the initialization flow. Because some trackers |
| 135 | // do not support reporting "failures" we need to support timeouts that can result in race conditions in some |
| 136 | // situations. In these situations we use the first success / fail callback to determine the tracker state and |
| 137 | // we change the callbacks to noops to prevent later calls from changing the init state. |
| 138 | this.onInitializeSuccess = () => {} |
| 139 | this.onInitializeFail = () => {} |
| 140 | } |
| 141 | |
| 142 | this.initializationCompletePromise = new Promise((resolve, reject) => { |
| 143 | this.onInitializeSuccess = () => { |
| 144 | resolve() |
| 145 | finishInitialization(true) |
| 146 | } |
| 147 | |
| 148 | this.onInitializeFail = (e) => { |
| 149 | if (!/init timeout/.test(e.message)) { |
| 150 | console.error(`${this.constructor.name || 'Tracker'} initialization failed`, e) |
| 151 | } |
| 152 | reject(e) |
| 153 | finishInitialization(false) |
| 154 | } |
| 155 | }) |
| 156 | } |
| 157 | |
| 158 | log (...args) { |
| 159 | if (!this.loggingEnabled) { |