/
intercal.require.js
667 lines (534 loc) · 19.7 KB
/
intercal.require.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/*global define, console, window*/
define(["path/to/jquery"], function ($) {
"use strict";
var intercal;
function buildDeferreds(onces, obj, external, isTopLevel) {
var key, value, names, i, name;
for (key in onces) {
value = onces[key];
if (isTopLevel && key === "reset") {
throw new intercal.Error("using reserved name for toplevel deferred: " + key);
}
if ($.isPlainObject(value)) {
// if the value is an object create the nested deferreds
external[key] = {};
obj[key] = buildDeferreds(value, {}, external[key]);
} else if (value === "") {
// if the value is an empty string construct the deferred here
obj[key] = $.Deferred();
external[key] = obj[key].promise();
} else {
// if the value is a space separated string construct
// the child deferreds
names = onces[key].split(/\s+/);
obj[key] = {};
external[key] = {};
for (i = 0; i < names.length; i += 1) {
name = names[i];
if (name !== "") {
obj[key][name] = $.Deferred();
external[key][name] = obj[key][name].promise();
}
}
}
}
return obj;
}
function makePublicCallbackAPI(callback) {
return {
// NOTE: you can reach the original callback with the this parameter
// on your callback, not sure if it's worth preventing
"add": callback.add,
"has": callback.has,
"remove": callback.remove,
"locked": callback.locked
};
}
function buildCallbacks(events, obj, external) {
var key, value, names, i, name, callback, listener,
globalListener = $.Callbacks();
function listenCallback(callback, listener) {
listener = listener || $.Callbacks();
callback.add(listener.fire);
return listener;
}
for (key in events) {
value = events[key];
listener = $.Callbacks();
if (key === "then") {
throw new intercal.Error("using reserved name for callback: " + key);
}
if ($.isPlainObject(value)) {
// if the value is an object create the nested callbacks
external[key] = {};
obj[key] = buildCallbacks(value, {}, external[key]);
obj[key].then(listener.fire);
} else if (value === "") {
// if the value is an empty string construct the callback here
obj[key] = $.Callbacks();
external[key] = makePublicCallbackAPI(obj[key]);
listenCallback(obj[key], listener);
} else {
// if the value is a space separated string construct
// the child callbacks
names = events[key].split(/\s+/);
obj[key] = {"then": listener.add};
external[key] = {"then": listener.add};
for (i = 0; i < names.length; i += 1) {
name = names[i];
if (name === "then") {
throw new intercal.Error("using reserved name for callback: " + name);
} else if (name !== "") {
obj[key][name] = $.Callbacks();
external[key][name] = makePublicCallbackAPI(obj[key][name]);
listenCallback(obj[key][name], listener);
}
}
}
obj[key].then = listener.add;
external[key].then = listener.add;
listener.add(globalListener.fire);
}
obj.then = globalListener.add;
external.then = globalListener.add;
return obj;
}
function buildRequester(intercalInstance, path, method, options, noBodyMethod) {
if (noBodyMethod) {
return function (params, callOptions) {
var
interpolatedPath = intercal.template(path, params),
mergedOptions = $.extend(true, {}, options || {}, callOptions || {});
return intercalInstance.request(interpolatedPath, null, method, mergedOptions);
};
} else {
return function (body, params, callOptions) {
var
interpolatedPath = intercal.template(path, params),
mergedOptions = $.extend(true, {}, options || {}, callOptions || {});
return intercalInstance.request(interpolatedPath, body, method, mergedOptions);
};
}
}
function buildRequesters(methods, options, intercalInstance) {
var key, method, path, name, obj = {}, isNoBodyMethod;
for (key in methods) {
path = methods[key];
method = key.toUpperCase();
isNoBodyMethod = $.inArray(method, intercal._.noBodyMethods) !== -1;
// if method has no map in httpMethodMap use the original key
// to preserve case
name = intercal._.httpMethodMap[method] || key;
obj[name] = buildRequester(intercalInstance, path, method, options, isNoBodyMethod);
}
return obj;
}
function buildResourceHandlers(resources, obj, globalConfig, intercalInstance) {
var key, config, resource, method, methods = {}, parts, i;
for (key in resources) {
resource = resources[key];
// merge global config with resource-specific config
config = $.extend(true, {}, globalConfig, resource.config || {});
if (typeof resource.path === "string") {
methods = {
"get": resource.path,
"post": resource.path,
"put": resource.path,
"delete": resource.path
};
} else if ($.isPlainObject(resource.path)) {
for (method in resource.path) {
parts = method.split(/\s+/);
for (i = 0; i < parts.length; i += 1) {
if (parts[i] === "") {
continue;
}
methods[parts[i]] = resource.path[method];
}
}
} else {
throw new intercal.Error("resource path missing");
}
obj[key] = buildRequesters(methods, config, intercalInstance);
}
}
function now() {
return (new Date()).getTime();
}
function formatParams(options) {
var str = "", first = true, option, key;
for (key in options) {
if (first) {
str += "?";
}
option = options[key];
if (option !== undefined && option !== null) {
if (!first) {
str += "&";
}
str += key + "=" + option;
}
first = false;
}
return str;
}
// join a list of path parts with slashes
// the last parameter can be an object with the params to
// be appended
function joinPath() {
var i, current, accum = [];
for (i = 0; i < arguments.length; i += 1) {
current = arguments[i];
if (typeof current === "string") {
if (i !== 0 && current.charAt(0) === "/") {
current = current.slice(1);
}
if (current.charAt(current.length - 1) === "/") {
current = current.slice(0, current.length - 1);
}
accum.push(current);
} else {
// if it's not a string it should be a plain object
// with the params
// return here with the path since it should be the
// last argument
return accum.join("/") + formatParams(current);
}
}
return accum.join("/");
}
function joinPathList(paths, params) {
return joinPath.apply(null, paths.concat([params || {}]));
}
// parse path given as param, if undefined parse the current path
// in the location variable
function parsePath(path) {
var i, parts, valparts, result = {}, key, value, pathname, query;
if (path === undefined) {
pathname = location.pathname;
query = location.search.slice(1);
} else {
parts = path.split("?");
pathname = parts[0];
query = parts[1] || "";
}
parts = query.split("&");
for (i = 0; i < parts.length; i += 1) {
valparts = parts[i].split("=");
if (valparts.length !== 2) {
continue;
}
key = $.trim(valparts[0]);
value = $.trim(valparts[1]);
result[key] = value;
}
return {
"path": pathname,
"params": result
};
}
function addParam(path, name, value) {
var sep;
if (path.indexOf("?") !== -1) {
sep = "&";
} else {
sep = "?";
}
return path + sep + name + "=" + value;
}
function request(path, body, method, options) {
var opts = {}, tvar;
if (options.basePath) {
path = intercal.path.join(options.basePath, path);
}
if (options.addTimestampParam) {
path = addParam(path, options.timestampParamName || "t", intercal.now());
}
if (body) {
if (options.contentType === "application/json") {
opts.data = JSON.stringify(body);
} else {
opts.data = body;
}
}
if (options.contentType) {
opts.contentType = options.contentType;
}
opts.type = method || "GET";
if (options.timeout) {
opts.timeout = options.timeout;
}
return $.ajax(path, opts);
}
intercal = function (data) {
data = data || {};
var
// once definitions
onces = data.once || {},
// event definitions
events = data.on || {},
// resource definitions
resources = data.resource || {},
// the object to be returned
obj = {
"once": {},
"on": {},
"resource": {}
},
resetCallback,
// external deferred API
onceAPI = {},
// external callback API
eventAPI = {},
// flag that should update onceAPI
shouldUpdateOnceAPI = false;
buildCallbacks(events, obj.on, eventAPI);
buildResourceHandlers(resources, obj.resource, data.resourceConfig || {}, obj);
resetCallback = $.Callbacks();
function reset() {
onceAPI = {};
obj.once = buildDeferreds(onces, {"reset": obj.once.reset || reset}, onceAPI, true);
shouldUpdateOnceAPI = true;
resetCallback.fire();
}
function buildOnceAPI() {
return onceAPI;
}
function buildOnAPI(events) {
return eventAPI;
}
reset.done = resetCallback.add;
// build deferreds for the first time
reset();
obj.api = {
once: buildOnceAPI,
on: buildOnAPI
};
obj.request = request;
return obj;
};
intercal.barrier = function (itemCount, timeout, waitCompletedCount) {
var
// if it doesn't accept more additions
locked = false,
// items to wait for
items = [],
// items that will be removed once they are completed
volatileItems = [],
// total number of items after it's locked
total = 0,
// number of completed items
completed = 0,
// time it was locked
startTime = 0,
// time all items were completed
endTime = -1,
// the deferred to use
barrier = $.Deferred(),
// callback list for timeout
timeoutList = $.Callbacks("once memory"),
// the id from the timeout to cancel it later
timeoutId = null,
// flag to know if it already timed out
timedOut = false,
// list of list of arguments from failed deferreds
failedArgs = [],
// the promise for the barrier
promise,
// object to return from this function
obj,
tmpItems;
itemCount = itemCount || 0;
timeout = timeout || 0;
function fireTimeout() {
timedOut = true;
timeoutList.fire();
barrier.reject('timeout');
}
function checkCompletion() {
if (timedOut) {
return;
}
// if all completed
if (waitCompletedCount === (total - volatileItems.length)) {
if (timeoutId !== null) {
clearTimeout(timeoutId);
}
endTime = intercal.now();
if (failedArgs.length === 0) {
barrier.resolve();
} else {
barrier.reject(failedArgs);
}
}
}
function lock() {
if (locked) {
return;
}
if (itemCount > 0 && items.length !== itemCount) {
throw new intercal.Error("trying to lock a barrier before reaching itemCount");
}
locked = true;
total = items.length;
startTime = intercal.now();
if (waitCompletedCount === 0) {
waitCompletedCount = total;
}
if (timeout > 0) {
timeoutId = setTimeout(fireTimeout, timeout);
}
checkCompletion();
return obj;
}
function arrayRemove(arr, element) {
var i;
for (i = 0; i < arr.length; i += 1) {
if (element === arr[i]) {
arr.splice(i, 1);
}
}
return arr;
}
function listenForCompletion(action) {
function actionFired() {
arrayRemove(volatileItems, action);
// unsubscribe if it's a callback
if (action.add && action.has) {
action.remove(actionFired);
}
if (locked) {
checkCompletion();
}
}
if (action.add && action.has) {
// if is a callbacks object
action.add(actionFired);
} else {
// it must be a deferred/promise
action.done(actionFired);
action.fail(function () {
failedArgs.push($.makeArray(arguments));
actionFired();
});
}
}
function add(item) {
var i, action;
if (!$.isArray(item)) {
item = [item];
}
if (!locked) {
for (i = 0; i < item.length; i += 1) {
action = item[i];
items.push(action);
volatileItems.push(action);
listenForCompletion(action);
if (items.length === itemCount) {
lock();
break;
}
}
}
return obj;
}
// if the first parameter is an array, add the items and lock
// set the length of the array to itemCount
if ($.isArray(itemCount)) {
tmpItems = itemCount;
itemCount = tmpItems.length;
add(tmpItems);
}
// number of items to wait for completion, by default wait until all
// are completed
waitCompletedCount = waitCompletedCount || itemCount;
if (waitCompletedCount > itemCount) {
throw new intercal.Error("waitCompletedCount can't be bigger than itemCount, will wait forever: " + waitCompletedCount + " > " + itemCount);
}
promise = barrier.promise({timeout: timeoutList.add});
obj = barrier.promise({
lock: lock,
locked: function () {
return locked;
},
add: add,
timeout: timeoutList.add,
status: function () {
var completed = total - volatileItems.length,
totalTime = -1,
remainingTime = -1;
if (endTime > 0) {
totalTime = endTime - startTime;
}
return {
// total number of items waiting for
total: total,
// number of items that already completed
completed: completed,
// number of items still waiting for
remaining: total - completed,
// time the barrier was locked
startTime: startTime,
// time running since the barrier was locked
ellapsedTime: (endTime > 0) ? totalTime : intercal.now() - startTime,
// time when the barrier finished
endTime: endTime,
// total time between start and end
totalTime: totalTime,
// time remaining before timeout or -1 if already finished or timed out
remainingTime: (endTime < 1 && timeout > 0) ? intercal.now() - (startTime + timeout) : -1,
// true if the barrier finished in some way
finished: locked && itemCount > 0 && volatileItems.length === 0,
// true if the barrier timedOut
timedOut: timedOut
};
}
});
// override the promise function from obj (added by barrier.promise)
// since we want the timeout function in the promise too
obj.promise = function () {
return promise;
};
return obj;
};
// the global default request function
// you can override it in your instance and then get back the original
// here
intercal.request = request;
intercal.all = intercal.barrier;
intercal.any = function (items, count, timeout) {
return intercal.barrier(items, timeout || 0, count || 1);
};
// internal data exposed for testing and possible customization
// may change in the future
intercal._ = {
"httpMethodMap": {
"POST": "create",
"PUT": "update",
"DELETE": "remove",
"GET": "get"
},
"noBodyMethods": ["GET", "DELETE", "HEAD"]
};
intercal.path = {
join: joinPath,
joinList: joinPathList,
parse: parsePath
};
intercal.template = function interpolate(str, vars) {
if (!vars) {
return str;
}
return str.replace(/\{\w*?\}/g, function (match, v, t) {
var varname = match.slice(1, match.length - 1);
return vars[varname] || match;
});
};
intercal.now = now;
intercal.Error = function (message) {
this.name = "intercal.Error";
this.message = message;
};
$.intercal = intercal;
return intercal;
});