-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: all providers are now singletons
- Loading branch information
1 parent
938636d
commit 57480f4
Showing
2 changed files
with
45 additions
and
12 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 |
---|---|---|
@@ -1,13 +1,31 @@ | ||
import React from 'react' | ||
import { IndexeddbPersistence } from 'y-indexeddb' | ||
import { useDoc } from '../../doc' | ||
|
||
import { useDoc, useProviders } from '../../doc' | ||
|
||
export const useIndexedDb = (name: string): IndexeddbPersistence => { | ||
const doc = useDoc() | ||
const providers = useProviders() | ||
|
||
return React.useMemo( | ||
const existingProvider = React.useMemo( | ||
() => | ||
new IndexeddbPersistence(name, doc), | ||
[name] | ||
Array.from(providers.values()) | ||
.find((provider): provider is IndexeddbPersistence => | ||
provider instanceof IndexeddbPersistence && provider.db?.name === name | ||
), | ||
[providers, name] | ||
) | ||
|
||
if (existingProvider !== undefined) { | ||
return existingProvider | ||
} else { | ||
const provider = React.useMemo( | ||
() => new IndexeddbPersistence(name, doc), | ||
[doc, name] | ||
) | ||
|
||
providers.add(provider) | ||
|
||
return provider | ||
} | ||
} |
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,21 +1,36 @@ | ||
import React from 'react' | ||
import { WebsocketProvider } from 'y-websocket' | ||
|
||
import { useDoc } from '../../doc' | ||
import { useDoc, useProviders } from '../../doc' | ||
|
||
export const useWebSocket = ( | ||
url: string, | ||
room: string | ||
): WebsocketProvider => { | ||
const doc = useDoc() | ||
const providers = useProviders() | ||
|
||
return React.useMemo( | ||
const existingProvider = React.useMemo( | ||
() => | ||
new WebsocketProvider( | ||
url, | ||
room, | ||
doc | ||
), | ||
[url, room] | ||
Array.from(providers.values()) | ||
.find((provider): provider is WebsocketProvider => | ||
provider instanceof WebsocketProvider && | ||
provider.url === url && | ||
provider.roomname === room | ||
), | ||
[providers, url, room] | ||
) | ||
|
||
if (existingProvider !== undefined) { | ||
return existingProvider | ||
} else { | ||
const provider = React.useMemo( | ||
() => new WebsocketProvider(url, room, doc), | ||
[doc, url, room] | ||
) | ||
|
||
providers.add(provider) | ||
|
||
return provider | ||
} | ||
} |