From d6f8c675800dca0ef9b64298745d42c0b5de048b Mon Sep 17 00:00:00 2001 From: Sidhartha Chatterjee Date: Tue, 24 May 2016 23:50:36 +0530 Subject: [PATCH] Allow passing in a promise dependency --- .jshintrc | 1 - lib/caching.js | 2 ++ lib/multi_caching.js | 2 ++ lib/stores/memory.js | 1 + test/caching.unit.js | 4 +++- test/multi_caching.unit.js | 8 +++++--- test/run.js | 4 ---- 7 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.jshintrc b/.jshintrc index fb5dc81..d13b44d 100644 --- a/.jshintrc +++ b/.jshintrc @@ -20,7 +20,6 @@ "predef" : [ // Extra globals. "__dirname", - "Promise", "Buffer", "event", "exports", diff --git a/lib/caching.js b/lib/caching.js index af2dc93..2116637 100644 --- a/lib/caching.js +++ b/lib/caching.js @@ -29,6 +29,8 @@ var caching = function(args) { // do we handle a cache error the same as a cache miss? self.ignoreCacheErrors = args.ignoreCacheErrors || false; + var Promise = args.promiseDependency || global.Promise; + var callbackFiller = new CallbackFiller(); if (typeof args.isCacheableValue === 'function') { diff --git a/lib/multi_caching.js b/lib/multi_caching.js index 1e94f3d..c831cb3 100644 --- a/lib/multi_caching.js +++ b/lib/multi_caching.js @@ -19,6 +19,8 @@ var multiCaching = function(caches, options) { var self = {}; options = options || {}; + var Promise = options.promiseDependency || global.Promise; + if (!Array.isArray(caches)) { throw new Error('multiCaching requires an array of caches'); } diff --git a/lib/stores/memory.js b/lib/stores/memory.js index dad43dc..25963c2 100644 --- a/lib/stores/memory.js +++ b/lib/stores/memory.js @@ -4,6 +4,7 @@ var memoryStore = function(args) { args = args || {}; var self = {}; self.name = 'memory'; + var Promise = args.promiseDependency || global.Promise; self.usePromises = (typeof Promise === 'undefined' || args.noPromises) ? false : true; var ttl = args.ttl; diff --git a/test/caching.unit.js b/test/caching.unit.js index 394ee4d..348506f 100644 --- a/test/caching.unit.js +++ b/test/caching.unit.js @@ -8,6 +8,8 @@ var checkErr = support.checkErr; var caching = require('../index').caching; var memoryStore = require('../lib/stores/memory'); +var Promise = require('es6-promise').Promise; + var methods = { getWidget: function(name, cb) { process.nextTick(function() { @@ -27,7 +29,7 @@ describe("caching", function() { ['memory'].forEach(function(store) { context("using " + store + " store", function() { beforeEach(function() { - cache = caching({store: store}); + cache = caching({store: store, promiseDependency: Promise}); key = support.random.string(20); value = support.random.string(); }); diff --git a/test/multi_caching.unit.js b/test/multi_caching.unit.js index 77ece21..a21f579 100644 --- a/test/multi_caching.unit.js +++ b/test/multi_caching.unit.js @@ -7,6 +7,8 @@ var caching = require('../index').caching; var multiCaching = require('../index').multiCaching; var memoryStore = require('../lib/stores/memory'); +var Promise = require('es6-promise').Promise; + var methods = { getWidget: function(name, cb) { process.nextTick(function() { @@ -29,9 +31,9 @@ describe("multiCaching", function() { memoryTtl = 0.1; defaultTtl = 5; - memoryCache = caching({store: 'memory', ttl: memoryTtl}); - memoryCache2 = caching({store: 'memory', ttl: memoryTtl}); - memoryCache3 = caching({store: 'memory', ttl: memoryTtl}); + memoryCache = caching({store: 'memory', ttl: memoryTtl, promiseDependency: Promise}); + memoryCache2 = caching({store: 'memory', ttl: memoryTtl, promiseDependency: Promise}); + memoryCache3 = caching({store: 'memory', ttl: memoryTtl, promiseDependency: Promise}); key = support.random.string(20); name = support.random.string(); diff --git a/test/run.js b/test/run.js index 7aac560..2bfa5c0 100755 --- a/test/run.js +++ b/test/run.js @@ -6,10 +6,6 @@ var Mocha = require('mocha'); var optimist = require('optimist'); var walkDir = require('./support').walkDir; -if (typeof Promise === "undefined") { - global.Promise = require('es6-promise').Promise; -} - var argv = optimist .usage("Usage: $0 -t [types] --reporter [reporter] --timeout [timeout]")['default']( {types: 'unit,functional', reporter: 'spec', timeout: 6000})