-
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.
Merge pull request #1 from joebobmiles/feat/only-one-provider-of-type
Providers are unique per document
- Loading branch information
Showing
9 changed files
with
136 additions
and
25 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
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
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,2 +1,2 @@ | ||
export { DocumentProvider } from './component' | ||
export { useDoc } from './hook' | ||
export { useDoc, useProviders } from './hook' |
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,8 @@ | ||
import { IndexeddbPersistence } from 'y-indexeddb' | ||
import { WebrtcProvider } from 'y-webrtc' | ||
import { WebsocketProvider } from 'y-websocket' | ||
|
||
export type Provider = | ||
| WebrtcProvider | ||
| WebsocketProvider | ||
| IndexeddbPersistence |
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
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 { WebrtcProvider } from 'y-webrtc' | ||
|
||
import { useDoc } from '../../doc' | ||
import { useDoc, useProviders } from '../../doc' | ||
|
||
export const useWebRtc = (room: string): WebrtcProvider => { | ||
const doc = useDoc() | ||
const providers = useProviders() | ||
|
||
return React.useMemo( | ||
() => new WebrtcProvider(room, doc), | ||
[room] | ||
const existingProvider = React.useMemo( | ||
() => | ||
Array.from(providers.values()) | ||
.find((provider): provider is WebrtcProvider => | ||
provider instanceof WebrtcProvider && provider.roomName === room | ||
), | ||
[providers, room] | ||
) | ||
|
||
if (existingProvider !== undefined) { | ||
return existingProvider | ||
} else { | ||
const provider = React.useMemo( | ||
() => new WebrtcProvider(room, doc), | ||
[doc, room] | ||
) | ||
|
||
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 | ||
} | ||
} |