Skip to content

Commit

Permalink
Refactoring (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
hobroker committed Feb 9, 2020
1 parent 85828f5 commit fe46130
Show file tree
Hide file tree
Showing 12 changed files with 993 additions and 116 deletions.
928 changes: 911 additions & 17 deletions package-lock.json

Large diffs are not rendered by default.

17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
"debug": "^4.1.1",
"esm": "^3.2.25",
"monet": "^0.9.1",
"ramda": "^0.26.1",
"ramda-adjunct": "^2.24.0"
"ramda": "^0.27.0",
"ramda-adjunct": "^2.25.0"
},
"devDependencies": {
"@babel/core": "^7.8.4",
"@babel/preset-env": "^7.8.4",
"babel-eslint": "^10.0.3",
"babel-jest": "^25.1.0",
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-config-prettier": "^6.10.0",
Expand Down Expand Up @@ -57,6 +60,14 @@
"modulePathIgnorePatterns": [
"/dist"
],
"testEnvironment": "node"
"testEnvironment": "node",
"transform": {
"\\.js$": [
"babel-jest",
{
"rootMode": "upward"
}
]
}
}
}
16 changes: 7 additions & 9 deletions src/constants/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const PKG_NAME = 'oxium';
export const PKG_NAME = 'oxium';

const ID = 'id';
const HANDLER = 'handler';
const IS_LOADED = 'isLoaded';
const FEATURES = 'features';
const META = '_';
export const ID = 'id';
export const HANDLER = 'handler';
export const IS_LOADED = 'isLoaded';
export const FEATURES = 'features';
export const META = '_';

export { PKG_NAME };

export { ID, FEATURES, META, HANDLER, IS_LOADED };
export const HANDLER_NOT_READY_RESULT = false;
27 changes: 8 additions & 19 deletions src/decorators/deferHandler.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
import { Left } from 'monet';
import {
always,
apply,
compose,
curry,
defaultTo,
equals,
identity,
ifElse,
then,
unless,
useWith,
} from 'ramda';
import { updateHandler } from '../lens/feature';
import { ensurePromise } from '../util/async';
import { ensureEitherOrRight } from '../util/either';

const mapValidResult = compose(then(ensureEitherOrRight), ensurePromise);
const mapInvalidResult = compose(Left, defaultTo(null));
import pipeAsync from '../util/pipeAsync';
import { HANDLER_NOT_READY_RESULT } from '../constants';

const fnTransformation = curry((validator, originalHandler) => (...args) => {
const applyValidator = compose(ensurePromise, apply(validator));
const callOriginalHandler = compose(apply(originalHandler), always(args));

return compose(
then(
ifElse(
identity,
compose(mapValidResult, apply(originalHandler), always(args)),
mapInvalidResult,
),
),
applyValidator,
return pipeAsync(
apply(validator),
unless(equals(HANDLER_NOT_READY_RESULT), callOriginalHandler),
)(args);
});

Expand Down
35 changes: 20 additions & 15 deletions src/oxium.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import { compose, curry, map, not, then, when } from 'ramda';
import { compose, converge, map, not, o, pipe, when } from 'ramda';
import { getFeatures } from './lens/app';
import { promiseAll } from './util/async';
import { promiseAllAndThen } from './util/promise';
import applyFeatureTo from './util/applyFeatureTo';
import replaceFeaturesIn from './util/replaceFeaturesIn';
import pipeAsync from './util/pipeAsync';

const createAppRunner = curry((filterFn, isDoneFn, app) => {
const runAgainOrReturn = when(
compose(not, isDoneFn),
createAppRunner(filterFn, isDoneFn),
);
const resolveFeaturesWith = converge(pipeAsync, [
o(map, applyFeatureTo),
o(promiseAllAndThen, replaceFeaturesIn),
]);

return compose(
then(compose(runAgainOrReturn, replaceFeaturesIn(app))),
promiseAll,
map(applyFeatureTo(app)),
filterFn,
getFeatures,
)(app);
});
const takeFeatures = fn => pipe(getFeatures, fn);

const createAppRunner = (filterFn, isDoneFn) => {
const runAgain = app => createAppRunner(filterFn, isDoneFn)(app);
const shouldRunAgain = compose(not, isDoneFn);

return app =>
pipeAsync(
takeFeatures(filterFn),
resolveFeaturesWith(app),
when(shouldRunAgain, runAgain),
)(app);
};

export default createAppRunner;
48 changes: 17 additions & 31 deletions src/util/applyFeatureTo.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
import {
always,
applyTo,
compose,
converge,
curry,
identity,
ifElse,
then,
} from 'ramda';
import { cata, isFunction } from 'ramda-adjunct';
import { curry } from 'ramda';
import { isFunction } from 'ramda-adjunct';
import { getHandler, setFeatureIsLoaded } from '../lens/feature';
import { ensureEitherOrRight } from './either';
import { ensurePromise } from './async';
import { ensurePromise } from './promise';
import pipeAsync from './pipeAsync';
import { HANDLER_NOT_READY_RESULT } from '../constants';

const mapRightResult = converge(compose, [
always(setFeatureIsLoaded(true)),
ifElse(isFunction, identity, always(identity)),
]);
const transformRightResult = setFeatureIsLoaded(true);

const mapLeftResult = always(identity);
const applyFeatureTo = curry(async (app, feature) => {
const handler = getHandler(feature);
const result = await ensurePromise(handler(app));

const resolveHandler = curry((app, handler) =>
compose(then(ensureEitherOrRight), ensurePromise, handler)(app),
);
if (result === HANDLER_NOT_READY_RESULT) {
return feature;
}

const callFeatureWith = curry((app, feature) =>
compose(resolveHandler(app), getHandler)(feature),
);
if (isFunction(result)) {
return pipeAsync(transformRightResult, result)(feature);
}

const applyFeatureTo = curry((app, feature) =>
compose(
then(applyTo(feature)),
then(cata(mapLeftResult, mapRightResult)),
callFeatureWith,
)(app, feature),
);
return transformRightResult(feature);
});

export default applyFeatureTo;
8 changes: 0 additions & 8 deletions src/util/async.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/util/debug.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import debug from 'debug';
import { apply, bind, compose, tap, unapply } from 'ramda';
import { apply, bind, pipe, tap, unapply } from 'ramda';
import { PKG_NAME } from '../constants';

const baseDebug = debug(PKG_NAME);
Expand All @@ -8,7 +8,7 @@ const debugIt = baseDebug;

const extend = bind(debugIt.extend, debugIt);

const createDebug = compose(unapply, apply, extend);
const createDebug = pipe(extend, apply, unapply);

const debugItFp = tap(createDebug('fp'));

Expand Down
11 changes: 0 additions & 11 deletions src/util/either.js

This file was deleted.

1 change: 0 additions & 1 deletion src/util/noop.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/util/pipeAsync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { andThen, call, pipeWith, unapply, useWith } from 'ramda';
import { ensurePromise } from './promise';

const pipeAsync = unapply(pipeWith(useWith(call, [andThen, ensurePromise])));

export default pipeAsync;
8 changes: 8 additions & 0 deletions src/util/promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { andThen, pipe, unless } from 'ramda';
import { allP, isPromise } from 'ramda-adjunct';

export const toPromise = value => Promise.resolve(value);

export const ensurePromise = unless(isPromise, toPromise);

export const promiseAllAndThen = fn => pipe(allP, andThen(fn));

0 comments on commit fe46130

Please sign in to comment.