-
Notifications
You must be signed in to change notification settings - Fork 239
/
fastdom-promised.js
77 lines (68 loc) · 1.68 KB
/
fastdom-promised.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
!(function() {
/**
* Wraps fastdom in a Promise API
* for improved control-flow.
*
* @example
*
* // returning a result
* fastdom.measure(() => el.clientWidth)
* .then(result => ...);
*
* // returning promises from tasks
* fastdom.measure(() => {
* var w = el1.clientWidth;
* return fastdom.mutate(() => el2.style.width = w + 'px');
* }).then(() => console.log('all done'));
*
* // clearing pending tasks
* var promise = fastdom.measure(...)
* fastdom.clear(promise);
*
* @type {Object}
*/
var exports = {
initialize: function() {
this._tasks = new Map();
},
mutate: function(fn, ctx) {
return create(this, 'mutate', fn, ctx);
},
measure: function(fn, ctx) {
return create(this, 'measure', fn, ctx);
},
clear: function(promise) {
var tasks = this._tasks;
var task = tasks.get(promise);
this.fastdom.clear(task);
tasks.delete(promise);
}
};
/**
* Create a fastdom task wrapped in
* a 'cancellable' Promise.
*
* @param {FastDom} fastdom
* @param {String} type - 'measure'|'mutate'
* @param {Function} fn
* @return {Promise}
*/
function create(promised, type, fn, ctx) {
var tasks = promised._tasks;
var fastdom = promised.fastdom;
var task;
var promise = new Promise(function(resolve, reject) {
task = fastdom[type](function() {
tasks.delete(promise);
try { resolve(ctx ? fn.call(ctx) : fn()); }
catch (e) { reject(e); }
}, ctx);
});
tasks.set(promise, task);
return promise;
}
// Expose to CJS, AMD or global
if ((typeof define)[0] == 'f') define(function() { return exports; });
else if ((typeof module)[0] == 'o') module.exports = exports;
else window.fastdomPromised = exports;
})();