Skip to content

Commit

Permalink
use useIsLocked everywher
Browse files Browse the repository at this point in the history
  • Loading branch information
jgoux committed Nov 14, 2024
1 parent 9abf0e5 commit 0f0b4a8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
7 changes: 4 additions & 3 deletions apps/postgres-new/app/(main)/db/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import Link from 'next/link'
import { useRouter } from 'next/navigation'
import { useEffect } from 'react'
import { useApp } from '~/components/app-provider'
import { useAcquireLock } from '~/components/lock-provider'
import { useAcquireLock, useIsLocked } from '~/components/lock-provider'
import Workspace from '~/components/workspace'
import NewDatabasePage from '../../page'

export default function Page({ params }: { params: { id: string } }) {
const databaseId = params.id
const router = useRouter()
const { dbManager } = useApp()
const hasAcquiredLock = useAcquireLock(databaseId)
useAcquireLock(databaseId)
const isLocked = useIsLocked(databaseId, true)

useEffect(() => {
async function run() {
Expand All @@ -29,7 +30,7 @@ export default function Page({ params }: { params: { id: string } }) {
run()
}, [dbManager, databaseId, router])

if (!hasAcquiredLock) {
if (isLocked) {
return (
<div className="relative h-full w-full">
<NewDatabasePage />
Expand Down
5 changes: 0 additions & 5 deletions apps/postgres-new/components/lock-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ export function useIsLocked(key: string, excludeSelf = false) {
*/
export function useAcquireLock(key: string) {
const context = useContext(LockContext)
const [hasAcquiredLock, setHasAcquiredLock] = useState(false)

if (!context) {
throw new Error(
Expand Down Expand Up @@ -195,7 +194,6 @@ export function useAcquireLock(key: string) {

broadcastChannel.postMessage({ type: 'acquire', key })
messagePort.postMessage({ type: 'acquire', key })
setHasAcquiredLock(true)
setSelfLocks((locks) => locks.union(new Set([key])))

return new Promise<void>((resolve) => {
Expand All @@ -211,7 +209,6 @@ export function useAcquireLock(key: string) {

broadcastChannel.postMessage({ type: 'release', key })
messagePort.postMessage({ type: 'release', key })
setHasAcquiredLock(false)
setSelfLocks((locks) => locks.difference(new Set([key])))
})
.catch(() => {})
Expand All @@ -230,6 +227,4 @@ export function useAcquireLock(key: string) {
window.removeEventListener('beforeunload', unload)
}
}, [lockName, lockPrefix, broadcastChannel, messagePort, setSelfLocks])

return hasAcquiredLock
}

0 comments on commit 0f0b4a8

Please sign in to comment.