Skip to content
This repository has been archived by the owner on Dec 9, 2023. It is now read-only.

Commit

Permalink
update events
Browse files Browse the repository at this point in the history
  • Loading branch information
LukePulverenti committed Dec 16, 2015
1 parent 65c9f95 commit ac6b6e3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 70 deletions.
47 changes: 13 additions & 34 deletions apiclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,40 +39,18 @@
throw new Error('Invalid url: ' + val);
}

serverAddress = val;
}

return serverAddress;
};
var changed = val != serverAddress;

var callbacks = {};
self.on = function (name, fn) {
callbacks[name] = (callbacks[name] || []);
serverAddress = val;

if (callbacks[name].indexOf(fn) == -1) {
callbacks[name].push(fn);
if (changed) {
Events.trigger(this, 'serveraddresschanged');
}
}
};

self.off = function (name, fn) {

callbacks[name] = (callbacks[name] || []).filter(function (i) {
return i != fn;
});
return serverAddress;
};

function trigger(name, data) {

var eventObject = {
type: name,
detail: data
};

(callbacks[name] || []).forEach(function (c) {
c.call(self, eventObject);
});
}

self.serverInfo = function (info) {

serverInfo = info || serverInfo;
Expand Down Expand Up @@ -159,11 +137,12 @@

function onFetchFail(url, response) {

trigger('requestfail', {
Events.trigger(self, 'requestfail', [
{
url: url,
status: response.status,
errorCode: response.headers ? response.headers["X-Application-Error-Code"] : null
});
}]);
}

self.setRequestHeaders = function (headers) {
Expand Down Expand Up @@ -559,17 +538,17 @@

logger.log('web socket connection opened');
setTimeout(function () {
trigger('websocketopen');
Events.trigger(self, 'websocketopen');
}, 0);
};
webSocket.onerror = function () {
setTimeout(function () {
trigger('websocketerror');
Events.trigger(self, 'websocketerror');
}, 0);
};
webSocket.onclose = function () {
setTimeout(function () {
trigger('websocketclose');
Events.trigger(self, 'websocketclose');
}, 0);
};
};
Expand All @@ -594,7 +573,7 @@
}
}

trigger('websocketmessage', msg);
Events.trigger(self, 'websocketmessage', [msg]);
}

self.sendWebSocketMessage = function (name, data) {
Expand Down
44 changes: 8 additions & 36 deletions connectionmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,34 +209,6 @@
});
}

var callbacks = {};
self.on = function (name, fn) {
callbacks[name] = (callbacks[name] || []);

if (callbacks[name].indexOf(fn) == -1) {
callbacks[name].push(fn);
}
};

self.off = function (name, fn) {

callbacks[name] = (callbacks[name] || []).filter(function (i) {
return i != fn;
});
};

function trigger(name, data) {

var eventObject = {
type: name,
detail: data
};

(callbacks[name] || []).forEach(function (c) {
c.call(self, eventObject);
});
}

var connectUser;
self.connectUser = function () {
return connectUser;
Expand Down Expand Up @@ -341,7 +313,7 @@
credentialProvider.credentials(credentials);
}

trigger('apiclientcreated', apiClient);
Events.trigger(self, 'apiclientcreated', [apiClient]);

if (existingServer.Id) {
return;
Expand Down Expand Up @@ -373,7 +345,7 @@
function onConnectUserSignIn(user) {

connectUser = user;
trigger('connectusersignedin', user);
Events.trigger(self, 'connectusersignedin', [user]);
}

function getOrAddApiClient(server, connectionMode) {
Expand All @@ -394,7 +366,7 @@
onAuthenticated(instance, result, {}, true);
};

trigger('apiclientcreated', apiClient);
Events.trigger(self, 'apiclientcreated', [apiClient]);
}

logger.log('returning instance from getOrAddApiClient');
Expand Down Expand Up @@ -482,7 +454,7 @@

function onLocalUserSignIn(user) {

trigger('localusersignedin', user);
Events.trigger(self, 'localusersignedin', [user]);
}

function ensureConnectUser(credentials) {
Expand Down Expand Up @@ -747,7 +719,7 @@

if (connectUser) {
connectUser = null;
trigger('connectusersignedout');
Events.trigger(self, 'connectusersignedout');
}
});
};
Expand All @@ -762,10 +734,10 @@

return apiClient.logout().then(function () {

trigger('localusersignedout', logoutInfo);
Events.trigger(self, 'localusersignedout', [logoutInfo]);
}, function () {

trigger('localusersignedout', logoutInfo);
Events.trigger(self, 'localusersignedout', [logoutInfo]);
});
}

Expand Down Expand Up @@ -1170,7 +1142,7 @@

resolve(result);

trigger('connected', result);
Events.trigger(self, 'connected', [result]);
}

function normalizeAddress(address) {
Expand Down

0 comments on commit ac6b6e3

Please sign in to comment.