Browse Source

Refactor the whole promise thing

feature/remove-domains
Jonathan Muller 9 years ago
parent
commit
a4e2eb5725
  1. 34
      lib/caching.js
  2. 6
      lib/callback_filler.js
  3. 108
      lib/multi_caching.js

34
lib/caching.js

@ -42,6 +42,24 @@ var caching = function(args) {
}; };
} }
function wrapPromise(key, promise, options) {
return new Promise(function(resolve, reject) {
self.wrap(key, function(cb) {
Promise.resolve()
.then(promise)
.then(function(result) {
cb(null, result);
})
.catch(cb);
}, options, function(err, result) {
if (err) {
return reject(err);
}
resolve(result);
});
});
}
/** /**
* Wraps a function in cache. I.e., the first time the function is run, * Wraps a function in cache. I.e., the first time the function is run,
* its results are stored in cache so subsequent calls retrieve from cache * its results are stored in cache so subsequent calls retrieve from cache
@ -70,14 +88,7 @@ var caching = function(args) {
} }
if (!cb) { if (!cb) {
cb = Promise.defer(); return wrapPromise(key, work, options);
var work2 = work;
work = function(cb) {
Promise.resolve().then(work2).then(function(res) {
cb(null, res);
})
.catch(cb);
};
} }
var hasKey = callbackFiller.has(key); var hasKey = callbackFiller.has(key);
@ -102,9 +113,6 @@ var caching = function(args) {
} }
if (!self._isCacheableValue(data)) { if (!self._isCacheableValue(data)) {
if (typeof cb === 'object') {
return cb.resolve(data);
}
return cb(); return cb();
} }
@ -118,10 +126,6 @@ var caching = function(args) {
}); });
} }
}); });
if (typeof cb === 'object') {
return cb.promise;
}
}; };
/** /**

6
lib/callback_filler.js

@ -12,12 +12,6 @@ CallbackFiller.prototype.fill = function(key, err, data) {
waiting.forEach(function(task) { waiting.forEach(function(task) {
var taskDomain = task.domain || domain.create(); var taskDomain = task.domain || domain.create();
if (typeof task.cb === 'object') {
if (err) {
return taskDomain.bind(task.cb.reject)(err);
}
return taskDomain.bind(task.cb.resolve)(data);
}
taskDomain.bind(task.cb)(err, data); taskDomain.bind(task.cb)(err, data);
}); });
}; };

108
lib/multi_caching.js

@ -47,16 +47,25 @@ var multiCaching = function(caches, options) {
} }
} }
function getFromHighestPriorityCachePromise(key, options) {
return new Promise(function(resolve, reject) {
getFromHighestPriorityCache(key, options, function(err, result) {
if (err) {
return reject(err);
}
resolve(result);
});
});
}
function getFromHighestPriorityCache(key, options, cb) { function getFromHighestPriorityCache(key, options, cb) {
if (typeof options === 'function') { if (typeof options === 'function') {
cb = options; cb = options;
options = {}; options = {};
} }
var promised = false;
if (!cb) { if (!cb) {
cb = Promise.defer(); return getFromHighestPriorityCachePromise(key, options);
promised = true;
} }
var i = 0; var i = 0;
@ -70,10 +79,7 @@ var multiCaching = function(caches, options) {
if (_isCacheableValue(result)) { if (_isCacheableValue(result)) {
// break out of async loop. // break out of async loop.
if (!promised) { return cb(err, result, i);
return cb(err, result, i);
}
return cb.resolve(result);
} }
i += 1; i += 1;
@ -82,25 +88,26 @@ var multiCaching = function(caches, options) {
cache.store.get(key, options, callback); cache.store.get(key, options, callback);
}, function(err, result) { }, function(err, result) {
if (!promised) { return cb(err, result);
return cb(err, result);
}
return (err) ? cb.reject(err) : cb.resolve(result);
}); });
}
if (promised) { function setInMultipleCachesPromise(caches, opts) {
return cb.promise; return new Promise(function(resolve, reject) {
} setInMultipleCaches(caches, opts, function(err, result) {
if (err) {
return reject(err);
}
resolve(result);
});
});
} }
function setInMultipleCaches(caches, opts, cb) { function setInMultipleCaches(caches, opts, cb) {
opts.options = opts.options || {}; opts.options = opts.options || {};
var promised = false;
if (!cb) { if (!cb) {
promised = true; return setInMultipleCachesPromise(caches, opts);
cb = Promise.defer();
} }
async.each(caches, function(cache, next) { async.each(caches, function(cache, next) {
@ -112,16 +119,19 @@ var multiCaching = function(caches, options) {
next(); next();
} }
}, function(err, result) { }, function(err, result) {
if (promised) { cb(err, result);
return (err) ? cb.reject(err) : cb.resolve(result);
} else {
cb(err, result);
}
}); });
}
if (promised) { function getAndPassUpPromise(key) {
return cb.promise; return new Promise(function(resolve, reject) {
} self.getAndPassUp(key, function(err, result) {
if (err) {
return reject(err);
}
resolve(result);
});
});
} }
/** /**
@ -132,15 +142,13 @@ var multiCaching = function(caches, options) {
* @param {function} cb * @param {function} cb
*/ */
self.getAndPassUp = function(key, cb) { self.getAndPassUp = function(key, cb) {
var promised = false;
if (!cb) { if (!cb) {
promised = true; return getAndPassUpPromise(key);
cb = Promise.defer();
} }
getFromHighestPriorityCache(key, function(err, result, index) { getFromHighestPriorityCache(key, function(err, result, index) {
if (err) { if (err) {
return (!promised) ? cb(err) : cb.reject(err); return cb(err);
} }
if (index) { if (index) {
@ -154,14 +162,28 @@ var multiCaching = function(caches, options) {
}); });
} }
return (!promised) ? cb(err, result) : cb.resolve(result); return cb(err, result);
}); });
if (promised) {
return cb.promise;
}
}; };
function wrapPromise(key, promise, options) {
return new Promise(function(resolve, reject) {
self.wrap(key, function(cb) {
Promise.resolve()
.then(promise)
.then(function(result) {
cb(null, result);
})
.catch(cb);
}, options, function(err, result) {
if (err) {
return reject(err);
}
resolve(result);
});
});
}
/** /**
* Wraps a function in one or more caches. * Wraps a function in one or more caches.
* Has same API as regular caching module. * Has same API as regular caching module.
@ -192,14 +214,7 @@ var multiCaching = function(caches, options) {
} }
if (!cb) { if (!cb) {
cb = Promise.defer(); return wrapPromise(key, work, options);
var work2 = work;
work = function(cb) {
Promise.resolve().then(work2).then(function(res) {
cb(null, res);
})
.catch(cb);
};
} }
var hasKey = callbackFiller.has(key); var hasKey = callbackFiller.has(key);
@ -230,9 +245,6 @@ var multiCaching = function(caches, options) {
} }
if (!self._isCacheableValue(data)) { if (!self._isCacheableValue(data)) {
if (typeof cb === 'object') {
return cb.resolve(data);
}
return cb(); return cb();
} }
@ -244,10 +256,6 @@ var multiCaching = function(caches, options) {
}); });
} }
}); });
if (typeof cb === 'object') {
return cb.promise;
}
}; };
/** /**

Loading…
Cancel
Save