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