-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69f24a0
commit b0a0ae8
Showing
4 changed files
with
64 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
"name": "@vencakrecl/vuex-simple-cache", | ||
"author": "Venca Krecl <[email protected]>", | ||
"description": "Simple cache for vuex action", | ||
"version": "1.0.0-alpha.7", | ||
"version": "1.0.0-alpha.8", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,57 @@ | ||
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, | ||
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); | ||
class VuexSimpleCache { | ||
private items: Map<string, number>; | ||
|
||
constructor() { | ||
this.items = new Map(); | ||
} | ||
|
||
public cacheAction = <S extends Record<string, any>, R>( | ||
key: string, | ||
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) { | ||
if (this.items.has(key)) { | ||
if (Date.now() - expiration > (this.items.get(key) || 0)) { | ||
this.items.delete(key); | ||
|
||
return action(context); | ||
} | ||
} else { | ||
this.items.set(key, Date.now()); | ||
} | ||
} else { | ||
expirations.set(timestampKey, Date.now()); | ||
} | ||
} | ||
|
||
if (Array.isArray(context.state[key])) { | ||
if (context.state[key].length) { | ||
return context.state[key]; | ||
if (Array.isArray(context.state[key])) { | ||
if (context.state[key].length) { | ||
return context.state[key]; | ||
} | ||
|
||
return action(context); | ||
} | ||
|
||
return action(context); | ||
} | ||
if (context.state[key] instanceof Object) { | ||
if (Object.keys(context.state[key]).length) { | ||
return context.state[key]; | ||
} | ||
|
||
return action(context); | ||
} | ||
|
||
if (context.state[key] instanceof Object) { | ||
if (Object.keys(context.state[key]).length) { | ||
if (context.state[key]) { | ||
return context.state[key]; | ||
} | ||
|
||
return action(context); | ||
} | ||
|
||
if (context.state[key]) { | ||
return context.state[key]; | ||
} | ||
|
||
return action(context); | ||
}; | ||
}; | ||
}; | ||
} | ||
|
||
export default VuexSimpleCache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters