sw.go raw

   1  package sw
   2  
   3  // Service Worker jsbridge — lifecycle, cache, clients, fetch, SSE.
   4  
   5  // Event is an opaque handle to a service worker event.
   6  type Event int
   7  
   8  // Client is an opaque handle to a service worker client (window/tab).
   9  type Client int
  10  
  11  // Cache is an opaque handle to a CacheStorage cache.
  12  type Cache int
  13  
  14  // Response is an opaque handle to a fetch Response.
  15  type Response int
  16  
  17  // SSE is an opaque handle to an EventSource connection.
  18  type SSE int
  19  
  20  // --- Lifecycle ---
  21  
  22  // OnInstall registers the install handler. fn receives the event handle.
  23  func OnInstall(fn func(Event)) { panic("jsbridge") }
  24  
  25  // OnActivate registers the activate handler.
  26  func OnActivate(fn func(Event)) { panic("jsbridge") }
  27  
  28  // OnFetch registers the fetch handler.
  29  func OnFetch(fn func(Event)) { panic("jsbridge") }
  30  
  31  // OnMessage registers the message handler.
  32  func OnMessage(fn func(Event)) { panic("jsbridge") }
  33  
  34  // --- Event methods ---
  35  
  36  // WaitUntil extends the event lifetime until done is called.
  37  // Call done() when your async work is complete.
  38  func WaitUntil(event Event, fn func(done func())) { panic("jsbridge") }
  39  
  40  // RespondWith provides a response for a fetch event.
  41  func RespondWith(event Event, resp Response) { panic("jsbridge") }
  42  
  43  // RespondWithNetwork lets the fetch fall through to the network.
  44  func RespondWithNetwork(event Event) { panic("jsbridge") }
  45  
  46  // RespondWithCacheFirst tries cache, falls back to network.
  47  // Must be called synchronously from the fetch handler.
  48  func RespondWithCacheFirst(event Event) { panic("jsbridge") }
  49  
  50  // GetRequestURL returns the request URL from a fetch event.
  51  func GetRequestURL(event Event) string { panic("jsbridge") }
  52  
  53  // GetRequestPath returns just the pathname from a fetch event.
  54  func GetRequestPath(event Event) string { panic("jsbridge") }
  55  
  56  // GetMessageData returns the string data from a message event.
  57  // If data is not a string, returns JSON.stringify(data).
  58  func GetMessageData(event Event) string { panic("jsbridge") }
  59  
  60  // GetMessageClientID returns the source client ID from a message event.
  61  func GetMessageClientID(event Event) string { panic("jsbridge") }
  62  
  63  // --- SW globals ---
  64  
  65  // Origin returns self.location.origin (e.g. "https://example.com").
  66  func Origin() string { panic("jsbridge") }
  67  
  68  // SkipWaiting forces the waiting service worker to become active.
  69  func SkipWaiting() { panic("jsbridge") }
  70  
  71  // ClaimClients takes control of all clients. Calls done when complete.
  72  func ClaimClients(done func()) { panic("jsbridge") }
  73  
  74  // MatchClients gets all window clients. Calls fn with each client.
  75  func MatchClients(fn func(Client)) { panic("jsbridge") }
  76  
  77  // PostMessage sends a string message to a client.
  78  func PostMessage(client Client, msg string) { panic("jsbridge") }
  79  
  80  // PostMessageJSON sends a raw JSON message to a client (parsed by receiver).
  81  func PostMessageJSON(client Client, json string) { panic("jsbridge") }
  82  
  83  // GetClientByID looks up a client by ID. Calls fn with (client, true) or (0, false).
  84  func GetClientByID(id string, fn func(Client, bool)) { panic("jsbridge") }
  85  
  86  // Navigate navigates a client to a URL.
  87  func Navigate(client Client, url string) { panic("jsbridge") }
  88  
  89  // --- Cache ---
  90  
  91  // CacheOpen opens a named cache. Calls fn with the cache handle.
  92  func CacheOpen(name string, fn func(Cache)) { panic("jsbridge") }
  93  
  94  // CacheAddAll caches all the given URLs. Calls done when complete.
  95  func CacheAddAll(cache Cache, urls []string, done func()) { panic("jsbridge") }
  96  
  97  // CachePut stores a response in the cache. Calls done when complete.
  98  func CachePut(cache Cache, url string, resp Response, done func()) { panic("jsbridge") }
  99  
 100  // CacheMatch looks up a URL in all caches. Calls fn with response (0 if miss).
 101  func CacheMatch(url string, fn func(Response)) { panic("jsbridge") }
 102  
 103  // CacheDelete deletes a named cache. Calls done when complete.
 104  func CacheDelete(name string, done func()) { panic("jsbridge") }
 105  
 106  // --- Fetch ---
 107  
 108  // Fetch fetches a URL. Calls fn with (response, ok).
 109  func Fetch(url string, fn func(Response, bool)) { panic("jsbridge") }
 110  
 111  // FetchAll fetches multiple URLs in parallel.
 112  // Calls onEach(index, response, ok) for each completed fetch.
 113  // Calls onDone() when all fetches have completed.
 114  func FetchAll(urls []string, onEach func(int, Response, bool), onDone func()) { panic("jsbridge") }
 115  
 116  // ResponseOK returns whether the response status is 200-299.
 117  func ResponseOK(resp Response) bool { panic("jsbridge") }
 118  
 119  // --- SSE ---
 120  
 121  // SSEConnect opens an EventSource. Calls onMessage with each data string.
 122  func SSEConnect(url string, onMessage func(string)) SSE { panic("jsbridge") }
 123  
 124  // SSEClose closes an EventSource.
 125  func SSEClose(id SSE) { panic("jsbridge") }
 126  
 127  // --- Timers ---
 128  
 129  // Timer is an opaque handle to a setTimeout/setInterval.
 130  type Timer int
 131  
 132  // SetTimeout calls fn after ms milliseconds. Returns timer handle.
 133  func SetTimeout(ms int, fn func()) Timer { panic("jsbridge") }
 134  
 135  // ClearTimeout cancels a timer.
 136  func ClearTimeout(t Timer) { panic("jsbridge") }
 137  
 138  // --- Time ---
 139  
 140  // NowSeconds returns current time as Unix seconds.
 141  func NowSeconds() int64 { panic("jsbridge") }
 142  
 143  // NowMillis returns current time as Unix milliseconds.
 144  func NowMillis() int64 { panic("jsbridge") }
 145  
 146  // --- Logging ---
 147  
 148  // Log writes to console.log in the SW context.
 149  func Log(msg string) { panic("jsbridge") }
 150