Skip to content

Commit

Permalink
Update CI, add expiration for cache
Browse files Browse the repository at this point in the history
  • Loading branch information
vencakrecl committed Nov 23, 2020
1 parent 1db59b0 commit 088cdcc
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 21 deletions.
25 changes: 22 additions & 3 deletions .github/workflows/main.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: CI
on: [push]
jobs:
build:
test:
name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }}

runs-on: ${{ matrix.os }}
strategy:
matrix:
node: ['10.x', '12.x', '14.x']
os: [ubuntu-latest, windows-latest, macOS-latest]
node: ['14.x']
os: [ubuntu-latest]

steps:
- name: Checkout repo
Expand All @@ -30,3 +30,22 @@ jobs:

- name: Build
run: yarn build

publish:
name: Publish package

runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/v')
needs: test
steps:
- uses: actions/checkout@v2
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v1
with:
node-version: '14.x'
registry-url: 'https://registry.npmjs.org'
scope: '@vencakrecl'
- run: yarn
- run: yarn publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
12 changes: 0 additions & 12 deletions .github/workflows/size.yml

This file was deleted.

48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
# vuex-simple-cache

[![NPM package version][npm]](https://www.npmjs.com/package/@vencakrecl/vuex-simple-cache)
[![License][license]](https://github.com/VencaKrecl/vuex-simple-cache/blob/master/LICENSE)
[![Last test status][ci]](https://github.com/VencaKrecl/vuex-simple-cache/actions?query=workflow%3ACI)

* simple cache for vuex action

## How to install
```bash
npm install
```
npm install vuex-simple-cache
# OR
yarn add vuex-simple-cache
```

## How to use
```js
import Vue from 'vue';
import Vuex, { Store } from 'vuex';
import { cacheAction } from 'vuex-simple-cache';

Vue.use(Vuex);

const store = new Store({});

store.registerModule('test', {
state: {
items: [{ name: 'cache' }],
},
actions: {
testAction: cacheAction('items', ({ commit }) => { // cache data for 30 seconds
// call API
const data = [{ name: 'cache' }];

commit('testMutation', data);

return data;
}, 30),
},
mutation: {
testMutation: (state, payload) => {
state.items = payload
}
}
})
```

[npm]: https://img.shields.io/npm/v/@vencakrecl/vuex-simple-cache.svg?style=flat-square
[license]: https://img.shields.io/npm/l/@vencakrecl/vuex-simple-cache.svg?style=flat-square
[ci]: https://img.shields.io/github/workflow/status/VencaKrecl/vuex-simple-cache/CI
17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
{
"name": "@vencakrecl/vuex-simple-cache",
"author": "Venca Krecl",
"version": "1.0.0-alpha.1",
"author": "Venca Krecl <[email protected]>",
"description": "Simple cache for vuex action",
"version": "1.0.0-alpha.6",
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/VencaKrecl/vuex-simple-cache"
},
"keywords": [
"vue",
"vuex",
"cache"
],
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand All @@ -19,7 +29,8 @@
"lint": "tsdx lint",
"prepare": "tsdx build",
"size": "size-limit",
"analyze": "size-limit --why"
"analyze": "size-limit --why",
"publish-package": "yarn publish --non-interactive --access public"
},
"peerDependencies": {},
"husky": {
Expand Down
19 changes: 18 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import { ActionContext } from 'vuex';

const expirations = new Map();

export const cacheAction = <S extends Record<string, any>, R>(
key: string,
action: (context: ActionContext<S, R>) => any
action: (context: ActionContext<S, R>) => any,
expiration: number = 0
): any => {
return (context: ActionContext<S, R>) => {
if (!(key in context.state)) {
throw new Error(`Key "${key}" in state doesn't exist.`);
}

if (expiration > 0) {
const timestampKey: string = `__timestamp_${key}`;

if (expirations.has(timestampKey)) {
if (Date.now() - expiration > expirations.get(timestampKey)) {
expirations.delete(timestampKey);

return action(context);
}
} else {
expirations.set(timestampKey, Date.now());
}
}

if (Array.isArray(context.state[key])) {
if (context.state[key].length) {
return context.state[key];
Expand Down
27 changes: 27 additions & 0 deletions test/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,31 @@ describe('test vuex simple cache', () => {
},
});
});

it('expiration', async () => {
const store = new Store({});

store.registerModule('test', {
state: {
items: [{ name: 'cache' }],
},
actions: {
[TEST_ACTION_CACHE]: cacheAction(
'items',
() => {
// call API
return [{ name: 'test' }];
},
2
),
},
});

let data = await store.dispatch(TEST_ACTION_CACHE);
expect(data).toStrictEqual([{ name: 'cache' }]);
// wait for expiration
await new Promise(r => setTimeout(r, 2 * 1000));
data = await store.dispatch(TEST_ACTION_CACHE);
expect(data).toStrictEqual([{ name: 'test' }]);
});
});

0 comments on commit 088cdcc

Please sign in to comment.