Skip to content

Commit

Permalink
feat(okam-core): add boradcast support
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhy committed Nov 6, 2018
1 parent 3aa2bae commit 9bba7a4
Showing 1 changed file with 77 additions and 12 deletions.
89 changes: 77 additions & 12 deletions packages/okam-core/src/extend/broadcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,27 @@

import eventCenter from '../helper/eventCenter';

const ONCE_LISTEN_REGEXP = /^(.*)\.once$/;

const broadcastAPIs = {

/**
* Send broadcast event
*
* @param {...any} args the broadcast args
*/
$broadcast(...args) {
eventCenter.emit.apply(eventCenter, args);
},

$onbroadcast(eventName, handler, isOnce) {
/**
* Listen broadcast event
*
* @param {string} eventName the event name to broadcast
* @param {Function} handler the listen handler
* @param {boolean=} isOnce whether is once listen, optional, by default false
*/
$onBroadcast(eventName, handler, isOnce) {
let bindEvents = this._bindBroadcastEvents;
if (!bindEvents) {
bindEvents = this._bindBroadcastEvents = [];
Expand All @@ -30,24 +45,38 @@ const broadcastAPIs = {
}
},

$offbroadcast(eventName, handler) {
/**
* Remove broadcast event listener
*
* @param {string} eventName the event to remove
* @param {Function} handler the handler to remove
*/
$offBroadcast(eventName, handler) {
eventCenter.off(eventName, handler);
},

bindBroadcastEvents() {
let events = this.events;
/**
* Bind the declaration broadcast events
*
* @private
*/
__bindBroadcastEvents() {
let events = this.$rawBroadcastEvents;
if (typeof events === 'function') {
events = this.$rawBroadcastEvents = events();
}

if (!events) {
return;
}

let onceRegexp = /^(.*)\.once$/;
let bindEvents = this._bindBroadcastEvents;
if (!bindEvents) {
bindEvents = this._bindBroadcastEvents = [];
}

Object.keys(events).forEach(k => {
let result = onceRegexp.exec(k);
let result = ONCE_LISTEN_REGEXP.exec(k);
let eventName = result ? result[1] : k;
let handler = events[k].bind(this);
bindEvents.push([eventName, handler]);
Expand All @@ -62,7 +91,12 @@ const broadcastAPIs = {
this._bindBroadcastEvents = bindEvents;
},

removeBroadcastEventListeners() {
/**
* Remove the bind broadcast events
*
* @private
*/
__removeBroadcastEventListeners() {
let bindEvents = this._bindBroadcastEvents;
if (!bindEvents) {
return;
Expand All @@ -75,18 +109,49 @@ const broadcastAPIs = {

export default {
app: Object.assign({
ready() {
this.bindBroadcastEvents();

/**
* The hook when app launch
*
* @private
*/
onLaunch() {
this.__bindBroadcastEvents();
}
}, broadcastAPIs),

component: {
ready() {
this.bindBroadcastEvents();

/**
* The instance initialization before the instance is normalized and created.
*
* @param {boolean} isPage whether is page component
* @private
*/
$init(isPage) {
let events = this.broadcastEvents;
if (events) {
this.$rawBroadcastEvents = isPage ? events : () => events;
delete this.broadcastEvents;
}
},

/**
* The hook when component created
*
* @private
*/
created() {
this.__bindBroadcastEvents();
},

/**
* The hook when component detached
*
* @private
*/
detached() {
this.removeBroadcastEventListeners();
this.__removeBroadcastEventListeners();
},

methods: broadcastAPIs
Expand Down

0 comments on commit 9bba7a4

Please sign in to comment.