-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
71 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@verdant-web/store': patch | ||
--- | ||
|
||
Experimental support for periodic background sync |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export async function attemptToRegisterBackgroundSync() { | ||
try { | ||
const status = await navigator.permissions.query({ | ||
name: 'periodic-background-sync' as any, | ||
}); | ||
if (status.state === 'granted') { | ||
// Periodic background sync can be used. | ||
const registration = await navigator.serviceWorker.ready; | ||
if ('periodicSync' in registration) { | ||
try { | ||
await (registration.periodicSync as any).register('verdant-sync', { | ||
// An interval of one day. | ||
minInterval: 24 * 60 * 60 * 1000, | ||
}); | ||
} catch (error) { | ||
// Periodic background sync cannot be used. | ||
console.warn('Failed to register background sync:', error); | ||
} | ||
} | ||
} else { | ||
// Periodic background sync cannot be used. | ||
console.debug('Background sync permission is not granted:', status); | ||
} | ||
} catch (error) { | ||
console.error('Failed to initiate background sync:', error); | ||
} | ||
} |
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,27 @@ | ||
import type { ClientDescriptor } from '../client/ClientDescriptor.js'; | ||
|
||
export async function registerBackgroundSync(clientDesc: ClientDescriptor) { | ||
self.addEventListener('periodicsync', (event: any) => { | ||
if (event.tag === 'verdant-sync') { | ||
// See the "Think before you sync" section for | ||
// checks you could perform before syncing. | ||
event.waitUntil(sync(clientDesc)); | ||
} | ||
}); | ||
} | ||
|
||
async function sync(clientDesc: ClientDescriptor) { | ||
try { | ||
const client = await clientDesc.open(); | ||
|
||
await client.sync.syncOnce(); | ||
} catch (err) { | ||
console.error('Failed to sync:', err); | ||
if (err instanceof Error) { | ||
localStorage.setItem( | ||
'backgroundSyncError', | ||
`${err.name}: ${err.message}`, | ||
); | ||
} | ||
} | ||
} |