Skip to content

Commit

Permalink
Pull out ReadStorage and WriteStorage to avoid need for runtime
Browse files Browse the repository at this point in the history
exceptions.

Addresses: rocicorp/replidraw#10
  • Loading branch information
aboodman committed Mar 4, 2021
1 parent 3c43ff8 commit 84b4fc1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
18 changes: 6 additions & 12 deletions frontend/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
keyPrefix as clientStatePrefix,
selectShape,
} from "../shared/client-state";
import type Storage from "../shared/storage";
import type { ReadStorage, WriteStorage } from "../shared/storage";
import type { UserInfo } from "../shared/client-state";
import { newID } from "../shared/id";

Expand Down Expand Up @@ -156,21 +156,15 @@ export function createData(rep: Replicache) {
};
}

function readStorage(tx: ReadTransaction): Storage {
function readStorage(tx: ReadTransaction): ReadStorage {
return {
getObject: tx.get.bind(tx),
putObject: () => {
throw new Error("Cannot write inside ReadTransaction");
},
delObject: () => {
throw new Error("Cannot delete inside ReadTransaction");
},
getObject: (key: string) => tx.get(key),
};
}

function writeStorage(tx: WriteTransaction): Storage {
function writeStorage(tx: WriteTransaction): WriteStorage {
return Object.assign(readStorage(tx), {
putObject: tx.put.bind(tx),
delObject: tx.del.bind(tx),
putObject: (key: string, value: JSONValue) => tx.put(key, value),
delObject: async (key: string) => void (await tx.del(key)),
});
}
14 changes: 7 additions & 7 deletions shared/client-state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as t from "io-ts";
import { must } from "../backend/decode";
import Storage from "./storage";
import { ReadStorage, WriteStorage } from "./storage";
import { randInt } from "./rand";

const colors = [
Expand Down Expand Up @@ -56,7 +56,7 @@ export type UserInfo = t.TypeOf<typeof userInfo>;
export type ClientState = t.TypeOf<typeof clientState>;

export async function initClientState(
storage: Storage,
storage: WriteStorage,
{ id, defaultUserInfo }: { id: string; defaultUserInfo: UserInfo }
): Promise<void> {
if (await storage.getObject(key(id))) {
Expand All @@ -77,7 +77,7 @@ export async function initClientState(
}

export async function getClientState(
storage: Storage,
storage: ReadStorage,
id: string
): Promise<ClientState> {
const jv = await storage.getObject(key(id));
Expand All @@ -88,14 +88,14 @@ export async function getClientState(
}

export function putClientState(
storage: Storage,
storage: WriteStorage,
{ id, clientState }: { id: string; clientState: ClientState }
): Promise<void> {
return storage.putObject(key(id), clientState);
}

export async function setCursor(
storage: Storage,
storage: WriteStorage,
{ id, x, y }: { id: string; x: number; y: number }
): Promise<void> {
const clientState = await getClientState(storage, id);
Expand All @@ -105,7 +105,7 @@ export async function setCursor(
}

export async function overShape(
storage: Storage,
storage: WriteStorage,
{ clientID, shapeID }: { clientID: string; shapeID: string }
): Promise<void> {
const client = await getClientState(storage, clientID);
Expand All @@ -114,7 +114,7 @@ export async function overShape(
}

export async function selectShape(
storage: Storage,
storage: WriteStorage,
{ clientID, shapeID }: { clientID: string; shapeID: string }
): Promise<void> {
const client = await getClientState(storage, clientID);
Expand Down
10 changes: 5 additions & 5 deletions shared/shape.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as t from "io-ts";
import { must } from "../backend/decode";
import Storage from "./storage";
import { ReadStorage, WriteStorage } from "./storage";

export const shape = t.type({
type: t.literal("rect"),
Expand All @@ -15,7 +15,7 @@ export const shape = t.type({
export type Shape = t.TypeOf<typeof shape>;

export async function getShape(
storage: Storage,
storage: ReadStorage,
id: string
): Promise<Shape | null> {
const jv = await storage.getObject(key(id));
Expand All @@ -26,18 +26,18 @@ export async function getShape(
}

export function putShape(
storage: Storage,
storage: WriteStorage,
{ id, shape }: { id: string; shape: Shape }
): Promise<void> {
return storage.putObject(key(id), shape);
}

export function deleteShape(storage: Storage, id: string): Promise<void> {
export function deleteShape(storage: WriteStorage, id: string): Promise<void> {
return storage.delObject(key(id));
}

export async function moveShape(
storage: Storage,
storage: WriteStorage,
{ id, dx, dy }: { id: string; dx: number; dy: number }
): Promise<void> {
const shape = await getShape(storage, id);
Expand Down
5 changes: 4 additions & 1 deletion shared/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { JSONValue } from "replicache";
/**
* Interface required of underlying storage.
*/
export default interface Storage {
export interface ReadStorage {
getObject(key: string): Promise<JSONValue | undefined>;
}

export interface WriteStorage extends ReadStorage {
putObject(key: string, value: JSONValue): Promise<void>;
delObject(key: string): Promise<void>;
}

0 comments on commit 84b4fc1

Please sign in to comment.