| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 CNIT - Consorzio Nazionale Interuniversitario per le Telecomunicazioni |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | if (typeof dreamer === 'undefined') { |
| 18 | var dreamer = {}; |
| 19 | } |
| 20 | |
| 21 | dreamer.Event = (function (global) { |
| 22 | 'use strict'; |
| 23 | |
| 24 | function Event () { |
| 25 | this._listeners = {}; |
| 26 | } |
| 27 | |
| 28 | Event.prototype.addL = function (type, listener) { |
| 29 | if (typeof this._listeners[type] == "undefined"){ |
| 30 | this._listeners[type] = []; |
| 31 | } |
| 32 | this._listeners[type].push(listener); |
| 33 | }; |
| 34 | |
| 35 | Event.prototype.fire = function (event, args) { |
| 36 | if (typeof event == "string"){ |
| 37 | event = { type: event }; |
| 38 | } |
| 39 | if (!event.target){ |
| 40 | event.target = this; |
| 41 | } |
| 42 | |
| 43 | if (!event.type){ //falsy |
| 44 | throw new Error("Event object missing 'type' property."); |
| 45 | } |
| 46 | |
| 47 | if (this._listeners[event.type] instanceof Array){ |
| 48 | var listeners = this._listeners[event.type]; |
| 49 | for (var i=0, len=listeners.length; i < len; i++){ |
| 50 | listeners[i].call(this, event, args); |
| 51 | } |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | Event.prototype.addListener = function (type, listener) { |
| 56 | if (this._listeners[type] instanceof Array){ |
| 57 | var listeners = this._listeners[type]; |
| 58 | for (var i=0, len=listeners.length; i < len; i++){ |
| 59 | if (listeners[i] === listener){ |
| 60 | listeners.splice(i, 1); |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | }; |
| 67 | |
| 68 | return Event; |
| 69 | }()); |
| 70 | |
| 71 | if (typeof module === 'object') { |
| 72 | module.exports = dreamer.Event; |
| 73 | } |