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

Redux Middleware #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions packages/ae-redux/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @algebraic-effects/effects
A collection of effects to use while writing your program with algebraic effects in javascript

59 changes: 59 additions & 0 deletions packages/ae-redux/build/Store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;

var _core = require("@algebraic-effects/core");

var _utils = require("@algebraic-effects/utils");

var _utils2 = require("./utils");

var Store = (0, _core.createEffect)('Store', {
dispatch: (0, _core.func)(['action']),
getState: (0, _core.func)([], 'state'),
selectState: (0, _core.func)(['?state -> a'], 'a'),
getAction: (0, _core.func)([], 'action'),
waitFor: (0, _core.func)(['actionType | Action -> Boolean'])
});

Store.of = function (_ref) {
var _ref$store = _ref.store,
_dispatch = _ref$store.dispatch,
_getState = _ref$store.getState,
action = _ref.action;
return Store.handler({
dispatch: function dispatch(_ref2) {
var resume = _ref2.resume;
return (0, _utils.compose)(resume, _dispatch, _utils2.decorateAction);
},
getState: function getState(_ref3) {
var resume = _ref3.resume;
return (0, _utils.compose)(resume, _getState);
},
selectState: function selectState(_ref4) {
var resume = _ref4.resume;
return function (fn) {
return (0, _utils.compose)(resume, fn || _utils.identity, _getState)();
};
},
getAction: function getAction(_ref5) {
var resume = _ref5.resume;
return function () {
return resume(action);
};
},
waitFor: function waitFor(_ref6) {
var resume = _ref6.resume,
end = _ref6.end;
return function (filter) {
return (0, _utils2.filterAction)(filter, action) ? resume(action) : end(action);
};
}
});
};

var _default = Store;
exports.default = _default;
35 changes: 35 additions & 0 deletions packages/ae-redux/build/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "Store", {
enumerable: true,
get: function get() {
return _Store.default;
}
});
exports.createEffectsMiddleware = void 0;

var _Store = _interopRequireDefault(require("./Store"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var createEffectsMiddleware = function createEffectsMiddleware(program, handler) {
var middleware = function middleware(store) {
return function (next) {
return function (action) {
_Store.default.of({
store: store,
action: action
}).with(handler).run(program).fork(console.error, console.log);

return next(action);
};
};
};

return middleware;
};

exports.createEffectsMiddleware = createEffectsMiddleware;
34 changes: 34 additions & 0 deletions packages/ae-redux/build/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.filterAction = exports.isEffectfulAction = exports.decorateAction = exports.AE_REDUX_ACTION = void 0;

var _utils = require("@algebraic-effects/utils");

var AE_REDUX_ACTION = (0, _utils.createSymbol)('algebraic-effects/redux-action');
exports.AE_REDUX_ACTION = AE_REDUX_ACTION;

var decorateAction = function decorateAction(action) {
action && (action.$$type = AE_REDUX_ACTION);
return action;
};

exports.decorateAction = decorateAction;

var isEffectfulAction = function isEffectfulAction(action) {
return action ? action.$$type === AE_REDUX_ACTION : false;
};

exports.isEffectfulAction = isEffectfulAction;

var filterAction = function filterAction(filter, action) {
if (!action) return false;
if (isEffectfulAction(action)) return false;
if (typeof filter === 'string') return filter === action.type;
if (typeof filter === 'function') return filter(action);
return false;
};

exports.filterAction = filterAction;
1 change: 1 addition & 0 deletions packages/ae-redux/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./build/index');
26 changes: 26 additions & 0 deletions packages/ae-redux/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@algebraic-effects/redux",
"version": "0.0.2",
"description": "Redux middleware to allow using algebraic effects to write your actions",
"main": "index.js",
"isBuildTarget": true,
"repository": "https://github.com/phenax/algebraic-effects",
"author": "Akshay Nair <[email protected]>",
"license": "MIT",
"dependencies": {
"@algebraic-effects/core": "0.0.2"
},
"keywords": [
"functional",
"algebraic",
"effects",
"generators",
"fp",
"redux",
"saga",
"middleware"
],
"peerDependencies": {
"redux": "^4.0.1"
}
}
21 changes: 21 additions & 0 deletions packages/ae-redux/src/Store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createEffect, func } from '@algebraic-effects/core';
import { compose, identity } from '@algebraic-effects/utils';
import { decorateAction, filterAction } from './utils';

const Store = createEffect('Store', {
dispatch: func(['action']),
getState: func([], 'state'),
selectState: func(['?state -> a'], 'a'),
getAction: func([], 'action'),
waitFor: func(['actionType | Action -> Boolean']),
});

Store.of = ({ store: { dispatch, getState }, action }) => Store.handler({
dispatch: ({ resume }) => compose(resume, dispatch, decorateAction),
getState: ({ resume }) => compose(resume, getState),
selectState: ({ resume }) => fn => compose(resume, fn || identity, getState)(),
getAction: ({ resume }) => () => resume(action),
waitFor: ({ resume, end }) => filter => filterAction(filter, action) ? resume(action) : end(action),
});

export default Store;
15 changes: 15 additions & 0 deletions packages/ae-redux/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Store from './Store';

export { Store };

export const createEffectsMiddleware = (program, handler) => {
const middleware = store => next => action => {
Store.of({ store, action })
.with(handler)
.run(program)
.fork(console.error, console.log);
return next(action);
};

return middleware;
};
19 changes: 19 additions & 0 deletions packages/ae-redux/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

import { createSymbol } from '@algebraic-effects/utils';

export const AE_REDUX_ACTION = createSymbol('algebraic-effects/redux-action');

export const decorateAction = action => {
action && (action.$$type = AE_REDUX_ACTION);
return action;
};

export const isEffectfulAction = action => action ? action.$$type === AE_REDUX_ACTION : false;

export const filterAction = (filter, action) => {
if (!action) return false;
if (isEffectfulAction(action)) return false;
if (typeof filter === 'string') return filter === action.type;
if (typeof filter === 'function') return filter(action);
return false;
};
Loading