From c9874a748972a99da1f3574c95f8cfd1a52afbb2 Mon Sep 17 00:00:00 2001 From: humbletim Date: Wed, 21 Jun 2017 16:35:29 -0400 Subject: [PATCH] more workarounds for the backwards-incompatible EventBridge changes --- .../marketplace/camera-move/app.html | 1 - .../marketplace/camera-move/modules/_utils.js | 30 ++++++++++++++----- .../browser/BridgedSettings.js | 13 +++++++- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/unpublishedScripts/marketplace/camera-move/app.html b/unpublishedScripts/marketplace/camera-move/app.html index 6ab439d58d..100a8237b5 100644 --- a/unpublishedScripts/marketplace/camera-move/app.html +++ b/unpublishedScripts/marketplace/camera-move/app.html @@ -97,7 +97,6 @@ try { log('openEventBridge.opened', window.EventBridge); bridgedSettings = new BridgedSettings({ - eventBridge: window.EventBridge, namespace: PARAMS.namespace, uuid: PARAMS.uuid, debug: PARAMS.debug, diff --git a/unpublishedScripts/marketplace/camera-move/modules/_utils.js b/unpublishedScripts/marketplace/camera-move/modules/_utils.js index 65314ef98e..bf0f09aac8 100644 --- a/unpublishedScripts/marketplace/camera-move/modules/_utils.js +++ b/unpublishedScripts/marketplace/camera-move/modules/_utils.js @@ -233,28 +233,42 @@ function BrowserUtils(global) { }, // openEventBridge handles the cluster of scenarios Interface has imposed on webviews for making EventBridge connections openEventBridge: function openEventBridge(callback) { - this.log('openEventBridge |', 'typeof global.EventBridge == ' + typeof global.EventBridge); + this.log('openEventBridge |', 'typeof global.EventBridge == ' + [typeof global.EventBridge, global.EventBridge ]); + var error; try { + global.EventBridge.toString = function() { return '[global.EventBridge at startup]'; }; global.EventBridge.scriptEventReceived.connect.exists; // this.log('openEventBridge| EventBridge already exists... -- invoking callback', 'typeof EventBridge == ' + typeof global.EventBridge); - return callback(global.EventBridge); + try { + return callback(global.EventBridge); + } catch(e) { + error = e; + } } catch (e) { - this.log('EventBridge does not yet exist in a usable state -- attempting to instrument via qt.webChannelTransport', - Object.keys(global.EventBridge)); + this.log('EventBridge not found in a usable state -- attempting to instrument via qt.webChannelTransport', + Object.keys(global.EventBridge||{})); var QWebChannel = assert(global.QWebChannel, 'expected global.QWebChannel to exist'), qt = assert(global.qt, 'expected global.qt to exist'); assert(qt.webChannelTransport, 'expected global.qt.webChannelTransport to exist'); new QWebChannel(qt.webChannelTransport, bind(this, function (channel) { var objects = channel.objects; - assert(!global.EventBridge, '... global.EventBridge was unavailable at page load, but has unexpectedly materialized; ' + - Object.keys(global.EventBridge)); - global.EventBridge = objects.eventBridge || (objects.eventBridgeWrapper && objects.eventBridgeWrapper.eventBridge); - assert(global.EventBridge, '!global.EventBridge'); + if (global.EventBridge) { + log('>>> global.EventBridge was unavailable at page load, but has spontaneously materialized; ' + + [ typeof global.EventBridge, global.EventBridge ]); + } + var eventBridge = objects.eventBridge || (objects.eventBridgeWrapper && objects.eventBridgeWrapper.eventBridge); + eventBridge.toString = function() { return '[window.EventBridge per QWebChannel]'; }; + assert(!global.EventBridge || global.EventBridge === eventBridge, 'global.EventBridge !== QWebChannel eventBridge\n' + + [global.EventBridge, eventBridge]); + global.EventBridge = eventBridge; global.EventBridge.$WebChannel = channel; this.log('openEventBridge opened -- invoking callback', 'typeof EventBridge === ' + typeof global.EventBridge); callback(global.EventBridge); })); } + if (error) { + throw error; + } }, }; } diff --git a/unpublishedScripts/marketplace/camera-move/modules/custom-settings-app/browser/BridgedSettings.js b/unpublishedScripts/marketplace/camera-move/modules/custom-settings-app/browser/BridgedSettings.js index a5d3b104a7..9b4735689f 100644 --- a/unpublishedScripts/marketplace/camera-move/modules/custom-settings-app/browser/BridgedSettings.js +++ b/unpublishedScripts/marketplace/camera-move/modules/custom-settings-app/browser/BridgedSettings.js @@ -30,8 +30,19 @@ function BridgedSettings(options) { options = options || {}; + // Note: Interface changed how window.EventBridge behaves again; it now arbitrarily replaces the global value + // sometime after the initial page load, invaliding any held references to it. + // As a workaround this proxies the local property to the current global value. + var _lastEventBridge = global.EventBridge; + Object.defineProperty(this, 'eventBridge', { enumerable: true, get: function() { + if (_lastEventBridge !== global.EventBridge) { + log('>>> EventBridge changed in-flight', '(was: ' + _lastEventBridge + ' | is: ' + global.EventBridge + ')'); + _lastEventBridge = global.EventBridge; + } + return global.EventBridge; + }}); Object.assign(this, { - eventBridge: options.eventBridge || global.EventBridge, + //eventBridge: options.eventBridge || global.EventBridge, namespace: options.namespace || 'BridgedSettings', uuid: options.uuid || undefined, valueReceived: signal(function valueReceived(key, newValue, oldValue, origin){}),