Firefox 扩展开发:观察偏好,避免多重通知
假设我的 Firefox 扩展有多个首选项,但其中一些首选项是分组的,例如检查间隔、失败重试间隔、目标 url。这些仅用于单一功能。
当我订阅偏好服务并添加观察者时,将为每个更改的偏好调用 observe
回调,因此如果用户偶然更改了组中的所有设置,那么我将不得不执行相同的例程对于同一个子系统,我可以多次使用该首选项组中的项目。这是一个很大的冗余!
我想要的是 observe
对于一组首选项仅调用一次。也就是说
extensions.myextension.interval1
extensions.myextension.site
extensions.myextension.retry
,如果其中一项或全部首选项发生更改,我只会收到 1 条相关通知。换句话说,无论分支中有多少首选项发生变化,我都希望调用一次observe回调。
Let's say my Firefox extension has multiple preferences, but some of them are grouped, like check interval, fail retry interval, destination url. Those are used in just single function.
When I subscribe to preference service and add observer, the observe
callback will be called for each changed preference, so if by chance user changed all of the settings in group, then I will have to do the same routine for the same subsystem as many times as I have items in that preferences group. It's a great redundancy!
What I want is observe
to be called just once for group of preferences. Say
extensions.myextension.interval1
extensions.myextension.site
extensions.myextension.retry
so if one or all of those preferences are changed, I receive only 1 notification about it. In other words, no matter how many preferences changed in branch, I want the observe
callback to called once.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在分支上注册偏好观察者时(在您的情况下为extensions.myextension),它会通知您每个偏好更改。没有办法让它只告诉您一次有关分支上的更改的信息。当您的观察方法被调用时,您确实会获得更改的首选项的名称(它是第三个参数)。您必须根据此过滤您的回调。
When you register a pref observer on a branch (in your case, extensions.myextension), it will notify you about each preference change. There is no way to get it to tell you only once about a change on a branch. When your observe method is called, you do get the name of the preference that changes (it's the third parameter). You will have to filter your callbacks based on that.