Skip to content

Commit

Permalink
v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelklaessen committed Apr 1, 2018
1 parent 46f9bf8 commit 89c0061
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Version 1.0.1
Released 2018-04-01

- Use promise syntax rather than async/await

# Version 1.0.0
Released 2018-04-01

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-global-from-firebase",
"version": "1.0.0",
"version": "1.0.1",
"description": "React component that sets up a global state from Firebase refs",
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('updateCacheOnChange.js', () => {
let attachedListener;
const ref = {
ref: {
once: () => ({ val: () => 'newBar' })
once: () => new Promise(resolve => resolve({ val: () => 'newBar' }))
},
idRef: {
on: (type, listener) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ afterEach(() => {
describe('updateCache.js', () => {
it('updates the cached value and its cached ID with the value from given listener', async () => {
const ref = {
once: () => ({ val: () => 'newBar' })
once: () => new Promise(resolve => resolve({ val: () => 'newBar' }))
};
await updateCache('foo', ref, '321');
expect(localStorage.getItem('react-global-from-firebase:foo')).toBe(
Expand Down
5 changes: 2 additions & 3 deletions src/utils/setupFirebaseRefs/updateCacheOnChange/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import getCachedId from './getCachedId';
import updateCache from './updateCache';

const updateCacheOnChange = (key, ref, setStateAndGlobal) => {
ref.idRef.on('value', async (snapshot) => {
ref.idRef.on('value', (snapshot) => {
const id = snapshot.val();
const cachedId = getCachedId(key)
if (id !== cachedId) {
const value = await updateCache(key, ref.ref, id);
setStateAndGlobal(value);
return updateCache(key, ref.ref, id).then(setStateAndGlobal);
}
});
};
Expand Down
14 changes: 7 additions & 7 deletions src/utils/setupFirebaseRefs/updateCacheOnChange/updateCache.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getLocalKey, getLocalKeyId } from '../getLocalKeys';

const updateCache = async (key, ref, id) => {
const snapshot = await ref.once('value');
const value = snapshot.val();
localStorage.setItem(getLocalKey(key), value);
localStorage.setItem(getLocalKeyId(key), id);
return value;
};
const updateCache = (key, ref, id) =>
ref.once('value').then((snapshot) => {
const value = snapshot.val();
localStorage.setItem(getLocalKey(key), value);
localStorage.setItem(getLocalKeyId(key), id);
return value;
});

export default updateCache;

0 comments on commit 89c0061

Please sign in to comment.