cross-storeon

0.0.1 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i cross-storeon

Weekly Downloads

1

Version

0.0.1

License

MIT

Unpacked Size

6.33 kB

Total Files

5

Last publish

Collaborators

  • jeetiss