Skip to content

Commit

Permalink
Merge pull request #3 from aprilmintacpineda/v5.0.10
Browse files Browse the repository at this point in the history
release v5.0.10
  • Loading branch information
aprilmintacpineda committed Dec 30, 2020
2 parents f047d15 + 94a9ec5 commit fa080b7
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 55 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [5.0.10] - 2017-06-20

See [diff](https://github.com/aprilmintacpineda/fluxible-js/compare/master...v5.0.10)

### Changed
- Calls to `restore` on `initializeStore` will now merge `initialStore` and `savedStore`; `savedStore` will always take precedence over `initializeStore`. This is to allow for better defaults when adding new values to `restore`.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Smaller, faster, better state management system that supports asynchronicity and

# Change logs

For the change logs, please see the [release notes](https://github.com/aprilmintacpineda/fluxible-js/releases)
See [CHANGELOG](./CHANGELOG.md) which follows [keepachangelog](https://keepachangelog.com/en/1.0.0/).

# Tests

Expand All @@ -22,9 +22,10 @@ For the change logs, please see the [release notes](https://github.com/aprilmint

All performance test was ran on:

MacBook Pro (Retina, 15-inch, Mid 2015).
Processor: 2.5 GHz Intel Core i7, 1 processor, 4 cores.
Memory: 16 GB 1600 MHz DDR3
MacBook Pro (15-inch, 2016)
Processor: 2.7 GHz Quad-Core Intel Core i7
Memory: 16 GB 2133 MHz LPDDR3
Graphics: Radeon Pro 455 2 GB, Intel HD Graphics 530 1536 MB

**Complete disclosure**

Expand Down
106 changes: 98 additions & 8 deletions __tests__/unit/persist.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,43 @@
import { updateStore, initializeStore, store, addObserver } from '../../src';

describe('persist using syncStorage', () => {
test('persist merges savedStore and initialStore', () => {
const initialStore = {
user: null,
testValue: 'value',
anotherValue: 'test value'
};

const syncStorage = {
getItem: jest.fn(() => {
return JSON.stringify({
user: {
name: 'test user'
}
});
}),
setItem: jest.fn(() => {})
};

const restore = jest.fn(({ user }) => ({ user }));

initializeStore({
initialStore,
persist: {
syncStorage,
restore
}
});

expect(restore).toHaveBeenCalledWith({
user: {
name: 'test user'
},
testValue: 'value',
anotherValue: 'test value'
});
});

test('calls getItem and setItem on config.persist.syncStorage', () => {
expect.assertions(4);

Expand Down Expand Up @@ -36,11 +73,7 @@ describe('persist using syncStorage', () => {
});

expect(syncStorage.getItem).toHaveBeenCalledWith('fluxible-js');
expect(restore).toHaveBeenCalledWith({
user: {
name: 'test user'
}
});
expect(restore).toHaveBeenCalled();
expect(store).toEqual({
user: {
name: 'test user'
Expand Down Expand Up @@ -340,6 +373,65 @@ describe('persist using syncStorage', () => {
});

describe('persist using asyncStorage', () => {
test('persist merges savedStore and initialStore', () => {
expect.assertions(2);

const initialStore = {
user: null,
testValue: 'value',
anotherValue: 'test value'
};

const asyncStorage = {
getItem: jest.fn(
() =>
new Promise(resolve => {
setTimeout(() => {
resolve(
JSON.stringify({
user: {
name: 'test user'
}
})
);
}, 200);
})
),
setItem: jest.fn(() => Promise.resolve())
};

const restore = jest.fn(savedStore => {
return {
user: savedStore.user
};
});

const asyncInitCallback = jest.fn();

initializeStore(
{
initialStore,
persist: {
asyncStorage,
restore
}
},
asyncInitCallback
);

return new Promise(resolve => setTimeout(resolve, 200)).then(() => {
expect(restore).toHaveBeenCalledWith({
user: {
name: 'test user'
},
testValue: 'value',
anotherValue: 'test value'
});

expect(asyncInitCallback).toHaveBeenCalled();
});
});

test('gets item from asyncStorage then calls restore', () => {
expect.assertions(4);

Expand Down Expand Up @@ -392,9 +484,7 @@ describe('persist using asyncStorage', () => {

addObserver(observer2, ['user']);

return new Promise(resolve => {
setTimeout(resolve, 200);
})
return new Promise(resolve => setTimeout(resolve, 200))
.then(() => {
expect(asyncInitCallback).toHaveBeenCalledTimes(1);
expect(asyncStorage.getItem).toHaveBeenCalled();
Expand Down
Binary file modified docs/test-perf-persist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/test-perf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/test-unit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 16 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ exports.emitEvent = emitEvent;
exports.emitEvents = emitEvents;
exports.store = void 0;

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/** @format */

/** @fluxible-no-synth-events */
Expand Down Expand Up @@ -68,8 +74,9 @@ asyncInitCallback
/** @end-fluxible-config-persist */

/** @fluxible-config-sync */
config.persist.asyncStorage.getItem('fluxible-js').then(function (savedStore) {
var persistedStates = config.persist.restore(savedStore ?
persistStorage = config.persist.asyncStorage;
persistStorage.getItem('fluxible-js').then(function (savedStore) {
var parsedSavedStore = savedStore ?
/** @fluxible-config-no-JSON */

/** @fluxible-config-use-JSON */
Expand All @@ -81,9 +88,9 @@ asyncInitCallback
/** @end-fluxible-config-no-JSON */
savedStore
/** @end-fluxible-config-use-JSON */
: store);
: store;
var persistedStates = config.persist.restore(_objectSpread(_objectSpread({}, store), parsedSavedStore));
persistedStateKeys = Object.keys(persistedStates);
persistStorage = config.persist.asyncStorage;
persistedStateKeys.forEach(function (field) {
store[field] = persistedStates[field];
});
Expand All @@ -96,8 +103,9 @@ asyncInitCallback
/** @end-fluxible-config-persist */

/** @fluxible-config-async */
var savedStore = config.persist.syncStorage.getItem('fluxible-js');
var persistedStates = config.persist.restore(savedStore ?
persistStorage = config.persist.syncStorage;
var savedStore = persistStorage.getItem('fluxible-js');
var parsedSavedStore = savedStore ?
/** @fluxible-config-no-JSON */

/** @fluxible-config-use-JSON */
Expand All @@ -109,9 +117,9 @@ asyncInitCallback
/** @end-fluxible-config-no-JSON */
savedStore
/** @end-fluxible-config-use-JSON */
: store);
: store;
var persistedStates = config.persist.restore(_objectSpread(_objectSpread({}, store), parsedSavedStore));
persistedStateKeys = Object.keys(persistedStates);
persistStorage = config.persist.syncStorage;
persistedStateKeys.forEach(function (field) {
store[field] = persistedStates[field];
});
Expand Down
2 changes: 1 addition & 1 deletion lib/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fluxible-js",
"version": "5.0.9",
"version": "5.0.10",
"description": "Smaller, faster, better state management system that supports asynchronicity and state persistence out of the box.",
"main": "lib/index.js",
"files": [
Expand All @@ -11,7 +11,6 @@
"README.md"
],
"scripts": {
"reinstallNodeModules": "rm -rf node_modules package-lock.json && npm i",
"test": "npm run test-unit && npm run test-perf",
"test-unit": "npm run build && jest __tests__/unit/* --env=node --coverage",
"test-perf": "npm run build && babel-node __tests__/perf.spec.js",
Expand Down
24 changes: 16 additions & 8 deletions playground/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ exports.emitEvent = emitEvent;
exports.emitEvents = emitEvents;
exports.store = void 0;

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/** @format */

/** @fluxible-no-synth-events */
Expand Down Expand Up @@ -68,8 +74,9 @@ asyncInitCallback
/** @end-fluxible-config-persist */

/** @fluxible-config-sync */
config.persist.asyncStorage.getItem('fluxible-js').then(function (savedStore) {
var persistedStates = config.persist.restore(savedStore ?
persistStorage = config.persist.asyncStorage;
persistStorage.getItem('fluxible-js').then(function (savedStore) {
var parsedSavedStore = savedStore ?
/** @fluxible-config-no-JSON */

/** @fluxible-config-use-JSON */
Expand All @@ -81,9 +88,9 @@ asyncInitCallback
/** @end-fluxible-config-no-JSON */
savedStore
/** @end-fluxible-config-use-JSON */
: store);
: store;
var persistedStates = config.persist.restore(_objectSpread(_objectSpread({}, store), parsedSavedStore));
persistedStateKeys = Object.keys(persistedStates);
persistStorage = config.persist.asyncStorage;
persistedStateKeys.forEach(function (field) {
store[field] = persistedStates[field];
});
Expand All @@ -96,8 +103,9 @@ asyncInitCallback
/** @end-fluxible-config-persist */

/** @fluxible-config-async */
var savedStore = config.persist.syncStorage.getItem('fluxible-js');
var persistedStates = config.persist.restore(savedStore ?
persistStorage = config.persist.syncStorage;
var savedStore = persistStorage.getItem('fluxible-js');
var parsedSavedStore = savedStore ?
/** @fluxible-config-no-JSON */

/** @fluxible-config-use-JSON */
Expand All @@ -109,9 +117,9 @@ asyncInitCallback
/** @end-fluxible-config-no-JSON */
savedStore
/** @end-fluxible-config-use-JSON */
: store);
: store;
var persistedStates = config.persist.restore(_objectSpread(_objectSpread({}, store), parsedSavedStore));
persistedStateKeys = Object.keys(persistedStates);
persistStorage = config.persist.syncStorage;
persistedStateKeys.forEach(function (field) {
store[field] = persistedStates[field];
});
Expand Down
Loading

0 comments on commit fa080b7

Please sign in to comment.