Skip to content

Commit

Permalink
- Refactor ember-debug to use ES6.
Browse files Browse the repository at this point in the history
- Minor fixes.
  • Loading branch information
Mawaheb committed Jun 22, 2015
1 parent 4e907b8 commit 3cb4460
Show file tree
Hide file tree
Showing 25 changed files with 871 additions and 956 deletions.
67 changes: 30 additions & 37 deletions ember_debug/adapters/basic.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
/* globals requireModule */
var Ember = window.Ember;
var computed = Ember.computed;
var $ = Ember.$;
var RSVP = Ember.RSVP;
var Promise = RSVP.Promise;

export default Ember.Object.extend({
init: function() {
var self = this;
RSVP.resolve(this.connect(), 'ember-inspector').then(function() {
self.onConnectionReady();
const Ember = window.Ember;
const { $, A, computed, RSVP, Object: EmberObject } = Ember;
const { Promise, resolve } = RSVP;

export default EmberObject.extend({
init() {
resolve(this.connect(), 'ember-inspector').then(() => {
this.onConnectionReady();
}, null, 'ember-inspector');
},

Expand All @@ -24,11 +21,11 @@ export default Ember.Object.extend({
return requireModule('ember-debug/config')['default'].environment;
}),

debug: function() {
debug() {
return console.debug.apply(console, arguments);
},

log: function() {
log() {
return console.log.apply(console, arguments);
},

Expand All @@ -37,7 +34,7 @@ export default Ember.Object.extend({
*
* @method warn
*/
warn: function() {
warn() {
return console.warn.apply(console, arguments);
},

Expand All @@ -46,15 +43,15 @@ export default Ember.Object.extend({
@param {Object} type the message to the send
*/
sendMessage: function(/* options */) {},
sendMessage(/* options */) {},

/**
Register functions to be called
when a message from EmberExtension is received
@param {Function} callback
*/
onMessageReceived: function(callback) {
onMessageReceived(callback) {
this.get('_messageCallbacks').pushObject(callback);
},

Expand All @@ -69,12 +66,12 @@ export default Ember.Object.extend({
@param {DOM Element} elem
*/
inspectElement: function(/* elem */) {},
inspectElement(/* elem */) {},

_messageCallbacks: Ember.computed(function() { return Ember.A(); }).property(),
_messageCallbacks: computed(function() { return A(); }),

_messageReceived: function(message) {
this.get('_messageCallbacks').forEach(function(callback) {
_messageReceived(message) {
this.get('_messageCallbacks').forEach(callback => {
callback.call(null, message);
});
},
Expand All @@ -92,7 +89,7 @@ export default Ember.Object.extend({
* @method handleError
* @param {Error} error
*/
handleError: function(error) {
handleError(error) {
if (this.get('environment') === 'production') {
if (error && error instanceof Error) {
error = 'Error message: ' + error.message + '\nStack trace: ' + error.stack;
Expand All @@ -114,30 +111,29 @@ export default Ember.Object.extend({
@return {Promise}
*/
connect: function() {
var self = this;
return new Promise(function(resolve, reject) {
$(function() {
if (self.isDestroyed) { reject(); }
self.interval = setInterval(function() {
connect() {
return new Promise((resolve, reject) => {
$(() => {
if (this.isDestroyed) { reject(); }
this.interval = setInterval(function() {
if (document.documentElement.dataset.emberExtension) {
clearInterval(self.interval);
clearInterval(this.interval);
resolve();
}
}, 10);
});
}, 'ember-inspector');
},

willDestroy: function() {
willDestroy() {
this._super();
clearInterval(this.interval);
},

_isReady: false,
_pendingMessages: computed(function() { return Ember.A(); }).property(),
_pendingMessages: computed(function() { return A(); }),

send: function(options) {
send(options) {
if (this._isReady) {
this.sendMessage.apply(this, arguments);
} else {
Expand All @@ -149,13 +145,10 @@ export default Ember.Object.extend({
Called when the connection is set up.
Flushes the pending messages.
*/
onConnectionReady: function() {
onConnectionReady() {
// Flush pending messages
var self = this;
var messages = this.get('_pendingMessages');
messages.forEach(function(options) {
self.sendMessage(options);
});
const messages = this.get('_pendingMessages');
messages.forEach(options => this.sendMessage(options));
messages.clear();
this._isReady = true;
}
Expand Down
21 changes: 10 additions & 11 deletions ember_debug/adapters/bookmarklet.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
import BasicAdapter from "./basic";
var Ember = window.Ember;
var $ = Ember.$;
const Ember = window.Ember;
const { $ } = Ember;

export default BasicAdapter.extend({
init: function() {
init() {
this._super();
this._listen();
},

sendMessage: function(options) {
sendMessage(options) {
options = options || {};
window.emberInspector.w.postMessage(options, window.emberInspector.url);
},

_listen: function() {
var self = this;
window.addEventListener('message', function(e) {
_listen() {
window.addEventListener('message', (e) => {
if (e.origin !== window.emberInspector.url) {
return;
}
var message = e.data;
const message = e.data;
if (message.from === 'devtools') {
self._messageReceived(message);
this._messageReceived(message);
}
});

$(window).on('unload', function() {
self.sendMessage({
$(window).on('unload', () => {
this.sendMessage({
unloading: true
});
});
Expand Down
39 changes: 18 additions & 21 deletions ember_debug/adapters/chrome.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,45 @@
import BasicAdapter from "./basic";
var Ember = window.Ember;
const Ember = window.Ember;
const { computed, run } = Ember;

var ChromeAdapter = BasicAdapter.extend({
connect: function() {
var channel = this.get('_channel');
var self = this;
return this._super.apply(this, arguments).then(function() {
export default BasicAdapter.extend({
connect() {
const channel = this.get('_channel');
return this._super(...arguments).then(() => {
window.postMessage('debugger-client', [channel.port2], '*');
self._listen();
this._listen();
}, null, 'ember-inspector');
},

sendMessage: function(options) {
sendMessage(options) {
options = options || {};
this.get('_chromePort').postMessage(options);
},

inspectElement: function(elem) {
inspectElement(elem) {
/* globals inspect */
inspect(elem);
},

_channel: Ember.computed(function() {
_channel: computed(function() {
return new MessageChannel();
}).property().readOnly(),
}).readOnly(),

_chromePort: Ember.computed(function() {
_chromePort: computed(function() {
return this.get('_channel.port1');
}).property().readOnly(),
}).readOnly(),

_listen: function() {
var self = this,
chromePort = this.get('_chromePort');
_listen() {
let chromePort = this.get('_chromePort');

chromePort.addEventListener('message', function(event) {
var message = event.data;
Ember.run(function() {
self._messageReceived(message);
const message = event.data;
run(() => {
this._messageReceived(message);
});
});

chromePort.start();

}
});

export default ChromeAdapter;
25 changes: 12 additions & 13 deletions ember_debug/adapters/firefox.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* eslint no-empty:0 */
import BasicAdapter from "./basic";
var Ember = window.Ember;
const Ember = window.Ember;
const { run } = Ember;

var FirefoxAdapter = BasicAdapter.extend({
init: function() {
export default BasicAdapter.extend({
init() {
this._super();
this._listen();
},

debug: function() {
debug() {
// WORKAROUND: temporarily workaround issues with firebug console object:
// - https://github.com/tildeio/ember-extension/issues/94
// - https://github.com/firebug/firebug/pull/109
Expand All @@ -17,7 +18,7 @@ var FirefoxAdapter = BasicAdapter.extend({
this._super.apply(this, arguments);
} catch(e) { }
},
log: function() {
log() {
// WORKAROUND: temporarily workaround issues with firebug console object:
// - https://github.com/tildeio/ember-extension/issues/94
// - https://github.com/firebug/firebug/pull/109
Expand All @@ -27,26 +28,25 @@ var FirefoxAdapter = BasicAdapter.extend({
} catch(e) { }
},

sendMessage: function(options) {
sendMessage(options) {
options = options || {};
var event = document.createEvent("CustomEvent");
let event = document.createEvent("CustomEvent");
event.initCustomEvent("ember-debug-send", true, true, options);
document.documentElement.dispatchEvent(event);
},

inspectElement: function(elem) {
inspectElement(elem) {
this.sendMessage({
type: 'view:devtools:inspectDOMElement',
elementSelector: "#" + elem.getAttribute('id')
});
},

_listen: function() {
var self = this;
_listen() {

window.addEventListener('ember-debug-receive', function(event) {
var message = event.detail;
Ember.run(function() {
run(() => {
// FIX: needed to fix permission denied exception on Firefox >= 30
// - https://github.com/emberjs/ember-inspector/issues/147
// - https://blog.mozilla.org/addons/2014/04/10/changes-to-unsafewindow-for-the-add-on-sdk/
Expand All @@ -59,11 +59,10 @@ var FirefoxAdapter = BasicAdapter.extend({
default:
throw new Error("ember-debug-receive: string or object expected");
}
self._messageReceived(message);
this._messageReceived(message);
});
});
}

});

export default FirefoxAdapter;
Loading

0 comments on commit 3cb4460

Please sign in to comment.