Skip to content

Commit

Permalink
refactor folder structure to reflect components better
Browse files Browse the repository at this point in the history
  • Loading branch information
petercrona committed Apr 29, 2017
1 parent 31adf4f commit 77fca7a
Show file tree
Hide file tree
Showing 28 changed files with 55 additions and 54 deletions.
6 changes: 3 additions & 3 deletions src/builder.js
Expand Up @@ -2,8 +2,8 @@ import {mapObject, mapValues, compose, toObject, reduce, toPairs,
prop, filterObject, isEqual, not, curry, copyFunction
} from 'ladda-fp';

import {decorator} from './decorator';
import {dedup} from './dedup';
import {cachePlugin} from './plugins/cache';
import {dedupPlugin} from './plugins/dedup';
import {createListenerStore} from './listener-store';
import {validateConfig} from './validator';

Expand Down Expand Up @@ -136,5 +136,5 @@ export const build = (c, ps = []) => {
const applyPlugin_ = applyPlugin(listenerStore.addChangeListener, config);
const applyPlugins = reduce(applyPlugin_, entityConfigs);
const createApi = compose(toApi, applyPlugins);
return createApi([decorator(listenerStore.onChange), ...ps, dedup]);
return createApi([cachePlugin(listenerStore.onChange), ...ps, dedupPlugin]);
};
1 change: 1 addition & 0 deletions src/plugins/cache/.tern-port
@@ -0,0 +1 @@
44785
6 changes: 3 additions & 3 deletions src/decorator/create.js → src/plugins/cache/create.js
@@ -1,7 +1,7 @@
import {passThrough, compose} from 'ladda-fp';
import {put} from '../entity-store';
import {invalidate} from '../query-cache';
import {addId} from '../id-helper';
import {put} from './entity-store';
import {invalidate} from './query-cache';
import {addId} from './id-helper';

