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,
* its results are stored in cache so subsequent calls retrieve from cache
@ -70,14 +88,7 @@ var caching = function(args) {
}
if (!cb) {
cb = Promise.defer();
var work2 = work;
work = function(cb) {
Promise.resolve().then(work2).then(function(res) {
cb(null, res);
})
.catch(cb);
};
return wrapPromise(key, work, options);
}
var hasKey = callbackFiller.has(key);
@ -102,9 +113,6 @@ var caching = function(args) {
}
if (!self._isCacheableValue(data)) {
if (typeof cb === 'object') {
return cb.resolve(data);
}
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) {
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);
});
};

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

Loading…
Cancel
Save