-
Notifications
You must be signed in to change notification settings - Fork 0
/
actionhero-client-provider.js
184 lines (165 loc) · 6.48 KB
/
actionhero-client-provider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
(function() {
'use strict';
/*jshint -W030*/
/**
* @ngdoc service
* @name actionhero.provider:ActionHeroClient
*
* @description
*
*/
angular
.module('actionhero', [])
.provider('ActionHeroClient', ActionHeroClientService);
function ActionHeroClientService() {
this.autoConnect = true;
this.waitOnActions = true;
this.skipDebug = true;
this.skipConsole = false;
this.$get = function($window, $q, $rootScope, $timeout) {
var self = this;
var apiClient = new $window.ActionheroClient(); //defined from external script
apiClient.on('disconnected', function() {
self.skipConsole || console.log('[AH] disconnected :(');
$rootScope.$emit('ah-disconnected');
//TODO: Event to show disconnection
//TODO: Service connection to change userData
// app.$.toast.text = '[API] Disconnected from API server';
// app.$.toast.show();
// app.userData.username = null;
});
apiClient.on('error', function(err) {
$rootScope.$emit('ah-error', err);
self.skipConsole || console.error('[AH] error', err.stack);
//TODO: Event to show
});
apiClient.on('reconnect', function() {
self.skipConsole || console.log('[AH] reconnect');
$rootScope.$emit('ah-reconnect');
$rootScope.$emit('ah-' + apiClient.state);
//TODO: Event to show reconnection
// app.$.toast.text = '[API] Reconnected to API server';
// app.$.toast.show();
// app.gameState.validateToken(function() {});
});
apiClient.on('reconnecting', function() {
self.skipConsole || console.log('[AH] reconnecting');
$rootScope.$emit('ah-reconnecting');
$rootScope.$emit('ah-disconnected');
//TODO: Event to show
});
// this will log all messages send the apiClient
//apiClient.on('message', function(message){ console.log(message) })
apiClient.on('alert', function(message) {
$rootScope.$emit('ah-alert', message);
self.skipConsole || console.error('[AH] ' + JSON.stringify(message));
});
apiClient.on('api', function(message) {
$rootScope.$emit('ah-api', message);
self.skipConsole || console.error('[AH] api: ' + JSON.stringify(message));
});
apiClient.on('welcome', function(message) {
self.skipConsole || console.log('[AH] welcome: ' + JSON.stringify(message));
});
apiClient.on('say', function(data) {
$rootScope.$emit('ah-say', data);
/*jshint -W030*/
self.skipDebug || console.log('[AH] say: ' + JSON.stringify(data));
});
var ActionHeroBase = {};
ActionHeroBase.someValue = 'ActionHero';
ActionHeroBase._client = apiClient;
ActionHeroBase.waitOnActions = this.waitOnActions;
ActionHeroBase.isConnected = function() {
return apiClient.state === 'connected';
};
//Valuable as a method to hold up processing prior to connections
ActionHeroBase.connect = function() {
//console.log('[AH] connect');
var deferred = $q.defer();
apiClient.connect(function(err, details) {
//console.log('[AH] connect: err=' + JSON.stringify(err) + ' details=' + JSON.stringify(details));
if (err !== null) {
console.error(err);
$rootScope.$emit('ah-connect-error');
deferred.reject(err);
} else {
self.skipConsole || console.log('[AH] connection details: ' + JSON.stringify(details));
self.skipConsole || console.log('[AH] Connected to actionhero server.');
ActionHeroBase.id = details.data.id;
$rootScope.$emit('ah-connected', details);
deferred.resolve(true);
}
});
return deferred.promise;
};
ActionHeroBase.waitConnected = function(timeoutMs) {
var deferred = $q.defer();
if (this.isConnected()) {
//console.log('[AH] waitConnected: already connected');
//already connected
deferred.resolve(true);
} else {
timeoutMs = timeoutMs || 10000;
var timeoutRequest = null;
//console.log('[AH] waitConnected: waiting');
var unregisterFunc = $rootScope.$on('ah-connected', function() {
//console.log('[AH] waitConnected: connected!');
deferred.resolve(true);
unregisterFunc();
$timeout.cancel(timeoutRequest);
});
timeoutRequest = $timeout(function() {
//console.log('[AH] waitConnected: timeout fired after ' + timeoutMs + 'ms');
deferred.reject('Timeout connecting to server.');
unregisterFunc();
}, timeoutMs);
}
return deferred.promise;
};
ActionHeroBase.runAction = function(action, params) {
if (this.waitOnActions) {
//console.log('[AH] runAction: checking connection before running ' + action);
return this.waitConnected()
.then(function() {
//console.log('[AH] runAction: running with params ' + JSON.stringify(params));
return $q(function(resolve, reject) {
apiClient.action(action, params, function(resp) {
//console.log('[AH] runAction: resp=' + JSON.stringify(resp));
if (resp.error) {
reject(resp);
} else {
resolve(resp);
}
});
});
});
} else {
//console.log('[AH] runAction: (no wait) running with params ' + JSON.stringify(params));
return $q(function(resolve, reject) {
apiClient.action(action, params, function(resp) {
//console.log('[AH] runAction: resp=' + JSON.stringify(resp));
if (resp.error) {
reject(resp);
} else {
resolve(resp);
}
});
});
}
};
ActionHeroBase.subscribe = function(scope, eventName, eventCallback) {
var handler = $rootScope.$on(eventName, eventCallback);
scope.$on('$destroy', handler);
return handler; //Call this function to unsubscribe early
};
if (this.autoConnect) {
self.skipConsole || console.log('[AH] auto-connecting');
ActionHeroBase.connect();
} else {
self.skipConsole || console.log('[AH] not connecting on start');
}
return ActionHeroBase;
};
}
}());