@@ -15,6 +15,7 @@ import { setUser, setContext, withScope, captureException } from '@sentry/browse
1515import AsyncLock from 'async-lock'
1616import CachingTreeWrapper from './CachingTreeWrapper'
1717import { UnexpectedFolderPathError } from '../errors/Error'
18+ import { createOffscreen , destroyOffscreen } from './offscreen'
1819
1920declare const DEBUG : boolean
2021
@@ -84,6 +85,7 @@ export default class Account {
8485 protected localTabs : TLocalTree
8586 protected lockTimeout : number
8687
88+ private offscreenPingInterval : any = null
8789 private localCachingResource : CachingTreeWrapper
8890
8991 constructor ( id :string , storageAdapter :IAccountStorage , serverAdapter : TAdapter , treeAdapter :TLocalTree ) {
@@ -191,6 +193,16 @@ export default class Account {
191193 if ( oldPath && newPath !== oldPath ) {
192194 throw new UnexpectedFolderPathError ( oldPath , newPath )
193195 }
196+
197+ // eslint-disable-next-line no-undef
198+ if ( self . constructor . name === 'ServiceWorkerGlobalScope' || ( typeof chrome !== 'undefined' && 'offscreen' in chrome ) ) {
199+ // Create an offscreen page in chrome and ping it regularly to prevent this worker from getting killed
200+ await createOffscreen ( )
201+ this . offscreenPingInterval = setInterval ( ( ) => {
202+ // eslint-disable-next-line no-undef
203+ chrome . runtime . sendMessage ( { type : 'sync-progress' } )
204+ } , 20000 )
205+ }
194206 }
195207
196208 if ( this . server . onSyncStart ) {
@@ -389,6 +401,12 @@ export default class Account {
389401 await this . init ( )
390402 }
391403 }
404+ // eslint-disable-next-line no-undef
405+ if ( self . constructor . name === 'ServiceWorkerGlobalScope' || ( typeof chrome !== 'undefined' && 'offscreen' in chrome ) ) {
406+ // We destroy the offscreen page when the sync is done to allow the worker to be killed
407+ await destroyOffscreen ( )
408+ }
409+ clearInterval ( this . offscreenPingInterval )
392410 this . syncProcess = null
393411 this . localCachingResource = null
394412 await Logger . persist ( )
@@ -422,6 +440,10 @@ export default class Account {
422440 if ( ! this . syncProcess ) {
423441 return
424442 }
443+ if ( self . constructor . name === 'ServiceWorkerGlobalScope' ) {
444+ // eslint-disable-next-line no-undef
445+ chrome . runtime . sendMessage ( { type : 'sync-progress' } )
446+ }
425447 if ( actionsDone ) {
426448 if ( this . server . isAtomic ( ) ) {
427449 const cache = ( await this . localCachingResource . getCacheTree ( ) ) . clone ( false )
0 commit comments