Comparing version 0.3.0 to 0.4.0
112
index.js
@@ -27,3 +27,2 @@ "use strict"; | ||
{ | ||
// If specific key clear | ||
if (url != null) | ||
@@ -38,19 +37,6 @@ { | ||
} | ||
if (this.callbacks[url] !== undefined) | ||
{ | ||
delete this.callbacks[url]; | ||
} | ||
if (this.settingStore[url] !== undefined) | ||
{ | ||
delete this.settingStore[url]; | ||
} | ||
} | ||
// Clear all keys | ||
else | ||
{ | ||
this.cache = {}; | ||
this.callbacks = {}; | ||
this.settingStore = {}; | ||
} | ||
@@ -61,43 +47,14 @@ }; | ||
UrlCache.prototype.contains = function(url) | ||
UrlCache.prototype.get = function(url) | ||
{ | ||
url = parseUrl(url, this.options); | ||
url = stringifyUrl(url); | ||
url = formatUrl(url, this.options); | ||
removeOld(url, this); | ||
removeExpired(url, this.cache); | ||
return this.cache[url]!==undefined || this.settingStore[url]!==undefined; | ||
}; | ||
UrlCache.prototype.get = function(url, callback) | ||
{ | ||
url = parseUrl(url, this.options); | ||
url = stringifyUrl(url); | ||
removeOld(url, this); | ||
// If value has been set | ||
if (this.cache[url] !== undefined) | ||
{ | ||
callback( this.cache[url].value ); | ||
return Promise.resolve( this.cache[url].value ); | ||
} | ||
// If value will be set -- in other words, `setting()` was called | ||
else if (this.settingStore[url] !== undefined) | ||
{ | ||
if (this.callbacks[url] !== undefined) | ||
{ | ||
this.callbacks[url].push(callback); | ||
} | ||
else | ||
{ | ||
this.callbacks[url] = [callback]; | ||
} | ||
} | ||
// Nothing has been or will be set | ||
else | ||
{ | ||
callback(); | ||
} | ||
return Promise.reject( new Error("key not found") ); | ||
}; | ||
@@ -109,6 +66,6 @@ | ||
{ | ||
var callbacks,i,numCallbacks; | ||
// Avoid filling cache with values that will only cause rejection | ||
if (value === undefined) return; | ||
url = parseUrl(url, this.options); | ||
url = stringifyUrl(url); | ||
url = formatUrl(url, this.options); | ||
@@ -122,23 +79,2 @@ if (expiryTime == null) expiryTime = this.options.expiryTime; | ||
}; | ||
// If `setting()` was called -- not a manual `set()` | ||
if (this.settingStore[url] !== undefined) | ||
{ | ||
callbacks = this.callbacks[url]; | ||
// If `get()` was called | ||
if (callbacks !== undefined) | ||
{ | ||
numCallbacks = callbacks.length; | ||
for (i=0; i<numCallbacks; i++) | ||
{ | ||
callbacks[i](value); | ||
} | ||
delete this.callbacks[url]; | ||
} | ||
delete this.settingStore[url]; | ||
} | ||
}; | ||
@@ -148,17 +84,13 @@ | ||
UrlCache.prototype.setting = function(url) | ||
{ | ||
url = parseUrl(url, this.options); | ||
if (this.contains(url) === false) | ||
{ | ||
url = stringifyUrl(url); | ||
this.settingStore[url] = true; | ||
} | ||
}; | ||
//::: PRIVATE FUNCTIONS | ||
//::: PRIVATE FUNCTIONS | ||
function formatUrl(url, options) | ||
{ | ||
url = parseUrl(url, options); | ||
url = stringifyUrl(url); | ||
return url; | ||
} | ||
@@ -181,2 +113,3 @@ | ||
{ | ||
// TODO :: this mutates input | ||
urlobj.normalize(url); | ||
@@ -187,2 +120,3 @@ } | ||
{ | ||
// TODO :: this mutates input | ||
url.hash = null; | ||
@@ -197,9 +131,9 @@ url.href = stringifyUrl(url); | ||
function removeOld(url, thisObj) | ||
function removeExpired(url, cache) | ||
{ | ||
if (thisObj.cache[url] !== undefined) | ||
if (cache[url] !== undefined) | ||
{ | ||
if ( thisObj.cache[url].expiryTime < Date.now() ) | ||
if ( cache[url].expiryTime < Date.now() ) | ||
{ | ||
delete thisObj.cache[url]; | ||
delete cache[url]; | ||
} | ||
@@ -206,0 +140,0 @@ } |
{ | ||
"name": "urlcache", | ||
"description": "URL key-value cache and store.", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"license": "MIT", | ||
@@ -20,9 +20,11 @@ "homepage": "https://github.com/stevenvachon/urlcache", | ||
"dependencies": { | ||
"object-assign": "^3.0.0", | ||
"object-assign": "^4.0.1", | ||
"urlobj": "0.0.7" | ||
}, | ||
"devDependencies": { | ||
"bhttp": "^1.2.1", | ||
"chai": "^3.2.0", | ||
"mocha": "^2.2.5", | ||
"bhttp": "^1.2.1", | ||
"bluebird": "^2.10.2", | ||
"chai": "^3.3.0", | ||
"chai-as-promised": "^5.1.0", | ||
"mocha": "^2.3.3", | ||
"node-static": "~0.7.7" | ||
@@ -29,0 +31,0 @@ }, |
@@ -7,3 +7,3 @@ # urlcache [![NPM Version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][david-image]][david-url] | ||
[Node.js](http://nodejs.org/) `~0.10` is required. To install, type this at the command line: | ||
[Node.js](http://nodejs.org/) `>= 0.10` is required. To install, type this at the command line: | ||
@@ -13,12 +13,9 @@ ```shell | ||
``` | ||
**Note:** Node.js v0.10 will need a `Promise` polyfill. | ||
## Usage | ||
## Constructor | ||
```js | ||
var UrlCache = require("urlcache"); | ||
var cache = new UrlCache(options); | ||
cache.set("http://domain.com/#hash", "value"); | ||
cache.set("http://domain.com/path/to/something.html", {"key":"value"}); | ||
``` | ||
@@ -31,28 +28,38 @@ | ||
### .clear([url]) | ||
Removes `url` from cache (whether defined with `set()` or `setting()`). If `url` is not defined, *all* cached key value pairs will be removed. | ||
Removes `url` from cache. If `url` is not defined, *all* cached key value pairs will be removed. | ||
### .contains(url) | ||
Returns `true` if `url` currently has a value stored or in the process of being stored in cache; `false` if it does not. | ||
### .get(url, callback) | ||
Runs `callback` when the value of `url` has been stored. If called before `set()` and/or `setting()`, the value will be `undefined`. | ||
### .get(url) | ||
Returns a `Promise` with the stored value of `url`. If no such value exists, the promise will be rejected. | ||
```js | ||
cache.get("url", function(value) { | ||
console.log(value); //-> undefined | ||
cache.get("url").then(function(value) { | ||
console.log(value); //-> "value" | ||
}); | ||
cache.setting("url"); | ||
cache.get("url", function(value) { | ||
console.log(value); //-> "value" | ||
cache.get("unstored").catch(function(error) { | ||
// not in cache (or value is a rejected Promise) | ||
}); | ||
cache.set("url", "value"); | ||
``` | ||
### .set(url, value, expiryTime) | ||
### .set(url, value[, expiryTime]) | ||
Stores `value` (any type) into `url` key. Optionally, define `expiryTime` to override `options.expiryTime`. | ||
```js | ||
cache.set("url", {"key":"value"}); | ||
### .setting(url) | ||
Marks `url` as being in the process of storing its value in cache. If the value of `url` has already been stored, nothing will be marked. | ||
cache.get("url").then(function(value) { | ||
console.log(value); //-> {"key":"value"} | ||
}); | ||
cache.set("url", new Promise(function(resolve, reject) { | ||
// set value after some delayed event | ||
setTimeout(function() { | ||
resolve("value"); | ||
}, 500); | ||
}); | ||
cache.get("url").then(function(value) { | ||
console.log(value); //-> "value" | ||
}); | ||
``` | ||
## Options | ||
@@ -82,2 +89,3 @@ | ||
## Changelog | ||
* 0.4.0 simpler `Promise`-based API | ||
* 0.3.0 added `options.defaultPorts`, more tests | ||
@@ -84,0 +92,0 @@ * 0.2.0 simplified API |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
98
7262
6
103
+ Addedobject-assign@4.1.1(transitive)
Updatedobject-assign@^4.0.1