码迷,mamicode.com
首页 > 其他好文 > 详细

先发布,后订阅

时间:2019-03-10 20:52:22      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:line   type   his   nts   lob   none   function   argument   cli   

技术图片
var Event = (function() {
    var global = this,
        Event,
        _default = ‘default‘;
    Event = function() {
        var _listen,
            _trigger,
            _remove,
            _slice = Array.prototype.slice,
            _shift = Array.prototype.shift,
            _unshift = Array.prototype.unshift,
            namespaceCache = {},
            _create,
            find,
            each = function(ary, fn) {
                var ret;
                for (var i = 0, l = ary.length; i < l; i++) {
                    var n = ary[i];
                    ret = fn.call(n, i, n);
                }
                return ret;
            };
        _listen = function(key, fn, cache) {
            if (!cache[key]) {
                cache[key] = [];
            }
            cache[key].push(fn);
        };
        _remove = function(key, cache, fn) {
            if (cache[key]) {
                if (fn) {
                    for (var i = cache[key].length; i >= 0; i--) {
                        if (cache[key] === fn) {
                            cache[key].splice(i, 1);
                        }
                    }
                } else {
                    cache[key] = [];
                }
            }
        };
        _trigger = function() {
            var cache = _shift.call(arguments),
                key = _shift.call(arguments),
                args = arguments,
                _self = this,
                ret,
                stack = cache[key];

            if (!stack || !stack.length) {
                return;
            }
            return each(stack, function() {
                return this.apply(_self, args);
            });
        };

        _create = function(namespace) {
            namespace = namespace || _default;
            var cache = {},
                offlineStack = [],
                ret = {
                    listen: function(key, fn, last) {
                        if (offlineStack === null) {
                            return;
                        }
                        if (last === ‘last‘) {
                            offlineStack.length && (offlineStack.pop()());
                        } else {
                            each(offlineStack, function() {
                                this();
                            });
                        }
                        offlineStack = null;
                    },
                    one: function(key, fn, last) {
                        _remove(key, cache);
                        this.liten(key, fn, last);
                    },
                    remove: function(key, fn) {
                        _remove(key, cache, last);
                    },
                    trigger: function() {
                        var fn,
                            args,
                            _self = this;
                        _shift.call(arguments, cache);
                        args = arguments;
                        fn = function() {
                            return _trigger.apply(_self, args);
                        };
                        if (offlineStack) {
                            return offlineStack.push(fn);
                        }
                        return fn();
                    }
                };
            return namespace ?
                (namespaceCache[namespace] ? namespaceCache[namespace] :
                    namespaceCache[namespace] = ret) : ret;
        };
        return {
            create: _create,
            one: function(key, fn, last) {
                var event = this.create();
                event.one(key, fn, last);
            },
            remove: function(key, fn) {
                var event = this.create();
                event.remove(key, fn);
            },
            listen: function(key, fn, last) {
                var event = this.create();
                event.listen(key, fn, last);
            },
            trigger: function() {
                var event = this.create();
                event.trigger.apply(this, arguments);
            }
        };

    }();
    return Event;
})();


// 先发布后订阅

Event.trigger(‘click‘, 1);

Event.listen(‘click‘, function(a) {
    console.log(a);
});

// 使用命名空间
Event.create(‘namespace1‘).listen(‘click‘, function(a) {
    console.log(a);
});

Event.create(‘namespace1‘).trigger(‘click‘, 1);

Event.create(‘namespace2‘).listen(‘click‘, function(a) {
    console.log(a);
});

Event.create(‘namespace2‘).trigger(‘click‘, 2);
View Code

 

先发布,后订阅

标签:line   type   his   nts   lob   none   function   argument   cli   

原文地址:https://www.cnblogs.com/tujw/p/10506762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!