0.0.1 • Published 5 years ago

cross-storeon v0.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Cross Storeon

Module for Storeon to synchronize actions through different ways with filtering of events that need to be synchronized.

It size is 131 bytes (minified and gzipped) and uses Size Limit to control size.

Installation

npm install cross-storeon
# or
yarn add cross-storeon

API

import crossstore from 'cross-storeon'

const crossTab = function ({ key, filter }) {
  var ignoreDate = 0
  var counter = 0

  return crossstore({
    key,
    filter,
    send: function (event) {
      ignoreDate = Date.now() + '' + counter++
      localStorage[key] = JSON.stringify([event, ignoreDate])
    },

    subscribe: function (cb) {
      window.addEventListener('storage', function (event) {
        if (event.key === key) {
          var tip = JSON.parse(event.newValue)

          if (ignoreDate !== tip[1]) {
            cb(tip[0])
          }
        }
      })
    }
  })
}

Function crossstore could have options:

  • key: key.
  • filter: callback function to filter actions to be synchronized. Should return true if need sync this action. Takes parameters of an event name and a data that is sent.
  • send: callback to send events to different stores
  • subscribe: callback to subscribe on events from different stores

LICENSE

MIT