Skip to content

Commit

Permalink
fix(timetravel): simplify ticks to share state
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Feb 23, 2023
1 parent 0aa5155 commit 5b0a141
Show file tree
Hide file tree
Showing 10 changed files with 506 additions and 594 deletions.
2 changes: 1 addition & 1 deletion core/dbs/SessionDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import WebsocketMessagesTable from '../models/WebsocketMessagesTable';
import FrameNavigationsTable from '../models/FrameNavigationsTable';
import FramesTable from '../models/FramesTable';
import PageLogsTable from '../models/PageLogsTable';
import SessionTable, { ISessionRecord } from '../models/SessionTable';
import SessionTable from '../models/SessionTable';
import MouseEventsTable from '../models/MouseEventsTable';
import FocusEventsTable from '../models/FocusEventsTable';
import ScrollEventsTable from '../models/ScrollEventsTable';
Expand Down
26 changes: 8 additions & 18 deletions core/lib/CommandFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,14 @@ export default class CommandFormatter {
) {
command.result = result.value;
}

if (result.nodePointer) {
command.resultNodeIds = [result.nodePointer.id];
command.resultNodeType = result.nodePointer.type;
if (result.nodePointer.iterableItems) {
command.result = result.nodePointer.iterableItems;
}
if (result.nodePointer.iterableIsNodePointers) {
command.resultNodeIds = result.nodePointer.iterableItems.map(x => x.id);
}
} else if (result.nodePointer) {
command.resultNodeIds = [result.nodePointer.id];
command.resultNodeType = result.nodePointer.type;
if (result.nodePointer.iterableItems) {
command.result = result.nodePointer.iterableItems;
}
if (result.nodePointer.iterableIsNodePointers) {
command.resultNodeIds = result.nodePointer.iterableItems.map(x => x.id);
}
} else if (meta.resultType === 'Array' && result.length) {
if (result[0].nodePointerId) {
Expand All @@ -152,14 +150,6 @@ export default class CommandFormatter {
}
}

if (command.result && command.name === 'detachTab') {
command.result.prefetchedJsPaths = undefined;
}

if (command.result && command.name.startsWith('go')) {
command.result = undefined;
}

// we have shell objects occasionally coming back. hide from ui
if (meta.args?.includes(getNodePointerFnName)) {
command.result = undefined;
Expand Down
29 changes: 23 additions & 6 deletions core/models/CommandsTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default class CommandsTable extends SqliteTable<ICommandMeta> {
return this.last?.id;
}

private historyById: { [id_retryNumber: string]: ICommandMeta } = {};

constructor(db: SqliteDatabase) {
super(
db,
Expand All @@ -26,6 +28,8 @@ export default class CommandsTable extends SqliteTable<ICommandMeta> {
['frameId', 'INTEGER'],
['flowCommandId', 'INTEGER'],
['activeFlowHandlerId', 'INTEGER'],
['startNavigationId', 'INTEGER'],
['endNavigationId', 'INTEGER'],
['name', 'TEXT'],
['args', 'TEXT'],
['clientStartDate', 'INTEGER'],
Expand All @@ -46,6 +50,7 @@ export default class CommandsTable extends SqliteTable<ICommandMeta> {
this.history = this.all();

for (const command of this.history) {
this.historyById[`${command.id}_${command.retryNumber}`] = command;
if (typeof command.callsite === 'string') {
command.callsite = JSON.parse((command.callsite as any) ?? '[]');
}
Expand All @@ -64,24 +69,36 @@ export default class CommandsTable extends SqliteTable<ICommandMeta> {
}

public insert(commandMeta: ICommandMeta): void {
commandMeta.retryNumber ??= 0;
commandMeta.resultType = commandMeta.result?.constructor?.name ?? typeof commandMeta.result;
this.history.push(commandMeta);
this.history.sort((a, b) => {
if (a.id === b.id) return a.retryNumber - b.retryNumber;
return a.id - b.id;
});
const key = `${commandMeta.id}_${commandMeta.retryNumber}`;

if (this.historyById[key]) {
const idx = this.history.indexOf(this.historyById[key]);
this.history[idx] = commandMeta;
} else {
this.history.push(commandMeta);
this.history.sort((a, b) => {
if (a.id === b.id) return a.retryNumber - b.retryNumber;
return a.id - b.id;
});
}
this.historyById[key] = commandMeta;

let args = commandMeta.args;
if (typeof commandMeta.args !== 'string') {
if (commandMeta.args.length === 0) args = undefined;
else args = TypeSerializer.stringify(args);
}
this.queuePendingInsert([
commandMeta.id,
commandMeta.retryNumber ?? 0,
commandMeta.retryNumber,
commandMeta.tabId,
commandMeta.frameId,
commandMeta.flowCommandId,
commandMeta.activeFlowHandlerId,
commandMeta.startNavigationId,
commandMeta.endNavigationId,
commandMeta.name,
args,
commandMeta.clientStartDate,
Expand Down
4 changes: 2 additions & 2 deletions timetravel/lib/CommandTimeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ export default class CommandTimeline<T extends ICommandMeta = ICommandMeta> {
}

private addNavigation(id: number): void {
if (id !== undefined && !this.navigationsById.has(id)) {
if (id !== undefined && id !== null && !this.navigationsById.has(id)) {
const nav = this.allNavigationsById.get(id);
this.navigationsById.set(nav.id, nav);
if (nav) this.navigationsById.set(nav.id, nav);
}
}

Expand Down
23 changes: 18 additions & 5 deletions timetravel/lib/MirrorPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class MirrorPage extends TypedEventEmitter<{
close: void;
open: void;
goto: { url: string; loaderId: string };
paint: { paintIndex: number };
}> {
public static newPageOptions = {
runPageScripts: false,
Expand All @@ -44,6 +45,14 @@ export default class MirrorPage extends TypedEventEmitter<{
return this.page?.id;
}

public get loadedPaintEvent(): IPaintEvent {
return this.domRecording.paintEvents[this.loadedPaintIndex];
}

public get hasSubscription(): boolean {
return !!this.subscribeToTab;
}

public domRecording: IDomRecording;

private events = new EventSubscriber();
Expand All @@ -56,6 +65,7 @@ export default class MirrorPage extends TypedEventEmitter<{
private subscribeToTab: Tab;
private loadQueue = new Queue(null, 1);
private logger: IBoundLog;
private loadedPaintIndex = -1;

private get useIsolatedContext(): boolean {
return this.page.installJsPathIntoIsolatedContext;
Expand Down Expand Up @@ -105,9 +115,8 @@ export default class MirrorPage extends TypedEventEmitter<{
this.showChromeInteractions
? InjectedScripts.installInteractionScript(page, this.useIsolatedContext)
: null,
page
.addNewDocumentScript(injectedScript, this.useIsolatedContext)
// .then(() => page.reload()),
page.addNewDocumentScript(injectedScript, this.useIsolatedContext),
// .then(() => page.reload()),
);
page[installedScriptsSymbol] = true;
}
Expand Down Expand Up @@ -181,16 +190,18 @@ export default class MirrorPage extends TypedEventEmitter<{
const onPageEvents = this.events.on(tab, 'page-events', this.onPageEvents);
this.events.once(tab, 'close', () => {
this.events.off(onPageEvents);
this.subscribeToTab = null;
if (cleanupOnTabClose) {
this.subscribeToTab = null;
this.domRecording = null;
this.loadedPaintIndex = -1;
this.pendingDomChanges.length = 0;
this.loadedDocument = null;
this.isReady = null;
// @ts-ignore
this.loadQueue.reset();
} else {
this.domRecording.domNodePathByFrameId = { ...tab.session.db.frames.frameDomNodePathsById };
this.domRecording.mainFrameIds = new Set(tab.session.db.frames.mainFrameIds(tab.tabId));
this.subscribeToTab = null;
}
});
this.subscribeToTab = tab;
Expand All @@ -209,6 +220,7 @@ export default class MirrorPage extends TypedEventEmitter<{
}
this.processPendingDomChanges();
newPaintIndex ??= this.domRecording.paintEvents.length - 1;
this.loadedPaintIndex = newPaintIndex;

const loadingDocument = this.getActiveDocument(newPaintIndex);

Expand Down Expand Up @@ -246,6 +258,7 @@ export default class MirrorPage extends TypedEventEmitter<{
});
}

this.emit('paint', { paintIndex: newPaintIndex });
const showOverlay = (overlayLabel || isLoadingDocument) && this.showChromeInteractions;

if (showOverlay) await this.evaluate('window.overlay();');
Expand Down
Loading

0 comments on commit 5b0a141

Please sign in to comment.