Skip to content

PersistHydrate

James edited this page Jun 25, 2017 · 1 revision

LocalStorage

localstorage mock
const store = {}
const storage = {
  getItem: key => store[key],
  setItem: (key, value) => (store[key] = value),
}
const ls = {
  get: key => JSON.parse(storage.getItem(key)),
  set: (key, value) => storage.setItem(key, JSON.stringify(value)),
}
const {Chain, eq} = require('chain-able')

class Canada extends Chain {
  constructor(parent) {
    super(parent)
    this.extend(['eh'])
  }
}

const canada = new Canada()
  .eh('eh!')
  .merge({canada: true})
  .tap('canada', value => '🇨🇦')
  .setIfEmpty('ooo', 'ahh')

ls.set('canada', canada.entries())
const hydrated = new Canada().from(ls.get('canada'))

const hydratedIsTheSame = eq(canada.entries(), hydrated.entries())
console.log(hydratedIsTheSame)
Clone this wiki locally