JS 的基于主题的发布/订阅

发布于 2024-12-01 03:35:40 字数 308 浏览 0 评论 0原文

我希望能够拥有类似于 jQuery 自定义事件和 PubSubJS (http://github.com/mroderick/PubSubJS) 中的 pubsub 机制。

问题是这些 pubsub 库中的每一个都在主题上进行了完全匹配。 IO 希望能够发布这样的主题:

"Order/Sent/1234"

并收听订阅:

"Order/Sent/1234"
"Order/Sent/*"
"Order/*/1234"

有谁知道 JS 有这样的东西吗?

I want to be able to have a pubsub mechanism similar to found in jQuery's custom events and PubSubJS (http://github.com/mroderick/PubSubJS).

The problem is that each one of these pubsub libraries does an exact match on the subject. IO want to be able to publish a subject like:

"Order/Sent/1234"

And have a listen subscribe to either:

"Order/Sent/1234"
"Order/Sent/*"
"Order/*/1234"

Does anyone know of anything like this for JS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回梦里 2024-12-08 03:35:40

修改你喜欢的就可以了。在 github 上分叉它,打开 pubsub.js 并执行以下操作:

var deliverMessage = function(){
    var subscribers = [];

    for(var n in messages){
        if( new RegExp('^' + n + '

您可能需要对其进行一些修改,例如将所有 * 替换为 < code>.* 或 [^\/]* 在转换为正则表达式之前,等等...

).test( message ) ){ subscribers = subscribers.concat( messages[n] ); } ... } }

您可能需要对其进行一些修改,例如将所有 * 替换为 < code>.* 或 [^\/]* 在转换为正则表达式之前,等等...

Just modify the one you like. Fork it on github, go open pubsub.js and do something like:

var deliverMessage = function(){
    var subscribers = [];

    for(var n in messages){
        if( new RegExp('^' + n + '

You'll probably need to modify that a bit, do something like replace all *'s with .* or [^\/]* before converting to a regex, etc...

).test( message ) ){ subscribers = subscribers.concat( messages[n] ); } ... } }

You'll probably need to modify that a bit, do something like replace all *'s with .* or [^\/]* before converting to a regex, etc...

十雾 2024-12-08 03:35:40
    // Paytm's turbo churged Publish - Subscribe Library

export const PaytmConnect = (() => {
  const topics = {};

  return {
    subscribe: (topic, listener) => {
      // Create the topic's object if not yet created
      if (!topics.hasOwnProperty(topic)) topics[topic] = [];

      // Add the listener to queue
      const index = topics[topic].push(listener) - 1;

      // Provide handle back for removal of topic
      return {
        remove: () => {
          delete topics[topic][index];
        }
      };
    },
    publish: (topic, info) => {
      // If the topic doesn't exist, or there's no listeners in queue, just leave
      if (!topics.hasOwnProperty(topic)) return;
      const allProperty = Object.getOwnPropertyNames(topic);
      allProperty.forEach((property) => {
        if (property.match(topic)) {
          // Cycle through topics queue, fire!
          topics[topic].forEach((item) => {
            item(info !== undefined ? info : {});
          });
        }
      });
    }
  };
})();

您必须稍微修改代码 if (property.match(topic)) { 以满足您的要求。

    // Paytm's turbo churged Publish - Subscribe Library

export const PaytmConnect = (() => {
  const topics = {};

  return {
    subscribe: (topic, listener) => {
      // Create the topic's object if not yet created
      if (!topics.hasOwnProperty(topic)) topics[topic] = [];

      // Add the listener to queue
      const index = topics[topic].push(listener) - 1;

      // Provide handle back for removal of topic
      return {
        remove: () => {
          delete topics[topic][index];
        }
      };
    },
    publish: (topic, info) => {
      // If the topic doesn't exist, or there's no listeners in queue, just leave
      if (!topics.hasOwnProperty(topic)) return;
      const allProperty = Object.getOwnPropertyNames(topic);
      allProperty.forEach((property) => {
        if (property.match(topic)) {
          // Cycle through topics queue, fire!
          topics[topic].forEach((item) => {
            item(info !== undefined ? info : {});
          });
        }
      });
    }
  };
})();

You will have to modify the code if (property.match(topic)) { a bit to satisfy your requirement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文