2022-03-20 08:56:28 +00:00
|
|
|
import { PageMeta } from "./types";
|
|
|
|
import { Socket } from "socket.io-client";
|
|
|
|
import { Update } from "@codemirror/collab";
|
|
|
|
import { ChangeSet, Text, Transaction } from "@codemirror/state";
|
|
|
|
|
|
|
|
import { CollabDocument, CollabEvents } from "./collab";
|
|
|
|
import { cursorEffect } from "./cursorEffect";
|
2022-03-21 14:21:34 +00:00
|
|
|
import { EventEmitter } from "../common/event";
|
|
|
|
import { Manifest } from "../common/manifest";
|
2022-03-27 09:31:12 +00:00
|
|
|
import { SystemJSON } from "../plugos/system";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
export type SpaceEvents = {
|
|
|
|
connect: () => void;
|
|
|
|
pageCreated: (meta: PageMeta) => void;
|
|
|
|
pageChanged: (meta: PageMeta) => void;
|
|
|
|
pageDeleted: (name: string) => void;
|
|
|
|
pageListUpdated: (pages: Set<PageMeta>) => void;
|
2022-03-21 14:21:34 +00:00
|
|
|
loadSystem: (systemJSON: SystemJSON<any>) => void;
|
2022-03-23 14:41:12 +00:00
|
|
|
plugLoaded: (plugName: string, plug: Manifest) => void;
|
|
|
|
plugUnloaded: (plugName: string) => void;
|
2022-03-20 08:56:28 +00:00
|
|
|
} & CollabEvents;
|
|
|
|
|
|
|
|
export type KV = {
|
|
|
|
key: string;
|
|
|
|
value: any;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class Space extends EventEmitter<SpaceEvents> {
|
|
|
|
socket: Socket;
|
|
|
|
reqId = 0;
|
|
|
|
allPages = new Set<PageMeta>();
|
|
|
|
|
|
|
|
constructor(socket: Socket) {
|
|
|
|
super();
|
|
|
|
this.socket = socket;
|
|
|
|
|
|
|
|
[
|
|
|
|
"connect",
|
|
|
|
"cursorSnapshot",
|
|
|
|
"pageCreated",
|
|
|
|
"pageChanged",
|
|
|
|
"pageDeleted",
|
2022-03-21 14:21:34 +00:00
|
|
|
"loadSystem",
|
2022-03-23 14:41:12 +00:00
|
|
|
"plugLoaded",
|
|
|
|
"plugUnloaded",
|
2022-03-20 08:56:28 +00:00
|
|
|
].forEach((eventName) => {
|
|
|
|
socket.on(eventName, (...args) => {
|
|
|
|
this.emit(eventName as keyof SpaceEvents, ...args);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.wsCall("page.listPages").then((pages) => {
|
|
|
|
this.allPages = new Set(pages);
|
|
|
|
this.emit("pageListUpdated", this.allPages);
|
|
|
|
});
|
|
|
|
this.on({
|
|
|
|
pageCreated: (meta) => {
|
|
|
|
// Cannot reply on equivalence in set, need to iterate over all pages
|
|
|
|
let found = false;
|
|
|
|
for (const page of this.allPages) {
|
|
|
|
if (page.name === meta.name) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-03-21 14:21:34 +00:00
|
|
|
if (!found) {
|
2022-03-20 08:56:28 +00:00
|
|
|
this.allPages.add(meta);
|
|
|
|
console.log("New page created", meta);
|
|
|
|
this.emit("pageListUpdated", this.allPages);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
pageDeleted: (name) => {
|
|
|
|
console.log("Page delete", name);
|
|
|
|
this.allPages.forEach((meta) => {
|
|
|
|
if (name === meta.name) {
|
|
|
|
this.allPages.delete(meta);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.emit("pageListUpdated", this.allPages);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-30 13:16:22 +00:00
|
|
|
openRequests = new Map<number, string>();
|
2022-03-25 11:03:06 +00:00
|
|
|
public wsCall(eventName: string, ...args: any[]): Promise<any> {
|
2022-03-20 08:56:28 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.reqId++;
|
2022-03-30 13:16:22 +00:00
|
|
|
const reqId = this.reqId;
|
|
|
|
this.openRequests.set(reqId, eventName);
|
|
|
|
this.socket!.once(`${eventName}Resp${reqId}`, (err, result) => {
|
|
|
|
this.openRequests.delete(reqId);
|
2022-03-20 08:56:28 +00:00
|
|
|
if (err) {
|
2022-03-28 13:25:05 +00:00
|
|
|
reject(new Error(err));
|
2022-03-20 08:56:28 +00:00
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
});
|
2022-03-30 13:16:22 +00:00
|
|
|
this.socket!.emit(eventName, reqId, ...args);
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async pushUpdates(
|
|
|
|
pageName: string,
|
|
|
|
version: number,
|
|
|
|
fullUpdates: readonly (Update & { origin: Transaction })[]
|
|
|
|
): Promise<boolean> {
|
|
|
|
if (this.socket) {
|
|
|
|
let updates = fullUpdates.map((u) => ({
|
|
|
|
clientID: u.clientID,
|
|
|
|
changes: u.changes.toJSON(),
|
|
|
|
cursors: u.effects?.map((e) => e.value),
|
|
|
|
}));
|
|
|
|
return this.wsCall("page.pushUpdates", pageName, version, updates);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
async pullUpdates(
|
|
|
|
pageName: string,
|
|
|
|
version: number
|
|
|
|
): Promise<readonly Update[]> {
|
|
|
|
let updates: Update[] = await this.wsCall(
|
|
|
|
"page.pullUpdates",
|
|
|
|
pageName,
|
|
|
|
version
|
|
|
|
);
|
|
|
|
return updates.map((u) => ({
|
|
|
|
changes: ChangeSet.fromJSON(u.changes),
|
|
|
|
effects: u.effects?.map((e) => cursorEffect.of(e.value)),
|
|
|
|
clientID: u.clientID,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
async listPages(): Promise<PageMeta[]> {
|
|
|
|
return Array.from(this.allPages);
|
|
|
|
}
|
|
|
|
|
|
|
|
async openPage(name: string): Promise<CollabDocument> {
|
|
|
|
this.reqId++;
|
|
|
|
let pageJSON = await this.wsCall("page.openPage", name);
|
|
|
|
|
|
|
|
return new CollabDocument(
|
|
|
|
Text.of(pageJSON.text),
|
|
|
|
pageJSON.version,
|
|
|
|
new Map(Object.entries(pageJSON.cursors))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async closePage(name: string): Promise<void> {
|
|
|
|
this.socket.emit("page.closePage", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
async readPage(name: string): Promise<{ text: string; meta: PageMeta }> {
|
|
|
|
return this.wsCall("page.readPage", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
async writePage(name: string, text: string): Promise<PageMeta> {
|
|
|
|
return this.wsCall("page.writePage", name, text);
|
|
|
|
}
|
|
|
|
|
|
|
|
async deletePage(name: string): Promise<void> {
|
|
|
|
return this.wsCall("page.deletePage", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getPageMeta(name: string): Promise<PageMeta> {
|
|
|
|
return this.wsCall("page.getPageMeta", name);
|
|
|
|
}
|
|
|
|
}
|