Socket
Socket
Sign inDemoInstall

reduxed-chrome-storage

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reduxed-chrome-storage

Redux interface to chrome.storage, the only way to get Redux working in event-driven (non-persistent) Chrome extensions


Version published
Weekly downloads
236
increased by61.64%
Maintainers
1
Weekly downloads
 
Created
Source

Reduxed Chrome Storage

Redux interface to chrome.storage. The only way to get Redux working in event-driven (non-persistent) Chrome extensions (aside from full reproduction of the Redux code).

Related article

Installation

With NPM:

npm install reduxed-chrome-storage

Usage

Standard way (Promises):

// Note: the usage is the SAME for ANY extension component
// (background or content script or popup - no matter)

import { createStore } from 'redux';
import storeCreatorFactory from 'reduxed-chrome-storage';
import reducer from './reducer';

const storeCreator = storeCreatorFactory({ createStore });
storeCreator(reducer).then(store => {
  const state = store.getState();
  ...
});

Advanced way (async/await):

...
async () => {
  const storeCreator = storeCreatorFactory({ createStore });
  const store = await storeCreator(reducer);
  ...
}
...
One-liner:
...
async () => {
  const store = await storeCreatorFactory({ createStore }) (reducer);
  ...
}
...

Notes

storeCreator function returned by storeCreatorFactory is similar to the original Redux's createStore function except that the former runs in async way returning a promise instead of a new store (which is due to asynchronous nature of chrome.storage API). Although storeCreator has the same syntax as its Redux's counterpart, its 2nd parameter has a slightly different meaning. Unlike Redux, this library features state persistence through extension's activity periods (browser sessions in the case of persistent extension). With Reduxed Chrome Storage the current state is always persisted in chrome.storage by default. So there is no need to specify a previosly (somewhere) serialized state upon store creation/instantiation. However there may be a need to reset some parts (properties) of the state (e.g. user session variables) to their initial values upon store instantiation. And this is how the 2nd parameter is supposed to be used in Reduxed Chrome Storage: as initial values for some specific state properties. To be more specific, when a new store is created by storeCreator, first it tries to restore its last state from chrome.storage, then the result is merged with the 2nd parameter (if supplied).

If you declare your background script as non-persistent, you have to keep in mind that it must comply with requirements of event-based model. In the context of usage of this library it means that storeCreator's promise callback should not contain any extension event listener (e.g. chrome.runtime.onStartup etc). If async/await syntax is used, there should not be any event listener after first await occurrence. Furthermore, storeCreatorFactory should not be called inside any event listener (as it implicitly sets up chrome.storage.onChanged event listener).

License

Licensed under the MIT license.

Keywords

FAQs

Package last updated on 07 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc