当 indexedDB 条目在其他选项卡中发生更改时收到通知

Get notified when indexedDB entry gets changed in other tab

如果用户在多个选项卡中打开网络应用程序的多个实例,任何实例都可以通过注册 storage 事件侦听器来通知任何其他实例对 localStorage 所做的更改。收到此类通知的选项卡可能会做出反应,例如根据更改更新 UI。

Indexed DB 似乎没有为此类通知提供类似的便利机制。每当对 Indexed DB 进行更改时,是否必须使用 localStorage 到 "send" 向其他选项卡发送通知?唯一的其他选择可能是轮询,这显然不如 storage 事件。

没有为索引数据库定义当前 "observer" API。但是,它在功能请求列表中,Mozilla 和 Google.

也有类似的提议

https://github.com/w3c/IndexedDB/issues/51

也就是说:是的,你需要想出一些自定义的跨表通信机制,比如轮询,存储事件,设置MessageChannel链接(Chrome),BroadcastChannel(Firefox),或者使用 Service Worker 作为客户端之间的中继。


快讯!

Chrome 有一个实验性的 Indexed DB Observers API. You need to run chrome with --enable-experimental-web-platform-features to use it so it's not useful in production yet. We'd love to get feedback on it - file bugs on the github repo.

更新

  1. Dexie 现在有一个 Observables 插件,以防您开始一个新项目:https://dexie.org/docs/Observable/Dexie.Observable
  2. Oleg 对 Ionic 存储包装器进行了深入研究。但这可以为任何本地存储库创建。固体工程。 https://medium.com/@OlegVaraksin/how-to-make-ionic-storage-reactive-acdd8996f1e6

Dexie 3.2 具有内置的可观察性(没有 Dexie.Observable)。它使用 BroadcastChannel 跨浏览上下文传达更改。它只能观察使用 Dexie>=3.2 所做的更改(因为仍然没有稳定的本机 API 可以执行此操作)。