82 lines
3.3 KiB
JavaScript
82 lines
3.3 KiB
JavaScript
|
|
!function(t){"use strict";function e(){var e=this;e.reads=[],e.writes=[],e.raf=u.bind(t)}function n(t){t.scheduled||(t.scheduled=!0,t.raf(i.bind(null,t)))}function i(t){var e,i=t.writes,o=t.reads;try{o.length,r(o),i.length,r(i)}catch(t){e=t}if(t.scheduled=!1,(o.length||i.length)&&n(t),e){if(e.message,!t.catch)throw e;t.catch(e)}}function r(t){for(var e;e=t.shift();)e()}function o(t,e){var n=t.indexOf(e);return!!~n&&!!t.splice(n,1)}function s(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])}var u=t.requestAnimationFrame||t.webkitRequestAnimationFrame||t.mozRequestAnimationFrame||t.msRequestAnimationFrame||function(t){return setTimeout(t,16)};e.prototype={constructor:e,measure:function(t,e){var i=e?t.bind(e):t;return this.reads.push(i),n(this),i},mutate:function(t,e){var i=e?t.bind(e):t;return this.writes.push(i),n(this),i},clear:function(t){return o(this.reads,t)||o(this.writes,t)},extend:function(t){if("object"!=typeof t)throw new Error("expected object");var e=Object.create(this);return s(e,t),e.fastdom=this,e.initialize&&e.initialize(),e},catch:null};var exports=t.fastdom=t.fastdom||new e;"function"==typeof define?define(function(){return exports}):"object"==typeof module&&(module.exports=exports)}("undefined"!=typeof window?window:this);
|
||
|
|
|
||
|
|
|
||
|
|
!(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'|'muatate'
|
||
|
|
* @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;
|
||
|
|
|
||
|
|
})();
|
||
|
|
|
||
|
|
fastdom = fastdom.extend(fastdomPromised);
|