Skip to content

jeetiss/cross-storeon

Repository files navigation

Cross Storeon

Storeon logo by Anton Lovchikov

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published