-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Introduce a little data abstraction layer
- Loading branch information
Showing
10 changed files
with
196 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/// <reference types="next" /> | ||
/// <reference types="next/types/global" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,34 @@ | ||
import { useEffect, useState } from 'react'; | ||
import Replicache from 'replicache'; | ||
import {Data} from '../src/data'; | ||
import {Designer2} from '../src/Designer2'; | ||
|
||
export default function Home() { | ||
const [rep, setRep] = useState(null); | ||
const [data, setData] = useState(null); | ||
|
||
// TODO: Think through React under SSR. | ||
// TODO: Think through Replicache + SSR. | ||
useEffect(() => { | ||
if (rep) { | ||
if (data) { | ||
return; | ||
} | ||
// TODO: Use clientID from Replicache: | ||
// https://github.com/rocicorp/replicache-sdk-js/issues/275 | ||
let clientID = localStorage.clientID; | ||
if (!clientID) { | ||
clientID = localStorage.clientID = Math.random().toString(36).substring(2); | ||
} | ||
|
||
const isProd = location.host.indexOf('.vercel.app') > -1; | ||
setRep(new Replicache({ | ||
const rep = new Replicache({ | ||
clientViewURL: new URL('/api/replicache-client-view', location.href).toString(), | ||
diffServerURL: isProd ? 'https://serve.replicache.dev/pull' : 'http://localhost:7001/pull', | ||
diffServerAuth: isProd ? '1000000' : 'sandbox', | ||
wasmModule: '/replicache/replicache.dev.wasm', | ||
syncInterval: 5000, | ||
})); | ||
}); | ||
|
||
setData(new Data(rep)); | ||
}); | ||
|
||
return rep && <Designer2 rep={rep}/>; | ||
return data && <Designer2 {...{data}}/>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Replicache, {JSONObject, ReadTransaction, WriteTransaction} from 'replicache'; | ||
import {useSubscribe} from 'replicache-react-util'; | ||
|
||
export interface Shape { | ||
x: number, | ||
y: number, | ||
} | ||
|
||
export class Data { | ||
private rep: Replicache; | ||
|
||
constructor(rep: Replicache) { | ||
this.rep = rep; | ||
|
||
// TODO: Is there a way to consolidate the type information and re-use what is declared below? | ||
this.moveShape = rep.register('moveShape', async (tx: WriteTransaction, args: {id: string, dx: number, dy: number}) => { | ||
const {id, dx, dy} = args; | ||
const shape = await this.getShape(tx, id); | ||
shape.x += dx; | ||
shape.y += dy; | ||
await this.putShape(tx, id, shape); | ||
}); | ||
} | ||
|
||
readonly moveShape: (args: {id: string, dx: number, dy: number}) => Promise<void>; | ||
|
||
useShapeIDs(): Array<string> { | ||
return useSubscribe(this.rep, async (tx: ReadTransaction) => { | ||
const shapes = await tx.scanAll({prefix:'/object/'}); | ||
return shapes.map(([k, _]) => k.split('/')[2]); | ||
}, []); | ||
} | ||
|
||
useShapeByID(id: string): Shape|null { | ||
return useSubscribe(this.rep, (tx: ReadTransaction) => { | ||
return this.getShape(tx, id); | ||
}, null); | ||
} | ||
|
||
private async getShape(tx: ReadTransaction, id: string): Promise<Shape> { | ||
// TODO: Is there an automated way to check that the returned value implements Shape? | ||
return await tx.get(`/object/${id}`) as unknown as Shape; | ||
} | ||
private async putShape(tx: WriteTransaction, id: string, shape: Shape) { | ||
return await tx.put(`/object/${id}`, shape as unknown as JSONObject) as unknown as Shape; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// TODO: Why isn't Typescript picking up the delcaration in | ||
// replicache-react-util? | ||
declare module 'replicache-react-util'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import React from 'react'; | ||
import {Data} from '../data'; | ||
import {getObjectAttributes} from './attribs'; | ||
|
||
export function Rect2({data, id}: {data: Data, id: string}) { | ||
const shape = data.useShapeByID(id); | ||
if (!shape) { | ||
return null; | ||
} | ||
return <rect {...getObjectAttributes(shape)} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"lib": [ | ||
"dom", | ||
"dom.iterable", | ||
"esnext" | ||
], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": false, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve" | ||
}, | ||
"include": [ | ||
"next-env.d.ts", | ||
"**/*.ts", | ||
"**/*.tsx" | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} |
Oops, something went wrong.