Created
March 30, 2017 18:00
-
-
Save spentak/87d8059aadd911f4d61216e3a66ad054 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const NOTIF_WISHLIST_CHANGED = "notif_wishlist_changed"; | |
let instance = null; | |
var observers = {}; | |
class NotificationService { | |
constructor() { | |
if (!instance) { | |
instance = this; | |
} | |
return instance; | |
} | |
postNotification = (notifName, data) => { | |
let obs = observers[notifName]; | |
for (var x = 0; x < obs.length; x++) { | |
var obj = obs[x]; | |
obj.callBack(data); | |
} | |
} | |
removeObserver = (observer, notifName) => { | |
var obs = observers[notifName]; | |
if (obs) { | |
for (var x = 0; x < obs.length; x++) { | |
if (observer === obs[x].observer) { | |
obs.splice(x,1); | |
observers[notifName] = obs; | |
break; | |
} | |
} | |
} | |
} | |
addObserver = (notifName, observer, callBack) => { | |
let obs = observers[notifName]; | |
if (!obs) { | |
observers[notifName] = []; | |
} | |
let obj = {observer: observer, callBack: callBack}; | |
observers[notifName].push(obj); | |
} | |
} | |
export default NotificationService; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment