Skip to content
This repository was archived by the owner on Oct 2, 2021. It is now read-only.

Now we keep a collection to determine if a script needs to be added, removed or changed on the client #329

Merged
merged 1 commit into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/chrome/chromeDebugAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ export abstract class ChromeDebugAdapter implements IDebugAdapter {

public readonly events: StepProgressEventsEmitter;

private _loadedSourcesByScriptId = new Map<Crdp.Runtime.ScriptId, CrdpScript>();

public constructor({ chromeConnection, lineColTransformer, sourceMapTransformer, pathTransformer, targetFilter, enableSourceMapCaching }: IChromeDebugAdapterOpts,
session: ChromeDebugSession) {
telemetry.setupEventHandler(e => session.sendEvent(e));
Expand Down Expand Up @@ -603,8 +605,7 @@ export abstract class ChromeDebugAdapter implements IDebugAdapter {
this.clearTargetContext();
return this.doAfterProcessingSourceEvents(async () => { // This will not execute until all the on-flight 'new' source events have been processed
for (let scriptedParseEvent of cachedScriptParsedEvents) {
const scriptEvent = await this.scriptToLoadedSourceEvent('removed', scriptedParseEvent);
this._session.sendEvent(scriptEvent);
this.sendLoadedSourceEvent(scriptedParseEvent, 'removed');
}
});
}
Expand Down Expand Up @@ -856,7 +857,28 @@ export abstract class ChromeDebugAdapter implements IDebugAdapter {
}

protected async sendLoadedSourceEvent(script: Crdp.Debugger.ScriptParsedEvent, loadedSourceEventReason: LoadedSourceEventReason = 'new'): Promise<void> {
switch (loadedSourceEventReason) {
case 'new':
case 'changed':
if (this._loadedSourcesByScriptId.get(script.scriptId)) {
loadedSourceEventReason = 'changed';
} else {
loadedSourceEventReason = 'new';
}
this._loadedSourcesByScriptId.set(script.scriptId, script);
break;
case 'removed':
if (!this._loadedSourcesByScriptId.delete(script.scriptId)) {
telemetry.reportEvent('LoadedSourceEventError', { issue: 'Tried to remove non-existent script', scriptId: script.scriptId });
return;
}
break;
default:
telemetry.reportEvent('LoadedSourceEventError', { issue: 'Unknown reason', reason: loadedSourceEventReason });
}

const scriptEvent = await this.scriptToLoadedSourceEvent(loadedSourceEventReason, script);

this._session.sendEvent(scriptEvent);
}

Expand Down
42 changes: 32 additions & 10 deletions test/chrome/chromeDebugAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import * as testUtils from '../testUtils';
import * as utils from '../../src/utils';

/** Not mocked - use for type only */
import {ChromeDebugAdapter as _ChromeDebugAdapter, LoadedSourceEventReason } from '../../src/chrome/chromeDebugAdapter';
import {ChromeDebugAdapter as _ChromeDebugAdapter } from '../../src/chrome/chromeDebugAdapter';
import { InitializedEvent, LoadedSourceEvent, Source, BreakpointEvent } from 'vscode-debugadapter/lib/debugSession';

const MODULE_UNDER_TEST = '../../src/chrome/chromeDebugAdapter';
Expand Down Expand Up @@ -559,17 +559,29 @@ suite('ChromeDebugAdapter', () => {
});
});

// This is needed for Edge debug adapter, please keep the signature and logic of sendLoadedSourceEvent() method intact.
test('tests that sendLoadedSourceEvent can accept an additional paramter to override the `reason` parameter', () => {
const loadedSourceEventReason: LoadedSourceEventReason = 'changed';
// This is needed for Edge debug adapter, please keep the logic of sendLoadedSourceEvent()
test('tests that sendLoadedSourceEvent will set the `reason` parameter based on our internal view of the events we sent to the client', () => {
let eventIndex = 0;
sendEventHandler = (event) => {
assert.equal('loadedSource', event.event);
assert.notEqual(null, event.body);
assert.equal(loadedSourceEventReason, event.body.reason);
switch (eventIndex) {
case 0:
assert.equal('loadedSource', event.event);
assert.notEqual(null, event.body);
assert.equal('new', event.body.reason);
break;
case 1:
assert.equal('loadedSource', event.event);
assert.notEqual(null, event.body);
assert.equal('changed', event.body.reason);
break;
default:
throw new RangeError('Unexpected event index');
}
++eventIndex;
};

return chromeDebugAdapter.attach(ATTACH_ARGS).then(() => {
return (<any>chromeDebugAdapter).sendLoadedSourceEvent({
return chromeDebugAdapter.attach(ATTACH_ARGS).then(async () => {
await (<any>chromeDebugAdapter).sendLoadedSourceEvent({
scriptId: 1,
url: '',
startLine: 0,
Expand All @@ -578,7 +590,17 @@ suite('ChromeDebugAdapter', () => {
endColumn: 0,
executionContextId: 0,
hash: ''
}, loadedSourceEventReason);
});
await (<any>chromeDebugAdapter).sendLoadedSourceEvent({
scriptId: 1,
url: '',
startLine: 0,
startColumn: 0,
endLine: 0,
endColumn: 0,
executionContextId: 0,
hash: ''
});
});
});

Expand Down