<commit_before>import { Log } from '../../util/log';
import { EventEmitter } from 'events';
import { DebugConnection } from '../connection';
import { ActorProxy } from './interface';
import { WorkerActorProxy } from './worker';
import { IThreadActorProxy, ThreadActorProxy } from './thread';
import { SourceMappingThreadActorProxy } from '../sourceMaps/thread';
import { PendingRequests } from '../../util/pendingRequests';
import { PathMapper } from '../../util/pathMapper';

let log = Log.create('TabActorProxy');

/**
 * Proxy class for a tab actor
 * ([docs](https://github.com/mozilla/gecko-dev/blob/master/devtools/docs/backend/protocol.md#listing-browser-tabs))
 */
export class TabActorProxy extends EventEmitter implements ActorProxy {

	private pendingAttachRequests = new PendingRequests<IThreadActorProxy>();
	private pendingDetachRequests = new PendingRequests<void>();
	private pendingWorkersRequests = new PendingRequests<Map<string, WorkerActorProxy>>();
	private pendingReloadRequests = new PendingRequests<void>();
	private workers = new Map<string, WorkerActorProxy>();

	constructor(
		public readonly name: string,
		private _title: string,
		private _url: string,
		private readonly pathMapper: PathMapper,
		private readonly connection: DebugConnection
	) {
		super();
		this.connection.register(this);
	}

	public get title(): string {
		return this._title;
	}

	public get url(): string {
		return this._url;
	}

	public attach(): Promise<IThreadActorProxy> {

		log.debug(`Attaching to tab ${this.name}`);

		return new Promise<IThreadActorProxy>((resolve, reject) => {
			this.pendingAttachRequests.enqueue({ resolve, reject });
			this.connection.sendRequest({ to: this.name, type: 'attach' });
		});
	}

	public detach(): Promise<void> {

		log.debug(`Detaching from tab ${this.name}`);

		return new Promise<void>((resolve, reject) => {
			this.pendingDetachRequests.enqueue({ resolve, reject });
			this.connection.sendRequest({ to: this.name, type: 'detach' });
		});
	}

	public fetchWorkers(): Promise<Map<string, WorkerActorProxy>> {
		
		log.debug('Fetching workers');
		
		return new Promise<Map<string, WorkerActorProxy>>((resolve, reject) => {
			this.pendingWorkersRequests.enqueue({ resolve, reject });
			this.connection.sendRequest({ to: this.name, type: 'listWorkers' });
		});
	}

	public reload(): Promise<void> {
		
		log.debug(`Reloading ${this.name}`);
		
		return new Promise<void>((resolve, reject) => {
			this.pendingReloadRequests.enqueue({ resolve, reject });
			this.connection.sendRequest({ to: this.name, type: 'reload' });
		});
	}

	public dispose(): void {
		this.connection.unregister(this);
	}

	public receiveResponse(response: FirefoxDebugProtocol.Response): void {

		if (response['type'] === 'tabAttached') {

			log.debug(`Attached to tab ${this.name}`);
			let tabAttachedResponse = <FirefoxDebugProtocol.TabAttachedResponse>response;

			let threadActor: IThreadActorProxy = this.connection.getOrCreate(
				tabAttachedResponse.threadActor, <commit_msg>fix the detection of the attach response in Firefox 76<commit_after>import { Log } from '../../util/log';
import { EventEmitter } from 'events';
import { DebugConnection } from '../connection';
import { ActorProxy } from './interface';
import { WorkerActorProxy } from './worker';
import { IThreadActorProxy, ThreadActorProxy } from './thread';
import { SourceMappingThreadActorProxy } from '../sourceMaps/thread';
import { PendingRequests } from '../../util/pendingRequests';
import { PathMapper } from '../../util/pathMapper';

let log = Log.create('TabActorProxy');

/**
 * Proxy class for a tab actor
 * ([docs](https://github.com/mozilla/gecko-dev/blob/master/devtools/docs/backend/protocol.md#listing-browser-tabs))
 */
export class TabActorProxy extends EventEmitter implements ActorProxy {

	private pendingAttachRequests = new PendingRequests<IThreadActorProxy>();
	private pendingDetachRequests = new PendingRequests<void>();
	private pendingWorkersRequests = new PendingRequests<Map<string, WorkerActorProxy>>();
	private pendingReloadRequests = new PendingRequests<void>();
	private workers = new Map<string, WorkerActorProxy>();

	constructor(
		public readonly name: string,
		private _title: string,
		private _url: string,
		private readonly pathMapper: PathMapper,
		private readonly connection: DebugConnection
	) {
		super();
		this.connection.register(this);
	}

	public get title(): string {
		return this._title;
	}

	public get url(): string {
		return this._url;
	}

	public attach(): Promise<IThreadActorProxy> {

		log.debug(`Attaching to tab ${this.name}`);

		return new Promise<IThreadActorProxy>((resolve, reject) => {
			this.pendingAttachRequests.enqueue({ resolve, reject });
			this.connection.sendRequest({ to: this.name, type: 'attach' });
		});
	}

	public detach(): Promise<void> {

		log.debug(`Detaching from tab ${this.name}`);

		return new Promise<void>((resolve, reject) => {
			this.pendingDetachRequests.enqueue({ resolve, reject });
			this.connection.sendRequest({ to: this.name, type: 'detach' });
		});
	}

	public fetchWorkers(): Promise<Map<string, WorkerActorProxy>> {
		
		log.debug('Fetching workers');
		
		return new Promise<Map<string, WorkerActorProxy>>((resolve, reject) => {
			this.pendingWorkersRequests.enqueue({ resolve, reject });
			this.connection.sendRequest({ to: this.name, type: 'listWorkers' });
		});
	}

	public reload(): Promise<void> {
		
		log.debug(`Reloading ${this.name}`);
		
		return new Promise<void>((resolve, reject) => {
			this.pendingReloadRequests.enqueue({ resolve, reject });
			this.connection.sendRequest({ to: this.name, type: 'reload' });
		});
	}

	public dispose(): void {
		this.connection.unregister(this);
	}

	public receiveResponse(response: FirefoxDebugProtocol.Response): void {

		if (response['threadActor']) {

			log.debug(`Attached to tab ${this.name}`);
			let tabAttachedResponse = <FirefoxDebugProtocol.TabAttachedResponse>response;

			let threadActor: IThreadActorProxy = this.connection.getOrCreate(
				tabAttachedResponse.threadActor, 
{'fraction_non_alphanumeric': 0.1446675444371297, 'fraction_numerical': 0.0003291639236339697, 'mean_word_length': 4.923001949317738, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 45, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7573369', 'n_tokens_mistral': 1907, 'n_tokens_neox': 1773, 'n_words': 420}