Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

persistant layer for graphcache #137

Merged
merged 5 commits into from
Dec 14, 2019
Merged

persistant layer for graphcache #137

merged 5 commits into from
Dec 14, 2019

Conversation

JoviDeCroock
Copy link

@JoviDeCroock JoviDeCroock commented Dec 14, 2019

Due to changing a lot of internals I found that making this implementation again gave profit, it's smaller 🎉

This PR adds the ability to supply two async functions to the cache, these will function as an adapter for the persistence layer of your choice.

cache({ storage: { read: idb.read, set: idb.set })

--> will for instance write to an indexedDb and read from it, first time you start the cache it will buffer every incoming operation until the store has been hydrated by the data from your persistence layer.

During gc we perform set(key, undefined) on garbage collected fields/links to avoid our offline storage from heaping up.

TODO: evaluate if this is sufficient

This is a first-step to a fully offline-compatible cache

src/store/__snapshots__/store.test.ts.snap Show resolved Hide resolved
src/cacheExchange.ts Outdated Show resolved Hide resolved
src/store/data.ts Outdated Show resolved Hide resolved
src/store/data.ts Outdated Show resolved Hide resolved
src/store/data.ts Outdated Show resolved Hide resolved
src/store/data.ts Outdated Show resolved Hide resolved
src/store/store.ts Outdated Show resolved Hide resolved
src/store/data.ts Outdated Show resolved Hide resolved
src/store/data.ts Outdated Show resolved Hide resolved
Copy link
Member

@kitten kitten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳🥳🥳

let hydration: void | Promise<void>;
if (opts.storage) {
hydration = opts.storage.read().then(data => {
store.hydrateData(data, (opts as any).storage);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What’s going on here with any?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typescript being weird.... It's saying .storage is possibly undefined....

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be because storage isn’t reassigned. Maybe we can try opts.storage!?

Copy link
Author

@JoviDeCroock JoviDeCroock Dec 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works.... opts!.storage!

EDIT: moving to a var works better

@kitten kitten merged commit 3b51618 into master Dec 14, 2019
@kitten kitten deleted the persistance-v2 branch December 14, 2019 13:10
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants