Comparing version 3.1.0 to 4.0.0-next.1576634213.7c586c0c81f722033d8f02ba1bbff3378528d26b
@@ -1,153 +0,79 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var Cachely = | ||
/*#__PURE__*/ | ||
function () { | ||
/** Construct our Cachely class, setting the configuration from the options */ | ||
function Cachely(opts) { | ||
_classCallCheck(this, Cachely); | ||
_defineProperty(this, "duration", void 0); | ||
_defineProperty(this, "log", void 0); | ||
_defineProperty(this, "retrieve", void 0); | ||
_defineProperty(this, "data", void 0); | ||
_defineProperty(this, "refresh", false); | ||
_defineProperty(this, "lastRequested", void 0); | ||
_defineProperty(this, "lastRetrieval", void 0); | ||
_defineProperty(this, "lastUpdated", null); | ||
if (!opts || typeof opts.retrieve !== 'function') { | ||
throw new Error('Cachely requires a retrieve method to be specified that returns a promise'); | ||
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; | ||
} | ||
this.duration = opts.duration || require('oneday'); | ||
this.log = opts.log || function () {}; | ||
this.retrieve = opts.retrieve; | ||
} | ||
/** Creates and returns new instance of the current class */ | ||
_createClass(Cachely, [{ | ||
key: "validate", | ||
/** 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 */ | ||
value: function validate() { | ||
var 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? | ||
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'; | ||
} | ||
} | ||
// 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. */ | ||
}, { | ||
key: "invalidate", | ||
value: function invalidate() { | ||
this.refresh = true; | ||
return this; | ||
invalidate() { | ||
this.refresh = true; | ||
return this; | ||
} | ||
/** Resolve the cache, if it is valid use the cache's data, otherwise retrieve new data */ | ||
}, { | ||
key: "resolve", | ||
value: function resolve() { | ||
var cache; | ||
return regeneratorRuntime.async(function resolve$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
cache = this.validate(); | ||
_context.t0 = cache; | ||
_context.next = _context.t0 === 'valid' ? 4 : _context.t0 === 'invalid' ? 6 : _context.t0 === 'empty' ? 6 : _context.t0 === 'updating' ? 23 : 25; | ||
break; | ||
case 4: | ||
this.log('debug', 'Cachely has resolved cached data'); | ||
return _context.abrupt("return", this.data); | ||
case 6: | ||
this.log('debug', 'Cachely must resolve new data'); | ||
this.lastRequested = new Date().getTime(); | ||
this.lastUpdated = null; | ||
this.lastRetrieval = Promise.resolve(this.retrieve()); | ||
_context.prev = 10; | ||
_context.next = 13; | ||
return regeneratorRuntime.awrap(this.lastRetrieval); | ||
case 13: | ||
this.data = _context.sent; | ||
this.lastUpdated = new Date().getTime(); | ||
_context.next = 21; | ||
break; | ||
case 17: | ||
_context.prev = 17; | ||
_context.t1 = _context["catch"](10); | ||
this.log('debug', 'Cachely failed to resolve new data'); | ||
return _context.abrupt("return", Promise.reject(_context.t1)); | ||
case 21: | ||
this.log('debug', 'Cachely has resolved the new data'); | ||
return _context.abrupt("return", this.data); | ||
case 23: | ||
this.log('debug', 'Cachely is waiting for the new data to resolve'); | ||
return _context.abrupt("return", this.lastRetrieval); | ||
case 25: | ||
return _context.abrupt("return", Promise.reject(new Error('Unknown cache state'))); | ||
case 26: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
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')); | ||
} | ||
}, null, this, [[10, 17]]); | ||
} | ||
}], [{ | ||
key: "create", | ||
value: function create(opts) { | ||
return new this(opts); | ||
} | ||
}]); | ||
return Cachely; | ||
}(); | ||
exports.default = Cachely; | ||
module.exports = exports.default; | ||
} |
# History | ||
## v4.0.0 2019 December 18 | ||
- If you are using CommonJS, you must now do `require('cachely').default` | ||
- Updated dependencies, [base files](https://github.com/bevry/base), and [editions](https://editions.bevry.me) using [boundation](https://github.com/bevry/boundation) | ||
## v3.1.0 2019 December 9 | ||
@@ -4,0 +9,0 @@ |
112
package.json
{ | ||
"name": "cachely", | ||
"version": "3.1.0", | ||
"version": "4.0.0-next.1576634213.7c586c0c81f722033d8f02ba1bbff3378528d26b", | ||
"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", | ||
@@ -8,5 +8,11 @@ "homepage": "https://github.com/bevry/cachely", | ||
"keywords": [ | ||
"browser", | ||
"cache", | ||
"caching", | ||
"fetching" | ||
"export-default", | ||
"fetching", | ||
"module", | ||
"typed", | ||
"types", | ||
"typescript" | ||
], | ||
@@ -50,4 +56,3 @@ "badges": { | ||
"contributors": [ | ||
"Benjamin Lupton <b@lupton.cc> (http://balupton.com)", | ||
"dependabot-preview[bot] (http://github.com/apps/dependabot-preview)" | ||
"Benjamin Lupton <b@lupton.cc> (http://balupton.com)" | ||
], | ||
@@ -66,3 +71,3 @@ "bugs": { | ||
{ | ||
"description": "typescript source code with import for modules", | ||
"description": "TypeScript source code with Import for modules", | ||
"directory": "source", | ||
@@ -77,3 +82,3 @@ "entry": "index.ts", | ||
{ | ||
"description": "typescript compiled for browsers with import for modules", | ||
"description": "TypeScript compiled against ESNext for web browsers with Import for modules", | ||
"directory": "edition-browsers", | ||
@@ -91,11 +96,12 @@ "entry": "index.js", | ||
{ | ||
"description": "typescript compiled for node.js 12 with require for modules", | ||
"directory": "edition-node-12", | ||
"description": "TypeScript compiled against ESNext for Node.js with Require for modules", | ||
"directory": "edition-esnext", | ||
"entry": "index.js", | ||
"tags": [ | ||
"javascript", | ||
"esnext", | ||
"require" | ||
], | ||
"engines": { | ||
"node": "8 || 10 || 12", | ||
"node": "8 || 10 || 12 || 13", | ||
"browsers": false | ||
@@ -107,38 +113,31 @@ } | ||
"type": "commonjs", | ||
"main": "edition-node-12/index.js", | ||
"main": "edition-esnext/index.js", | ||
"browser": "edition-browsers/index.js", | ||
"module": "edition-browsers/index.js", | ||
"dependencies": { | ||
"oneday": "^2.2.0" | ||
"oneday": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.7.5", | ||
"@babel/core": "^7.7.5", | ||
"@babel/plugin-proposal-class-properties": "^7.7.4", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.7.4", | ||
"@babel/plugin-proposal-optional-chaining": "^7.7.5", | ||
"@babel/preset-env": "^7.7.6", | ||
"@babel/preset-typescript": "^7.7.4", | ||
"@typescript-eslint/eslint-plugin": "^2.10.0", | ||
"@typescript-eslint/parser": "^2.10.0", | ||
"@typescript-eslint/eslint-plugin": "^2.12.0", | ||
"@typescript-eslint/parser": "^2.12.0", | ||
"assert-helpers": "^5.8.0", | ||
"babel-plugin-add-module-exports": "^1.0.2", | ||
"eslint": "^6.7.2", | ||
"eslint-config-bevry": "^2.2.0", | ||
"eslint-config-bevry": "^2.3.0", | ||
"eslint-config-prettier": "^6.7.0", | ||
"eslint-plugin-prettier": "^3.1.1", | ||
"kava": "^4.3.0", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"kava": "^4.4.0", | ||
"prettier": "^1.19.1", | ||
"projectz": "^1.15.0", | ||
"projectz": "^1.19.0", | ||
"surge": "^0.21.3", | ||
"typechecker": "^6.2.0", | ||
"typedoc": "^0.15.4", | ||
"typechecker": "^6.3.0", | ||
"typedoc": "^0.15.5", | ||
"typescript": "^3.7.3", | ||
"valid-directory": "^1.5.0" | ||
"valid-directory": "^1.6.0", | ||
"valid-module": "^1.0.0" | ||
}, | ||
"scripts": { | ||
"our:clean": "rm -Rf ./docs ./edition* ./es2015 ./es5 ./out ./.next", | ||
"our:compile": "npm run our:compile:edition-browsers && npm run our:compile:edition-node-12", | ||
"our:compile:edition-browsers": "env BABEL_ENV=edition-browsers babel --extensions \".ts,.tsx\" --out-dir ./edition-browsers ./source", | ||
"our:compile:edition-node-12": "env BABEL_ENV=edition-node-12 babel --extensions \".ts,.tsx\" --out-dir ./edition-node-12 ./source", | ||
"our:compile": "npm run our:compile:edition-browsers && npm run our:compile:edition-esnext", | ||
"our:compile:edition-browsers": "tsc --module ESNext --target ESNext --outDir ./edition-browsers --project tsconfig.json && test -d edition-browsers/source && ( mv edition-browsers/source edition-temp && rm -Rf edition-browsers && mv edition-temp edition-browsers ) || true", | ||
"our:compile:edition-esnext": "tsc --module commonjs --target ESNext --outDir ./edition-esnext --project tsconfig.json && test -d edition-esnext/source && ( mv edition-esnext/source edition-temp && rm -Rf edition-esnext && mv edition-temp edition-esnext ) || true", | ||
"our:deploy": "echo no need for this project", | ||
@@ -158,8 +157,9 @@ "our:meta": "npm run our:meta:docs && npm run our:meta:projectz", | ||
"our:test": "npm run our:verify && npm test", | ||
"our:verify": "npm run our:verify:directory && npm run our:verify:eslint && npm run our:verify:prettier && npm run our:verify:typescript", | ||
"our:verify:directory": "npx valid-directory", | ||
"our:verify": "npm run our:verify:directory && npm run our:verify:eslint && npm run our:verify:module && npm run our:verify:prettier && npm run our:verify:typescript", | ||
"our:verify:directory": "valid-directory", | ||
"our:verify:eslint": "eslint --fix --ignore-pattern '**/*.d.ts' --ignore-pattern '**/vendor/' --ignore-pattern '**/node_modules/' --ext .mjs,.js,.jsx,.ts,.tsx ./source", | ||
"our:verify:module": "valid-module", | ||
"our:verify:prettier": "prettier --write ./source/**", | ||
"our:verify:typescript": "tsc --noEmit --project tsconfig.json", | ||
"test": "node ./edition-node-12/test.js" | ||
"test": "node ./edition-esnext/test.js" | ||
}, | ||
@@ -174,47 +174,3 @@ "eslintConfig": { | ||
"singleQuote": true | ||
}, | ||
"babel": { | ||
"env": { | ||
"edition-browsers": { | ||
"sourceType": "module", | ||
"presets": [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": "defaults", | ||
"modules": "commonjs" | ||
} | ||
], | ||
"@babel/preset-typescript" | ||
], | ||
"plugins": [ | ||
"@babel/proposal-object-rest-spread", | ||
"@babel/plugin-proposal-optional-chaining", | ||
"@babel/proposal-class-properties", | ||
"add-module-exports" | ||
] | ||
}, | ||
"edition-node-12": { | ||
"sourceType": "module", | ||
"presets": [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": { | ||
"node": "12" | ||
}, | ||
"modules": "commonjs" | ||
} | ||
], | ||
"@babel/preset-typescript" | ||
], | ||
"plugins": [ | ||
"@babel/proposal-object-rest-spread", | ||
"@babel/plugin-proposal-optional-chaining", | ||
"@babel/proposal-class-properties", | ||
"add-module-exports" | ||
] | ||
} | ||
} | ||
} | ||
} |
105
README.md
@@ -36,39 +36,15 @@ <!-- TITLE/ --> | ||
<!-- INSTALL/ --> | ||
## Usage | ||
<h2>Install</h2> | ||
<a href="https://npmjs.com" title="npm is a package manager for javascript"><h3>npm</h3></a> | ||
<ul> | ||
<li>Install: <code>npm install --save cachely</code></li> | ||
<li>Require: <code>require('cachely')</code></li> | ||
</ul> | ||
<a href="https://jspm.io" title="Native ES Modules CDN"><h3>jspm</h3></a> | ||
```javascript | ||
// Import cachely | ||
import Cachely from 'cachely' | ||
``` html | ||
<script type="module"> | ||
import * as pkg from '//dev.jspm.io/cachely' | ||
</script> | ||
``` | ||
// For our retriever, have a locally scoped variable that will showcase the caching | ||
let fetches = 0 | ||
<h3><a href="https://editions.bevry.me" title="Editions are the best way to produce and consume packages you care about.">Editions</a></h3> | ||
<p>This package is published with the following editions:</p> | ||
<ul><li><code>cachely/source/index.ts</code> is typescript source code with import for modules</li> | ||
<li><code>cachely/edition-browsers/index.js</code> is typescript compiled for browsers with import for modules</li> | ||
<li><code>cachely</code> aliases <code>cachely/edition-node-12/index.js</code></li> | ||
<li><code>cachely/edition-node-12/index.js</code> is typescript compiled for node.js 12 with require for modules</li></ul> | ||
<!-- /INSTALL --> | ||
## Usage | ||
[API Documentation.](http://master.cachely.bevry.surge.sh/docs/) | ||
```javascript | ||
let fetches = 0 | ||
const cachely = require('cachely').create({ | ||
// Create the wrapper retriever in the cachely cache service | ||
const cachely = new Cachely({ | ||
// The method that will fetch the data | ||
@@ -92,3 +68,3 @@ retrieve() { | ||
// do an initial fetch of the dat | ||
// do an initial fetch of the data | ||
cachely | ||
@@ -104,3 +80,3 @@ .resolve() | ||
// do a subsequent fetch of the data that will be from the cach | ||
// do a subsequent fetch of the data that will be from the cache | ||
cachely | ||
@@ -116,5 +92,5 @@ .resolve() | ||
// wait for the cache to invalidate itself | ||
// wait 3000ms for the cache to invalidate itself | ||
setTimeout(function() { | ||
// do an second fetch of the data | ||
// do a second fetch of the data | ||
cachely | ||
@@ -168,2 +144,59 @@ .resolve() | ||
[Complete API Documentation.](http://master.cachely.bevry.surge.sh/docs/) | ||
<!-- INSTALL/ --> | ||
<h2>Install</h2> | ||
<a href="https://npmjs.com" title="npm is a package manager for javascript"><h3>npm</h3></a> | ||
<ul> | ||
<li>Install: <code>npm install --save cachely</code></li> | ||
<li>Import: <code>import pkg from ('cachely')</code></li> | ||
<li>Require: <code>const pkg = require('cachely').default</code></li> | ||
</ul> | ||
<a href="https://www.pika.dev/cdn" title="100% Native ES Modules CDN"><h3>pika</h3></a> | ||
``` html | ||
<script type="module"> | ||
import pkg from '//cdn.pika.dev/cachely/^4.0.0' | ||
</script> | ||
``` | ||
<a href="https://unpkg.com" title="unpkg is a fast, global content delivery network for everything on npm"><h3>unpkg</h3></a> | ||
``` html | ||
<script type="module"> | ||
import pkg from '//unpkg.com/cachely@^4.0.0' | ||
</script> | ||
``` | ||
<a href="https://jspm.io" title="Native ES Modules CDN"><h3>jspm</h3></a> | ||
``` html | ||
<script type="module"> | ||
import pkg from '//dev.jspm.io/cachely@4.0.0' | ||
</script> | ||
``` | ||
<h3><a href="https://editions.bevry.me" title="Editions are the best way to produce and consume packages you care about.">Editions</a></h3> | ||
<p>This package is published with the following editions:</p> | ||
<ul><li><code>cachely/source/index.ts</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> source code with <a href="https://babeljs.io/docs/learn-es2015/#modules" title="ECMAScript Modules">Import</a> for modules</li> | ||
<li><code>cachely/edition-browsers/index.js</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> compiled against <a href="https://en.wikipedia.org/wiki/ECMAScript#ES.Next" title="ECMAScript Next">ESNext</a> for web browsers with <a href="https://babeljs.io/docs/learn-es2015/#modules" title="ECMAScript Modules">Import</a> for modules</li> | ||
<li><code>cachely</code> aliases <code>cachely/edition-esnext/index.js</code></li> | ||
<li><code>cachely/edition-esnext/index.js</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> compiled against <a href="https://en.wikipedia.org/wiki/ECMAScript#ES.Next" title="ECMAScript Next">ESNext</a> for <a href="https://nodejs.org" title="Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine">Node.js</a> with <a href="https://nodejs.org/dist/latest-v5.x/docs/api/modules.html" title="Node/CJS Modules">Require</a> for modules</li></ul> | ||
<!-- /INSTALL --> | ||
<!-- HISTORY/ --> | ||
@@ -170,0 +203,0 @@ |
@@ -0,1 +1,3 @@ | ||
import oneday from 'oneday' | ||
type Log = (logLevel: string, ...args: any[]) => any | ||
@@ -32,3 +34,3 @@ type Retrieve<Result> = () => Promise<Result> | ||
this.duration = opts.duration || require('oneday') | ||
this.duration = opts.duration || oneday | ||
this.log = opts.log || function() {} | ||
@@ -35,0 +37,0 @@ this.retrieve = opts.retrieve |
@@ -8,3 +8,2 @@ { | ||
"moduleResolution": "node", | ||
"noEmit": true, | ||
"strict": true, | ||
@@ -11,0 +10,0 @@ "target": "esnext" |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
16
264
33982
9
275
1
1
+ Addedoneday@3.0.0(transitive)
- Removedoneday@2.3.0(transitive)
Updatedoneday@^3.0.0