| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- /**
- * @Author: Caven
- * @Date: 2020-01-02 15:24:38
- */
-
- class Event {
- constructor() {
- this._cache = {}
- this._registerEvent()
- }
-
- /**
- * Event registration
- * Subclasses need to override
- * @private
- */
- _registerEvent() {}
-
- /**
- * @param type
- * @param callback
- * @param context
- * @returns {any}
- * @private
- */
- _on(type, callback, context) {
- let event = this.getEvent(type)
- let removeCallback = undefined
- if (event && callback) {
- removeCallback = event.addEventListener(callback, context || this)
- }
- return removeCallback
- }
-
- /**
- * @param type
- * @param callback
- * @param context
- * @returns {boolean}
- * @private
- */
- _off(type, callback, context) {
- let event = this.getEvent(type)
- let removed = false
- if (event && callback) {
- removed = event.removeEventListener(callback, context || this)
- }
- return removed
- }
-
- /**
- * @param type
- * @param params
- * @private
- */
- _fire(type, params) {
- let event = this.getEvent(type)
- if (event) {
- event.raiseEvent(params)
- }
- }
-
- /**
- * Subscribe event
- * @param type
- * @param callback
- * @param context
- * @returns remove callback function
- */
- on(type, callback, context) {
- return this._on(type, callback, context)
- }
-
- /**
- * Subscribe once event
- * @param type
- * @param callback
- * @param context
- */
- once(type, callback, context) {
- let removeCallback = this._on(
- type,
- e => {
- callback(e)
- removeCallback && removeCallback()
- },
- context
- )
- }
-
- /**
- * Unsubscribe event
- * @param type
- * @param callback
- * @param context
- * @returns Boolean
- */
- off(type, callback, context) {
- return this._off(type, callback, context)
- }
-
- /**
- * Trigger subscription event
- * @param type
- * @param params
- */
- fire(type, params) {
- this._fire(type, params)
- }
-
- /**
- * Returns events by type
- * @param type
- * @returns Event
- */
- getEvent(type) {
- return this._cache[type] || undefined
- }
- }
-
- export default Event
|