export function decorateCreate(c, es, qc, e, aFn) {
return (...args) => {
Expand Down
@@ -1,8 +1,8 @@
import sinon from 'sinon';
import {decorateCreate} from './create';
import {createEntityStore, get} from '../entity-store';
import {createQueryCache} from '../query-cache';
import {createApiFunction} from '../test-helper';
import {createEntityStore, get} from './entity-store';
import {createQueryCache} from './query-cache';
import {createApiFunction} from './test-helper';

const config = [
{
Expand Down
6 changes: 3 additions & 3 deletions src/decorator/delete.js → src/plugins/cache/delete.js
@@ -1,7 +1,7 @@
import {passThrough} from 'ladda-fp';
import {remove} from '../entity-store';
import {invalidate} from '../query-cache';
import {serialize} from '../serializer';
import {remove} from './entity-store';
import {invalidate} from './query-cache';
import {serialize} from './serializer';

export function decorateDelete(c, es, qc, e, aFn) {
return (...args) => {
Expand Down
@@ -1,9 +1,9 @@
import sinon from 'sinon';
import {decorateDelete} from './delete';
import {createEntityStore, get, put} from '../entity-store';
import {createQueryCache} from '../query-cache';
import {addId} from '../id-helper';
import {createApiFunction} from '../test-helper';
import {createEntityStore, get, put} from './entity-store';
import {createQueryCache} from './query-cache';
import {addId} from './id-helper';
import {createApiFunction} from './test-helper';

const config = [
{
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 3 additions & 4 deletions src/decorator/index.js → src/plugins/cache/index.js
@@ -1,6 +1,6 @@
import {compose, values} from 'ladda-fp';
import {createEntityStore} from '../entity-store';
import {createQueryCache} from '../query-cache';
import {createEntityStore} from './entity-store';
import {createQueryCache} from './query-cache';
import {decorateCreate} from './create';
import {decorateRead} from './read';
import {decorateUpdate} from './update';
Expand All @@ -15,12 +15,11 @@ const HANDLERS = {
NO_OPERATION: decorateNoOperation
};

export const decorator = (onChange) => ({ config, entityConfigs }) => {
export const cachePlugin = (onChange) => ({ config, entityConfigs }) => {
const entityStore = compose((c) => createEntityStore(c, onChange), values)(entityConfigs);
const queryCache = createQueryCache(entityStore, onChange);
return ({ entity, fn }) => {
const handler = HANDLERS[fn.operation];
return handler(config, entityStore, queryCache, entity, fn);
};
};

@@ -1,10 +1,10 @@
/* eslint-disable no-unused-expressions */

import {decorate} from './index';
import {createEntityStore} from '../entity-store';
import {createQueryCache, put, contains} from '../query-cache';
import {addId} from '../id-helper';
import {createApiFunction} from '../test-helper';
import {createEntityStore} from './entity-store';
import {createQueryCache, put, contains} from './query-cache';
import {addId} from './id-helper';
import {createApiFunction} from './test-helper';

const config = [
{
Expand Down
File renamed without changes.
File renamed without changes.
@@ -1,5 +1,5 @@
import {passThrough} from 'ladda-fp';
import {invalidate} from '../query-cache';
import {invalidate} from './query-cache';

export function decorateNoOperation(c, es, qc, e, aFn) {
return (...args) => {
Expand Down
Expand Up @@ -2,9 +2,9 @@

import sinon from 'sinon';
import {decorateNoOperation} from './no-operation';
import {createEntityStore} from '../entity-store';
import {createQueryCache, contains, put} from '../query-cache';
import {createSampleConfig, createApiFunction} from '../test-helper';
import {createEntityStore} from './entity-store';
import {createQueryCache, contains, put} from './query-cache';
import {createSampleConfig, createApiFunction} from './test-helper';

const config = createSampleConfig();

Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/decorator/read.js → src/plugins/cache/read.js
Expand Up @@ -2,13 +2,13 @@ import {passThrough, compose, curry, reduce, toIdMap, map, concat, zip} from 'la
import {get as getFromEs,
put as putInEs,
mPut as mPutInEs,
contains as inEs} from '../entity-store';
contains as inEs} from './entity-store';
import {get as getFromQc,
invalidate,
put as putInQc,
contains as inQc,
getValue} from '../query-cache';
import {addId, removeId} from '../id-helper';
getValue} from './query-cache';
import {addId, removeId} from './id-helper';

const getTtl = e => e.ttl * 1000;

Expand Down
6 changes: 3 additions & 3 deletions src/decorator/read.spec.js → src/plugins/cache/read.spec.js
Expand Up @@ -3,9 +3,9 @@
import sinon from 'sinon';
import {map} from 'ladda-fp';
import {decorateRead} from './read';
import {createEntityStore} from '../entity-store';
import {createQueryCache} from '../query-cache';
import {createSampleConfig, createApiFunction} from '../test-helper';
import {createEntityStore} from './entity-store';
import {createQueryCache} from './query-cache';
import {createSampleConfig, createApiFunction} from './test-helper';

const config = createSampleConfig();

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/decorator/update.js → src/plugins/cache/update.js
@@ -1,7 +1,7 @@
import {passThrough} from 'ladda-fp';
import {put} from '../entity-store';
import {invalidate} from '../query-cache';
import {addId} from '../id-helper';
import {put} from './entity-store';
import {invalidate} from './query-cache';
import {addId} from './id-helper';

export function decorateUpdate(c, es, qc, e, aFn) {
return (eValue, ...args) => {
Expand Down
@@ -1,8 +1,8 @@
import sinon from 'sinon';
import {decorateUpdate} from './update';
import {createEntityStore, get} from '../entity-store';
import {createQueryCache} from '../query-cache';
import {createApiFunction} from '../test-helper';
import {createEntityStore, get} from './entity-store';
import {createQueryCache} from './query-cache';
import {createApiFunction} from './test-helper';

const config = [
{
Expand Down
1 change: 1 addition & 0 deletions src/plugins/dedup/.tern-port
@@ -0,0 +1 @@
39381
2 changes: 1 addition & 1 deletion src/dedup/index.js → src/plugins/dedup/index.js
Expand Up @@ -8,7 +8,7 @@ const isActive = reduce(
true
);

export const dedup = ({ config }) => ({ entity, fn }) => {
export const dedupPlugin = ({ config }) => ({ entity, fn }) => {
if (fn.operation !== 'READ') { return fn; }
const cache = {};

Expand Down
26 changes: 13 additions & 13 deletions src/dedup/index.spec.js → src/plugins/dedup/index.spec.js
@@ -1,7 +1,7 @@
/* eslint-disable no-unused-expressions */

import sinon from 'sinon';
import { dedup } from '.';
import { dedupPlugin } from '.';

const delay = (cb, t = 5) => new Promise((res, rej) => {
setTimeout(() => cb().then(res, rej), t);
Expand All @@ -13,7 +13,7 @@ describe('dedup', () => {
const user = { id: 'x' };
const fn = sinon.spy(() => Promise.resolve({ ...user }));
fn.operation = 'UPDATE';
const wrappedApiFn = dedup({})({ fn });
const wrappedApiFn = dedupPlugin({})({ fn });
expect(wrappedApiFn).to.equal(fn);
});
});
Expand All @@ -23,15 +23,15 @@ describe('dedup', () => {
const user = { id: 'x' };
const fn = sinon.spy(() => Promise.resolve(user));
fn.operation = 'READ';
const wrappedApiFn = dedup({})({ fn });
const wrappedApiFn = dedupPlugin({})({ fn });
expect(wrappedApiFn).not.to.equal(fn);
});

it('makes several calls when apiFn is called with different args', () => {
const user = { id: 'x' };
const fn = sinon.spy(() => Promise.resolve({ ...user }));
fn.operation = 'READ';
const wrappedApiFn = dedup({})({ fn });
const wrappedApiFn = dedupPlugin({})({ fn });
return Promise.all([
wrappedApiFn('x'),
wrappedApiFn('y')
Expand All @@ -44,7 +44,7 @@ describe('dedup', () => {
const user = { id: 'x' };
const fn = sinon.spy(() => delay(() => Promise.resolve({ ...user })));
fn.operation = 'READ';
const wrappedApiFn = dedup({})({ fn });
const wrappedApiFn = dedupPlugin({})({ fn });
return Promise.all([
wrappedApiFn('x'),
wrappedApiFn('x')
Expand All @@ -57,7 +57,7 @@ describe('dedup', () => {
const user = { id: 'x' };
const fn = sinon.spy(() => delay(() => Promise.resolve({ ...user })));
fn.operation = 'READ';
const wrappedApiFn = dedup({})({ fn });
const wrappedApiFn = dedupPlugin({})({ fn });
return Promise.all([
wrappedApiFn({ a: 1, b: [2, 3] }, 'a'),
wrappedApiFn({ a: 1, b: [2, 3] }, 'a')
Expand All @@ -70,7 +70,7 @@ describe('dedup', () => {
const user = { id: 'x' };
const fn = sinon.spy(() => delay(() => Promise.resolve({ ...user })));
fn.operation = 'READ';
const wrappedApiFn = dedup({})({ fn });
const wrappedApiFn = dedupPlugin({})({ fn });
return Promise.all([
wrappedApiFn(),
wrappedApiFn()
Expand All @@ -85,7 +85,7 @@ describe('dedup', () => {
const user = { id: 'x' };
const fn = sinon.spy(() => delay(() => Promise.resolve({ ...user })));
fn.operation = 'READ';
const wrappedApiFn = dedup({})({ fn });
const wrappedApiFn = dedupPlugin({})({ fn });

return wrappedApiFn().then(() => {
return wrappedApiFn().then(() => {
Expand All @@ -98,7 +98,7 @@ describe('dedup', () => {
const user = { id: 'x' };
const fn = sinon.spy(() => delay(() => Promise.reject({ ...user })));
fn.operation = 'READ';
const wrappedApiFn = dedup({})({ fn });
const wrappedApiFn = dedupPlugin({})({ fn });

return wrappedApiFn().catch(() => {
return wrappedApiFn().catch(() => {
Expand All @@ -110,7 +110,7 @@ describe('dedup', () => {
it('propagates errors to all callees', () => {
const error = { error: 'ERROR' };
const fn = sinon.spy(() => delay(() => Promise.reject(error)));
const wrappedApiFn = dedup({})({ fn });
const wrappedApiFn = dedupPlugin({})({ fn });
return Promise.all([
wrappedApiFn().catch((err) => err),
wrappedApiFn().catch((err) => err)
Expand All @@ -125,7 +125,7 @@ describe('dedup', () => {
const fn = sinon.spy(() => delay(() => Promise.resolve({ ...user })));
fn.operation = 'READ';
const config = { enableDeduplication: false };
const wrappedApiFn = dedup({ config })({ fn });
const wrappedApiFn = dedupPlugin({ config })({ fn });

return Promise.all([
wrappedApiFn(),
Expand All @@ -140,7 +140,7 @@ describe('dedup', () => {
const fn = sinon.spy(() => delay(() => Promise.resolve({ ...user })));
fn.operation = 'READ';
const entity = { enableDeduplication: false };
const wrappedApiFn = dedup({})({ fn, entity });
const wrappedApiFn = dedupPlugin({})({ fn, entity });

return Promise.all([
wrappedApiFn(),
Expand All @@ -155,7 +155,7 @@ describe('dedup', () => {
const fn = sinon.spy(() => delay(() => Promise.resolve({ ...user })));
fn.operation = 'READ';
fn.enableDeduplication = false;
const wrappedApiFn = dedup({})({ fn });
const wrappedApiFn = dedupPlugin({})({ fn });

return Promise.all([
wrappedApiFn(),
Expand Down

0 comments on commit 77fca7a

Please sign in to comment.