-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for read-only token functionality
- Loading branch information
Showing
2 changed files
with
115 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,35 @@ | ||
import { EVENT_LOCAL_CHANGES } from '@y-sweet/client' | ||
import { YSweetProvider } from '@y-sweet/react' | ||
|
||
export async function waitForProviderSync(provider: YSweetProvider, timeoutMillis: number = 1_000) { | ||
return new Promise<void>((resolve, reject) => { | ||
provider.on('sync', (synced) => { | ||
function onSync(synced: boolean) { | ||
if (synced) { | ||
resolve() | ||
} | ||
}) | ||
} | ||
provider.once('sync', onSync) | ||
setTimeout(() => { | ||
provider.off('sync', onSync) | ||
reject('Timed out waiting for provider to sync.') | ||
}, timeoutMillis) | ||
}) | ||
} | ||
|
||
setTimeout(() => reject('Timed out waiting for provider to sync.'), timeoutMillis) | ||
export async function waitForProviderSyncChanges( | ||
provider: YSweetProvider, | ||
timeoutMillis: number = 1_000, | ||
) { | ||
return new Promise<void>((resolve, reject) => { | ||
// wait for local changes to be false, meaning that the provider has synced its changes to the server | ||
function onLocalChanges(hasLocalChanges: boolean) { | ||
if (!hasLocalChanges) { | ||
resolve() | ||
} | ||
} | ||
provider.on('local-changes', onLocalChanges) | ||
setTimeout(() => { | ||
provider.off('local-changes', onLocalChanges) | ||
reject('Timed out waiting for provider to have local changes.') | ||
}, timeoutMillis) | ||
}) | ||
} |