Skip to content

Commit

Permalink
Use getRaw and setRaw in PilesPersister so that rehydrate and dehydra…
Browse files Browse the repository at this point in the history
…te don't kick in. This helps avoid situations in which things that are already dehydrated get dehydrated into nulls.
  • Loading branch information
jimkang committed Mar 30, 2021
1 parent 082aa44 commit 32fe9de
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 23 deletions.
23 changes: 12 additions & 11 deletions machines/main-machine.ts
Expand Up @@ -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';
Expand All @@ -34,19 +36,19 @@ 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)
}
)
);

var deckStores = deckCollectionStore
.get()
.map((thing) =>
registry.makeStoreHappen(thing.id, () =>
ThingStore(thingPersister, thing) // Rehydrate
ThingStore(thingPersister, thing, DehydrateDeck(thingPersister), RehydrateDeck(thingPersister))
)
);

Expand Down Expand Up @@ -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
}
)
);

Expand Down
4 changes: 3 additions & 1 deletion 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:
8 changes: 5 additions & 3 deletions persisters/piles-persister.ts
Expand Up @@ -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;
},
};
}
32 changes: 32 additions & 0 deletions 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;
}
}
15 changes: 7 additions & 8 deletions wily.js/stores/stores.ts
Expand Up @@ -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<Thing[]>(idsPersister, vals, dehydrate, rehydrate);

return Object.assign(base, { add, remove, kind, parentThingId });
Expand All @@ -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) {
Expand Down

0 comments on commit 32fe9de

Please sign in to comment.