Skip to content
This repository has been archived by the owner on Dec 23, 2019. It is now read-only.

Commit

Permalink
Bugfix/remove cosmic (#9)
Browse files Browse the repository at this point in the history
* Remove cosmic and (almost) disable cache by default
  • Loading branch information
cvburgess authored Nov 16, 2017
1 parent c43869f commit c3414a8
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 87 deletions.
32 changes: 1 addition & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ withData({

#### Cache control options

By default, every entry is cached for one minute. You can overwrite this by passing an array (instead of a function) as the entry's value, with the first item being the action (function that returns a promise) and the second being an object with any of the following properties:
By default, every entry is cached for one second. You can overwrite this by passing an array (instead of a function) as the entry's value, with the first item being the action (function that returns a promise) and the second being an object with any of the following properties:

- `maxAge: Number` - sets the time (in seconds) that the data should be cached
- `noCache: Boolean` - skips the cache lookup and forces the action to be run
Expand Down Expand Up @@ -175,33 +175,3 @@ const Notifications = ({ data: { notifications } }) => (
Currently `withData` assumes that your error will be [formatted like an Axios error](https://github.com/axios/axios#handling-errors). This was explicitly added to filter out any errors from the child component that were not caused by data fetching.
In the future, this may become more generic and support other formats.
### Configuration
This project uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) to handle global defaults. You can specify a default `maxAge`, `noCache`, and `pollInterval`.
#### via `package.json` file
The following code will disable all caching.
```json
...
"withdata": {
"maxAge": 0,
"noCache": true
}
...
```
#### via `withdata.config.js` file
The following code will disable all caching (same as above).
```js
const config = {
"maxAge": 0,
"noCache": true
};

export default config;
```
15 changes: 3 additions & 12 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ var _observe = require("store/plugins/observe");

var _observe2 = _interopRequireDefault(_observe);

var _cosmiconfig = require("cosmiconfig");

var _cosmiconfig2 = _interopRequireDefault(_cosmiconfig);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
Expand All @@ -31,18 +27,13 @@ var SECOND = 1000;

_store2.default.addPlugin([_expire2.default, _observe2.default]);

var explorer = (0, _cosmiconfig2.default)("withdata", { sync: true });
var defaultConfig = explorer.load(process.cwd()) || {};
var defaultConfig = { maxAge: 1 };

var set = exports.set = function set(key, value) {
var maxAge = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultConfig.maxAge;

if (maxAge) {
var expiresAt = new Date().getTime() + maxAge * SECOND;
_store2.default.set(key, value, expiresAt);
} else {
_store2.default.set(key, value);
}
var expiresAt = new Date().getTime() + maxAge * SECOND;
_store2.default.set(key, value, expiresAt);
};

var observeData = function observeData(defaultKey, dataFn, onNext, onError) {
Expand Down
43 changes: 9 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"react": ">=15.0.0"
},
"dependencies": {
"cosmiconfig": "^3.1.0",
"immutability-helper": "^2.4.0",
"store": "^2.0.12"
}
Expand Down
12 changes: 3 additions & 9 deletions src/cache.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import store from "store";
import expirePlugin from "store/plugins/expire";
import observePlugin from "store/plugins/observe";
import cosmiconfig from "cosmiconfig";

const SECOND = 1000;

store.addPlugin([expirePlugin, observePlugin]);

const explorer = cosmiconfig("withdata", { sync: true });
const defaultConfig = explorer.load(process.cwd()) || {};
const defaultConfig = { maxAge: 1 };

export const set = (key, value, maxAge = defaultConfig.maxAge) => {
if (maxAge) {
const expiresAt = new Date().getTime() + maxAge * SECOND;
store.set(key, value, expiresAt);
} else {
store.set(key, value);
}
const expiresAt = new Date().getTime() + maxAge * SECOND;
store.set(key, value, expiresAt);
};

export const observeData = (
Expand Down

0 comments on commit c3414a8

Please sign in to comment.