JavaScript 对象之间的发布订阅通信的同步与异步
我在 JavaScript 模块中实现了发布订阅模式,以供完全面向客户端的 JavaScript 对象使用。该模块与任何方式、形状或形式的客户端-服务器通信无关。
我的问题是这样的模块中的发布方法是同步还是异步更好,为什么。
作为一个非常简单的示例,假设我正在为 HTML5 视频播放器小部件构建自定义 UI:
我的模块之一是“视频”模块,它包含 VIDEO 元素并处理与该元素关联的各种功能和事件。这可能有一个类似于“widgets.player.video”的命名空间。
另一个是“controls”模块,它有各种按钮 - 播放、暂停、音量、擦洗、全屏等。这可能有一个类似于“widgets.player.controls”的命名空间。
这两个模块是父“player”模块(“widgets.player”??)的子模块,因此当实例化为“player”对象的子模块时,彼此之间没有固有的知识。
“controls”元素显然需要能够对视频进行一些更改(单击“Play”并且视频应该播放),反之亦然(视频的“timeUpdate”事件触发并且当前时间的视觉显示在控件应该更新)。
我可以紧密耦合这些模块并相互传递引用,但我宁愿采用更松散耦合的方法,设置一个既可以订阅也可以发布的 pubsub 类型模块。
SO(感谢您耐心等待)在这种情况下,同步发布与异步发布相比是否有某种优势?我在网上看到一些解决方案允许使用布尔标志,而其他解决方案则自动异步执行。我个人还没有看到一个自动同步发布的实现......这是因为它没有优势吗?
我知道我可以使用 jQuery 提供的功能来完成此任务,但似乎这里可能涉及太多开销。发布订阅模式可以使用专门为此特定目的而设计的相对轻量级的代码来实现,因此我宁愿使用它,然后使用像 jQuery 这样的更通用的事件系统(我将使用它来满足更一般的事件需求:-)。
I implemented the publish subscribe pattern in a JavaScript module to be used by entirely client-side oriented JavaScript objects. This module has nothing to do with client-server communications in any way, shape or form.
My question is whether it's better for the publish method in such a module to be synchronous or asynchronous, and why.
As a very simplified example let's say I'm building a custom UI for an HTML5 video player widget:
One of my modules is the "video" module that contains the VIDEO element and handles the various features and events associated with that element. This would probably have a namespace something like "widgets.player.video."
Another is the "controls" module that has the various buttons - play, pause, volume, scrub, fullscreen, etc. This might have a namespace along the lines of "widgets.player.controls."
These two modules are children of a parent "player" module ("widgets.player" ??), and as such would have no inherent knowledge of each other when instantiated as children of the "player" object.
The "controls" elements would obviously need to be able to effect some changes on the video (click "Play" and the video should play), and vice versa (video's "timeUpdate" event fires and the visual display of the current time in the controls should update).
I could tightly couple these modules and pass references to each other, but I'd rather take a more loosely coupled approach by setting up a pubsub type module that both can subscribe to and publish from.
SO (thanks for bearing with me) in this kind of a scenario is there an advantage one way or another for synchronous publication versus asynchronous publication? I've seen some solutions posted online that allow for either/or with a boolean flag whereas others automatically do it asynchronously. I haven't personally seen an implementation that just automatically goes with synchronous publication... is this because there's no advantage to it?
I know that I can accomplish this with features provided by jQuery, but it seems that there may be too much overhead involved here. The publish subscribe pattern can be implemented with relatively lightweight code designed specifically for this particular purpose so I'd rather go with that then a more general purpose eventing system like jQuery's (which I'll use for more general eventing needs :-).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论