(function () { 'use strict'; //service that allows the server to notify the client of specific events using SignalR var serviceId = 'notifier'; angular.module('app').factory(serviceId, ['Hub', 'common', '$rootScope', 'userAuth', 'datacontext', notifier]); function notifier(Hub, common, $rootScope, userAuth,datacontext) { var service = { start: start }; return service; function start() { var tokenData = userAuth.getAuthToken(); if (tokenData) { //SignalR Hub setup var hub = new Hub('notifier', { //these functions are effectively called from the server listeners: { //a showcase has been published (by the out of process showcase publisher) //update the list of showcases accordingly 'showcasePublished': function (val) { common.logger.logSuccess("Showcase published", val, null, true); $rootScope.$broadcast('UpdateShowcases'); }, //an Evernote item has been updated e.g. an Evernote note that was used to create an item has been changed //broadcast the 'UpdateItems' message which will refresh the items 'evernoteItemUpdated':function (itemId,title) { common.logger.logSuccess(title + " updated in Evernote", itemId, null, true); datacontext.clearCachesForItemUpdated([itemId]); $rootScope.$broadcast('UpdateItems'); }, //an Evernote item has been created (an Evernote note is created that automatically creates an item) //broadcast the 'UpdateItems' message which will refresh the items 'evernoteItemCreated':function (itemId,title) { common.logger.logSuccess(title + " created in Evernote", itemId, null, true); datacontext.clearCachesForItemAdded(); $rootScope.$broadcast('UpdateItems'); }, //an item has been created externally (e.g. by the mobile app) //broadcast the 'UpdateItems' message which will refresh the items 'externalItemCreated':function (title) { common.logger.logSuccess(title + " created externally", null, true); datacontext.clearCachesForItemAdded(); $rootScope.$broadcast('UpdateItems'); }, //a new notification has arrived 'notificationsChanged': function (numberNotifications) { common.logger.logSuccess("Notification added", null, true); $rootScope.$broadcast('UpdateNotifications', { notificationsNum: numberNotifications }); } }, errorHandler: function (error) { console.error(error); }, queryParams: { bearer: tokenData.access_token } //send the bearer token with every SignalR request }); } } } })();