sw.mx 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  // RespondWithNetworkFirst tries network, falls back to cache.
  51  // Updates cache on successful fetch. Use for code files that change on rebuild.
  52  func RespondWithNetworkFirst(event Event) { panic("jsbridge") }
  53  
  54  // GetRequestURL returns the request URL from a fetch event.
  55  func GetRequestURL(event Event) string { panic("jsbridge") }
  56  
  57  // GetRequestPath returns just the pathname from a fetch event.
  58  func GetRequestPath(event Event) string { panic("jsbridge") }
  59  
  60  // GetMessageData returns the string data from a message event.
  61  // If data is not a string, returns JSON.stringify(data).
  62  func GetMessageData(event Event) string { panic("jsbridge") }
  63  
  64  // GetMessageClientID returns the source client ID from a message event.
  65  func GetMessageClientID(event Event) string { panic("jsbridge") }
  66  
  67  // --- SW globals ---
  68  
  69  // Origin returns self.location.origin (e.g. "https://example.com").
  70  func Origin() string { panic("jsbridge") }
  71  
  72  // SkipWaiting forces the waiting service worker to become active.
  73  func SkipWaiting() { panic("jsbridge") }
  74  
  75  // ClaimClients takes control of all clients. Calls done when complete.
  76  func ClaimClients(done func()) { panic("jsbridge") }
  77  
  78  // MatchClients gets all window clients. Calls fn with each client.
  79  func MatchClients(fn func(Client)) { panic("jsbridge") }
  80  
  81  // PostMessage sends a string message to a client.
  82  func PostMessage(client Client, msg string) { panic("jsbridge") }
  83  
  84  // PostMessageJSON sends a raw JSON message to a client (parsed by receiver).
  85  func PostMessageJSON(client Client, json string) { panic("jsbridge") }
  86  
  87  // GetClientByID looks up a client by ID. Calls fn with (client, true) or (0, false).
  88  func GetClientByID(id string, fn func(Client, bool)) { panic("jsbridge") }
  89  
  90  // Navigate navigates a client to a URL.
  91  func Navigate(client Client, url string) { panic("jsbridge") }
  92  
  93  // --- Cache ---
  94  
  95  // CacheOpen opens a named cache. Calls fn with the cache handle.
  96  func CacheOpen(name string, fn func(Cache)) { panic("jsbridge") }
  97  
  98  // CacheAddAll caches all the given URLs. Calls done when complete.
  99  func CacheAddAll(cache Cache, urls []string, done func()) { panic("jsbridge") }
 100  
 101  // CacheFromManifests fetches the app and SW build manifests, combines
 102  // them with the given static files, and caches everything. Calls done when complete.
 103  func CacheFromManifests(cache Cache, staticFiles []string, done func()) { panic("jsbridge") }
 104  
 105  // CachePut stores a response in the cache. Calls done when complete.
 106  func CachePut(cache Cache, url string, resp Response, done func()) { panic("jsbridge") }
 107  
 108  // CacheMatch looks up a URL in all caches. Calls fn with response (0 if miss).
 109  func CacheMatch(url string, fn func(Response)) { panic("jsbridge") }
 110  
 111  // CacheDelete deletes a named cache. Calls done when complete.
 112  func CacheDelete(name string, done func()) { panic("jsbridge") }
 113  
 114  // --- Fetch ---
 115  
 116  // Fetch fetches a URL. Calls fn with (response, ok).
 117  func Fetch(url string, fn func(Response, bool)) { panic("jsbridge") }
 118  
 119  // FetchAll fetches multiple URLs in parallel.
 120  // Calls onEach(index, response, ok) for each completed fetch.
 121  // Calls onDone() when all fetches have completed.
 122  func FetchAll(urls []string, onEach func(int, Response, bool), onDone func()) { panic("jsbridge") }
 123  
 124  // ResponseOK returns whether the response status is 200-299.
 125  func ResponseOK(resp Response) bool { panic("jsbridge") }
 126  
 127  // --- SSE ---
 128  
 129  // SSEConnect opens an EventSource. Calls onMessage with each data string.
 130  func SSEConnect(url string, onMessage func(string)) SSE { panic("jsbridge") }
 131  
 132  // SSEClose closes an EventSource.
 133  func SSEClose(id SSE) { panic("jsbridge") }
 134  
 135  // --- Timers ---
 136  
 137  // Timer is an opaque handle to a setTimeout/setInterval.
 138  type Timer int
 139  
 140  // SetTimeout calls fn after ms milliseconds. Returns timer handle.
 141  func SetTimeout(ms int, fn func()) Timer { panic("jsbridge") }
 142  
 143  // ClearTimeout cancels a timer.
 144  func ClearTimeout(t Timer) { panic("jsbridge") }
 145  
 146  // --- Time ---
 147  
 148  // NowSeconds returns current time as Unix seconds.
 149  func NowSeconds() int64 { panic("jsbridge") }
 150  
 151  // NowMillis returns current time as Unix milliseconds.
 152  func NowMillis() int64 { panic("jsbridge") }
 153  
 154  // --- Logging ---
 155  
 156  // Log writes to console.log in the SW context.
 157  func Log(msg string) { panic("jsbridge") }
 158