Skip to content

Commit

Permalink
Merge pull request #52 from fullstackedorg/v0.5.0
Browse files Browse the repository at this point in the history
v0.5.0
  • Loading branch information
cplepage authored May 16, 2024
2 parents 6fea947 + f0b2781 commit be43e83
Show file tree
Hide file tree
Showing 19 changed files with 8,156 additions and 6,370 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
node_modules
.cache
.DS_Store
Demo.zip
.DS_Store
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ node_modules
out
platform/ios
platform/**/editor
editor-sample-demo
2 changes: 1 addition & 1 deletion editor-sample-demo
20 changes: 14 additions & 6 deletions editor/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,38 @@ function recurseInProxy(target: Function, methodPath: string[] = []) {
}

export abstract class tsWorkerDelegate {
abstract onCreate(): void;
abstract onReq(id: number): void;
abstract onReqEnd(id: number): void;
}

export class tsWorker {
worker: Worker;
static delegate?: tsWorkerDelegate;
private worker: Worker;
workingDirectory: string;
delegate?: tsWorkerDelegate;
private reqsCount = 0;
private reqs = new Map<number, Function>();
private isReady = false;
private readyAwaiter: Function[] = [];

private postMessage(methodPath: string[], ...args: any) {
const id = ++this.reqsCount;
if (this.delegate) this.delegate.onReq(id);
if (tsWorker.delegate) tsWorker.delegate.onReq(id);
return new Promise((resolve) => {
this.reqs.set(id, resolve);
this.worker.postMessage({ id, methodPath, args });
});
}

dispose() {
this.worker.terminate();
for (const [id, promiseResolve] of this.reqs.entries()) {
tsWorker.delegate.onReqEnd(id);
promiseResolve(undefined);
}
this.reqs.clear();
this.reqsCount = 0;
}

constructor(workingDirectory: string) {
this.workingDirectory = workingDirectory;

Expand All @@ -44,15 +53,14 @@ export class tsWorker {
if (message.data.ready) {
this.isReady = true;
this.readyAwaiter.forEach((resolve) => resolve());
if (this.delegate) this.delegate.onCreate();
return;
}

const { id, data } = message.data;
const promiseResolve = this.reqs.get(id);
promiseResolve(data);
this.reqs.delete(id);
if (this.delegate) this.delegate.onReqEnd(id);
if (tsWorker.delegate) tsWorker.delegate.onReqEnd(id);
};
}

Expand Down
38 changes: 19 additions & 19 deletions editor/typescript/worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ts, {
import {
IScriptSnapshot,
createLanguageService,
createDocumentRegistry,
Expand All @@ -9,7 +9,8 @@ import ts, {
ModuleKind,
ModuleResolutionKind,
ScriptSnapshot,
ScriptTarget
ScriptTarget,
isSourceFile
} from "typescript";
import type { AdapterEditor } from "../rpc";
import type { rpcSync as rpcSyncFn } from "../../src/index";
Expand All @@ -18,19 +19,19 @@ import type rpcFn from "../../src/index";
const rpc = globalThis.rpc as typeof rpcFn<AdapterEditor>;
const rpcSync = globalThis.rpcSync as typeof rpcSyncFn<AdapterEditor>;

// source: https://stackoverflow.com/a/69881039/9777391
function JSONCircularRemover() {
const visited = new WeakSet();
return (key, value) => {
if (typeof value !== "object" || value === null) return value;

if (visited.has(value)) {
return "[Circular]";
}

visited.add(value);
return value;
};
function removeSourceObjects(obj: any) {
if (typeof obj === "object") {
Object.keys(obj).forEach((key) => {
if (key === "file" && isSourceFile(obj[key])) {
obj[key] = "[File]";
} else {
obj[key] = removeSourceObjects(obj[key]);
}
});
} else if(typeof obj === "function") {
return "[Function]"
}
return obj;
}

self.onmessage = (message: MessageEvent) => {
Expand All @@ -42,12 +43,10 @@ self.onmessage = (message: MessageEvent) => {
) as any;

if (typeof method === "function") {
const data = method(...args);
const data = removeSourceObjects(method(...args));
self.postMessage({
id,
data: data
? JSON.parse(JSON.stringify(data, JSONCircularRemover()))
: undefined
data
});
}
};
Expand Down Expand Up @@ -230,6 +229,7 @@ function initLanguageServiceHost(
scriptSnapshotCache[path].getLength()
);
}

return rpcSync().fs.readFile(path, {
absolutePath: true,
encoding: "utf8"
Expand Down
32 changes: 19 additions & 13 deletions editor/views/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export class Editor {
static currentDirectory: string;
static ignoredTypes = new Set<string>();

static async restartTSWorker() {
if (Editor.tsWorker) Editor.tsWorker.dispose();
Editor.tsWorker = new tsWorker(Editor.currentDirectory);
await Editor.tsWorker.ready();
await Editor.tsWorker.call().start(Editor.currentDirectory);
}

private extensions = [
basicSetup,
oneDark,
Expand All @@ -69,8 +76,6 @@ export class Editor {
}[] = [];
filePath: string[];

tsWorkerDelegate: tsWorkerDelegate;

constructor(filePath: string[]) {
this.filePath = filePath;

Expand Down Expand Up @@ -138,15 +143,6 @@ export class Editor {
this.editor.dispatch(setDiagnostics(this.editor.state, diagnostics));
}

private async restartTSWorker() {
if (Editor.tsWorker) Editor.tsWorker.worker.terminate();
Editor.tsWorker = new tsWorker(Editor.currentDirectory);
if (this.tsWorkerDelegate)
Editor.tsWorker.delegate = this.tsWorkerDelegate;
await Editor.tsWorker.ready();
await Editor.tsWorker.call().start(Editor.currentDirectory);
}

async loadFileContents() {
if (this.editor) {
this.editor.dom.remove();
Expand All @@ -170,7 +166,7 @@ export class Editor {
Editor.currentDirectory !==
Editor.tsWorker?.workingDirectory
)
await this.restartTSWorker();
await Editor.restartTSWorker();

await Editor.tsWorker
.call()
Expand Down Expand Up @@ -237,6 +233,16 @@ export class Editor {
});
}

async reRunExtensions(){
this.editor.dispatch({
changes: {
from: 0,
to: this.editor.state.doc.length,
insert: this.editor.state.doc.toString()
}
});
}

private async loadLanguageExtensions() {
const filename = this.filePath.at(-1) as string;
const extensions: Extension[] = [];
Expand Down Expand Up @@ -351,7 +357,7 @@ export class Editor {
Editor.ignoredTypes.add(name)
);

await this.restartTSWorker();
await Editor.restartTSWorker();
await this.updateFile();
tsErrors = await getAllTsError();
}
Expand Down
21 changes: 14 additions & 7 deletions editor/views/project/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { Project as TypeProject } from "../../api/projects/types";
import rpc from "../../rpc";
import api from "../../api";
import { PackageInstaller } from "../../packages/installer";
import { tsWorkerDelegate } from "../../typescript";
import { tsWorker, tsWorkerDelegate } from "../../typescript";

export class Project implements tsWorkerDelegate {
backAction: () => void;
Expand Down Expand Up @@ -63,6 +63,8 @@ export class Project implements tsWorkerDelegate {
this.renderEditors();
};

tsWorker.delegate = this;

window.addEventListener("keydown", (e) => {
if (e.key === "s" && (e.metaKey || e.ctrlKey)) {
e.preventDefault();
Expand All @@ -73,9 +75,6 @@ export class Project implements tsWorkerDelegate {

tsIcon = document.createElement("button");
activeReqs = new Set<number>();
onCreate(): void {
this.tsIcon.disabled = false;
}
checkForTsLoading = () => {
if (this.activeReqs.size) {
this.tsIcon.classList.add("loading");
Expand All @@ -84,6 +83,7 @@ export class Project implements tsWorkerDelegate {
}
};
onReq(id: number): void {
this.tsIcon.disabled = false;
this.activeReqs.add(id);
this.checkForTsLoading();
}
Expand Down Expand Up @@ -204,7 +204,16 @@ export class Project implements tsWorkerDelegate {
name,
deep: true
}))
).then(() => this.runProject());
).then(() => {
this.runProject();

if(Editor.tsWorker) {
setTimeout(async () => {
await Editor.restartTSWorker();
this.editors.forEach(editor => editor.reRunExtensions());
}, 500);
}
});
}
}

Expand Down Expand Up @@ -357,8 +366,6 @@ export class Project implements tsWorkerDelegate {
tabsContainer.classList.add("tabs-container");

this.editors.forEach(async (editor, index) => {
editor.tsWorkerDelegate = this;

const tab = document.createElement("li");
if (editor.hasBuildErrors()) {
tab.classList.add("has-errors");
Expand Down
Loading

0 comments on commit be43e83

Please sign in to comment.