From 32fe9deca12683dd923e55aa03c34545b7c595c7 Mon Sep 17 00:00:00 2001 From: Jim Kang Date: Mon, 29 Mar 2021 22:59:19 -0400 Subject: [PATCH] Use getRaw and setRaw in PilesPersister so that rehydrate and dehydrate don't kick in. This helps avoid situations in which things that are already dehydrated get dehydrated into nulls. --- machines/main-machine.ts | 23 ++++++++++++----------- meta/mscgen-sources.txt | 4 +++- persisters/piles-persister.ts | 8 +++++--- things/deck.ts | 32 ++++++++++++++++++++++++++++++++ wily.js/stores/stores.ts | 15 +++++++-------- 5 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 things/deck.ts diff --git a/machines/main-machine.ts b/machines/main-machine.ts index b0f0d94..8eeeec9 100644 --- a/machines/main-machine.ts +++ b/machines/main-machine.ts @@ -22,6 +22,8 @@ import { RenderDeck, RenderDeckCollection } from '../renderers/deck-renderers'; import { RenderPile, RenderPileCollection } from '../renderers/pile-renderers'; import curry from 'lodash.curry'; import { storeRegistry as registry } from '../wily.js/stores/store-registry'; +import { DehydrateDeck, RehydrateDeck } from '../things/deck'; +import { rehydratePile } from '../things/pile'; const deckIdsKey = 'ids__decks'; const pileIdsKey = 'ids__piles'; @@ -34,11 +36,11 @@ export function assembleMainMachine(switchToNewDecks) { null, () => CollectionStore( - deckIdsPersister, - thingPersister, - 'deck', - null, - loadThings(deckIdsKey) + { + idsPersister: deckIdsPersister, thingPersister, kind: 'deck', parentThingId: null, + vals: loadThings(deckIdsKey), + itemRehydrate: RehydrateDeck(thingPersister) + } ) ); @@ -46,7 +48,7 @@ export function assembleMainMachine(switchToNewDecks) { .get() .map((thing) => registry.makeStoreHappen(thing.id, () => - ThingStore(thingPersister, thing) // Rehydrate + ThingStore(thingPersister, thing, DehydrateDeck(thingPersister), RehydrateDeck(thingPersister)) ) ); @@ -106,11 +108,10 @@ function deckResponderMapper( null, () => CollectionStore( - pilesPersister, - thingPersister, - 'pile', - null, - loadThings(pileIdsKey) // This should come from the deck! + { + idsPersister: pilesPersister, thingPersister, kind: 'pile', parentThingId: null, + vals: deckStore.get().piles + } ) ); diff --git a/meta/mscgen-sources.txt b/meta/mscgen-sources.txt index 20322fe..74b6fc3 100644 --- a/meta/mscgen-sources.txt +++ b/meta/mscgen-sources.txt @@ -1,2 +1,4 @@ Constructing decks: -https://mscgen.js.org/?lang=msgenny&mirrorentities=true&style=classic&msc=width%3D%221200%22%2C%20watermark%3D%22App%20initialization%22%3B%0A%0AassembleMainMachine%20%3D%3E%20CollectionStore%3A%20(for%20deckCollectionStore)%3B%0ACollectionStore%20%3D%3E%20%22loadThings%22%3B%0AloadThings%20%3E%3E%20CollectionStore%3A%20deck%20objects%3B%0ACollectionStore%20%3E%3E%20assembleMainMachine%3A%20collectionStore%3B%0A%23assembleMainMachine%20note%20assembleMainMachine%3A%20After%20an%20indeterminate%20amount%20of%20time%E2%80%A6%3B%0A%0AassembleMainMachine%20%3D%3E%20collectionStore.get%3B%0AcollectionStore.get%20%3D%3E%20collectionStore.rehydrate%3B%0AcollectionStore.rehydrate%20%3D%3E%20idsPersister.get%3B%0A%0AidsPersister.get%20%3E%3E%20collectionStore.rehydrate%3A%20ids%3B%0AcollectionStore.rehydrate%20%3D%3E%20thingPersister.get%3A%20ids%3B%0AthingPersister.get%20%3E%3E%20collectionStore.rehydrate%3A%20Decks%3B%0AcollectionStore.rehydrate%20%3E%3E%20collectionStore.get%3A%20Decks%3B%0AcollectionStore.get%20%3E%3E%20assembleMainMachine%3A%20Decks%3B%0A%0AassembleMainMachine%20%3D%3E%20ThingStore%3A%20Deck%3B%0AThingStore%20%3E%3E%20assembleMainMachine%3A%20deckStore%3B%0A%0AassembleMainMachine%20%3D%3E%20deckResponderMapper%3A%20%22collectionStore%2C%20deckStore%22%3B%0AdeckResponderMapper%20%3D%3E%20deckStore.get%3B%0AdeckStore.get%20%3D%3E%20rehydrateDeck%3B%0ArehydrateDeck%20%3D%3E%20thingPersister.get%3A%20(pile%20id)%3B%0AthingPersister.get%20%3E%3E%20rehydrateDeck%3A%20Pile%3B%0ArehydrateDeck%20%3E%3E%20deckStore.get%20%3A%20Pile%3B%0AdeckStore.get%20%3E%3E%20deckResponderMapper%20%3A%20Deck%3B +https://mscgen.js.org/?lang=msgenny&mirrorentities=true&style=classic&msc=width%3D%221200%22%2C%20watermark%3D%22App%20initialization%22%3B%0A%0AassembleMainMachine%20%3D%3E%20CollectionStore%3A%20(for%20deckCollectionStore)%3B%0ACollectionStore%20%3D%3E%20%22loadThings%22%3B%0AloadThings%20%3E%3E%20CollectionStore%3A%20deck%20objects%3B%0ACollectionStore%20%3E%3E%20assembleMainMachine%3A%20collectionStore%3B%0A%23assembleMainMachine%20note%20assembleMainMachine%3A%20After%20an%20indeterminate%20amount%20of%20time%E2%80%A6%3B%0A%0AassembleMainMachine%20%3D%3E%20collectionStore.get%3B%0AcollectionStore.get%20%3D%3E%20collectionStore.rehydrate%3B%0AcollectionStore.rehydrate%20%3D%3E%20idsPersister.get%3B%0A%0AidsPersister.get%20%3E%3E%20collectionStore.rehydrate%3A%20ids%3B%0AcollectionStore.rehydrate%20%3D%3E%20thingPersister.get%3A%20ids%3B%0AthingPersister.get%20%3E%3E%20collectionStore.rehydrate%3A%20Decks%3B%0AcollectionStore.rehydrate%20%3E%3E%20collectionStore.get%3A%20Decks%3B%0AcollectionStore.get%20%3E%3E%20assembleMainMachine%3A%20Decks%3B%0A%0AassembleMainMachine%20%3D%3E%20ThingStore%3A%20Deck%3B%0AThingStore%20%3E%3E%20assembleMainMachine%3A%20deckStore%3B%0A%0AassembleMainMachine%20%3D%3E%20deckResponderMapper%3A%20%22collectionStore%2C%20deckStore%22%3B%0AdeckResponderMapper%20%3D%3E%20deckStore.get%3B%0AdeckStore.get%20%3D%3E%20rehydrateDeck%3B%0ArehydrateDeck%20%3D%3E%20thingPersister.get%3A%20(pile%20id)%3B%0AthingPersister.get%20%3E%3E%20rehydrateDeck%3A%20Pile%3B%0ArehydrateDeck%20%3E%3E%20deckStore.get%20%3A%20Deck%20(with%20Pile%20objects%20in%20piles)%3B%0AdeckStore.get%20%3E%3E%20deckResponderMapper%20%3A%20Deck%3B + +Add pile: diff --git a/persisters/piles-persister.ts b/persisters/piles-persister.ts index fd3e657..a215cb0 100644 --- a/persisters/piles-persister.ts +++ b/persisters/piles-persister.ts @@ -2,14 +2,16 @@ import type { Pile, Persister, ThingStoreType } from '../types'; export function PilesPersister(deckStore: ThingStoreType): Persister { return { - write(piles: Pile[]) { - deckStore.setPart({ piles }); + write(pileIds: string[]) { + // write gets called by CollectionStore (because piles is a collection) + // which works with raw ids, not objects. + deckStore.setRaw(Object.assign({}, deckStore.getRaw(), { piles: pileIds })); }, delete() { deckStore.setPart({ piles: [] }); }, get() { - return deckStore.get().piles; + return deckStore.getRaw().piles; }, }; } diff --git a/things/deck.ts b/things/deck.ts new file mode 100644 index 0000000..51c65a9 --- /dev/null +++ b/things/deck.ts @@ -0,0 +1,32 @@ +import { Deck, Persister } from "../types"; + +export function RehydrateDeck(thingPersister: Persister) { + return rehydrateDeck; + + // These functions should not mess with val. + function rehydrateDeck(val): Deck { + if (!val.piles) { + return; + } + var rehydrated = Object.assign({}, val, { piles: val.piles.map(getPileForId) }); + // What about rehydrating the properties of each pile? + return rehydrated; + } + + function getPileForId(id: string) { + return thingPersister.get(id); + } +} + +export function DehydrateDeck(thingPersister: Persister) { + return dehydrateDeck; + + function dehydrateDeck(val): Deck { + if (!val.piles) { + return val; + } + // TODO: Maybe deep copy? + var dehydrated = Object.assign({}, val, { piles: val.piles.map(pile => pile.id) }); + return dehydrated; + } +} \ No newline at end of file diff --git a/wily.js/stores/stores.ts b/wily.js/stores/stores.ts index d7b797b..46b56df 100644 --- a/wily.js/stores/stores.ts +++ b/wily.js/stores/stores.ts @@ -95,12 +95,8 @@ export function ThingStore( } export function CollectionStore( - idsPersister: Persister, - thingPersister: Persister, - kind: string, - parentThingId: string, - vals: Thing[] -): CollectionStoreType { +{ idsPersister, thingPersister, kind, parentThingId, vals, itemRehydrate }: +{ idsPersister: Persister; thingPersister: Persister; kind: string; parentThingId: string; vals: Thing[]; itemRehydrate?: (item: unknown) => unknown): CollectionStoreType { var base = Store(idsPersister, vals, dehydrate, rehydrate); return Object.assign(base, { add, remove, kind, parentThingId }); @@ -110,8 +106,11 @@ export function CollectionStore( } function rehydrate(ids) { - // TODO: Call base get/rehydrate - return ids.map(thingPersister.get); + var items = ids.map(thingPersister.get); + if (itemRehydrate) { + items = items.map(itemRehydrate); + } + return items; } function add(thing: Thing) {