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

Commit fcb37ec

Browse files
author
Diego Geffner
committed
Now we keep a collection to determine if a script needs to be added, removed or changed on the client
1 parent dd26072 commit fcb37ec

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

src/chrome/chromeDebugAdapter.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ export abstract class ChromeDebugAdapter implements IDebugAdapter {
156156

157157
public readonly events: StepProgressEventsEmitter;
158158

159+
private _loadedSourcesByScriptId = new Map<Crdp.Runtime.ScriptId, CrdpScript>();
160+
159161
public constructor({ chromeConnection, lineColTransformer, sourceMapTransformer, pathTransformer, targetFilter, enableSourceMapCaching }: IChromeDebugAdapterOpts,
160162
session: ChromeDebugSession) {
161163
telemetry.setupEventHandler(e => session.sendEvent(e));
@@ -856,7 +858,28 @@ export abstract class ChromeDebugAdapter implements IDebugAdapter {
856858
}
857859

858860
protected async sendLoadedSourceEvent(script: Crdp.Debugger.ScriptParsedEvent, loadedSourceEventReason: LoadedSourceEventReason = 'new'): Promise<void> {
861+
switch (loadedSourceEventReason) {
862+
case 'new':
863+
case 'changed':
864+
if (this._loadedSourcesByScriptId.get(script.scriptId)) {
865+
loadedSourceEventReason = 'changed';
866+
} else {
867+
loadedSourceEventReason = 'new';
868+
}
869+
this._loadedSourcesByScriptId.set(script.scriptId, script);
870+
break;
871+
case 'removed':
872+
if (!this._loadedSourcesByScriptId.delete(script.scriptId)) {
873+
telemetry.reportEvent('LoadedSourceEventError', { issue: 'Tried to remove non-existent script', scriptId: script.scriptId });
874+
return;
875+
}
876+
break;
877+
default:
878+
telemetry.reportEvent('LoadedSourceEventError', { issue: 'Unknown reason', reason: loadedSourceEventReason });
879+
}
880+
859881
const scriptEvent = await this.scriptToLoadedSourceEvent(loadedSourceEventReason, script);
882+
860883
this._session.sendEvent(scriptEvent);
861884
}
862885

test/chrome/chromeDebugAdapter.test.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import * as testUtils from '../testUtils';
2424
import * as utils from '../../src/utils';
2525

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

3030
const MODULE_UNDER_TEST = '../../src/chrome/chromeDebugAdapter';
@@ -559,17 +559,29 @@ suite('ChromeDebugAdapter', () => {
559559
});
560560
});
561561

562-
// This is needed for Edge debug adapter, please keep the signature and logic of sendLoadedSourceEvent() method intact.
563-
test('tests that sendLoadedSourceEvent can accept an additional paramter to override the `reason` parameter', () => {
564-
const loadedSourceEventReason: LoadedSourceEventReason = 'changed';
562+
// This is needed for Edge debug adapter, please keep the logic of sendLoadedSourceEvent()
563+
test('tests that sendLoadedSourceEvent will set the `reason` parameter based on our internal view of the events we sent to the client', () => {
564+
let eventIndex = 0;
565565
sendEventHandler = (event) => {
566-
assert.equal('loadedSource', event.event);
567-
assert.notEqual(null, event.body);
568-
assert.equal(loadedSourceEventReason, event.body.reason);
566+
switch (eventIndex) {
567+
case 0:
568+
assert.equal('loadedSource', event.event);
569+
assert.notEqual(null, event.body);
570+
assert.equal('new', event.body.reason);
571+
break;
572+
case 1:
573+
assert.equal('loadedSource', event.event);
574+
assert.notEqual(null, event.body);
575+
assert.equal('changed', event.body.reason);
576+
break;
577+
default:
578+
throw new RangeError("Unexpected event index");
579+
}
580+
++eventIndex;
569581
};
570582

571-
return chromeDebugAdapter.attach(ATTACH_ARGS).then(() => {
572-
return (<any>chromeDebugAdapter).sendLoadedSourceEvent({
583+
return chromeDebugAdapter.attach(ATTACH_ARGS).then(async () => {
584+
await (<any>chromeDebugAdapter).sendLoadedSourceEvent({
573585
scriptId: 1,
574586
url: '',
575587
startLine: 0,
@@ -578,7 +590,17 @@ suite('ChromeDebugAdapter', () => {
578590
endColumn: 0,
579591
executionContextId: 0,
580592
hash: ''
581-
}, loadedSourceEventReason);
593+
});
594+
await (<any>chromeDebugAdapter).sendLoadedSourceEvent({
595+
scriptId: 1,
596+
url: '',
597+
startLine: 0,
598+
startColumn: 0,
599+
endLine: 0,
600+
endColumn: 0,
601+
executionContextId: 0,
602+
hash: ''
603+
});
582604
});
583605
});
584606

0 commit comments

Comments
 (0)