create-memo
Advanced tools
Comparing version 0.0.1 to 0.0.2
477
index.js
@@ -1,236 +0,317 @@ | ||
class AbstractMemoCache {} | ||
"use strict"; | ||
class LinkedMapNode { | ||
constructor(key, value) { | ||
this.key = key; | ||
this.value = value; | ||
this.prev = void 0; | ||
this.next = void 0; | ||
this.key = key; | ||
this.value = value; | ||
this.prev = null; | ||
this.next = null; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function _inheritsLoose(subClass, superClass) { | ||
subClass.prototype = Object.create(superClass.prototype); | ||
subClass.prototype.constructor = subClass; | ||
subClass.__proto__ = superClass; | ||
} | ||
class LinkedMapCache extends AbstractMemoCache { | ||
constructor(...args) { | ||
super(...args); | ||
this.head = null; | ||
this.tail = null; | ||
this.map = new Map(); | ||
} | ||
var AbstractMemoCache = function AbstractMemoCache() {}; | ||
get(key) { | ||
const node = this.map.get(key); | ||
return node ? node.value : undefined; | ||
} | ||
var LinkedMapNode = function LinkedMapNode(key, value) { | ||
this.prev = void 0; | ||
this.next = void 0; | ||
this.key = key; | ||
this.value = value; | ||
this.prev = null; | ||
this.next = null; | ||
}; | ||
unlinkNode(acc) { | ||
if (acc.prev) { | ||
acc.prev.next = acc.next; | ||
} else { | ||
this.head = acc.next; | ||
} | ||
var LinkedMapCache = | ||
/*#__PURE__*/ | ||
(function(_AbstractMemoCache) { | ||
_inheritsLoose(LinkedMapCache, _AbstractMemoCache); | ||
if (acc.next) { | ||
acc.next.prev = acc.prev; | ||
} else { | ||
this.tail = acc.prev; | ||
} | ||
} | ||
function LinkedMapCache() { | ||
var _this; | ||
insert(key, value) { | ||
let node = this.map.get(key); | ||
for ( | ||
var _len = arguments.length, args = new Array(_len), _key = 0; | ||
_key < _len; | ||
_key++ | ||
) { | ||
args[_key] = arguments[_key]; | ||
} | ||
if (node) { | ||
node.value = value; | ||
this.unlinkNode(node); | ||
} else { | ||
node = new LinkedMapNode(key, value); | ||
this.map.set(key, node); | ||
_this = | ||
_AbstractMemoCache.call.apply( | ||
_AbstractMemoCache, | ||
[this].concat(args), | ||
) || this; | ||
_this.head = null; | ||
_this.tail = null; | ||
_this.map = new Map(); | ||
return _this; | ||
} | ||
return node; | ||
} | ||
var _proto = LinkedMapCache.prototype; | ||
prime(key, value) { | ||
const node = this.insert(key, value); | ||
node.next = this.head; | ||
node.prev = null; | ||
_proto.get = function get(key) { | ||
var node = this.map.get(key); | ||
return node ? node.value : undefined; | ||
}; | ||
if (this.head) { | ||
this.head.prev = node; | ||
} | ||
_proto.unlinkNode = function unlinkNode(acc) { | ||
if (acc.prev) { | ||
acc.prev.next = acc.next; | ||
} else { | ||
this.head = acc.next; | ||
} | ||
this.head = node; | ||
if (acc.next) { | ||
acc.next.prev = acc.prev; | ||
} else { | ||
this.tail = acc.prev; | ||
} | ||
}; | ||
if (!this.tail) { | ||
this.tail = node; | ||
} | ||
_proto.insert = function insert(key, value) { | ||
var node = this.map.get(key); | ||
return this; | ||
} | ||
if (node) { | ||
node.value = value; | ||
this.unlinkNode(node); | ||
} else { | ||
node = new LinkedMapNode(key, value); | ||
this.map.set(key, node); | ||
} | ||
clear(key) { | ||
const node = this.map.get(key); | ||
return node; | ||
}; | ||
if (node) { | ||
this.map.delete(key); | ||
this.unlinkNode(node); | ||
} | ||
_proto.prime = function prime(key, value) { | ||
var node = this.insert(key, value); | ||
node.next = this.head; | ||
node.prev = null; | ||
return this; | ||
} | ||
if (this.head) { | ||
this.head.prev = node; | ||
} | ||
clearAll() { | ||
this.map.clear(); | ||
this.head = null; | ||
this.tail = null; | ||
return this; | ||
} | ||
} | ||
this.head = node; | ||
class LRUCache extends LinkedMapCache { | ||
constructor(options) { | ||
super(); | ||
this.options = void 0; | ||
this.options = options; | ||
} | ||
if (!this.tail) { | ||
this.tail = node; | ||
} | ||
get(key) { | ||
const node = this.map.get(key); | ||
return this; | ||
}; | ||
if (!node) { | ||
return undefined; | ||
_proto.clear = function clear(key) { | ||
var node = this.map.get(key); | ||
if (node) { | ||
this.map.delete(key); | ||
this.unlinkNode(node); | ||
} | ||
return this; | ||
}; | ||
_proto.clearAll = function clearAll() { | ||
this.map.clear(); | ||
this.head = null; | ||
this.tail = null; | ||
return this; | ||
}; | ||
return LinkedMapCache; | ||
})(AbstractMemoCache); | ||
var LRUCache = | ||
/*#__PURE__*/ | ||
(function(_LinkedMapCache) { | ||
_inheritsLoose(LRUCache, _LinkedMapCache); | ||
function LRUCache(options) { | ||
var _this; | ||
_this = _LinkedMapCache.call(this) || this; | ||
_this.options = void 0; | ||
_this.options = options; | ||
return _this; | ||
} | ||
this.prime(key, node.value); | ||
return node.value; | ||
} | ||
var _proto = LRUCache.prototype; | ||
removeLatterlyUsed() { | ||
const { maxSize } = this.options; | ||
const itemsToRemove = this.map.size - maxSize + 1; | ||
_proto.get = function get(key) { | ||
var node = this.map.get(key); | ||
if (itemsToRemove > 0) { | ||
const entries = this.map.entries(); | ||
if (!node) { | ||
return undefined; | ||
} | ||
for (let i = 0; i < itemsToRemove; i++) { | ||
const { | ||
value: [key], | ||
} = entries.next(); | ||
this.clear(key); | ||
this.prime(key, node.value); | ||
return node.value; | ||
}; | ||
_proto.removeLatterlyUsed = function removeLatterlyUsed() { | ||
var maxSize = this.options.maxSize; | ||
var itemsToRemove = this.map.size - maxSize + 1; | ||
if (itemsToRemove > 0) { | ||
var entries = this.map.entries(); | ||
for (var i = 0; i < itemsToRemove; i++) { | ||
var _entries$next = entries.next(), | ||
_entries$next$value = _entries$next.value, | ||
key = _entries$next$value[0]; | ||
this.clear(key); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
prime(key, value) { | ||
if (!this.map.has(key)) { | ||
this.removeLatterlyUsed(); | ||
_proto.prime = function prime(key, value) { | ||
if (!this.map.has(key)) { | ||
this.removeLatterlyUsed(); | ||
} | ||
return _LinkedMapCache.prototype.prime.call(this, key, value); | ||
}; | ||
return LRUCache; | ||
})(LinkedMapCache); | ||
var MapCache = | ||
/*#__PURE__*/ | ||
(function(_AbstractMemoCache) { | ||
_inheritsLoose(MapCache, _AbstractMemoCache); | ||
function MapCache() { | ||
var _this; | ||
for ( | ||
var _len = arguments.length, args = new Array(_len), _key = 0; | ||
_key < _len; | ||
_key++ | ||
) { | ||
args[_key] = arguments[_key]; | ||
} | ||
_this = | ||
_AbstractMemoCache.call.apply( | ||
_AbstractMemoCache, | ||
[this].concat(args), | ||
) || this; | ||
_this.map = new Map(); | ||
return _this; | ||
} | ||
return super.prime(key, value); | ||
} | ||
} | ||
var _proto = MapCache.prototype; | ||
class MapCache extends AbstractMemoCache { | ||
constructor(...args) { | ||
super(...args); | ||
this.map = new Map(); | ||
} | ||
_proto.get = function get(key) { | ||
return this.map.get(key); | ||
}; | ||
get(key) { | ||
return this.map.get(key); | ||
} | ||
_proto.prime = function prime(key, value) { | ||
this.map.set(key, value); | ||
return this; | ||
}; | ||
prime(key, value) { | ||
this.map.set(key, value); | ||
return this; | ||
} | ||
_proto.clear = function clear(key) { | ||
this.map.delete(key); | ||
return this; | ||
}; | ||
clear(key) { | ||
this.map.delete(key); | ||
return this; | ||
} | ||
_proto.clearAll = function clearAll() { | ||
this.map.clear(); | ||
return this; | ||
}; | ||
clearAll() { | ||
this.map.clear(); | ||
return this; | ||
} | ||
} | ||
return MapCache; | ||
})(AbstractMemoCache); | ||
class MemoCacheNode { | ||
constructor(value, expiresAt) { | ||
// noop. | ||
var MemoCacheNode = function MemoCacheNode(value, expiresAt) { | ||
// noop. | ||
}; | ||
this.value = value; | ||
this.expiresAt = expiresAt; | ||
} | ||
} | ||
var MemoCache = | ||
/*#__PURE__*/ | ||
(function(_AbstractMemoCache) { | ||
_inheritsLoose(MemoCache, _AbstractMemoCache); | ||
class MemoCache extends AbstractMemoCache { | ||
constructor({ | ||
maxSize, | ||
expireAfterWrite = 0, | ||
expireAfterAccess = 0, | ||
cacheKeyFn = key => key, | ||
} = {}) { | ||
super(); | ||
this.cache = void 0; | ||
this.cacheKeyFn = void 0; | ||
this.expireAfterWrite = void 0; | ||
this.expireAfterAccess = void 0; | ||
this.cacheKeyFn = cacheKeyFn; | ||
this.expireAfterWrite = expireAfterWrite; | ||
this.expireAfterAccess = expireAfterAccess; | ||
this.cache = !maxSize | ||
? new MapCache() | ||
: new LRUCache({ | ||
maxSize, | ||
}); | ||
} | ||
function MemoCache(_temp) { | ||
var _this; | ||
get(key) { | ||
const node = this.cache.get(this.cacheKeyFn(key)); | ||
var _ref = _temp === void 0 ? {} : _temp, | ||
maxSize = _ref.maxSize, | ||
_ref$expireAfterWrite = _ref.expireAfterWrite, | ||
expireAfterWrite = | ||
_ref$expireAfterWrite === void 0 ? 0 : _ref$expireAfterWrite, | ||
_ref$expireAfterAcces = _ref.expireAfterAccess, | ||
expireAfterAccess = | ||
_ref$expireAfterAcces === void 0 ? 0 : _ref$expireAfterAcces, | ||
_ref$cacheKeyFn = _ref.cacheKeyFn, | ||
cacheKeyFn = | ||
_ref$cacheKeyFn === void 0 | ||
? function(key) { | ||
return key; | ||
} | ||
: _ref$cacheKeyFn; | ||
if (node) { | ||
if (node.expiresAt > Date.now()) { | ||
if (this.expireAfterAccess) { | ||
node.expiresAt = Math.max( | ||
node.expiresAt, | ||
Date.now() + this.expireAfterAccess, | ||
); | ||
_this = _AbstractMemoCache.call(this) || this; | ||
_this.cache = void 0; | ||
_this.cacheKeyFn = void 0; | ||
_this.expireAfterWrite = void 0; | ||
_this.expireAfterAccess = void 0; | ||
_this.cacheKeyFn = cacheKeyFn; | ||
_this.expireAfterWrite = expireAfterWrite; | ||
_this.expireAfterAccess = expireAfterAccess; | ||
_this.cache = !maxSize | ||
? new MapCache() | ||
: new LRUCache({ | ||
maxSize: maxSize, | ||
}); | ||
return _this; | ||
} | ||
var _proto = MemoCache.prototype; | ||
_proto.get = function get(key) { | ||
var node = this.cache.get(this.cacheKeyFn(key)); | ||
if (node) { | ||
if (node.expiresAt > Date.now()) { | ||
if (this.expireAfterAccess) { | ||
node.expiresAt = Math.max( | ||
node.expiresAt, | ||
Date.now() + this.expireAfterAccess, | ||
); | ||
} | ||
return node.value; | ||
} | ||
return node.value; | ||
this.clear(key); | ||
} | ||
}; | ||
this.clear(key); | ||
} | ||
} | ||
_proto.prime = function prime(key, value) { | ||
var expiresAfter = Math.max( | ||
this.expireAfterWrite || 0, | ||
this.expireAfterAccess || 0, | ||
); | ||
var node = new MemoCacheNode( | ||
value, | ||
expiresAfter === 0 ? Infinity : expiresAfter + Date.now(), | ||
); | ||
this.cache.prime(this.cacheKeyFn(key), node); | ||
return this; | ||
}; | ||
prime(key, value) { | ||
const expiresAfter = Math.max( | ||
this.expireAfterWrite || 0, | ||
this.expireAfterAccess || 0, | ||
); | ||
const node = new MemoCacheNode( | ||
value, | ||
expiresAfter === 0 ? Infinity : expiresAfter + Date.now(), | ||
); | ||
this.cache.prime(this.cacheKeyFn(key), node); | ||
return this; | ||
} | ||
_proto.clear = function clear(key) { | ||
this.cache.clear(this.cacheKeyFn(key)); | ||
return this; | ||
}; | ||
clear(key) { | ||
this.cache.clear(this.cacheKeyFn(key)); | ||
return this; | ||
} | ||
_proto.clearAll = function clearAll() { | ||
this.cache.clearAll(); | ||
return this; | ||
}; | ||
clearAll() { | ||
this.cache.clearAll(); | ||
return this; | ||
} | ||
} | ||
return MemoCache; | ||
})(AbstractMemoCache); | ||
@@ -245,3 +326,3 @@ function assertFn(fn) { | ||
assertFn(fn); | ||
const cache = new MemoCache(options); | ||
var cache = new MemoCache(options); | ||
memo.cache = cache; | ||
@@ -251,3 +332,3 @@ return memo; | ||
function memo(key) { | ||
let value = cache.get(key); | ||
var value = cache.get(key); | ||
@@ -265,13 +346,13 @@ if (value === undefined) { | ||
assertFn(fn); | ||
const memo = createMemo( | ||
key => | ||
Promise.resolve(fn(key)).catch(error => { | ||
memo.cache.clear(key); | ||
return Promise.reject(error); | ||
}), | ||
options, | ||
); | ||
var memo = createMemo(function(key) { | ||
return Promise.resolve(fn(key)).catch(function(error) { | ||
memo.cache.clear(key); | ||
return Promise.reject(error); | ||
}); | ||
}, options); | ||
return memo; | ||
} | ||
export { MemoCache, createMemo, createPromiseMemo }; | ||
exports.MemoCache = MemoCache; | ||
exports.createMemo = createMemo; | ||
exports.createPromiseMemo = createPromiseMemo; |
@@ -5,3 +5,3 @@ { | ||
"private": false, | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Memoization utils for JavaScript", | ||
@@ -8,0 +8,0 @@ "repository": "https://github.com/umidbekkarimov/memo.git", |
@@ -6,3 +6,3 @@ # memo | ||
[![npm version](https://img.shields.io/npm/v/create-memo.svg)](https://npmjs.com/create-memo) | ||
[![npm minzipped size](https://img.shields.io/bundlephobia/minzip/create-memo.svg)](https://npmjs.com/create-memo) | ||
[![npm minzipped size](https://img.shields.io/bundlephobia/minzip/create-memo.svg)](https://bundlephobia.com/result?p=create-memo) | ||
[![npm type definitions](https://img.shields.io/npm/types/create-memo.svg)](https://npmjs.com/create-memo) | ||
@@ -9,0 +9,0 @@ [![npm downloads](https://img.shields.io/npm/dm/create-memo.svg)](https://npmjs.com/create-memo) |
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
28364
15
876