-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3db2d5
commit 4489902
Showing
5 changed files
with
261 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "commonjs" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters