reflux-store-cache-mixin
Advanced tools
Comparing version 0.0.3 to 0.0.4
"use strict"; | ||
var Map = require("immutable").Map; | ||
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; }; | ||
var _cache = require("./cache"); | ||
var _ = _interopRequire(require("lodash")); | ||
var getTimestamp = _cache.getTimestamp; | ||
var hasExpired = _cache.hasExpired; | ||
var Immutable = _interopRequire(require("immutable")); | ||
var _util = require("./util"); | ||
var getTimestamp = _util.getTimestamp; | ||
var hasExpired = _util.hasExpired; | ||
// Key not possible from plain JS | ||
var TIMESTAMPKEY = { _timestamp: "_timestamp" }; | ||
var DATAKEY = { _data: "_data" }; | ||
var TIMESTAMPKEY = "__timestamp"; | ||
var DATAKEY = "__data"; | ||
@@ -17,3 +21,3 @@ var StoreCacheMixin = { | ||
init: function init() { | ||
this.cache = new Map(); | ||
this.cache = new Immutable.Map(); | ||
}, | ||
@@ -39,2 +43,5 @@ | ||
dataKey = Immutable.fromJS(dataKey); | ||
instanceKey = Immutable.fromJS(instanceKey); | ||
if (this.cache.has(dataKey) && this.cache.get(dataKey).has(instanceKey) && !hasExpired(this.cache.get(dataKey).get(instanceKey).get(TIMESTAMPKEY), duration || this._ttl)) { | ||
@@ -58,4 +65,7 @@ return this.cache.get(dataKey).get(instanceKey).get(DATAKEY); | ||
var newData = new Map().set(DATAKEY, data).set(TIMESTAMPKEY, getTimestamp()); | ||
dataKey = Immutable.fromJS(dataKey); | ||
instanceKey = Immutable.fromJS(instanceKey); | ||
var newData = new Immutable.Map().set(DATAKEY, data).set(TIMESTAMPKEY, getTimestamp()); | ||
var oldData = null; | ||
@@ -68,3 +78,3 @@ | ||
if (!this.cache.has(dataKey)) { | ||
this.cache = this.cache.set(dataKey, new Map()); | ||
this.cache = this.cache.set(dataKey, new Immutable.Map()); | ||
} | ||
@@ -88,2 +98,5 @@ | ||
dataKey = Immutable.fromJS(dataKey); | ||
instanceKey = Immutable.fromJS(instanceKey); | ||
var oldData = null; | ||
@@ -96,3 +109,3 @@ | ||
if (!this.cache.has(dataKey)) { | ||
this.cache = this.cache.set(dataKey, new Map()); | ||
this.cache = this.cache.set(dataKey, new Immutable.Map()); | ||
} | ||
@@ -103,4 +116,25 @@ | ||
return oldData; | ||
}, | ||
/** | ||
* Simplified usage | ||
* @param {*} dataKey - Which data to fetch from | ||
* @param {*} instanceKey - Which instance for that data to fetch | ||
* @param {Func<Obj?,Func<Objc>>} handler - Loader and saver callback | ||
*/ | ||
loadData: function loadData(dataKey, instanceKey, handler) { | ||
if (!_.isFunction(handler)) { | ||
throw new Error("Invalid handler: must be a function"); | ||
} | ||
var data = this.fetchData(dataKey, instanceKey); | ||
if (!data) { | ||
return handler(null, this.storeData.bind(this, dataKey, instanceKey)); | ||
} | ||
return handler(data, _.noop); | ||
} }; | ||
module.exports = StoreCacheMixin; |
{ | ||
"name": "reflux-store-cache-mixin", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Reflux store chaching mixin to reduce unnecessary calls", | ||
@@ -26,2 +26,3 @@ "main": "dist/index.js", | ||
"immutable": "^3.7.4", | ||
"lodash": "^3.10.0", | ||
"moment": "^2.10.3" | ||
@@ -28,0 +29,0 @@ }, |
@@ -1,8 +0,9 @@ | ||
import { Map } from 'immutable'; | ||
import _ from 'lodash'; | ||
import Immutable from 'immutable'; | ||
import { getTimestamp, hasExpired } from './cache'; | ||
import { getTimestamp, hasExpired } from './util'; | ||
// Key not possible from plain JS | ||
const TIMESTAMPKEY = { _timestamp : '_timestamp' }; | ||
const DATAKEY = { _data : '_data' }; | ||
const TIMESTAMPKEY = '__timestamp'; | ||
const DATAKEY = '__data'; | ||
@@ -12,3 +13,3 @@ const StoreCacheMixin = { | ||
init() { | ||
this.cache = new Map(); | ||
this.cache = new Immutable.Map(); | ||
}, | ||
@@ -34,2 +35,5 @@ | ||
dataKey = Immutable.fromJS(dataKey); | ||
instanceKey = Immutable.fromJS(instanceKey); | ||
if ( | ||
@@ -59,3 +63,6 @@ this.cache.has(dataKey) && | ||
const newData = new Map() | ||
dataKey = Immutable.fromJS(dataKey); | ||
instanceKey = Immutable.fromJS(instanceKey); | ||
const newData = new Immutable.Map() | ||
.set(DATAKEY, data) | ||
@@ -77,3 +84,3 @@ .set(TIMESTAMPKEY, getTimestamp()); | ||
if (! this.cache.has(dataKey)) { | ||
this.cache = this.cache.set(dataKey, new Map()); | ||
this.cache = this.cache.set(dataKey, new Immutable.Map()); | ||
} | ||
@@ -101,2 +108,5 @@ | ||
dataKey = Immutable.fromJS(dataKey); | ||
instanceKey = Immutable.fromJS(instanceKey); | ||
let oldData = null; | ||
@@ -115,3 +125,3 @@ | ||
if (! this.cache.has(dataKey)) { | ||
this.cache = this.cache.set(dataKey, new Map()); | ||
this.cache = this.cache.set(dataKey, new Immutable.Map()); | ||
} | ||
@@ -128,4 +138,27 @@ | ||
/** | ||
* Simplified usage | ||
* @param {*} dataKey - Which data to fetch from | ||
* @param {*} instanceKey - Which instance for that data to fetch | ||
* @param {Func<Obj?,Func<Objc>>} handler - Loader and saver callback | ||
*/ | ||
loadData(dataKey, instanceKey, handler) { | ||
if (! _.isFunction(handler)) { | ||
throw new Error('Invalid handler: must be a function'); | ||
} | ||
const data = this.fetchData(dataKey, instanceKey); | ||
if (! data) { | ||
return handler(null, this.storeData.bind(this, dataKey, instanceKey)); | ||
} | ||
return handler(data, _.noop); | ||
}, | ||
}; | ||
export default StoreCacheMixin; |
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
14902
252
3
+ Addedlodash@^3.10.0
+ Addedlodash@3.10.1(transitive)