11import type { SysCallMapping } from "../system.ts" ;
22import type { Client } from "../../client.ts" ;
33
4- // TODO: Reimplement this
54export function syncSyscalls ( client : Client ) : SysCallMapping {
65 return {
76 "sync.hasInitialSyncCompleted" : ( ) : boolean => {
87 return client . fullSyncCompleted ;
98 } ,
109 "sync.performFileSync" : async ( _ctx , path : string ) : Promise < void > => {
11- try {
12- await client . postServiceWorkerMessage ( {
13- type : "perform-file-sync" ,
14- path,
15- } ) ;
16- } catch ( e : any ) {
17- console . warn (
18- "No service worker available, so sync is inactive" ,
19- e . message ,
20- ) ;
21- return ;
10+ await client . postServiceWorkerMessage ( {
11+ type : "perform-file-sync" ,
12+ path,
13+ } ) ;
14+ // postServiceWorkerMessage returns silently if no SW, so only wait if SW is active
15+ const registration = await navigator . serviceWorker . getRegistration ( ) ;
16+ if ( registration ?. active ) {
17+ return waitForServiceWorkerActivation ( client , path ) ;
2218 }
23- return waitForServiceWorkerActivation ( path ) ;
2419 } ,
2520 "sync.performSpaceSync" : async ( ) : Promise < number > => {
26- try {
27- await client . postServiceWorkerMessage ( {
28- type : "perform-space-sync" ,
29- } ) ;
30- } catch ( e : any ) {
31- console . warn ( "No service worker available, so sync is inactive" , e ) ;
32- return 0 ;
21+ await client . postServiceWorkerMessage ( { type : "perform-space-sync" } ) ;
22+ const registration = await navigator . serviceWorker . getRegistration ( ) ;
23+ if ( registration ?. active ) {
24+ return waitForServiceWorkerActivation ( client ) ;
3325 }
34- return waitForServiceWorkerActivation ( ) ;
26+ return 0 ;
3527 } ,
3628 } ;
3729}
3830
39- function waitForServiceWorkerActivation ( path ?: string ) : Promise < any > {
31+ function waitForServiceWorkerActivation (
32+ client : Client ,
33+ path ?: string ,
34+ ) : Promise < any > {
4035 return new Promise < any > ( ( resolve , reject ) => {
4136 client . eventHook . addLocalListener (
4237 "service-worker:file-sync-complete" ,
@@ -51,33 +46,17 @@ function waitForServiceWorkerActivation(path?: string): Promise<any> {
5146 errorHandler ,
5247 ) ;
5348 function eventHandler ( data : any ) {
54- // If data.path is set, we are notified about a specific file sync -> all good, even for an individual file sync
55- // If data.path is not set, we are notified about a full space sync
56- // If we were waiting for a specific path, ignore other paths
5749 if ( data . path && path && data . path !== path ) {
58- // Event for other file sync
5950 return ;
6051 }
61- // If we were waiting for a specific path, ignore other paths
6252 resolve ( data ) ;
63-
64- // Unsubscribe from all these events
65- client . eventHook . removeLocalListener (
66- "service-worker:file-sync-complete" ,
67- eventHandler ,
68- ) ;
69- client . eventHook . removeLocalListener (
70- "service-worker:space-sync-complete" ,
71- eventHandler ,
72- ) ;
73- client . eventHook . removeLocalListener (
74- "service-worker:sync-error" ,
75- errorHandler ,
76- ) ;
53+ cleanup ( ) ;
7754 }
7855 function errorHandler ( e : any ) {
7956 reject ( e ) ;
80- // Unsubscribe from all these events
57+ cleanup ( ) ;
58+ }
59+ function cleanup ( ) {
8160 client . eventHook . removeLocalListener (
8261 "service-worker:file-sync-complete" ,
8362 eventHandler ,
0 commit comments