2019-01-08 07:46:19 +07:00
|
|
|
import { field, logger, time, Time } from "@coder/logger";
|
2019-02-07 05:55:29 +07:00
|
|
|
import { SharedProcessData } from "@coder/protocol";
|
2019-01-31 04:40:01 +07:00
|
|
|
import { retry } from "./retry";
|
2019-01-31 06:46:17 +07:00
|
|
|
import { upload } from "./upload";
|
2019-01-19 04:46:40 +07:00
|
|
|
import { client } from "./fill/client";
|
2019-01-31 05:57:22 +07:00
|
|
|
import { clipboard } from "./fill/clipboard";
|
2019-02-01 05:19:12 +07:00
|
|
|
import { INotificationService, IProgressService } from "./fill/notification";
|
2019-02-27 01:01:14 +07:00
|
|
|
import "./fill/os"; // Ensure it fills before anything else waiting on initData.
|
2019-01-08 07:46:19 +07:00
|
|
|
|
|
|
|
/**
|
2019-01-15 03:58:34 +07:00
|
|
|
* A general abstraction of an IDE client.
|
2019-01-08 07:46:19 +07:00
|
|
|
*
|
|
|
|
* Everything the client provides is asynchronous so you can wait on what
|
|
|
|
* you need from it without blocking anything else.
|
|
|
|
*
|
2019-01-19 04:46:40 +07:00
|
|
|
* It also provides task management to help asynchronously load and time code.
|
2019-01-08 07:46:19 +07:00
|
|
|
*/
|
2019-02-06 23:18:24 +07:00
|
|
|
export abstract class IdeClient {
|
2019-01-31 04:40:01 +07:00
|
|
|
public readonly retry = retry;
|
2019-01-31 05:57:22 +07:00
|
|
|
public readonly clipboard = clipboard;
|
2019-01-31 06:46:17 +07:00
|
|
|
public readonly upload = upload;
|
|
|
|
|
2019-01-08 07:46:19 +07:00
|
|
|
private start: Time | undefined;
|
2019-02-06 23:32:58 +07:00
|
|
|
private readonly tasks = <string[]>[];
|
2019-01-19 04:46:40 +07:00
|
|
|
private finishedTaskCount = 0;
|
|
|
|
private readonly loadTime: Time;
|
2019-02-07 00:53:23 +07:00
|
|
|
|
|
|
|
public readonly initData = client.initData;
|
2019-02-07 05:55:29 +07:00
|
|
|
public readonly sharedProcessData: Promise<SharedProcessData>;
|
2019-02-07 00:53:23 +07:00
|
|
|
public readonly onSharedProcessActive = client.onSharedProcessActive;
|
2019-01-19 04:46:40 +07:00
|
|
|
|
|
|
|
public constructor() {
|
|
|
|
logger.info("Loading IDE");
|
|
|
|
this.loadTime = time(2500);
|
|
|
|
|
2019-02-22 00:55:42 +07:00
|
|
|
let appWindow: Window | undefined;
|
|
|
|
window.addEventListener("message", (event) => {
|
|
|
|
if (event.data === "app") {
|
|
|
|
appWindow = event.source as Window;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-02-07 00:53:23 +07:00
|
|
|
this.sharedProcessData = new Promise((resolve): void => {
|
2019-04-06 06:49:29 +07:00
|
|
|
let d = client.onSharedProcessActive((data) => {
|
|
|
|
d.dispose();
|
|
|
|
d = client.onSharedProcessActive(() => {
|
|
|
|
d.dispose();
|
|
|
|
this.retry.notificationService.error(
|
|
|
|
new Error("Disconnected from shared process. Searching, installing, enabling, and disabling extensions will not work until the page is refreshed."),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
resolve(data);
|
|
|
|
});
|
2019-01-23 01:27:59 +07:00
|
|
|
});
|
|
|
|
|
2019-01-19 04:46:40 +07:00
|
|
|
window.addEventListener("contextmenu", (event) => {
|
|
|
|
event.preventDefault();
|
2019-01-08 07:46:19 +07:00
|
|
|
});
|
2019-01-15 03:58:34 +07:00
|
|
|
|
|
|
|
// Prevent Firefox from trying to reconnect when the page unloads.
|
|
|
|
window.addEventListener("unload", () => {
|
2019-01-19 04:46:40 +07:00
|
|
|
this.retry.block();
|
|
|
|
logger.info("Unloaded");
|
|
|
|
});
|
|
|
|
|
|
|
|
this.initialize().then(() => {
|
|
|
|
logger.info("Load completed", field("duration", this.loadTime));
|
2019-02-22 00:55:42 +07:00
|
|
|
if (appWindow) {
|
|
|
|
appWindow.postMessage("loaded", "*");
|
|
|
|
}
|
2019-01-19 04:46:40 +07:00
|
|
|
}).catch((error) => {
|
|
|
|
logger.error(error.message);
|
|
|
|
logger.warn("Load completed with errors", field("duration", this.loadTime));
|
2019-01-15 03:58:34 +07:00
|
|
|
});
|
2019-01-08 07:46:19 +07:00
|
|
|
}
|
|
|
|
|
2019-01-19 04:46:40 +07:00
|
|
|
public async task<T>(description: string, duration: number, task: () => Promise<T>): Promise<T>;
|
|
|
|
public async task<T, V>(description: string, duration: number, task: (v: V) => Promise<T>, t: Promise<V>): Promise<T>;
|
|
|
|
public async task<T, V1, V2>(description: string, duration: number, task: (v1: V1, v2: V2) => Promise<T>, t1: Promise<V1>, t2: Promise<V2>): Promise<T>;
|
|
|
|
public async task<T, V1, V2, V3>(description: string, duration: number, task: (v1: V1, v2: V2, v3: V3) => Promise<T>, t1: Promise<V1>, t2: Promise<V2>, t3: Promise<V3>): Promise<T>;
|
|
|
|
public async task<T, V1, V2, V3, V4>(description: string, duration: number, task: (v1: V1, v2: V2, v3: V3, v4: V4) => Promise<T>, t1: Promise<V1>, t2: Promise<V2>, t3: Promise<V3>, t4: Promise<V4>): Promise<T>;
|
|
|
|
public async task<T, V1, V2, V3, V4, V5>(description: string, duration: number, task: (v1: V1, v2: V2, v3: V3, v4: V4, v5: V5) => Promise<T>, t1: Promise<V1>, t2: Promise<V2>, t3: Promise<V3>, t4: Promise<V4>, t5: Promise<V5>): Promise<T>;
|
|
|
|
public async task<T, V1, V2, V3, V4, V5, V6>(description: string, duration: number, task: (v1: V1, v2: V2, v3: V3, v4: V4, v5: V5, v6: V6) => Promise<T>, t1: Promise<V1>, t2: Promise<V2>, t3: Promise<V3>, t4: Promise<V4>, t5: Promise<V5>, t6: Promise<V6>): Promise<T>;
|
2019-04-06 06:49:29 +07:00
|
|
|
/**
|
|
|
|
* Wrap a task in some logging, timing, and progress updates. Can optionally
|
|
|
|
* wait on other tasks which won't count towards this task's time.
|
|
|
|
*/
|
2019-01-19 04:46:40 +07:00
|
|
|
public async task<T>(
|
2019-01-08 07:46:19 +07:00
|
|
|
description: string, duration: number = 100, task: (...args: any[]) => Promise<T>, ...after: Array<Promise<any>> // tslint:disable-line no-any
|
|
|
|
): Promise<T> {
|
|
|
|
this.tasks.push(description);
|
|
|
|
if (!this.start) {
|
|
|
|
this.start = time(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
let start: Time | undefined;
|
|
|
|
try {
|
|
|
|
const waitFor = await (after && after.length > 0 ? Promise.all(after) : Promise.resolve([]));
|
|
|
|
start = time(duration);
|
|
|
|
logger.info(description);
|
|
|
|
const value = await task(...waitFor);
|
|
|
|
logger.info(`Finished "${description}"`, field("duration", start));
|
|
|
|
const index = this.tasks.indexOf(description);
|
|
|
|
if (index !== -1) {
|
|
|
|
this.tasks.splice(index, 1);
|
|
|
|
}
|
|
|
|
++this.finishedTaskCount;
|
|
|
|
if (this.tasks.length === 0) {
|
|
|
|
logger.info("Finished all queued tasks", field("duration", this.start), field("count", this.finishedTaskCount));
|
|
|
|
this.start = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(`Failed "${description}"`, field("duration", typeof start !== "undefined" ? start : "not started"), field("error", error));
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 04:40:01 +07:00
|
|
|
public set notificationService(service: INotificationService) {
|
|
|
|
this.retry.notificationService = service;
|
|
|
|
this.upload.notificationService = service;
|
|
|
|
}
|
|
|
|
|
|
|
|
public set progressService(service: IProgressService) {
|
|
|
|
this.upload.progressService = service;
|
|
|
|
}
|
|
|
|
|
2019-01-19 04:46:40 +07:00
|
|
|
/**
|
|
|
|
* Initialize the IDE.
|
|
|
|
*/
|
|
|
|
protected abstract initialize(): Promise<void>;
|
2019-01-08 07:46:19 +07:00
|
|
|
}
|