-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathangular-cloudtypes.js
274 lines (230 loc) · 7.76 KB
/
angular-cloudtypes.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* Created by ticup on 17/11/13.
*/
angular
.module('cloudtypes', [])
.service('$client', function ($window, $q) {
var status = 'disconnected';
var loggedIn = false;
// Callbacks
var connected = [];
var disconnected = [];
var reconnected = [];
var yieldclbcks = [];
var stopYielding = function () {};
// Start the periodic yielding (every ms)
function startYielding(ms) {
API.stopYielding();
ms = ms || 1000;
var yielding = setInterval(function () {
yieldclbcks.forEach(function (clbck) { clbck(); });
}, ms);
API.stopYielding = function() { clearInterval(yielding); };
return this;
}
// Add callback to periodic yielding
function onYield(clbck) {
yieldclbcks.push(clbck);
}
var u, p;
var API = {
// Yielding functions
stopYielding : function () { },
startYielding : startYielding,
onYield : onYield,
// CloudTypes Client
client: CloudTypes.createClient(),
connect: function connect(host, ms) {
this.client.connect(host, function (state) {
status = 'connected';
connected.forEach(function (clbck) { clbck(state); });
startYielding(ms);
onYield(function () { state.yield(); });
}, function (state) {
status = 'reconnected';
this.client.login(u, p, function (err, user) {
reconnected.forEach(function (clbck) { clbck(state); });
});
}, function () {
status = 'disconnected';
disconnected.forEach(function (clbck) { clbck(); });
});
return this;
},
reconnect: function reconnect() {
this.client.reconnect();
},
disconnect: function disconnect() {
this.client.disconnect();
},
isLoggedIn: function isLoggedIn() {
return loggedIn;
},
login: function login(username, password, callback) {
var deferred = $q.defer();
this.client.login(username, password, function (err, user) {
if (err)
return deferred.reject(err);
u = username;
p = password;
loggedIn = true;
deferred.resolve(user);
});
return deferred.promise;
},
register: function register(username, password, callback) {
var deferred = $q.defer();
this.client.register(username, password, function (err, user) {
if (err)
return deferred.reject(err);
loggedIn = true;
deferred.resolve(user);
});
return deferred.promise;
},
getUser: function getUser() {
return this.client.getUser();
},
// CloudTypes Client event handlers
onConnect: function (callback) {
connected.push(callback);
if (status == 'connected') {
callback();
}
},
onReconnect: function (callback) {
reconnected.push(callback);
if (status == 'reconnected') {
callback();
}
},
onDisconnect: function (callback) {
disconnected.push(callback);
if (status == 'disconnected') {
callback();
}
}
};
$window.client = API.client;
return API;
})
// Connects to the CloudTypes server and provides a promise for a state.
.service('$ctstate', function ($window, $q, $client) {
var deferred = $q.defer();
$client.connect($window.location.hostname, 1000)
.onConnect(function (state) {
$window.state = state; // debug
deferred.resolve(state);
});
return deferred.promise;
})
// Takes a function that produces an array of ArrayEntry or EntityEntry objects (produced by all()/entities(name) methods)
// and creates a cached array with it. Calling update on the array will execute the provided function to update the array, but the
// cached array will make sure existing entries (from a previous update) will be reused. This is kind of necessary for decent AngularJS
// integration, otherwise your whole UI will be redrawn every yield.
.service('$cachedArray', function () {
function CachedArray(getArray) {
this.getArray = getArray;
this.entries = {};
this.array = [];
this.update();
}
// updates the cached array by getting the new array, reusing exisiting entries and returning the new array to be used on the model.
CachedArray.prototype.update = function () {
var entries = this.entries;
var newEntries = {};
this.array = this.getArray().map(function (item) {
var idx = item.key();
var entry = entries[idx];
if (entry)
item = entry;
newEntries[idx] = item;
return item;
});
this.entries = newEntries;
return this.array;
};
return {
create: function (getArray) {
return new CachedArray(getArray);
}
};
})
.service('$Index', function () {
return CloudTypes.Index;
})
.service('$Table', function () {
return CloudTypes.Table;
})
.service('$CInt', function () {
return CloudTypes.CInt;
})
.service('$CString', function () {
return CloudTypes.CString;
});
// .service('$cachedRows', function () {
// function CachedRows(getRows) {
// this.getRows = getRows;
// this.entries = {};
// this.tables = [];
// this.update();
// }
// // updates the cached tables by getting the new array of tables, reusing exisiting entries and returning the new tables to be used on the model.
// CachedTables.prototype.update = function () {
// var entries = this.entries;
// var newEntries = {};
// this.tables = this.getTables().map(function (table) {
// var idx = table.name;
// var entry = entries[idx];
// if (entry)
// table = entry;
// newEntries[idx] = table;
// return table;
// });
// this.entries = newEntries;
// return this.tables;
// };
// return {
// create: function (getTables) {
// return new CachedTables(getTables);
// }
// };
// })
// .service('$cachedTables', function () {
// function CachedTables(getTables) {
// this.getTables = getTables;
// this.entries = {};
// this.tables = [];
// this.rows = {};
// this.update();
// }
// // updates the cached tables by getting the new array of tables, reusing exisiting entries and returning the new tables to be used on the model.
// CachedTables.prototype.update = function () {
// var entries = this.entries;
// var rows = this.rows;
// var newEntries = {};
// this.tables = this.getTables().map(function (table) {
// var idx = table.name;
// var entry = entries[idx];
// if (entry)
// table = entry;
// newEntries[idx] = table;
// if (typeof rows === 'undefined') {
// }
// table.all().forEach(function (row) {
// var key = row.serialKey();
// var entry = rows[idx];
// if (entry)
// row = entry;
// });
// return table;
// });
// this.entries = newEntries;
// return this.tables;
// };
// return {
// create: function (getTables) {
// return new CachedTables(getTables);
// }
// };
// });