Skip to content

Commit

Permalink
wip idb houses
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas O'Neill committed Jun 27, 2023
1 parent eddf5a5 commit b1e3229
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 166 deletions.
2 changes: 1 addition & 1 deletion app/data/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const invertModuleElementGeometriesKey = (input: string) => {
return { systemId, dna }
}

const useSpeckleObject = (speckleBranchUrl: string) => {
export const useSpeckleObject = (speckleBranchUrl: string) => {
const loader = useMemo(() => new BufferGeometryLoader(), [])

const geometries = useLiveQuery(async () => {
Expand Down
2 changes: 1 addition & 1 deletion app/debug/system/ui-3d/DebugSpeckleModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { pipe } from "fp-ts/lib/function"
import { useMemo, useRef } from "react"
import { Group, Material, MeshBasicMaterial } from "three"
import { Module } from "../../../../server/data/modules"
import { useSpeckleObject } from "../../../data/elements"
import { useGetDefaultElementMaterial } from "../../../design/state/hashedMaterials"
import { O, R, S } from "../../../utils/functions"
import useSpeckleObject from "../../../utils/speckle/useSpeckleObject"
import DebugSpeckleElement from "./DebugSpeckleElement"

const DebugSpeckleModule = ({ module }: { module: Module }) => {
Expand Down
56 changes: 37 additions & 19 deletions app/design/state/houses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,55 @@ import { none, some } from "fp-ts/lib/Option"
import { keys } from "fp-ts/lib/ReadonlyRecord"
import produce from "immer"
import { nanoid } from "nanoid"
import { useEffect, useMemo } from "react"
import { useMemo } from "react"
import { useKey } from "react-use"
import { Vector3 } from "three"
import { proxy, subscribe, useSnapshot } from "valtio"
import { A, R, RA, RR, S } from "~/utils/functions"
import { getHousesFromLocalStorage, House, Houses } from "../../data/houses"
import { proxy, snapshot, subscribe, useSnapshot } from "valtio"
import { A, clearRecord, R, RA, RR, S } from "~/utils/functions"
import { House, Houses } from "../../data/houses"
import { useHouseTypes } from "../../data/houseTypes"
import { useModules, useSystemModules } from "../../data/modules"
import { BUILDX_LOCAL_STORAGE_HOUSES_KEY } from "./constants"
import userDB from "../../db/user"
import { isSSR } from "../../utils/next"

const houses = proxy<Houses>(getHousesFromLocalStorage())
const houses = proxy<Houses>({})

export const useLocallyStoredHouses = () =>
useEffect(
() =>
subscribe(houses, () => {
localStorage.setItem(
BUILDX_LOCAL_STORAGE_HOUSES_KEY,
JSON.stringify(houses)
)
}),
[]
)
const initHouses = async () => {
if (isSSR()) return

const housesArray = await userDB.houses.toArray()
if (!A.isNonEmpty(housesArray)) {
clearRecord(houses)
}

housesArray.forEach((house) => {
houses[house.id] = house
})
}

initHouses().then(() => {
const unsubscribe = subscribe(houses, () => {
// This will run every time `houses` changes
Object.values(houses).forEach(async (house) => {
const snapshotHouse = snapshot(house) as typeof house
// Check if house exists in the DB
const existingHouse = await userDB.houses.get(house.id)
if (existingHouse) {
// If it exists, update it
userDB.houses.update(house.id, snapshotHouse)
} else {
// If it doesn't exist, add it
userDB.houses.add(snapshotHouse)
}
})
})
})

export const useHouses = () => {
useLocallyStoredHouses()
return useSnapshot(houses) as typeof houses
}

export const useHouseKeys = () => {
useLocallyStoredHouses()
return pipe(useSnapshot(houses) as typeof houses, RR.keys)
}

Expand Down
8 changes: 8 additions & 0 deletions app/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,11 @@ export const capitalizeFirstLetters = (str: string): string =>
.split(" ")
.map((x) => x.charAt(0).toUpperCase())
.join("")

export const clearRecord = (obj: Record<string, any>) => {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
delete obj[key]
}
}
}
119 changes: 0 additions & 119 deletions app/utils/speckle/useSpeckleObject.ts

This file was deleted.

4 changes: 3 additions & 1 deletion app/workers/systems/global.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"use client"
import { Remote, wrap } from "comlink"
import { isSSR } from "../../utils/next"
import { SystemsAPI } from "./worker"

let worker: Remote<SystemsAPI> | null = null

const getSystemsWorker = () => {
if (worker === null) {
if (!isSSR() && worker === null) {
worker = wrap<SystemsAPI>(
new Worker(new URL("./worker.ts", import.meta.url))
)
Expand Down
52 changes: 27 additions & 25 deletions app/workers/systems/worker.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { expose } from "comlink"
import { liveQuery } from "dexie"
import { pipe } from "fp-ts/lib/function"
import produce from "immer"
import { BufferGeometry } from "three"
import { mergeBufferGeometries } from "three-stdlib"
import { ComputeLayoutEventDetail } from "."
import { vanillaTrpc } from "../../../client/trpc"
import { Module } from "../../../server/data/modules"
import { getSpeckleObject } from "../../../server/data/speckleModel"
import systemsDB, { IndexedModule } from "../../db/systems"
import { modulesToColumnLayout } from "../../design/state/layouts"
import { A } from "../../utils/functions"
import { A, R } from "../../utils/functions"
import speckleIfcParser from "../../utils/speckle/speckleIfcParser"

const initModules = async () => {
const remoteModules = await vanillaTrpc.modules.query()
Expand Down Expand Up @@ -55,34 +60,31 @@ modulesObservable.subscribe((modules) => {

if (
maybeModel &&
new Date(maybeModel.lastFetched) === new Date(lastFetched)
new Date(maybeModel.lastFetched).getTime() ===
new Date(lastFetched).getTime()
) {
return
}

const geometries = await vanillaTrpc.speckleModel.query({
speckleBranchUrl,
})

// const speckleObjectData = await getSpeckleObject(speckleBranchUrl)
// const speckleObject = speckleIfcParser.parse(speckleObjectData)
// const geometries = pipe(
// speckleObject,
// A.reduce(
// {},
// (acc: { [e: string]: BufferGeometry[] }, { ifcTag, geometry }) => {
// return produce(acc, (draft) => {
// if (ifcTag in draft) draft[ifcTag].push(geometry)
// else draft[ifcTag] = [geometry]
// })
// }
// ),
// R.map((geoms) => mergeBufferGeometries(geoms)),
// R.filter((bg: BufferGeometry | null): bg is BufferGeometry =>
// Boolean(bg)
// ),
// R.map((x) => x.toJSON())
// )
const speckleObjectData = await getSpeckleObject(speckleBranchUrl)
const speckleObject = speckleIfcParser.parse(speckleObjectData)
const geometries = pipe(
speckleObject,
A.reduce(
{},
(acc: { [e: string]: BufferGeometry[] }, { ifcTag, geometry }) => {
return produce(acc, (draft) => {
if (ifcTag in draft) draft[ifcTag].push(geometry)
else draft[ifcTag] = [geometry]
})
}
),
R.map((geoms) => mergeBufferGeometries(geoms)),
R.filter((bg: BufferGeometry | null): bg is BufferGeometry =>
Boolean(bg)
),
R.map((x) => x.toJSON())
)

systemsDB.models.put({ speckleBranchUrl, lastFetched, geometries })
})
Expand Down

0 comments on commit b1e3229

Please sign in to comment.