-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(👻): extract cache behavior with haunted-polymer hook (#120)
* feat(👻): wip: extract behavior hooks with haunted-polymer Start with a small hook to extract as much of the caching as possible. Also fixes a test suite setup. WIP to move to haunted-polymer from cosmoz-utils if merged. Signed-off-by: Patrik Kullman <[email protected]> * fix: use haunted-polymer from cosmoz-utils Signed-off-by: Patrik Kullman <[email protected]> * fix: cache -> use-cache name (PR review) Signed-off-by: Patrik Kullman <[email protected]>
- Loading branch information
Patrik Kullman
authored
Jun 18, 2020
1 parent
68ec600
commit 126aeac
Showing
5 changed files
with
2,257 additions
and
6,851 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { useEffect } from 'haunted'; | ||
|
||
let storage = {}; | ||
|
||
const clear = () => { | ||
storage = {}; | ||
}, | ||
cachePurgeHandler = event => { | ||
if (Array.isArray(event?.detail?.ids) && event.detail.ids.length > 0) { | ||
event.detail.ids.forEach(id => delete storage[id]); | ||
return; | ||
} | ||
clear(); | ||
}; | ||
|
||
export const useCache = () => { | ||
|
||
useEffect(() => { | ||
window.addEventListener('cosmoz-cache-purge', cachePurgeHandler); | ||
return () => { | ||
clear(); | ||
window.removeEventListener('cosmoz-cache-purge', cachePurgeHandler); | ||
}; | ||
}, []); | ||
|
||
return { | ||
clear, | ||
drop(key) { | ||
delete storage[key]; | ||
}, | ||
dropItem(item) { | ||
Object.entries(storage) | ||
.filter(([, value]) => value === item) | ||
.forEach(([key]) => delete storage[key]); | ||
}, | ||
get(key) { | ||
return storage[key]; | ||
}, | ||
set(key, item) { | ||
storage[key] = item; | ||
} | ||
}; | ||
}; |
Oops, something went wrong.