Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use localForage #16

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
lib
npm-debug.log*
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ $ npm install --save relay-cache-manager

## Usage

[Until `RelayEnvironment` exposes `injectCacheManager`](https://github.com/facebook/relay/pull/1320) you have to inject it directly from the `RelayStoreData` instance used by your store. You can access that via the `getStoreData()` method on your `RelayEnvironment` instance. If you're using `Relay.Store` you can just do:
Use the `injectCacheManager` method on your `RelayEnvironment` instance to inject the cache manager. If you're using `Relay.Store` you can just do:

```js
import CacheManager from 'relay-cache-manager';
const cacheManager = new CacheManager();
Relay.Store.getStoreData().injectCacheManager(cacheManager);
Relay.Store.injectCacheManager(cacheManager);
```
28 changes: 26 additions & 2 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,35 @@

A fork of [relay-starter-kit](https://github.com/relayjs/relay-starter-kit) that implements `relay-cache-manager`.

## Try it out
This kit includes an app server, a GraphQL server, and a transpiler that you can use to get started building an app with Relay. For a walkthrough, see the [Relay tutorial](https://facebook.github.io/relay/docs/tutorial.html).

Make sure to run `npm install` in the root folder as well so `relay-cache-manager` builds correctly.
## Installation

```
npm install
```

## Running

Start a local server:

```
npm start
```

## Developing

Any changes you make to files in the `js/` directory will cause the server to
automatically rebuild the app and refresh your browser.

If at any time you make changes to `data/schema.js`, stop the server,
regenerate `data/schema.json`, and restart the server:

```
npm run update-schema
npm start
```

## License

Relay Starter Kit is [BSD licensed](./LICENSE). We also provide an additional [patent grant](./PATENTS).
4 changes: 2 additions & 2 deletions example/data/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@
{
"kind": "OBJECT",
"name": "__Directive",
"description": "A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.\n\nIn some cases, you need to provide options to alter GraphQLs execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor.",
"description": "A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.\n\nIn some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor.",
"fields": [
{
"name": "name",
Expand Down Expand Up @@ -1315,7 +1315,7 @@
"args": [
{
"name": "reason",
"description": "Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formattedin [Markdown](https://daringfireball.net/projects/markdown/).",
"description": "Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted in [Markdown](https://daringfireball.net/projects/markdown/).",
"type": {
"kind": "SCALAR",
"name": "String",
Expand Down
4 changes: 2 additions & 2 deletions example/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import AppHomeRoute from './routes/AppHomeRoute';
import React from 'react';
import ReactDOM from 'react-dom';
import Relay from 'react-relay';

import CacheManager from 'relay-cache-manager';

const cacheManager = new CacheManager();

Relay.Store.getStoreData().injectCacheManager(cacheManager);

Relay.Store.injectCacheManager(cacheManager)

ReactDOM.render(
<Relay.Renderer
Expand Down
39 changes: 20 additions & 19 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "relay-cache-manager-api",
"name": "relay-cache-manager-example",
"private": true,
"description": "An example for relay-cache-manager",
"repository": "ConciergeAuctions/relay-cache-manager",
"version": "0.1.0",
Expand All @@ -8,28 +9,28 @@
"update-schema": "babel-node ./scripts/updateSchema.js"
},
"dependencies": {
"babel-core": "6.9.0",
"babel-loader": "6.2.4",
"babel-polyfill": "6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "6.5.0",
"babel-relay-plugin": "0.9.1",
"chokidar": "1.5.1",
"babel-core": "^6.14.0",
"babel-loader": "^6.2.5",
"babel-polyfill": "^6.13.0",
"babel-preset-es2015": "^6.14.0",
"babel-preset-react": "^6.11.1",
"babel-preset-stage-0": "^6.5.0",
"babel-relay-plugin": "0.9.3",
"chokidar": "1.6.0",
"classnames": "2.2.5",
"express": "4.13.4",
"express-graphql": "0.5.3",
"graphql": "0.6.0",
"graphql-relay": "0.4.2",
"react": "15.1.0",
"react-dom": "15.1.0",
"react-relay": "0.9.1",
"express": "4.14.0",
"express-graphql": "0.5.4",
"graphql": "0.7.0",
"graphql-relay": "0.4.3",
"react": "^15.3.1",
"react-dom": "^15.3.1",
"react-relay": "0.9.3",
"relay-cache-manager": "file:../",
"require-clean": "0.1.3",
"webpack": "1.13.1",
"webpack-dev-server": "1.14.1"
"webpack": "1.13.2",
"webpack-dev-server": "1.15.1"
},
"devDependencies": {
"babel-cli": "6.9.0"
"babel-cli": "^6.14.0"
}
}
30 changes: 0 additions & 30 deletions npm-debug.log

This file was deleted.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@
"babel-plugin-transform-class-properties": "^6.11.5",
"babel-plugin-transform-flow-strip-types": "^6.8.0",
"babel-preset-es2015": "^6.9.0"
},
"dependencies": {
"localforage": "^1.4.3"
}
}
10 changes: 5 additions & 5 deletions src/CacheWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import CacheRecordStore from './CacheRecordStore';
import type { CacheRecord } from './CacheRecordStore';

import localForage from 'localforage';

const DEFAULT_CACHE_KEY: string = '__RelayCacheManager__';

type CacheWriterOptions = {
Expand All @@ -20,9 +22,8 @@ export default class CacheWriter {
constructor(options: CacheWriterOptions = {}) {
this.cacheKey = options.cacheKey || DEFAULT_CACHE_KEY
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to call localforage.config before using it, so lets let users pass in their own config for this, and default to an empty object.

localForage.config(options.localForage || {});

try {
let localCache = localStorage.getItem(this.cacheKey);
let localCache = localForage.getItem(this.cacheKey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

localForage is async, so it's not a drop in replacement for localStorage. Lets go ahead and use their Promise API.

localForage.getItem(this.cacheKey)
  .then(localCache => {
   if (!localCache) {
    this.cache = new CacheRecordStore();
  } else {
  this.cache = CacheRecordStore.fromJSON(localCache)
   }
  })
  .catch(err => this.cache = new CacheRecordStore())

if (localCache) {
localCache = JSON.parse(localCache);
this.cache = CacheRecordStore.fromJSON(localCache);
} else {
this.cache = new CacheRecordStore();
Expand All @@ -33,7 +34,7 @@ export default class CacheWriter {
}

clearStorage() {
localStorage.removeItem(this.cacheKey);
localForage.removeItem(this.cacheKey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, lets use the Promise API to reset this.cache after this key has been removed.

this.cache = new CacheRecordStore();
}

Expand All @@ -53,8 +54,7 @@ export default class CacheWriter {
record[field] = value;
this.cache.records[dataId] = record;
try {
const serialized = JSON.stringify(this.cache);
localStorage.setItem(this.cacheKey, serialized);
localForage.setItem(this.cacheKey, this.cache);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move this out of the try / catch since its async

} catch (err) {
/* noop */
}
Expand Down