From 72bcf32915f557ff11986c3527055f916bac82b5 Mon Sep 17 00:00:00 2001 From: Jason Wall Date: Sun, 23 Aug 2020 08:39:14 -0400 Subject: [PATCH] refactor card-cache operations to new file in common --- .../app/common/card-cache-operations.spec.ts | 75 +++++++++++++++++++ src/src/app/common/card-cache-operations.ts | 23 ++++++ .../saved-page-card.component.ts | 4 +- src/src/app/pages/search/search.page.ts | 5 +- .../app/services/app-state-reducer.spec.ts | 75 +------------------ src/src/app/services/app-state-reducer.ts | 39 +++------- src/src/app/services/app.service.ts | 3 + 7 files changed, 115 insertions(+), 109 deletions(-) create mode 100644 src/src/app/common/card-cache-operations.spec.ts create mode 100644 src/src/app/common/card-cache-operations.ts diff --git a/src/src/app/common/card-cache-operations.spec.ts b/src/src/app/common/card-cache-operations.spec.ts new file mode 100644 index 00000000..80d6c53e --- /dev/null +++ b/src/src/app/common/card-cache-operations.spec.ts @@ -0,0 +1,75 @@ +import { CardType, CardItem } from '../models/card-state'; +import { NoteItem } from '../models/note-state'; +import { HashTable } from './hashtable'; +import { updateInCardCache, getCardCacheKey, removeFromCardCache } from './card-cache-operations'; + +describe('Card Cache', () => { + it('updateCache', () => { + const card1: CardItem = { + qry: 'jason', + type: CardType.Passage, + data: null, + }; + const card2: CardItem = { + qry: 'jason', + type: CardType.Passage, + data: { + id: 'adsf', + xref: '', + title: 'adsf', + content: '', + } as NoteItem, + }; + const card3: CardItem = { + qry: 'jason3', + type: CardType.Passage, + data: null, + }; + + const cache: HashTable = {}; + let newCache = updateInCardCache(card1, cache); + + expect(newCache[getCardCacheKey(card1)].qry).toBe('jason', 'Should have added the card'); + expect(newCache[getCardCacheKey(card1)].data).toBe(null, 'Should have null data'); + + newCache = updateInCardCache(card2, newCache); + expect(newCache[getCardCacheKey(card2)].qry).toBe('jason', 'Should have added the card'); + expect((newCache[getCardCacheKey(card1)].data as NoteItem).title).toBe('adsf', 'Should have added the card'); + expect(Object.keys(newCache).length).toBe(1, 'Should still have only 1 item.'); + + newCache = updateInCardCache(card3, newCache); + expect(newCache[getCardCacheKey(card3)].qry).toBe('jason3', 'Should have added the card'); + expect(Object.keys(newCache).length).toBe(2, 'Should have 2 items.'); + }); + + it('removeFromCache', () => { + const card1: CardItem = { + qry: 'jason', + type: CardType.Passage, + data: null, + }; + const card2: CardItem = { + qry: 'jason', + type: CardType.Passage, + data: { + id: 'adsf', + xref: '', + title: 'adsf', + content: '', + } as NoteItem, + }; + const card3: CardItem = { + qry: 'jason3', + type: CardType.Passage, + data: null, + }; + + const cache: HashTable = {}; + let newCache = updateInCardCache(card1, cache); + newCache = updateInCardCache(card3, newCache); + expect(Object.keys(newCache).length).toBe(2, 'Should have 2 items.'); + + newCache = removeFromCardCache(card1, newCache); + expect(Object.keys(newCache).length).toBe(1, 'Should remove 1 item'); + }); +}); diff --git a/src/src/app/common/card-cache-operations.ts b/src/src/app/common/card-cache-operations.ts new file mode 100644 index 00000000..7f24e73f --- /dev/null +++ b/src/src/app/common/card-cache-operations.ts @@ -0,0 +1,23 @@ +import { HashTable } from '../common/hashtable'; +import { CardItem, DataReference } from '../models/card-state'; + +export function updateInCardCache(card: CardItem, cardCache: HashTable): HashTable { + const cache = { ...cardCache }; + cache[getCardCacheKey(card)] = card; + return cache; +} + +export function removeFromCardCache(card: CardItem, cardCache: HashTable): HashTable { + const cache = { ...cardCache }; + delete cache[getCardCacheKey(card)]; + return cache; +} + +export function getFromCardCache(ref: DataReference, cardCache: HashTable) { + const key = getCardCacheKey(ref); + return cardCache[key]; +} + +export function getCardCacheKey(card: DataReference) { + return `${card.qry}:${card.type}`; +} diff --git a/src/src/app/components/saved-page-card/saved-page-card.component.ts b/src/src/app/components/saved-page-card/saved-page-card.component.ts index 8a5b055d..59f5d786 100644 --- a/src/src/app/components/saved-page-card/saved-page-card.component.ts +++ b/src/src/app/components/saved-page-card/saved-page-card.component.ts @@ -11,7 +11,7 @@ import { MatSnackBar } from '@angular/material/snack-bar'; import { PageEditModalComponent } from '../page-edit-modal/page-edit-modal.component'; import { HashTable } from 'src/app/common/hashtable'; import { SubscriberBase } from 'src/app/common/subscriber-base'; -import { getCardFromCache } from 'src/app/services/app-state-reducer'; +import { getFromCardCache } from 'src/app/common/card-cache-operations'; @Component({ selector: 'app-saved-page-card', @@ -41,7 +41,7 @@ export class SavedPageCardComponent extends SubscriberBase implements OnInit { format(item: DataReference) { if (item.type === CardType.Note) { - return `Note: ${(getCardFromCache(item, this.cache).data as NoteItem).title}`; + return `Note: ${(getFromCardCache(item, this.cache).data as NoteItem).title}`; } else if (item.type === CardType.Passage) { return `Passage: ${item.qry}`; } else if (item.type === CardType.Strongs) { diff --git a/src/src/app/pages/search/search.page.ts b/src/src/app/pages/search/search.page.ts index 25f98895..e0ed40f6 100644 --- a/src/src/app/pages/search/search.page.ts +++ b/src/src/app/pages/search/search.page.ts @@ -6,10 +6,9 @@ import { MatAutocompleteTrigger, MatAutocomplete } from '@angular/material/autoc import { AppService } from '../../services/app.service'; import { NavService } from '../../services/nav.service'; import { SubscriberBase } from '../../common/subscriber-base'; -import { BibleReference } from '../../common/bible-reference'; import { VersePickerModalComponent } from '../../components/verse-picker-modal/verse-picker-modal.component'; import { CardItem, CardType } from 'src/app/models/card-state'; -import { getCardFromCache } from 'src/app/services/app-state-reducer'; +import { getFromCardCache } from 'src/app/common/card-cache-operations'; @Component({ selector: 'app-search-page', @@ -18,7 +17,7 @@ import { getCardFromCache } from 'src/app/services/app-state-reducer'; changeDetection: ChangeDetectionStrategy.OnPush, }) export class SearchPage extends SubscriberBase implements OnInit { - cards$ = this.appService.select((state) => state.currentCards.map((o) => getCardFromCache(o, state.cardCache))); + cards$ = this.appService.select((state) => state.currentCards.map((o) => getFromCardCache(o, state.cardCache))); suggestions$ = this.appService.select((state) => state.autocomplete); savedPagedLoaded = false; diff --git a/src/src/app/services/app-state-reducer.spec.ts b/src/src/app/services/app-state-reducer.spec.ts index 069e23e6..77e899cc 100644 --- a/src/src/app/services/app-state-reducer.spec.ts +++ b/src/src/app/services/app-state-reducer.spec.ts @@ -1,13 +1,11 @@ import { TestBed } from '@angular/core/testing'; -import { reducer, getNewestStorable, updateCache, getCardItemKey, removeFromCache } from './app-state-reducer'; +import { reducer, getNewestStorable } from './app-state-reducer'; import { AppActionFactory } from './app-state-actions'; import { Overlap } from '../common/bible-reference'; import { Storable } from '../common/storable'; import { CardType, CardItem } from '../models/card-state'; import { SavedPage } from '../models/page-state'; import { AppState } from '../models/app-state'; -import { HashTable } from '../common/hashtable'; -import { NoteItem } from '../models/note-state'; describe('getNewestStorable', () => { it('maybeMutateStorable', () => { @@ -28,77 +26,6 @@ describe('getNewestStorable', () => { }); }); -describe('Card Cache', () => { - it('updateCache', () => { - const card1: CardItem = { - qry: 'jason', - type: CardType.Passage, - data: null, - }; - const card2: CardItem = { - qry: 'jason', - type: CardType.Passage, - data: { - id: 'adsf', - xref: '', - title: 'adsf', - content: '', - } as NoteItem, - }; - const card3: CardItem = { - qry: 'jason3', - type: CardType.Passage, - data: null, - }; - - const cache: HashTable = {}; - let newCache = updateCache(card1, cache); - - expect(newCache[getCardItemKey(card1)].qry).toBe('jason', 'Should have added the card'); - expect(newCache[getCardItemKey(card1)].data).toBe(null, 'Should have null data'); - - newCache = updateCache(card2, newCache); - expect(newCache[getCardItemKey(card2)].qry).toBe('jason', 'Should have added the card'); - expect((newCache[getCardItemKey(card1)].data as NoteItem).title).toBe('adsf', 'Should have added the card'); - expect(Object.keys(newCache).length).toBe(1, 'Should still have only 1 item.'); - - newCache = updateCache(card3, newCache); - expect(newCache[getCardItemKey(card3)].qry).toBe('jason3', 'Should have added the card'); - expect(Object.keys(newCache).length).toBe(2, 'Should have 2 items.'); - }); - - it('removeFromCache', () => { - const card1: CardItem = { - qry: 'jason', - type: CardType.Passage, - data: null, - }; - const card2: CardItem = { - qry: 'jason', - type: CardType.Passage, - data: { - id: 'adsf', - xref: '', - title: 'adsf', - content: '', - } as NoteItem, - }; - const card3: CardItem = { - qry: 'jason3', - type: CardType.Passage, - data: null, - }; - - const cache: HashTable = {}; - let newCache = updateCache(card1, cache); - newCache = updateCache(card3, newCache); - expect(Object.keys(newCache).length).toBe(2, 'Should have 2 items.'); - - newCache = removeFromCache(card1, newCache); - expect(Object.keys(newCache).length).toBe(1, 'Should remove 1 item'); - }); -}); - describe('AppService Reducer', () => { const preState = { user: null, diff --git a/src/src/app/services/app-state-reducer.ts b/src/src/app/services/app-state-reducer.ts index 7f42ad03..c9614ac0 100644 --- a/src/src/app/services/app-state-reducer.ts +++ b/src/src/app/services/app-state-reducer.ts @@ -5,13 +5,13 @@ import { IStorable, Storable } from '../common/storable'; import { NoteItem } from '../models/note-state'; import { mergeCardList } from '../common/card-operations'; +import { updateInCardCache, removeFromCardCache, getFromCardCache } from '../common/card-cache-operations'; import { AppAction, AppActionFactory } from './app-state-actions'; import { initialState } from './app-state-initial-state'; import { SavedPage } from '../models/page-state'; import { CardType, CardItem, DataReference } from '../models/card-state'; import { moveItem, moveItemUpOrDown } from '../common/array-operations'; -import { HashTable } from '../common/hashtable'; export function getNewestStorable(candidate: IStorable, incumbant: IStorable): IStorable { // if the candidate is null, then return the state. @@ -28,27 +28,6 @@ export function getNewestStorable(candidate: IStorable, incumbant: IStorab return incumbant; } -export function updateCache(card: CardItem, cardCache: HashTable): HashTable { - const cache = { ...cardCache }; - cache[getCardItemKey(card)] = card; - return cache; -} - -export function removeFromCache(card: CardItem, cardCache: HashTable): HashTable { - const cache = { ...cardCache }; - delete cache[getCardItemKey(card)]; - return cache; -} - -export function getCardItemKey(card: DataReference) { - return `${card.qry}:${card.type}`; -} - -export function getCardFromCache(ref: DataReference, cardCache: HashTable) { - const key = getCardItemKey(ref); - return cardCache[key]; -} - export function reducer(state: AppState, action: AppAction): AppState { // somtimes the state is null. lets not complain if that happens. if (state === undefined) { @@ -287,7 +266,7 @@ export function reducer(state: AppState, action: AppAction): AppState { return { ...state, currentCards: cards, - cardCache: updateCache(action.card, state.cardCache), + cardCache: updateInCardCache(action.card, state.cardCache), }; } case 'UPDATE_CARD': { @@ -299,7 +278,7 @@ export function reducer(state: AppState, action: AppAction): AppState { } return c; }), - cardCache: updateCache(action.newCard, removeFromCache(action.oldCard, state.cardCache)), + cardCache: updateInCardCache(action.newCard, removeFromCardCache(action.oldCard, state.cardCache)), }; } case 'REMOVE_CARD': { @@ -329,7 +308,7 @@ export function reducer(state: AppState, action: AppAction): AppState { currentSavedPage, savedPages, currentCards: [...state.currentCards.filter((c) => c !== action.card)], - cardCache: removeFromCache(action.card, state.cardCache), + cardCache: removeFromCardCache(action.card, state.cardCache), }; } case 'MOVE_CARD': { @@ -344,7 +323,7 @@ export function reducer(state: AppState, action: AppAction): AppState { case 'UPDATE_CARDS': { let cardCache = { ...state.cardCache }; for (const card of action.cards) { - cardCache = updateCache(card, cardCache); + cardCache = updateInCardCache(card, cardCache); } return { ...state, @@ -431,7 +410,7 @@ export function reducer(state: AppState, action: AppAction): AppState { const cards = [ ...state.currentCards.map((o) => { - const n = getCardFromCache(o, state.cardCache).data as NoteItem; + const n = getFromCardCache(o, state.cardCache).data as NoteItem; if (n && n.id === action.note.id) { return { ...o, @@ -452,7 +431,7 @@ export function reducer(state: AppState, action: AppAction): AppState { return { ...sp, queries: sp.queries.map((o) => { - const n = getCardFromCache(o, state.cardCache).data as NoteItem; + const n = getFromCardCache(o, state.cardCache).data as NoteItem; if (n && n.id === action.note.id) { return { ...o, @@ -482,7 +461,7 @@ export function reducer(state: AppState, action: AppAction): AppState { const cards = [ ...state.currentCards.filter((o) => { - const n = getCardFromCache(o, state.cardCache).data as NoteItem; + const n = getFromCardCache(o, state.cardCache).data as NoteItem; return !n || n.id !== action.note.id; }), ]; @@ -494,7 +473,7 @@ export function reducer(state: AppState, action: AppAction): AppState { return { ...sp, queries: sp.queries.filter((o) => { - const n = getCardFromCache(o, state.cardCache).data as NoteItem; + const n = getFromCardCache(o, state.cardCache).data as NoteItem; return !n || n.id !== action.note.id; }), }; diff --git a/src/src/app/services/app.service.ts b/src/src/app/services/app.service.ts index 5dd26082..95a2fc7e 100644 --- a/src/src/app/services/app.service.ts +++ b/src/src/app/services/app.service.ts @@ -183,6 +183,7 @@ export class AppService extends createStateService(reducer, initialState) { updateSettings(settings: IStorable) { this.dispatch(AppActionFactory.newUpdateSettings(settings)); } + updateDisplaySettings(displaySettings: DisplaySettings) { const state = this.getState(); @@ -195,6 +196,7 @@ export class AppService extends createStateService(reducer, initialState) { ) ); } + updatePageSettings(pageSettings: PageSettings) { const state = this.getState(); @@ -207,6 +209,7 @@ export class AppService extends createStateService(reducer, initialState) { ) ); } + //#endregion //#region Notes