message.js
/**消息類悼凑,用來傳遞數(shù)據(jù)與方法 */
class Message {
? ? constructor() {
? ? ? ? if (!this.event_list) {
? ? ? ? ? ? this.event_list = {};
? ? ? ? }
? ? }
? ? static get Ins() {
? ? ? ? if (!this._ins)
? ? ? ? ? ? this._ins = new Message();
? ? ? ? return this._ins;
? ? }
? ? /**添加一個消息偵聽器 */
? ? on(eventName, content) {
? ? ? ? var _event, ctx;
? ? ? ? _event = this.event_list;
? ? ? ? //如果事件列表為空,就初始化
? ? ? ? if (!_event) {
? ? ? ? ? ? _event = this.event_list = {};
? ? ? ? } else {
? ? ? ? ? ? ctx = this.event_list[eventName];
? ? ? ? }
? ? ? ? //如果監(jiān)聽不存在璧瞬,就新建一個監(jiān)聽户辫,然后把監(jiān)聽器數(shù)量置為1
? ? ? ? if (!ctx) {
? ? ? ? ? ? ctx = this.event_list[eventName] = content;
? ? ? ? ? ? ctx.ListenerCount = 1;
? ? ? ? }
? ? ? ? //如果監(jiān)聽存在并且只有一個,就將監(jiān)聽賦值多個監(jiān)聽器的數(shù)組嗤锉,監(jiān)聽器數(shù)量為數(shù)組長度
? ? ? ? else if (typeof (ctx) == 'function') {
? ? ? ? ? ? ctx = this.event_list[eventName] = [ctx, content];
? ? ? ? ? ? ctx.ListenerCount = ctx.length;
? ? ? ? }
? ? ? ? //如果監(jiān)聽存在并且有多個渔欢,就直接push
? ? ? ? else if (Array.isArray(ctx)) {
? ? ? ? ? ? ctx.push(content);
? ? ? ? ? ? ctx.ListenerCount = ctx.length;
? ? ? ? }
? ? }
? ? /**派發(fā)一個消息 */
? ? emit(eventName) {
? ? ? ? try{
? ? ? ? ? ? var _event, ctx;
? ? ? ? ? ? //用slice方法將參數(shù)分離,第一個參數(shù)為監(jiān)聽器名稱瘟忱,后面的都會作為傳參使用
? ? ? ? ? ? var args = Array.prototype.slice.call(arguments, 1);
? ? ? ? ? ? // console.log(args)
? ? ? ? ? ? _event = this.event_list;
? ? ? ? ? ? if (_event) {
? ? ? ? ? ? ? ? ctx = this.event_list[eventName];
? ? ? ? ? ? }
? ? ? ? ? ? //如果沒有監(jiān)聽器奥额,就直接返回
? ? ? ? ? ? if (!ctx) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? ? ? //如果監(jiān)聽器只有一個,直接apply觸發(fā)
? ? ? ? ? ? else if (typeof (ctx) == 'function') {
? ? ? ? ? ? ? ? ctx.apply(this, args);
? ? ? ? ? ? ? ? console.log(111,this)
? ? ? ? ? ? }
? ? ? ? ? ? //如果監(jiān)聽器有多個访诱,數(shù)組循環(huán)apply
? ? ? ? ? ? else if (Array.isArray(ctx)) {
? ? ? ? ? ? ? ? for (var i = 0; i < ctx.length; i++) {
? ? ? ? ? ? ? ? ? ? ctx[i].apply(this, args);
? ? ? ? ? ? ? ? ? ? console.log("=======",i,this)
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }catch(e){
? ? ? ? ? ? console.warn('發(fā)送消息有問題---》',e)
? ? ? ? }
? ? }
? ? /**刪除一個消息偵聽器 */
? ? remove(eventName, content) {
? ? ? ? var _event, ctx, index = 0;
? ? ? ? _event = this.event_list;
? ? ? ? if (!_event) {
? ? ? ? ? ? return this;
? ? ? ? } else {
? ? ? ? ? ? ctx = this.event_list[eventName];
? ? ? ? }
? ? ? ? if (!ctx) {
? ? ? ? ? ? return this;
? ? ? ? }
? ? ? ? if (typeof (ctx) == 'function') {
? ? ? ? ? ? if (ctx === content) {
? ? ? ? ? ? ? ? delete _event[eventName];
? ? ? ? ? ? }
? ? ? ? } else if (Array.isArray(ctx)) {
? ? ? ? ? ? for (var i = 0; i < ctx.length; i++) {
? ? ? ? ? ? ? ? if (ctx[i] === content) {
? ? ? ? ? ? ? ? ? ? this.event_list[eventName].splice(i - index, 1);
? ? ? ? ? ? ? ? ? ? ctx.ListenerCount = ctx.length;
? ? ? ? ? ? ? ? ? ? if (this.event_list[eventName].length === 0) {
? ? ? ? ? ? ? ? ? ? ? ? delete this.event_list[eventName]
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? index++;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /**刪除所有消息偵聽器 */
? ? removeAllListener(eventName) {
? ? ? ? var _event, ctx;
? ? ? ? _event = this.event_list;
? ? ? ? if (!_event) {
? ? ? ? ? ? return this;
? ? ? ? }
? ? ? ? ctx = this.event_list[eventName];
? ? ? ? if (arguments.length === 0 && (!eventName)) {
? ? ? ? ? ? var keys = Object.keys(this.event_list);
? ? ? ? ? ? for (var i = 0, key; i < keys.length; i++) {
? ? ? ? ? ? ? ? key = keys[i];
? ? ? ? ? ? ? ? delete this.event_list[key];
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if (ctx || typeof (ctx) == 'function' || Array.isArray(ctx)) {
? ? ? ? ? ? delete this.event_list[eventName];
? ? ? ? } else {
? ? ? ? ? ? return this;
? ? ? ? }
? ? }
}
const messageIns = Message.Ins;
export default messageIns;
msg.js
const msg = {
? ? elephantSay: 'elephantSay',? //大象說話
? ? changeGuideStep: 'changeGuideStep',? //修改新手引導(dǎo)階段
? ? uploadGuideStep: 'uploadGuideStep',? //上傳新手引導(dǎo)階段
? ? changePlantGuideState: 'changePlantGuideState',? ? //改變植物新手引導(dǎo)狀態(tài)
? ? changeProgress:"changeProgress",? ? //改變進度條
? ? changePage:"changePage",? // 改變頁面
? ? refreshHome:'refreshHome',
? ? elephantWatering: 'elephantChange',? // 改變大象svga
? ? showWaterTap: 'showWaterTap',? // 展示水龍頭
? ? refreshFriendsPage: 'refreshFriendsPage',? //刷新好友頁面
? ? refreshWaterDrop: 'refreshWaterDrop',? //刷新好友頁面我的水滴
}
export default msg;
使用
import messageIns from '../../utils/message'
import msg from '../../utils/msg'
//添加偵聽器
messageIns.on(msg.refreshHome,this.refreshHome.bind(this))
//派發(fā)一個消息
messageIns.emit(msg.refreshHome)