Fix linting errors and formatting
This commit is contained in:
parent
83dac38f29
commit
0a71e7b321
4 changed files with 27 additions and 10 deletions
|
|
@ -74,16 +74,22 @@ function connect(roomName: string) {
|
|||
function computeDiff(oldText: string, newText: string) {
|
||||
// find common prefix
|
||||
let prefixLen = 0;
|
||||
while (prefixLen < oldText.length && prefixLen < newText.length &&
|
||||
oldText[prefixLen] === newText[prefixLen]) {
|
||||
while (
|
||||
prefixLen < oldText.length &&
|
||||
prefixLen < newText.length &&
|
||||
oldText[prefixLen] === newText[prefixLen]
|
||||
) {
|
||||
prefixLen++;
|
||||
}
|
||||
|
||||
// find common suffix
|
||||
let suffixLen = 0;
|
||||
while (suffixLen < oldText.length - prefixLen &&
|
||||
suffixLen < newText.length - prefixLen &&
|
||||
oldText[oldText.length - 1 - suffixLen] === newText[newText.length - 1 - suffixLen]) {
|
||||
while (
|
||||
suffixLen < oldText.length - prefixLen &&
|
||||
suffixLen < newText.length - prefixLen &&
|
||||
oldText[oldText.length - 1 - suffixLen] ===
|
||||
newText[newText.length - 1 - suffixLen]
|
||||
) {
|
||||
suffixLen++;
|
||||
}
|
||||
|
||||
|
|
@ -101,7 +107,10 @@ function setContent(newContent: string) {
|
|||
if (oldContent === newContent) return;
|
||||
|
||||
// compute minimal diff and apply
|
||||
const { deleteStart, deleteLen, insertText } = computeDiff(oldContent, newContent);
|
||||
const { deleteStart, deleteLen, insertText } = computeDiff(
|
||||
oldContent,
|
||||
newContent,
|
||||
);
|
||||
|
||||
const t = text;
|
||||
doc.transact(() => {
|
||||
|
|
|
|||
11
src/index.ts
11
src/index.ts
|
|
@ -1,5 +1,10 @@
|
|||
import { decode } from "./protocol";
|
||||
import { type Client, getOrCreateSession, getSession, removeSession } from "./session";
|
||||
import {
|
||||
type Client,
|
||||
getOrCreateSession,
|
||||
getSession,
|
||||
removeSession,
|
||||
} from "./session";
|
||||
|
||||
const PORT = Number(process.env.PORT) || 4040;
|
||||
|
||||
|
|
@ -8,7 +13,9 @@ Bun.serve({
|
|||
fetch(req, server) {
|
||||
const url = new URL(req.url);
|
||||
if (url.pathname === "/ws") {
|
||||
const upgraded = server.upgrade(req, { data: { room: null, client: null } });
|
||||
const upgraded = server.upgrade(req, {
|
||||
data: { room: null, client: null },
|
||||
});
|
||||
if (!upgraded) {
|
||||
return new Response("websocket upgrade failed", { status: 400 });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,8 @@ export type ServerMessage =
|
|||
| { type: "sync"; data: number[] } // full yjs state
|
||||
| { type: "update"; data: number[] }
|
||||
| { type: "awareness"; data: number[] }
|
||||
| { type: "peers"; count: number };
|
||||
| { type: "peers"; count: number }
|
||||
| { type: "error"; message: string };
|
||||
|
||||
export type BridgeMessage =
|
||||
| { type: "ready" }
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ export class Session {
|
|||
console.error(`failed to apply update in session ${this.name}:`, err);
|
||||
// optionally notify the client that sent the bad update
|
||||
try {
|
||||
from.ws.send(encode({ type: "error", message: "invalid update" } as any));
|
||||
from.ws.send(encode({ type: "error", message: "invalid update" }));
|
||||
} catch {
|
||||
// ignore send errors
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue