Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Commit

Permalink
allow binding listeners to Debugger when not connected
Browse files Browse the repository at this point in the history
  • Loading branch information
oddui committed Aug 11, 2017
1 parent e28dab5 commit 7b9b2e8
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 62 deletions.
15 changes: 2 additions & 13 deletions app/src/lib/debugger/client/cri.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ class CriDebugger {
return this.tabId_ !== null;
}

throwIfNotConnected_() {
if (!this.isConnected()) {
throw new Error('Debugger is not connected.');
}
}

connect(tabId) {
if (this.tabId_ !== null) {
return Promise.resolve();
Expand Down Expand Up @@ -142,7 +136,6 @@ class CriDebugger {
* @param {function(...)} cb
*/
on(eventName, cb) {
this.throwIfNotConnected_();
this.client_.on(eventName, cb);
}

Expand All @@ -154,7 +147,6 @@ class CriDebugger {
* @param {function(...)} cb
*/
once(eventName, cb) {
this.throwIfNotConnected_();
this.client_.once(eventName, cb);
}

Expand All @@ -165,7 +157,6 @@ class CriDebugger {
* @param {function(...)} cb
*/
off(eventName, cb) {
this.throwIfNotConnected_();
this.client_.removeListener(eventName, cb);
}

Expand Down Expand Up @@ -197,10 +188,8 @@ class CriDebugger {
* @return {!Promise}
*/
sendCommand(command, params, timeout) {
try {
this.throwIfNotConnected_();
} catch(e) {
return Promise.reject(e);
if (!this.isConnected()) {
return Promise.reject(new Error('Debugger is not connected.'));
}

return new Promise((resolve, reject) => {
Expand Down
15 changes: 2 additions & 13 deletions app/src/lib/debugger/client/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,6 @@ class ExtensionDebugger extends EventEmitter {
return this.tabId_ !== null;
}

throwIfNotConnected_() {
if (!this.isConnected()) {
throw new Error('Debugger is not connected.');
}
}

connect(tabId) {
if (this.tabId_ !== null) {
return Promise.resolve();
Expand Down Expand Up @@ -166,7 +160,6 @@ class ExtensionDebugger extends EventEmitter {
* @param {function(...)} cb
*/
on(eventName, cb) {
this.throwIfNotConnected_();
super.on(eventName, cb);
}

Expand All @@ -178,7 +171,6 @@ class ExtensionDebugger extends EventEmitter {
* @param {function(...)} cb
*/
once(eventName, cb) {
this.throwIfNotConnected_();
super.once(eventName, cb);
}

Expand All @@ -189,7 +181,6 @@ class ExtensionDebugger extends EventEmitter {
* @param {function(...)} cb
*/
off(eventName, cb) {
this.throwIfNotConnected_();
super.removeListener(eventName, cb);
}

Expand Down Expand Up @@ -221,10 +212,8 @@ class ExtensionDebugger extends EventEmitter {
* @return {!Promise}
*/
sendCommand(command, params, timeout) {
try {
this.throwIfNotConnected_();
} catch(e) {
return Promise.reject(e);
if (!this.isConnected()) {
return Promise.reject(new Error('Debugger is not connected.'));
}

return new Promise((resolve, reject) => {
Expand Down
6 changes: 1 addition & 5 deletions app/src/lib/debugger/client/extension.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ describe('debugger', () => {
describe('is event emitter', () => {
let EVENT = 'some event', spy;

beforeEach(() => {
spy = sinon.spy();
return dbg.connect(tab.id);
});
afterEach(() => dbg.disconnect());
beforeEach(() => spy = sinon.spy());

it('on', () => {
dbg.on(EVENT, spy);
Expand Down
34 changes: 7 additions & 27 deletions app/src/lib/debugger/client/fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FakeDebugger extends EventEmitter {
super();

this.tabId_ = null;
this.commandToResultMap_ = new Map();
this.commandResultMap_ = new Map();
}

getTabId() {
Expand Down Expand Up @@ -68,53 +68,33 @@ class FakeDebugger extends EventEmitter {
}

on(eventName, cb) {
if (this.tabId_ === null) {
throw new Error('connect() must be called before attempting to listen to events.');
}

super.on(eventName, cb);
}

once(eventName, cb) {
if (this.tabId_ === null) {
throw new Error('connect() must be called before attempting to listen to events.');
}

super.once(eventName, cb);
}

off(eventName, cb) {
if (this.tabId_ === null) {
throw new Error('connect() must be called before attempting to listen to events.');
}

super.removeListener(eventName, cb);
}

onCommandSuccess(cb) {
if (this.tabId_ === null) {
throw new Error('connect() must be called before attempting to listen to events.');
}

super.on('commandSuccess', cb);
this.on('commandSuccess', cb);
}

offCommandSuccess(cb) {
if (this.tabId_ === null) {
throw new Error('connect() must be called before attempting to listen to events.');
}

super.removeListener('commandSuccess', cb);
this.off('commandSuccess', cb);
}

sendCommand(command, params, timeout) {
if (this.tabId_ === null) {
return Promise.reject(new Error('connect() must be called before attempting to send commands.'));
if (!this.isConnected()) {
return Promise.reject(new Error('Debugger is not connected.'));
}

this.emit('commandSuccess', command, {}, timeout);

return Promise.resolve(this.commandToResultMap_.get(command) || {});
return Promise.resolve(this.commandResultMap_.get(command) || {});
}

/**
Expand All @@ -124,7 +104,7 @@ class FakeDebugger extends EventEmitter {
* @param {any} result
*/
setCommandResult(command, result) {
this.commandToResultMap_.set(command, result);
this.commandResultMap_.set(command, result);
return this;
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/lib/debugger/navigation_tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class NavigationTracker extends NavigationTrackerInterface {
}
})
.catch(e => {
if (/connect\(\) must be called/i.test(e.message)) {
if (/not connected/i.test(e.message)) {
// If the debugger is not connected, don't wait for pending navigations
// to complete, since we won't see any more events from it until we reconnect.
isPending = false;
Expand Down
5 changes: 2 additions & 3 deletions app/src/lib/debugger/navigation_tracker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ describe('debugger', () => {
});

it('resolves false if debugger is not connected', () => {
dbg.disconnect();

return tracker.isPendingNavigation()
return dbg.disconnect()
.then(() => tracker.isPendingNavigation())
.then(pending => expect(pending).to.be.false);
});

Expand Down

0 comments on commit 7b9b2e8

Please sign in to comment.