Created
July 13, 2011 03:21
-
-
Save ianbishop/1079647 to your computer and use it in GitHub Desktop.
Oh god, singletons
This file contains hidden or 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
| /* | |
| * Singleton service to manage notifications | |
| * for players using GET /notifications/token | |
| * for some player token | |
| */ | |
| function NotificationService() { | |
| var instance = (function() { | |
| /* | |
| * Private variables | |
| */ | |
| // currently active player token | |
| var _token = null; | |
| // State | |
| var _active = false; | |
| var _getting = false; | |
| // time between polls in miliseconds | |
| // default is 10s | |
| var _pollTime = 10000; | |
| // notifications queue for all users | |
| var _notifications = []; | |
| /* | |
| * Private methods | |
| */ | |
| /* | |
| * Get notifications for player token | |
| * if successful, poll for _pollTime | |
| * if error, stop service | |
| */ | |
| function _get(token) { | |
| if(!_getting) { | |
| _getting = true; | |
| $.get('/notifications', { token: token }, | |
| function(data) { | |
| var response = jQuery.parseJSON(data); | |
| _notifications.concat(response.notifications); | |
| } | |
| ) | |
| .success(function() { | |
| _getting = false; | |
| _poll(); | |
| }) | |
| .error(function() { | |
| _getting = false; | |
| this._stop(); | |
| debug('NotificationService: An error occurred fetching notifications for ' + _token + | |
| '. Service has been stopped'); | |
| }); | |
| } | |
| } | |
| /* | |
| * Polls server for notification for player | |
| * token that is currently active. _pollTime | |
| * represents time between polls. | |
| */ | |
| function _poll() { | |
| if(_active) { | |
| setTimeout(function() { _get(_token); }, _pollTime); | |
| } | |
| } | |
| /* | |
| * Validates current service state for a given | |
| * player token. | |
| * | |
| * returns true if valid, false otherwise. | |
| */ | |
| function _isValid(token) { | |
| if(_active) { | |
| if(_token == token) { | |
| debug('NotificationService: Already active for token ' + _token); | |
| return false; | |
| } | |
| // start notifier for a different user | |
| this._stop(); | |
| } | |
| if(token == null) { | |
| debug('NotificationService: ' + token + ' is not valid'); | |
| return false; | |
| } | |
| return true; | |
| } | |
| /* | |
| * Stops service | |
| */ | |
| function _stop() { | |
| _active = false; | |
| } | |
| /* | |
| * Public methods | |
| */ | |
| return { | |
| /* | |
| * Start service for a given player token. | |
| * Optional param of pollTime which indicates the time | |
| * between polls in ms. Default is 10s (10000).s | |
| */ | |
| start: function(token, pollTime) { | |
| if(_isValid(token)) { | |
| _active = true; | |
| _token = token; | |
| _pollTime = (pollTime != null) ? pollTime : 10000; // default 10s | |
| _poll(); | |
| } | |
| }, | |
| /* | |
| * Stops service | |
| */ | |
| stop: function() { | |
| _stop(); | |
| }, | |
| /* | |
| * Retrieves, but does not remove, notifications for a given player | |
| * token from the notification queue. | |
| * | |
| * returns [] if empty | |
| */ | |
| peekForPlayer: function(token) { | |
| return _.filter(_notifications, function(notification) { | |
| return (notification.token == token); | |
| }); | |
| }, | |
| /* | |
| * Retrieves, but does not remove, all notifications in the | |
| * notification queue. | |
| * | |
| * returns [] if empty | |
| */ | |
| peekAll: function() { | |
| return _notifications; | |
| }, | |
| /* | |
| * Retrieves and removes all notifications for a given player | |
| * token from the notification queue. | |
| * | |
| * returns [] if empty | |
| */ | |
| getForPlayer: function(token) { | |
| var notifications = []; | |
| _.each(_notifications, function(notification) { | |
| if(notification.token == token) { | |
| notifications.push(notification); | |
| delete notification; | |
| } | |
| }); | |
| _notifications = _.compact(_notifications); | |
| return notifications; | |
| }, | |
| /* | |
| * Retrieves and removes all notifications from the notification | |
| * queue. | |
| * | |
| * returns [] if empty | |
| */ | |
| getAll: function() { | |
| var notifications = _notifications; | |
| _notifications = []; | |
| return notifications; | |
| } | |
| } | |
| })(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment