Skip to content

Commit

Permalink
Initial alpha release with main library, tests and build scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmalburn committed Feb 23, 2018
1 parent 8affa90 commit 28ed2f6
Show file tree
Hide file tree
Showing 10 changed files with 480 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"presets": [
["env", {
"useBuiltIns": true,
"modules":false,
"target": {
"browsers": ["chrome last 2 version"]
}
}]
],
"env": {
"test":{
"presets": [
["env", {
"targets": {
"node": "current"
}
}]
]
},
"commonjs":{
"plugins": ["transform-es2015-modules-commonjs"],
"presets": [
["env", {
"modules":"commonjs"
}]
]
},
"umd":{
"plugins": ["transform-es2015-modules-umd"],
"presets": [
["env", {
"modules":"umd"
}]
]
}
}
}
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true

[*]
end_of_line = lf

[*.js]
indent_size = 2
8 changes: 8 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"parser": "babel-eslint",
"extends": [
"airbnb-base",
"plugin:import/errors",
"plugin:import/warnings"
]
}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
es
lib
dist
node_modules
.vscode
npm-debug.log*
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Redux Persist Chrome Storage

Storage adaptor for using [Google Chrome's Storage API](https://developer.chrome.com/apps/storage) with [redux-persist](https://github.com/rt2zz/redux-persist).

The main use case for this adaptor is when writing Chrome extensions and making use of either the Sync, Local or Managed StorageArea drivers to persist your redux state.

# Installation

`npm install redux-persist-chrome-storage`

# Usage

## createChromeStorage Helper (Recommended)

```javascript

import { createStore } from 'redux'
import { persistStore, persistReducer } from 'redux-persist'
import createChromeStorage from '../root/to/redux-persist-chrome-storage/src'
import reducer from './reducers'

// Create a ChromeStorage instance using the chrome runtime and the Sync StorageArea.
const storage = createChromeStorage(window.chrome, 'sync');

const config = {
key: 'root',
storage,
};

const persistedReducer = persistReducer(config, reducer);

export default () => {
let store = createStore(persistedReducer)
let persistor = persistStore(store)
return { store, persistor };
}

```

## Direct Instantiation

```javascript
import { createStore } from 'redux'
import { persistStore, persistReducer } from 'redux-persist'
import ChromeStorage from '../root/to/redux-persist-chrome-storage/src/storage/ChromeStorage'
import reducer from './reducers'

// Create a ChromeStorage instance using the chrome runtime and the Sync StorageArea.
const storage = new ChromeStorage(window.chrome.storage.sync, window.chrome.runtime);

const config = {
key: 'root',
storage,
};

const persistedReducer = persistReducer(config, reducer);

export default () => {
let store = createStore(persistedReducer)
let persistor = persistStore(store)
return { store, persistor };
}
```
56 changes: 56 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "redux-persist-chrome-storage",
"version": "0.0.1",
"description": "redux-persist storage adaptor for Chrome extensions storage API.",
"repository": {
"type": "git",
"url": "https://github.com/robinmalburn/redux-persist-chrome-storage.git"
},
"main": "lib/index.js",
"module": "es/index.js",
"files": [
"src",
"es",
"lib",
"dist",
"README.md",
"LICENSE"
],
"scripts": {
"build": "npm run build:es && npm run build:cjs && npm run build:umd",
"build:es": "babel src --out-dir es",
"build:cjs": "cross-env BABEL_ENV=commonjs babel src --out-dir lib",
"build:umd": "cross-env BABEL_ENV=umd babel src --out-dir dist",
"clean": "rimraf es lib dist",
"test": "cross-env BABEL_ENV=test mocha test --require babel-register",
"lint": "eslint src"
},
"author": "Robin Malburn <[email protected]>",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"babel-plugin-transform-es2015-modules-umd": "^6.24.1",
"babel-preset-env": "^1.6.1",
"babel-register": "^6.26.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"cross-env": "^5.1.3",
"eslint": "^4.18.1",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.9.0",
"mocha": "^5.0.1",
"rimraf": "^2.6.2",
"sinon-chrome": "^2.2.4"
},
"peerDependencies": {
"redux-persist": ">=5.0"
},
"babel": {
"presets": [
"env"
]
}
}
19 changes: 19 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright (c) 2018 Robin Malburn
*
* Released under the MIT license.
* See the file LICENSE for copying permission.
*/

import ChromeStorage from './storage/ChromeStorage';

/**
* Create a ChromeStorage instance from the given chrome instance
* and requested driver.
*
* @param {chrome} chrome
* @param {string} driver
*/
export default function createChromeStorage(chrome, driver) {
return new ChromeStorage(chrome.storage[driver], chrome.runtime);
}
109 changes: 109 additions & 0 deletions src/storage/ChromeStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Copyright (c) 2018 Robin Malburn
*
* Released under the MIT license.
* See the file LICENSE for copying permission.
*/

const driverMap = new WeakMap();
const runtimeMap = new WeakMap();

export default class ChromeStorage {
/**
* Construct a new class instance.
*
* @param {chrome.StorageArea} driver - Chrome Storage Area driver.
* @param {chrome.runtime} runtime - Chrome runtime.
*
* @return void
*/
constructor(driver, runtime) {
driverMap.set(this, driver);
runtimeMap.set(this, runtime);
}

/**
* Return the storage driver.
*
* @return {chrome.StorageArea}
*/
getDriver() {
return driverMap.get(this);
}

/**
* Determine whether the runtime has an error.
*
* @return {boolean}
*/
hasError() {
return this.getError() !== undefined;
}

/**
* Get the last error message
*
* @return {string|undefined}
*/
getError() {
return runtimeMap.get(this).lastError;
}

/**
* Set an item in storage.
*
* @param {string} key
* @param {mixed} item
*
* @return {Promise}
*/
setItem(key, item) {
return new Promise((resolve, reject) => {
this.getDriver().set(
{ [key]: item },
() => {
if (this.hasError()) {
return reject(this.getError());
}
return resolve();
},
);
});
}

/**
* Get an item from storage.
*
* @param {string} key
*
* @return {Promise}
*/
getItem(key) {
return new Promise((resolve, reject) => {
this.getDriver().get(key, (response) => {
if (this.hasError()) {
return reject(this.getError());
}
return resolve(response[key]);
});
});
}

/**
* Remove an item from storage.
*
* @param {string} key
*
* @return {Promise}
*/
removeItem(key) {
return new Promise((resolve, reject) => {
this.getDriver().remove(key, () => {
if (this.hasError()) {
return reject(this.getError());
}
return resolve();
});
});
}
}
Loading

0 comments on commit 28ed2f6

Please sign in to comment.