【问题标题】:Publish/Subscribe to notification after published发布后发布/订阅通知
【发布时间】:2011-10-13 00:49:41
【问题描述】:

我正在尝试理解 Observable/Observer 设计模式。

这是我目前拥有的代码(来自 Javascript Patterns book 的代码):

            var publisher = {
            subscribers: {
                any: [] // event type: subscribers
            },
            publications: {
                any: []
            },
            subscribe: function (fn, type) {
                type = type || 'any';
                if (typeof this.subscribers[type] === "undefined") {
                    this.subscribers[type] = [];
                }

                this.subscribers[type].push(fn);

                if(typeof this.publications[type] === "undefined"){
                    return;
                }

                var pubs = this.publications[type],
                i,
                max = pubs.length

                for(i = 0;i<max;i++){
                    this.subscribers[type][i](pubs[i]);
                }

            },
            unsubscribe: function (fn, type) {
                this.visitSubscribers('unsubscribe', fn, type);
            },
            publish: function (publication, type) {
                var pubtype = type || 'any';
                this.visitSubscribers('publish', publication, type);
                if(typeof this.publications[pubtype] === "undefined") {
                    this.publications[pubtype] = [];
                }
                this.publications[pubtype].push(publication);
            },
            visitSubscribers: function (action, arg, type) {
                var pubtype = type || 'any',
                subscribers = this.subscribers[pubtype],
                i,
                max;

                if(typeof subscribers === 'undefined') {
                    return;
                }

                max = subscribers.length;

                for (i = 0; i < max; i += 1) {
                    if (action === 'publish') {
                        subscribers[i](arg);
                    } else {
                        if (subscribers[i] === arg) {
                            subscribers.splice(i, 1);
                        }
                    }
                }
            }
        };

        function makePublisher(o) {
            var i;
            for (i in publisher) {
                if (publisher.hasOwnProperty(i) && typeof publisher[i] === "function") {
                    o[i] = publisher[i];
                }
            }
            o.subscribers = {any: []};
            o.publications = {any: []};
        }

        var paper = {
            daily: function () {
                this.publish("big news today");
            },
            monthly: function () {
                this.publish("interesting analysis", "monthly");
            },
            yearly: function () {
                this.publish("every year","yearly");
            }
        };

        makePublisher(paper);

        var joe = {
            drinkCoffee: function (paper) {
                console.log('Just read ' + paper);
            },
            sundayPreNap: function (monthly) {
                console.log('About to fall asleep reading this ' + monthly);
            },
            onHolidays: function(yearly) {
                console.log('Holidays!'+yearly);
            }
        };


        paper.daily();
        paper.monthly();
        paper.yearly();

        paper.subscribe(joe.drinkCoffee);
        paper.subscribe(joe.onHolidays,'yearly');
        paper.subscribe(joe.sundayPreNap, 'monthly'); 

我想知道是否有可能让客户端接收通知 即使他们在广播此类通知之后注册了自己。

我是否应该修改 publisher.subscribe 并使其检查是否未定义,如果是则发布事件类型?

提前致谢。

*编辑 1 *

我添加了一个发布对象来将发布保存在发布函数中。我还检查是否有发布类型的订阅者,如果没有,我会调用返回。现在我需要弄清楚如何在订阅时通知他们旧的发布。

*编辑 2 *

添加了新版本的工作脚本。它是正确的还是最好的方法?

【问题讨论】:

    标签: javascript design-patterns observer-pattern publish-subscribe


    【解决方案1】:

    如果您想让订阅者在通知发送后收到通知,您需要做的只是

    1. 对于每种不同的发布类型,保留一份您收到的所有通知的列表。 (发布时,将publication 添加到相应的列表中)

    2. 更改订阅功能,以便它还将所有适当的存储通知发送给迟到的订阅者。

      2.1 或许您还应该创建一个单独的send_notification_to(arg, type, subscriber) 方法,以避免与visitSubscribers 重复代码

    【讨论】:

    • 我已修改发布对象以保存发布。那样正确吗?我现在正在尝试计算第二步。问题是何时调用 paper.yearly() 。这调用了 publisher.publish,但没有订阅者,所以它返回 undefined。如果有这种类型的订阅者,也许我应该检查发布方法?真的很迷茫……
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-01
    • 1970-01-01
    • 2014-03-02
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多