Initial commit
This commit is contained in:
commit
880e39de2e
511 files changed
+38363
No files matched your search
@@ -0,0 +1,160 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
/**
|
||||
* Manages the STOMP connections.
|
||||
* @type String
|
||||
*/
|
||||
|
||||
angular.module('portal.common.services.triggers').factory('triggerManager', function ($log, Triggers, $rootScope, Map, Request, portalUtils, $mdDialog, $state, cache, $window) {
|
||||
|
||||
var subscriptions = new Map();
|
||||
var stompClient = null;
|
||||
var allTopics = [];
|
||||
var showTimeout = true;
|
||||
var retryCounter = 0;
|
||||
var useStomp = true;
|
||||
|
||||
function reconnect(onSuccess) {
|
||||
if (stompClient !== null && stompClient.connected) {
|
||||
stompClient.disconnect();
|
||||
}
|
||||
console.log('Reconnecting....');
|
||||
subscriptions = new Map();
|
||||
stompClient = null;
|
||||
stompClient = null;
|
||||
connect(onSuccess);
|
||||
}
|
||||
|
||||
function connect(onSuccess) {
|
||||
if (stompClient === null) {
|
||||
var socket = new SockJS(Request.baseURI + '/ws/stomp');
|
||||
stompClient = Stomp.over(socket);
|
||||
// turns off printing out the contents of all messages
|
||||
stompClient.debug = null;
|
||||
}
|
||||
if (!stompClient.connected) {
|
||||
stompClient.connect({}, function (frame) {
|
||||
$log.log('Stomp connection success');
|
||||
portalUtils.call(onSuccess);
|
||||
retryCounter = 0;
|
||||
}, function (error) {
|
||||
// this is called if the connection closes or times out.
|
||||
// Should we display a notice to the user? auto-reconnect?
|
||||
$log.log('Stomp connection error: ' + error);
|
||||
|
||||
if (retryCounter < 6) {
|
||||
setTimeout(function() { reconnect(onSuccess); }, 5000);
|
||||
retryCounter++;
|
||||
}
|
||||
|
||||
else if (showTimeout) {
|
||||
var modal = $mdDialog.show({
|
||||
templateUrl: 'common/partials/session-end/session-end.html',
|
||||
controller: 'SessionEndCtrl',
|
||||
clickOutsideToClose: false,
|
||||
escapeToClose: false,
|
||||
locals: {
|
||||
modalInstance: {
|
||||
message: 'Session lost. Please click below to connect',
|
||||
btnText: 'Connect',
|
||||
onOkay: function() {
|
||||
stompClient = null;
|
||||
retryCounter = 0;
|
||||
reconnect(onSuccess);
|
||||
$mdDialog.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
showTimeout = true;
|
||||
});
|
||||
} else {
|
||||
portalUtils.call(onSuccess);
|
||||
}
|
||||
}
|
||||
|
||||
function disconnect(callback) {
|
||||
subscriptions = new Map();
|
||||
if (stompClient !== null) {
|
||||
showTimeout = false;
|
||||
stompClient = null;
|
||||
} else {
|
||||
portalUtils.call(callback);
|
||||
}
|
||||
}
|
||||
|
||||
function stompMsgReceived(message) {
|
||||
var trigger = angular.fromJson(message.body);
|
||||
//$log.log('Received Trigger: ' + trigger.type + '\n' + message.body);
|
||||
processTrigger(trigger);
|
||||
}
|
||||
|
||||
function processTrigger(trigger) {
|
||||
//This function comes from stomp, which comes in on a non-angular javascript turn.
|
||||
//That means we need to call apply ourselves. If we are ever in a digest
|
||||
//cycle at this point in the code, something is wrong
|
||||
$rootScope.$apply(function () {
|
||||
Triggers.notify(Triggers[trigger.type], trigger);
|
||||
});
|
||||
}
|
||||
|
||||
function subscribe() {
|
||||
if (stompClient === null || stompClient === undefined || !stompClient.connected) {
|
||||
connect(function () {
|
||||
subscribe();
|
||||
});
|
||||
} else {
|
||||
for (var x = 0; x < allTopics.length; x++) {
|
||||
var topic = allTopics[x];
|
||||
if (!subscriptions.containsKey(topic)) {
|
||||
var subscription = stompClient.subscribe(topic, stompMsgReceived);
|
||||
subscription.topic = topic;
|
||||
subscriptions.put(topic, subscription);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function unsubscribe(topic) {
|
||||
var subscription = subscriptions.get(topic);
|
||||
if (subscription !== undefined) {
|
||||
subscription.unsubscribe();
|
||||
}
|
||||
subscriptions.removeByKey(topic);
|
||||
|
||||
var index = allTopics.indexOf(topic);
|
||||
if (index > -1) {
|
||||
allTopics.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
function unsubscribeAll() {
|
||||
for (var i = 0; i < subscriptions.length; i++) {
|
||||
var sub = subscriptions.values[i];
|
||||
sub.unsubscribe();
|
||||
subscriptions.removeByKey(sub.topic);
|
||||
}
|
||||
allTopics.length = 0;
|
||||
}
|
||||
|
||||
return {
|
||||
subscribe: function (topics) {
|
||||
//unsubscribeAll(); //For now unsubscribe all, eventually want to support listening for more than one.
|
||||
for (var x = 0; x < topics.length; x++) {
|
||||
var index = allTopics.indexOf(topics[x]);
|
||||
if (index < 0) {
|
||||
allTopics.push(topics[x]);
|
||||
}
|
||||
}
|
||||
connect(function () {
|
||||
subscribe();
|
||||
});
|
||||
},
|
||||
unsubscribe: unsubscribe,
|
||||
disconnect: disconnect,
|
||||
unsubscribeAll: unsubscribeAll
|
||||
};
|
||||
});
|
||||
})();
|
||||
@@ -0,0 +1,168 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* DESCRIPTION:
|
||||
*/
|
||||
angular.module('portal.common.services.triggers', ['ui.router'])
|
||||
.factory('Triggers', triggersService);
|
||||
|
||||
function triggersService($log, portalUtils, Map) {
|
||||
|
||||
function Trigger(id, validatorFn, preCall) {
|
||||
var trigger = this;
|
||||
trigger.id = id;
|
||||
trigger.validate = validatorFn;
|
||||
if (!angular.isFunction(trigger.validate)) {
|
||||
trigger.validate = function () {
|
||||
return true;
|
||||
};
|
||||
}
|
||||
Object.defineProperties(trigger, {
|
||||
isValid: {
|
||||
get: trigger.validate
|
||||
}
|
||||
});
|
||||
trigger.preCall = true;
|
||||
if (angular.isDefined(preCall)) {
|
||||
trigger.preCall = preCall;
|
||||
}
|
||||
}
|
||||
|
||||
Trigger.prototype.toString = function () {
|
||||
return this.id;
|
||||
};
|
||||
|
||||
var triggers = {
|
||||
APP_LOGS_UPDATED: new Trigger('APP_LOGS_UPDATED', undefined, false),
|
||||
APP_LOGS_PURGED: new Trigger('APP_LOGS_PURGED', undefined, false),
|
||||
SCRIPT_COMPLETED: new Trigger('SCRIPT_COMPLETED', undefined, false),
|
||||
SCRIPT_DELETED: new Trigger('SCRIPT_DELETED', undefined, false),
|
||||
SCRIPT_ADDED: new Trigger('SCRIPT_ADDED', undefined, false),
|
||||
SCRIPT_UPDATED: new Trigger('SCRIPT_UPDATED', undefined, false),
|
||||
SCRIPT_SCHEDULE_DELETED: new Trigger('SCRIPT_SCHEDULE_DELETED', undefined, false),
|
||||
USER_ADDED: new Trigger('USER_ADDED', undefined, false),
|
||||
USER_DELETED: new Trigger('USER_DELETED', undefined, false),
|
||||
HASH_TYPES_CHANGED: new Trigger('HASH_TYPES_CHANGED', undefined, false),
|
||||
PERMISSIONS_DELETED: new Trigger('PERMISSIONS_DELETED', undefined, false),
|
||||
PERMISSIONS_CHANGED: new Trigger('PERMISSIONS_CHANGED', undefined, false),
|
||||
PERMISSIONS_UPDATED: new Trigger('PERMISSIONS_UPDATED', undefined, false),
|
||||
PERMISSIONS_ADDED: new Trigger('PERMISSIONS_ADDED', undefined, false),
|
||||
FILE_DELETED: new Trigger('FILE_DELETED', undefined, false),
|
||||
ENV_VARS_ADDED: new Trigger('ENV_VARS_ADDED', undefined, false),
|
||||
ENV_VARS_DELETED: new Trigger('ENV_VARS_DELETED', undefined, false),
|
||||
EMAIL_TEMPLATE_UPDATED: new Trigger('EMAIL_TEMPLATE_UPDATED', undefined, false),
|
||||
EMAIL_TEMPLATE_DELETED: new Trigger('EMAIL_TEMPLATE_DELETED', undefined, false),
|
||||
SMTP_CONFIG_UPDATED: new Trigger('SMTP_CONFIG_UPDATED', undefined, false),
|
||||
SEED_UPDATED: new Trigger('SEED_UPDATED', undefined, false),
|
||||
};
|
||||
|
||||
triggers.Trigger = Trigger;
|
||||
|
||||
|
||||
/**
|
||||
* Registers to be notified when the passed in triggers are fired.
|
||||
* @param {type} params {required:array[Trigger],optional:array[Trigger]}
|
||||
* @param {type} callback
|
||||
* @returns {cache_L40.listen.cacheAnonym$4|undefined}
|
||||
*/
|
||||
triggers.listen = function (params, callback) {
|
||||
var optional = [];
|
||||
var ids = [];
|
||||
|
||||
if (angular.isArray(params)) {
|
||||
optional = params.slice();
|
||||
} else if (params instanceof Trigger) {
|
||||
optional.push(params);
|
||||
}
|
||||
|
||||
//Warn if data is incorrect.
|
||||
if (optional.length === 0) {
|
||||
$log.error('Attempting to listen for triggers without correct parameters');
|
||||
return;
|
||||
}
|
||||
for (var j = 0; j < optional.length; j++) {
|
||||
ids.push(subscribe(optional[j], callback));
|
||||
}
|
||||
|
||||
return {unlisten: function () {
|
||||
unsubscribe(ids);
|
||||
}};
|
||||
};
|
||||
|
||||
/**
|
||||
* Starting subscriber id for new listeners
|
||||
* @type Number
|
||||
*/
|
||||
var subscriberId = 0;
|
||||
/**
|
||||
*
|
||||
* @type cache_L40.MapMap of listeners.
|
||||
*/
|
||||
var subscribersMap = new Map('id', 'trigger');
|
||||
/**
|
||||
* Defines a subscriber object.
|
||||
* @param {type} trigger
|
||||
* @param {type} callback
|
||||
* @param {type} id
|
||||
* @returns {undefined}
|
||||
*/
|
||||
function Subscriber(trigger, callback, id) {
|
||||
var subscriber = this;
|
||||
subscriber.trigger = trigger;
|
||||
subscriber.callback = callback;
|
||||
subscriber.id = id;
|
||||
}
|
||||
/**
|
||||
* Function for managing subscription map.
|
||||
* @param {type} trigger
|
||||
* @param {type} callback
|
||||
* @param {type} now
|
||||
* @returns {Number}
|
||||
*/
|
||||
function subscribe(trigger, callback) {
|
||||
if (trigger instanceof Trigger) {
|
||||
subscribersMap.put(subscriberId, new Subscriber(trigger, callback, subscriberId));
|
||||
return subscriberId++;
|
||||
} else {
|
||||
$log.error('Failed to listen on invalid trigger.');
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Function for itterating over ids and unsubscribing.
|
||||
* @param {type} ids
|
||||
* @returns {undefined}
|
||||
*/
|
||||
function unsubscribe(ids) {
|
||||
for (var i = 0; i < ids.length; i++) {
|
||||
subscribersMap.removeByKey(ids[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies cache listeners of an event, propagates the data passed in.
|
||||
* @param {type} trigger
|
||||
* @param {type} data
|
||||
* @returns {undefined}
|
||||
*/
|
||||
triggers.notify = function (trigger, data) {
|
||||
if (trigger instanceof Trigger) {
|
||||
var subsMap = subscribersMap.getGroup('trigger', trigger);
|
||||
for (var i = 0; i < subsMap.length; i++) {
|
||||
var sub = subsMap.values[i];
|
||||
call(sub.callback, trigger, data);
|
||||
}
|
||||
} else {
|
||||
$log.error('Failed to notify on invalid trigger.');
|
||||
}
|
||||
};
|
||||
|
||||
function call(callback, trigger, data) {
|
||||
window.setTimeout(function () {
|
||||
portalUtils.call(callback, trigger, data);
|
||||
}, 0);
|
||||
}
|
||||
|
||||
return triggers;
|
||||
} //end triggersService function
|
||||
})();
|
||||
Reference in new issue
Block a user