美文网首页
观察者模式

观察者模式

作者: 夏目祐太 | 来源:发表于2017-11-09 09:45 被阅读13次
观察者模式

let Event = (function() {

let global = this,

Event,

_default = 'default';

Event = function() {

let _listen,

_trigger,

_remove,

_slice = Array.prototype.slice,

_shift = Array.prototype.shift,

_unshift = Array.prototype.unshift,

namespaceCache = {},

_create,

find,

each = function(ary, fn) {

let ret;

for (let i = 0, l = ary.length; i < l; i++) {

let 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 (let i = cache[key].length; i >= 0; i--) {

if (cache[key][i] === fn) {

cache[key].splice(i, 1);

}

}

} else {

cache[key] = [];

}

}

};

_trigger = function() {

let 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 thisl.apply(_self, args);

})

};

_create = function(namespace) {

var namespace = namespace || _default,

cache = {},

offlineStack = [], // 离线事件

ret = {

listen: function(key, fn, last) {

_listen(key, fn, cache);

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.listen(key, fn, last);

},

remove: function(key, fn) {

_remove(key, cache, fn);

},

trigger: function() {

let fn,

args,

_self = this;

_unshift.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) {

let event = this.create();

event.one(key, fn, last);

},

remove: function(key, fn) {

let event = this.create();

event.remove(key, fn);

},

listen: function(key, fn, last) {

let event = this.create();

event.listen(key, fn, last);

},

trigger: function() {

let event = this.create();

event.trigger.apply(this, arguments);

}

};

}();

return Event;

})();

相关文章

  • 11.9设计模式-观察者模式-详解

    设计模式-观察者模式 观察者模式详解 观察者模式在android中的实际运用 1.观察者模式详解 2.观察者模式在...

  • RxJava基础—观察者模式

    设计模式-观察者模式 观察者模式:观察者模式(有时又被称为发布(publish )-订阅(Subscribe)模式...

  • 前端面试考点之手写系列

    1、观察者模式 观察者模式(基于发布订阅模式) 有观察者,也有被观察者。 观察者需要放到被观察者列表中,被观察者的...

  • RxJava 原理篇

    一、框架思想 观察者模式观察者自下而上注入被观察者被观察者自上而下发射事件观察者模式 装饰器模式自上而下,被观察者...

  • 观察者模式

    观察者模式概念 观察者模式是对象的行为模式,又叫作发布-订阅(publish/subscrible)模式。 观察者...

  • 设计模式-观察者模式

    观察者模式介绍 观察者模式定义 观察者模式(又被称为发布-订阅(Publish/Subscribe)模式,属于行为...

  • 观察者模式

    观察者模式 观察者模式的定义 观察者模式(Observer Pattern)也叫做发布订阅模式(Publish/s...

  • iOS设计模式之观察者模式

    观察者模式 1、什么是观察者模式 观察者模式有时又被称为发布(publish)-订阅(Subscribe)模式、模...

  • 观察者模式和发布订阅模式区别

    观察者模式 所谓观察者模式,其实就是为了实现松耦合(loosely coupled)。 在观察者模式中,观察者需要...

  • RxJava(二)

    一、观察者模式 1.1、传统的观察者模式 1.2、RxJava 的观察者模式 区别传统的观察者模式是一个 Obse...

网友评论

      本文标题:观察者模式

      本文链接:https://www.haomeiwen.com/subject/knqpmxtx.html