pixiebrix-extension icon indicating copy to clipboard operation
pixiebrix-extension copied to clipboard

Move background state to in-memory storage

Open twschiller opened this issue 3 years ago • 0 comments

Context

  • The background service worker will be recycled every ~5ish minutes
  • In Chrome 102: Chrome introduced an in-memory storage (that's faster than localStorage) to accommodate saving state across service worker recycling: https://developer.chrome.com/docs/extensions/reference/storage/#property-session. It's only available in MV3 extensions

Implementation Sketch

  • [ ] Identify files in the background/ folder that defined module level mutable state
  • [ ] Introduce a chrome.storage session polyfill for MV2 (or see if one already exists). The Polyfill should use module-level state. (I.e., we shouldn't use local storage for the polyfill)
  • [ ] Incrementally shift uses of background state to use the polyfill. NOTE: should probably be done as a sequence of PRs instead of just one large PR

Discussions/Considerations

  • I think this forces all of our state updates/fetches to be asynchronous
  • We may want to have our polyfill check for serializability in debug mode
  • Random idea: how hard would it be to introduce a lint rule to ban module-level variables in background pages? Might be hard for linter to distinguish constants vs variables holding mutable state

Related Issues

  • https://github.com/pixiebrix/pixiebrix-extension/issues/287

twschiller avatar Sep 26 '22 15:09 twschiller