Skip to content

Commit

Permalink
Implement push
Browse files Browse the repository at this point in the history
  • Loading branch information
aboodman committed Dec 9, 2021
1 parent 0b2f985 commit 62cb238
Show file tree
Hide file tree
Showing 10 changed files with 492 additions and 215 deletions.
18 changes: 5 additions & 13 deletions backend/client-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,12 @@ export interface Socket {
export type ClientState = {
socket: Socket;

// A list of mutations awaiting application from this client. This list has
// a funny invariant. It is sorted by mutationID AND serverTimestamp ascending.
// The list needs to be sorted by mutationID because we need to process
// mutations in that order to preserve causality. But it also needs to be sorted
// by serverTimestamp so that mutations get processed in an order roughly
// corresponding to realtime. The push handler (below) ensures this invariant
// is preserved by adjusting serverTimestamp if necessary so that it is
// monotonically increasing.
pending: PendingMutation[];
// A list of mutations awaiting application from this client. Sorted by
// lastMutationID and de-duplicated. The timestamps in these mutations
// are in the server's timeframe. Note that they will generally increase
// with respect to mutationID but that is not guaranteed.
pending: Mutation[];

// How long is the client's timestamp behind the local timestamp?
clockBehindByMs: number;
};

export type PendingMutation = Mutation & {
serverTimestamp: number;
};
13 changes: 5 additions & 8 deletions backend/lock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from "chai";
import { test } from "mocha";
import { resolver } from "../frontend/resolver";
import { Lock } from "./lock";
import { sleep } from "./test-utils";

test("Lock", async () => {
type Task = () => Promise<void>;
Expand All @@ -17,10 +18,6 @@ test("Lock", async () => {
return [task, resolve] as [Task, () => void];
}

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

const [t1, r1] = makeTask("t1");
const [t2, r2] = makeTask("t2");
const [t3, r3] = makeTask("t3");
Expand All @@ -29,18 +26,18 @@ test("Lock", async () => {
loop.withLock(t1);
loop.withLock(t2);

await sleep(0);
await sleep();
expect(log).deep.equal(["t1 enter"]);
r1();
await sleep(0);
await sleep();
expect(log).deep.equal(["t1 enter", "t1 exit", "t2 enter"]);
r2();
await sleep(0);
await sleep();
expect(log).deep.equal(["t1 enter", "t1 exit", "t2 enter", "t2 exit"]);

r3();
loop.withLock(t3);
await sleep(0);
await sleep();
expect(log).deep.equal([
"t1 enter",
"t1 exit",
Expand Down
Loading

0 comments on commit 62cb238

Please sign in to comment.