Comparing version 4.0.0 to 5.0.0-next.1585174088.391fa2c4005dca37fbde3f4428c90af950175b4c
@@ -1,79 +0,80 @@ | ||
import oneday from 'oneday'; | ||
import oneday from 'oneday' | ||
export default class Cachely { | ||
/** Construct our Cachely class, setting the configuration from the options */ | ||
constructor(opts) { | ||
this.refresh = false; | ||
this.lastUpdated = null; | ||
if (!opts || typeof opts.retrieve !== 'function') { | ||
throw new Error('Cachely requires a retrieve method to be specified that returns a promise'); | ||
} | ||
this.duration = opts.duration || oneday; | ||
this.log = opts.log || function () { }; | ||
this.retrieve = opts.retrieve; | ||
} | ||
/** Creates and returns new instance of the current class */ | ||
static create(opts) { | ||
return new this(opts); | ||
} | ||
/** Determines whether or not the cache is still valid, by returning its current status */ | ||
validate() { | ||
const nowTime = new Date().getTime(); | ||
// have we manually invalidated the cache? | ||
if (this.refresh) { | ||
this.refresh = false; | ||
return 'invalid'; | ||
} | ||
// have we fetched the data yet? | ||
else if (this.lastUpdated && this.lastRequested) { | ||
// yes we have, so let's check if it is still valid | ||
// if the current time, minus the cache duration, is than the last time we requested the data, then our cache is invalid | ||
return new Date(nowTime - this.duration).getTime() < this.lastRequested | ||
? 'valid' | ||
: 'invalid'; | ||
} | ||
// are we doing the first fetch? | ||
else if (this.lastRequested) { | ||
return 'updating'; | ||
} | ||
// have we done no fetch yet? | ||
else { | ||
return 'empty'; | ||
} | ||
} | ||
/** Invalidates the current cache, so that it is retrieved again. | ||
/** Construct our Cachely class, setting the configuration from the options */ | ||
constructor(opts) { | ||
this.refresh = false | ||
this.lastUpdated = null | ||
if (!opts || typeof opts.retrieve !== 'function') { | ||
throw new Error( | ||
'Cachely requires a retrieve method to be specified that returns a promise' | ||
) | ||
} | ||
this.duration = opts.duration || oneday | ||
this.log = opts.log || function () {} | ||
this.retrieve = opts.retrieve | ||
} | ||
/** Creates and returns new instance of the current class */ | ||
static create(opts) { | ||
return new this(opts) | ||
} | ||
/** Determines whether or not the cache is still valid, by returning its current status */ | ||
validate() { | ||
const nowTime = new Date().getTime() | ||
// have we manually invalidated the cache? | ||
if (this.refresh) { | ||
this.refresh = false | ||
return 'invalid' | ||
} | ||
// have we fetched the data yet? | ||
else if (this.lastUpdated && this.lastRequested) { | ||
// yes we have, so let's check if it is still valid | ||
// if the current time, minus the cache duration, is than the last time we requested the data, then our cache is invalid | ||
return new Date(nowTime - this.duration).getTime() < this.lastRequested | ||
? 'valid' | ||
: 'invalid' | ||
} | ||
// are we doing the first fetch? | ||
else if (this.lastRequested) { | ||
return 'updating' | ||
} | ||
// have we done no fetch yet? | ||
else { | ||
return 'empty' | ||
} | ||
} | ||
/** Invalidates the current cache, so that it is retrieved again. | ||
Only applies to future resolution requets, does not cancel or modify active retrieval requests. */ | ||
invalidate() { | ||
this.refresh = true; | ||
return this; | ||
} | ||
/** Resolve the cache, if it is valid use the cache's data, otherwise retrieve new data */ | ||
async resolve() { | ||
const cache = this.validate(); | ||
switch (cache) { | ||
case 'valid': | ||
this.log('debug', 'Cachely has resolved cached data'); | ||
return this.data; | ||
case 'invalid': | ||
case 'empty': | ||
this.log('debug', 'Cachely must resolve new data'); | ||
this.lastRequested = new Date().getTime(); | ||
this.lastUpdated = null; | ||
this.lastRetrieval = Promise.resolve(this.retrieve()); | ||
try { | ||
this.data = await this.lastRetrieval; | ||
this.lastUpdated = new Date().getTime(); | ||
} | ||
catch (err) { | ||
this.log('debug', 'Cachely failed to resolve new data'); | ||
return Promise.reject(err); | ||
} | ||
this.log('debug', 'Cachely has resolved the new data'); | ||
return this.data; | ||
case 'updating': | ||
this.log('debug', 'Cachely is waiting for the new data to resolve'); | ||
return this.lastRetrieval; | ||
default: | ||
return Promise.reject(new Error('Unknown cache state')); | ||
} | ||
} | ||
invalidate() { | ||
this.refresh = true | ||
return this | ||
} | ||
/** Resolve the cache, if it is valid use the cache's data, otherwise retrieve new data */ | ||
async resolve() { | ||
const cache = this.validate() | ||
switch (cache) { | ||
case 'valid': | ||
this.log('debug', 'Cachely has resolved cached data') | ||
return this.data | ||
case 'invalid': | ||
case 'empty': | ||
this.log('debug', 'Cachely must resolve new data') | ||
this.lastRequested = new Date().getTime() | ||
this.lastUpdated = null | ||
this.lastRetrieval = Promise.resolve(this.retrieve()) | ||
try { | ||
this.data = await this.lastRetrieval | ||
this.lastUpdated = new Date().getTime() | ||
} catch (err) { | ||
this.log('debug', 'Cachely failed to resolve new data') | ||
return Promise.reject(err) | ||
} | ||
this.log('debug', 'Cachely has resolved the new data') | ||
return this.data | ||
case 'updating': | ||
this.log('debug', 'Cachely is waiting for the new data to resolve') | ||
return this.lastRetrieval | ||
default: | ||
return Promise.reject(new Error('Unknown cache state')) | ||
} | ||
} | ||
} |
@@ -1,85 +0,88 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const oneday_1 = __importDefault(require("oneday")); | ||
'use strict' | ||
var __importDefault = | ||
(this && this.__importDefault) || | ||
function (mod) { | ||
return mod && mod.__esModule ? mod : { default: mod } | ||
} | ||
Object.defineProperty(exports, '__esModule', { value: true }) | ||
const oneday_1 = __importDefault(require('oneday')) | ||
class Cachely { | ||
/** Construct our Cachely class, setting the configuration from the options */ | ||
constructor(opts) { | ||
this.refresh = false; | ||
this.lastUpdated = null; | ||
if (!opts || typeof opts.retrieve !== 'function') { | ||
throw new Error('Cachely requires a retrieve method to be specified that returns a promise'); | ||
} | ||
this.duration = opts.duration || oneday_1.default; | ||
this.log = opts.log || function () { }; | ||
this.retrieve = opts.retrieve; | ||
} | ||
/** Creates and returns new instance of the current class */ | ||
static create(opts) { | ||
return new this(opts); | ||
} | ||
/** Determines whether or not the cache is still valid, by returning its current status */ | ||
validate() { | ||
const nowTime = new Date().getTime(); | ||
// have we manually invalidated the cache? | ||
if (this.refresh) { | ||
this.refresh = false; | ||
return 'invalid'; | ||
} | ||
// have we fetched the data yet? | ||
else if (this.lastUpdated && this.lastRequested) { | ||
// yes we have, so let's check if it is still valid | ||
// if the current time, minus the cache duration, is than the last time we requested the data, then our cache is invalid | ||
return new Date(nowTime - this.duration).getTime() < this.lastRequested | ||
? 'valid' | ||
: 'invalid'; | ||
} | ||
// are we doing the first fetch? | ||
else if (this.lastRequested) { | ||
return 'updating'; | ||
} | ||
// have we done no fetch yet? | ||
else { | ||
return 'empty'; | ||
} | ||
} | ||
/** Invalidates the current cache, so that it is retrieved again. | ||
/** Construct our Cachely class, setting the configuration from the options */ | ||
constructor(opts) { | ||
this.refresh = false | ||
this.lastUpdated = null | ||
if (!opts || typeof opts.retrieve !== 'function') { | ||
throw new Error( | ||
'Cachely requires a retrieve method to be specified that returns a promise' | ||
) | ||
} | ||
this.duration = opts.duration || oneday_1.default | ||
this.log = opts.log || function () {} | ||
this.retrieve = opts.retrieve | ||
} | ||
/** Creates and returns new instance of the current class */ | ||
static create(opts) { | ||
return new this(opts) | ||
} | ||
/** Determines whether or not the cache is still valid, by returning its current status */ | ||
validate() { | ||
const nowTime = new Date().getTime() | ||
// have we manually invalidated the cache? | ||
if (this.refresh) { | ||
this.refresh = false | ||
return 'invalid' | ||
} | ||
// have we fetched the data yet? | ||
else if (this.lastUpdated && this.lastRequested) { | ||
// yes we have, so let's check if it is still valid | ||
// if the current time, minus the cache duration, is than the last time we requested the data, then our cache is invalid | ||
return new Date(nowTime - this.duration).getTime() < this.lastRequested | ||
? 'valid' | ||
: 'invalid' | ||
} | ||
// are we doing the first fetch? | ||
else if (this.lastRequested) { | ||
return 'updating' | ||
} | ||
// have we done no fetch yet? | ||
else { | ||
return 'empty' | ||
} | ||
} | ||
/** Invalidates the current cache, so that it is retrieved again. | ||
Only applies to future resolution requets, does not cancel or modify active retrieval requests. */ | ||
invalidate() { | ||
this.refresh = true; | ||
return this; | ||
} | ||
/** Resolve the cache, if it is valid use the cache's data, otherwise retrieve new data */ | ||
async resolve() { | ||
const cache = this.validate(); | ||
switch (cache) { | ||
case 'valid': | ||
this.log('debug', 'Cachely has resolved cached data'); | ||
return this.data; | ||
case 'invalid': | ||
case 'empty': | ||
this.log('debug', 'Cachely must resolve new data'); | ||
this.lastRequested = new Date().getTime(); | ||
this.lastUpdated = null; | ||
this.lastRetrieval = Promise.resolve(this.retrieve()); | ||
try { | ||
this.data = await this.lastRetrieval; | ||
this.lastUpdated = new Date().getTime(); | ||
} | ||
catch (err) { | ||
this.log('debug', 'Cachely failed to resolve new data'); | ||
return Promise.reject(err); | ||
} | ||
this.log('debug', 'Cachely has resolved the new data'); | ||
return this.data; | ||
case 'updating': | ||
this.log('debug', 'Cachely is waiting for the new data to resolve'); | ||
return this.lastRetrieval; | ||
default: | ||
return Promise.reject(new Error('Unknown cache state')); | ||
} | ||
} | ||
invalidate() { | ||
this.refresh = true | ||
return this | ||
} | ||
/** Resolve the cache, if it is valid use the cache's data, otherwise retrieve new data */ | ||
async resolve() { | ||
const cache = this.validate() | ||
switch (cache) { | ||
case 'valid': | ||
this.log('debug', 'Cachely has resolved cached data') | ||
return this.data | ||
case 'invalid': | ||
case 'empty': | ||
this.log('debug', 'Cachely must resolve new data') | ||
this.lastRequested = new Date().getTime() | ||
this.lastUpdated = null | ||
this.lastRetrieval = Promise.resolve(this.retrieve()) | ||
try { | ||
this.data = await this.lastRetrieval | ||
this.lastUpdated = new Date().getTime() | ||
} catch (err) { | ||
this.log('debug', 'Cachely failed to resolve new data') | ||
return Promise.reject(err) | ||
} | ||
this.log('debug', 'Cachely has resolved the new data') | ||
return this.data | ||
case 'updating': | ||
this.log('debug', 'Cachely is waiting for the new data to resolve') | ||
return this.lastRetrieval | ||
default: | ||
return Promise.reject(new Error('Unknown cache state')) | ||
} | ||
} | ||
} | ||
exports.default = Cachely; | ||
exports.default = Cachely |
# History | ||
## v5.0.0 2020 March 26 | ||
- Updated dependencies, [base files](https://github.com/bevry/base), and [editions](https://editions.bevry.me) using [boundation](https://github.com/bevry/boundation) | ||
- Minimum required node version changed from `node: >=8` to `node: >=10` to keep up with mandatory ecosystem changes | ||
## v4.0.0 2019 December 18 | ||
@@ -4,0 +9,0 @@ |
{ | ||
"name": "cachely", | ||
"version": "4.0.0", | ||
"version": "5.0.0-next.1585174088.391fa2c4005dca37fbde3f4428c90af950175b4c", | ||
"description": "A tiny wrapper that sits around your request function that caches its data for a specified duration, provides updates as requested rather than polling each interval", | ||
@@ -65,3 +65,3 @@ "homepage": "https://github.com/bevry/cachely", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
@@ -102,3 +102,3 @@ "editions": [ | ||
"engines": { | ||
"node": "8 || 10 || 12 || 13", | ||
"node": "10 || 12", | ||
"browsers": false | ||
@@ -117,16 +117,16 @@ } | ||
"devDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^2.12.0", | ||
"@typescript-eslint/parser": "^2.12.0", | ||
"assert-helpers": "^5.8.0", | ||
"eslint": "^6.7.2", | ||
"@typescript-eslint/eslint-plugin": "^2.25.0", | ||
"@typescript-eslint/parser": "^2.25.0", | ||
"assert-helpers": "^6.0.0", | ||
"eslint": "^6.8.0", | ||
"eslint-config-bevry": "^2.3.0", | ||
"eslint-config-prettier": "^6.7.0", | ||
"eslint-config-prettier": "^6.10.1", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"kava": "^4.4.0", | ||
"prettier": "^1.19.1", | ||
"prettier": "^2.0.2", | ||
"projectz": "^1.19.0", | ||
"surge": "^0.21.3", | ||
"typechecker": "^6.3.0", | ||
"typedoc": "^0.15.5", | ||
"typescript": "^3.7.3", | ||
"typedoc": "^0.17.3", | ||
"typescript": "^3.8.3", | ||
"valid-directory": "^1.6.0", | ||
@@ -158,3 +158,3 @@ "valid-module": "^1.0.0" | ||
"our:verify:module": "valid-module", | ||
"our:verify:prettier": "prettier --write ./source/**", | ||
"our:verify:prettier": "prettier --write .", | ||
"our:verify:typescript": "tsc --noEmit --project tsconfig.json", | ||
@@ -161,0 +161,0 @@ "test": "node ./edition-esnext/test.js" |
@@ -38,4 +38,4 @@ <!-- TITLE/ --> | ||
[Complete API Documentation.](http://master.cachely.bevry.surge.sh/docs/globals.html) | ||
```javascript | ||
@@ -52,3 +52,3 @@ // Import cachely | ||
retrieve() { | ||
return new Promise(function(resolve) { | ||
return new Promise(function (resolve) { | ||
// after a one second delay, return the number of fetches that we have done | ||
@@ -66,3 +66,3 @@ setTimeout(() => resolve(++fetches), 1000) | ||
// Defaults to nothing | ||
log: console.log | ||
log: console.log, | ||
}) | ||
@@ -93,3 +93,3 @@ | ||
// wait 3000ms for the cache to invalidate itself | ||
setTimeout(function() { | ||
setTimeout(function () { | ||
// do a second fetch of the data | ||
@@ -144,12 +144,2 @@ cachely | ||
[Complete API Documentation.](http://master.cachely.bevry.surge.sh/docs/) | ||
<!-- INSTALL/ --> | ||
@@ -170,3 +160,3 @@ | ||
<script type="module"> | ||
import pkg from '//cdn.pika.dev/cachely/^4.0.0' | ||
import pkg from '//cdn.pika.dev/cachely/^5.0.0' | ||
</script> | ||
@@ -179,3 +169,3 @@ ``` | ||
<script type="module"> | ||
import pkg from '//unpkg.com/cachely@^4.0.0' | ||
import pkg from '//unpkg.com/cachely@^5.0.0' | ||
</script> | ||
@@ -188,3 +178,3 @@ ``` | ||
<script type="module"> | ||
import pkg from '//dev.jspm.io/cachely@4.0.0' | ||
import pkg from '//dev.jspm.io/cachely@5.0.0' | ||
</script> | ||
@@ -231,3 +221,3 @@ ``` | ||
<ul><li><a href="http://balupton.com">Benjamin Lupton</a> — <a href="https://github.com/bevry/cachely/commits?author=balupton" title="View the GitHub contributions of Benjamin Lupton on repository bevry/cachely">view contributions</a></li></ul> | ||
<ul><li><a href="http://balupton.com">Benjamin Lupton</a></li></ul> | ||
@@ -252,4 +242,3 @@ <h3>Sponsors</h3> | ||
<ul><li><a href="http://balupton.com">Benjamin Lupton</a> — <a href="https://github.com/bevry/cachely/commits?author=balupton" title="View the GitHub contributions of Benjamin Lupton on repository bevry/cachely">view contributions</a></li> | ||
<li><a href="http://github.com/apps/dependabot-preview">dependabot-preview[bot]</a> — <a href="https://github.com/bevry/cachely/commits?author=dependabot-preview[bot]" title="View the GitHub contributions of dependabot-preview[bot] on repository bevry/cachely">view contributions</a></li></ul> | ||
<ul><li><a href="http://balupton.com">Benjamin Lupton</a></li></ul> | ||
@@ -256,0 +245,0 @@ <a href="https://github.com/bevry/cachely/blob/master/CONTRIBUTING.md#files">Discover how you can contribute by heading on over to the <code>CONTRIBUTING.md</code> file.</a> |
@@ -35,3 +35,3 @@ import oneday from 'oneday' | ||
this.duration = opts.duration || oneday | ||
this.log = opts.log || function() {} | ||
this.log = opts.log || function () {} | ||
this.retrieve = opts.retrieve | ||
@@ -38,0 +38,0 @@ } |
@@ -11,5 +11,3 @@ { | ||
}, | ||
"include": [ | ||
"source" | ||
] | ||
"include": ["source"] | ||
} |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
277
32397
1
253