Skip to content

Commit

Permalink
feat: 🎸 Cache prefixas med personnummer (#8)
Browse files Browse the repository at this point in the history
* feat: 🎸 Cache prefixas med personnummer

För enklare, selektive wipe
  • Loading branch information
JohanObrink authored Feb 14, 2021
1 parent 0108160 commit fc146ea
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 33 deletions.
13 changes: 8 additions & 5 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ const hook = <T>(
selector: StoreSelector<T>,
apiCaller: (api: Api) => ApiCall<T>,
): EntityHookResult<T> => {
const {
api, isLoggedIn, reporter, storage,
} = useApi()

const getState = (): EntityStoreRootState => store.getState() as unknown as EntityStoreRootState
const select = (storeState: EntityStoreRootState) => {
const stateMap = selector(storeState) || {}
const state = stateMap[key] || { status: 'pending', data: defaultValue }
return state
}
const {
api, isLoggedIn, reporter, storage,
} = useApi()
const initialState = select(getState())
const [state, setState] = useState(initialState)
const dispatch = useDispatch()
Expand All @@ -58,11 +59,13 @@ const hook = <T>(

// Only use cache when not in fake mode
if (!api.isFake) {
const pnr = api.getPersonalNumber()

// Only get from cache first time
if (state.status === 'pending') {
extra.getFromCache = () => storage.getItem(key)
extra.getFromCache = () => storage.getItem(`${pnr}_${key}`)
}
extra.saveToCache = (value: string) => storage.setItem(key, value)
extra.saveToCache = (value: string) => storage.setItem(`${pnr}_${key}`, value)
}
const action = loadAction<T>(entityName, extra)
dispatch(action)
Expand Down
3 changes: 2 additions & 1 deletion src/logout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ describe('logout - cleanup', () => {
beforeEach(() => {
response = [{ id: 1 }]
api = init()
api.getPersonalNumber.mockReturnValue('123')
api.getChildren.mockImplementation(() => (
new Promise((res) => {
setTimeout(() => res(response), 50)
})
))
storage = createStorage({
children: [{ id: 2 }],
'123_children': [{ id: 2 }],
}, 2)
})
afterEach(async () => {
Expand Down
7 changes: 4 additions & 3 deletions src/useCalendar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ describe('useCalendar(child)', () => {
beforeEach(() => {
response = [{ id: 1 }]
api = init()
api.getPersonalNumber.mockReturnValue('123')
api.getCalendar.mockImplementation(() => (
new Promise((res) => {
setTimeout(() => res(response), 50)
})
))
storage = createStorage({
calendar_10: [{ id: 2 }],
'123_calendar_10': [{ id: 2 }],
}, 2)
child = { id: 10 }
})
Expand Down Expand Up @@ -134,7 +135,7 @@ describe('useCalendar(child)', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.calendar_10).toEqual('[{"id":1}]')
expect(storage.cache['123_calendar_10']).toEqual('[{"id":1}]')
})
})
it('does not store in cache if fake', async () => {
Expand All @@ -148,7 +149,7 @@ describe('useCalendar(child)', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.calendar_10).toEqual('[{"id":2}]')
expect(storage.cache['123_calendar_10']).toEqual('[{"id":2}]')
})
})
it('retries if api fails', async () => {
Expand Down
7 changes: 4 additions & 3 deletions src/useChildlist.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ describe('useChildList()', () => {
beforeEach(() => {
response = [{ id: 1 }]
api = init()
api.getPersonalNumber.mockReturnValue('123')
api.getChildren.mockImplementation(() => (
new Promise((res) => {
setTimeout(() => res(response), 50)
})
))
storage = createStorage({
children: [{ id: 2 }],
'123_children': [{ id: 2 }],
}, 2)
})
afterEach(async () => {
Expand Down Expand Up @@ -120,7 +121,7 @@ describe('useChildList()', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.children).toEqual('[{"id":1}]')
expect(storage.cache['123_children']).toEqual('[{"id":1}]')
})
})
it('does not store in cache if fake', async () => {
Expand All @@ -134,7 +135,7 @@ describe('useChildList()', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.children).toEqual('[{"id":2}]')
expect(storage.cache['123_children']).toEqual('[{"id":2}]')
})
})
it('retries if api fails', async () => {
Expand Down
7 changes: 4 additions & 3 deletions src/useClassmates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ describe('useClassmates(child)', () => {
beforeEach(() => {
response = [{ id: 1 }]
api = init()
api.getPersonalNumber.mockReturnValue('123')
api.getClassmates.mockImplementation(() => (
new Promise((res) => {
setTimeout(() => res(response), 50)
})
))
storage = createStorage({
classmates_10: [{ id: 2 }],
'123_classmates_10': [{ id: 2 }],
}, 2)
child = { id: 10 }
})
Expand Down Expand Up @@ -122,7 +123,7 @@ describe('useClassmates(child)', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.classmates_10).toEqual('[{"id":1}]')
expect(storage.cache['123_classmates_10']).toEqual('[{"id":1}]')
})
})
it('does not store in cache if fake', async () => {
Expand All @@ -136,7 +137,7 @@ describe('useClassmates(child)', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.classmates_10).toEqual('[{"id":2}]')
expect(storage.cache['123_classmates_10']).toEqual('[{"id":2}]')
})
})
it('retries if api fails', async () => {
Expand Down
7 changes: 4 additions & 3 deletions src/useMenu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ describe('useMenu(child)', () => {
beforeEach(() => {
response = [{ id: 1 }]
api = init()
api.getPersonalNumber.mockReturnValue('123')
api.getMenu.mockImplementation(() => (
new Promise((res) => {
setTimeout(() => res(response), 50)
})
))
storage = createStorage({
menu_10: [{ id: 2 }],
'123_menu_10': [{ id: 2 }],
}, 2)
child = { id: 10 }
})
Expand Down Expand Up @@ -122,7 +123,7 @@ describe('useMenu(child)', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.menu_10).toEqual('[{"id":1}]')
expect(storage.cache['123_menu_10']).toEqual('[{"id":1}]')
})
})
it('does not store in cache if fake', async () => {
Expand All @@ -136,7 +137,7 @@ describe('useMenu(child)', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.menu_10).toEqual('[{"id":2}]')
expect(storage.cache['123_menu_10']).toEqual('[{"id":2}]')
})
})
it('retries if api fails', async () => {
Expand Down
7 changes: 4 additions & 3 deletions src/useNews.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ describe('useNews(child)', () => {
beforeEach(() => {
response = [{ id: 1 }]
api = init()
api.getPersonalNumber.mockReturnValue('123')
api.getNews.mockImplementation(() => (
new Promise((res) => {
setTimeout(() => res(response), 50)
})
))
storage = createStorage({
news_10: [{ id: 2 }],
'123_news_10': [{ id: 2 }],
}, 2)
child = { id: 10 }
})
Expand Down Expand Up @@ -122,7 +123,7 @@ describe('useNews(child)', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.news_10).toEqual('[{"id":1}]')
expect(storage.cache['123_news_10']).toEqual('[{"id":1}]')
})
})
it('does not store in cache if fake', async () => {
Expand All @@ -136,7 +137,7 @@ describe('useNews(child)', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.news_10).toEqual('[{"id":2}]')
expect(storage.cache['123_news_10']).toEqual('[{"id":2}]')
})
})
it('retries if api fails', async () => {
Expand Down
7 changes: 4 additions & 3 deletions src/useNewsDetails.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ describe('useNewsDetails(child, newsItem)', () => {
cached = { id: '1337', modified: 'yesterday', body: 'rich and old' }
response = { id: '1337', modified: 'now', body: 'rich and new' }
api = init()
api.getPersonalNumber.mockReturnValue('123')
api.getNewsDetails.mockImplementation(() => (
new Promise((res) => {
setTimeout(() => res(response), 50)
})
))
storage = createStorage({
news_details_1337: { ...cached },
'123_news_details_1337': { ...cached },
}, 2)
child = { id: 10 }
newsItem = { id: '1337', modified: 'now', body: 'simple' }
Expand Down Expand Up @@ -126,7 +127,7 @@ describe('useNewsDetails(child, newsItem)', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.news_details_1337).toEqual(JSON.stringify(response))
expect(storage.cache['123_news_details_1337']).toEqual(JSON.stringify(response))
})
})
it('does not store in cache if fake', async () => {
Expand All @@ -140,7 +141,7 @@ describe('useNewsDetails(child, newsItem)', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.news_details_1337).toEqual(JSON.stringify(cached))
expect(storage.cache['123_news_details_1337']).toEqual(JSON.stringify(cached))
})
})
it('retries if api fails', async () => {
Expand Down
7 changes: 4 additions & 3 deletions src/useNotifications.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ describe('useNotifications(child)', () => {
beforeEach(() => {
response = [{ id: 1 }]
api = init()
api.getPersonalNumber.mockReturnValue('123')
api.getNotifications.mockImplementation(() => (
new Promise((res) => {
setTimeout(() => res(response), 50)
})
))
storage = createStorage({
notifications_10: [{ id: 2 }],
'123_notifications_10': [{ id: 2 }],
}, 2)
child = { id: 10 }
})
Expand Down Expand Up @@ -122,7 +123,7 @@ describe('useNotifications(child)', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.notifications_10).toEqual('[{"id":1}]')
expect(storage.cache['123_notifications_10']).toEqual('[{"id":1}]')
})
})
it('does not store in cache if fake', async () => {
Expand All @@ -136,7 +137,7 @@ describe('useNotifications(child)', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.notifications_10).toEqual('[{"id":2}]')
expect(storage.cache['123_notifications_10']).toEqual('[{"id":2}]')
})
})
it('retries if api fails', async () => {
Expand Down
7 changes: 4 additions & 3 deletions src/useSchedule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ describe('useSchedule(child, from, to)', () => {
beforeEach(() => {
response = [{ id: 1 }]
api = init()
api.getPersonalNumber.mockReturnValue('123')
api.getSchedule.mockImplementation(() => (
new Promise((res) => {
setTimeout(() => res(response), 50)
})
))
storage = createStorage({
'schedule_10_2021-01-01_2021-01-08': [{ id: 2 }],
'123_schedule_10_2021-01-01_2021-01-08': [{ id: 2 }],
}, 2)
child = { id: 10 }
from = '2021-01-01'
Expand Down Expand Up @@ -126,7 +127,7 @@ describe('useSchedule(child, from, to)', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache['schedule_10_2021-01-01_2021-01-08']).toEqual('[{"id":1}]')
expect(storage.cache['123_schedule_10_2021-01-01_2021-01-08']).toEqual('[{"id":1}]')
})
})
it('does not store in cache if fake', async () => {
Expand All @@ -140,7 +141,7 @@ describe('useSchedule(child, from, to)', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache['schedule_10_2021-01-01_2021-01-08']).toEqual('[{"id":2}]')
expect(storage.cache['123_schedule_10_2021-01-01_2021-01-08']).toEqual('[{"id":2}]')
})
})
it('retries if api fails', async () => {
Expand Down
7 changes: 4 additions & 3 deletions src/useUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ describe('useUser()', () => {
beforeEach(() => {
response = { id: 1 }
api = init()
api.getPersonalNumber.mockReturnValue('123')
api.getUser.mockImplementation(() => (
new Promise((res) => {
setTimeout(() => res(response), 50)
})
))
storage = createStorage({
user: { id: 2 },
'123_user': { id: 2 },
}, 2)
})
afterEach(async () => {
Expand Down Expand Up @@ -120,7 +121,7 @@ describe('useUser()', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.user).toEqual('{"id":1}')
expect(storage.cache['123_user']).toEqual('{"id":1}')
})
})
it('does not store in cache if fake', async () => {
Expand All @@ -134,7 +135,7 @@ describe('useUser()', () => {
await waitForNextUpdate()
await pause(20)

expect(storage.cache.user).toEqual('{"id":2}')
expect(storage.cache['123_user']).toEqual('{"id":2}')
})
})
it('retries if api fails', async () => {
Expand Down

0 comments on commit fc146ea

Please sign in to comment.