Skip to content

Commit

Permalink
Add commonjs version
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenyOrekhov committed Jan 12, 2020
1 parent e3db2d5 commit 4489902
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 2 deletions.
15 changes: 15 additions & 0 deletions commonjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "init", {
enumerable: true,
get: function () {
return _init.default;
}
});

var _init = _interopRequireDefault(require("./src/init.js"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
3 changes: 3 additions & 0 deletions commonjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
62 changes: 62 additions & 0 deletions commonjs/src/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = init;
const ACTION_ARITY = 2;

function init({
state,
actions,
subscribers
}) {
// eslint-disable-next-line fp/no-let
let currentState = state; // eslint-disable-next-line fp/no-let, init-declarations

let shouldNotifySubscribers; // eslint-disable-next-line fp/no-let, init-declarations, prefer-const

let boundActions;

function notifySubscribers({
actionName,
value
} = {}) {
// eslint-disable-next-line fp/no-mutation
shouldNotifySubscribers = true;
subscribers.every(subscriber => {
subscriber({
state: currentState,
actions: boundActions,
actionName,
value
});
return shouldNotifySubscribers;
}); // eslint-disable-next-line fp/no-mutation

shouldNotifySubscribers = false;
} // eslint-disable-next-line fp/no-mutation


boundActions = Object.fromEntries(Object.entries(actions).map(([actionName, action]) => [actionName, function boundAction(value) {
function getNewState() {
if (action.length === ACTION_ARITY) {
return action(value, currentState);
}

const partiallyAppliedActionOrNewState = action(currentState);
return typeof partiallyAppliedActionOrNewState === "function" ? // Turns out we have a curried action here.
// Reapplying arguments in the correct order:
action(value)(currentState) : partiallyAppliedActionOrNewState;
} // eslint-disable-next-line fp/no-mutation


currentState = getNewState();
notifySubscribers({
actionName,
value
});
}]));
notifySubscribers();
return boundActions;
}
174 changes: 174 additions & 0 deletions package-lock.json

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

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"author": "Evgeny Orekhov",
"files": [
"index.js",
"src/init.js"
"src/init.js",
"commonjs/"
],
"sideEffects": false,
"type": "module",
Expand All @@ -16,7 +17,10 @@
"lint": "run-p --aggregate-output lint:**",
"lint:js": "eslint . --ext .js,.json",
"lint:prettier": "prettier --check *.{js,css,json,md,html,yml,yaml}",
"test": "jest"
"test": "jest",
"build": "babel index.js --out-dir commonjs/ && babel src/ --out-dir commonjs/src/ --ignore 'src/**/*.test.js'",
"preversion": "npm run build && git add commonjs/",
"postversion": "git push && git push --tags && npm publish"
},
"husky": {
"hooks": {
Expand All @@ -38,6 +42,7 @@
"collectCoverage": true
},
"devDependencies": {
"@babel/cli": "^7.8.0",
"@babel/core": "^7.8.0",
"@babel/preset-env": "^7.8.0",
"babel-jest": "^24.9.0",
Expand Down

0 comments on commit 4489902

Please sign in to comment.