Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

Add function name support for actions #24

Merged
merged 1 commit into from
Mar 21, 2017
Merged
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
4 changes: 4 additions & 0 deletions .storybook/stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ storiesOf('Button', module)
onClick={() => action('circular')(circular)}>
Circular Payload
</button>;
})
.add('Function Name', () => {
const fn = action('fnName');
return <button onClick={fn}>Action.name: {fn.name}</button>
});
2 changes: 1 addition & 1 deletion dist/components/ActionLogger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var ActionLogger = function (_Component) {
function ActionLogger() {
_classCallCheck(this, ActionLogger);

return _possibleConstructorReturn(this, Object.getPrototypeOf(ActionLogger).apply(this, arguments));
return _possibleConstructorReturn(this, (ActionLogger.__proto__ || Object.getPrototypeOf(ActionLogger)).apply(this, arguments));
}

_createClass(ActionLogger, [{
Expand Down
4 changes: 2 additions & 2 deletions dist/containers/ActionLogger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ var ActionLogger = function (_React$Component) {
_inherits(ActionLogger, _React$Component);

function ActionLogger(props) {
var _Object$getPrototypeO;
var _ref;

_classCallCheck(this, ActionLogger);

for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}

var _this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(ActionLogger)).call.apply(_Object$getPrototypeO, [this, props].concat(args)));
var _this = _possibleConstructorReturn(this, (_ref = ActionLogger.__proto__ || Object.getPrototypeOf(ActionLogger)).call.apply(_ref, [this, props].concat(args)));

_this.state = { actions: [] };
_this._actionListener = function (action) {
Expand Down
13 changes: 12 additions & 1 deletion dist/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ function _format(arg) {
}

function action(name) {
return function () {

var handler = function handler() {
for (var _len = arguments.length, _args = Array(_len), _key = 0; _key < _len; _key++) {
_args[_key] = arguments[_key];
}
Expand All @@ -41,6 +42,16 @@ function action(name) {
data: { name: name, args: args }
});
};

// some day when {[name]: function() {}} syntax is not transpiled by babel
// we can get rid of this eval as by ES2015 spec the above function gets the
// name `name`, but babel transpiles to Object.defineProperty which doesn't do
// the same.
//
// Ref: https://bocoup.com/weblog/whats-in-a-function-name
var fnName = name.replace(/\W+/g, '_');
var named = eval('(function ' + fnName + '() { return handler.apply(this, arguments) })');
return named;
}

function decorateAction(decorators) {
Expand Down
13 changes: 12 additions & 1 deletion src/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ function _format(arg) {
}

export function action(name) {
return function (..._args) {

const handler = function (..._args) {
const args = Array.from(_args).map(_format);
const channel = addons.getChannel();
const randomId = Math.random().toString(16).slice(2);
Expand All @@ -19,6 +20,16 @@ export function action(name) {
data: { name, args },
});
};

// some day when {[name]: function() {}} syntax is not transpiled by babel
// we can get rid of this eval as by ES2015 spec the above function gets the
// name `name`, but babel transpiles to Object.defineProperty which doesn't do
// the same.
//
// Ref: https://bocoup.com/weblog/whats-in-a-function-name
const fnName = name.replace(/\W+/g,'_');
const named = eval(`(function ${fnName}() { return handler.apply(this, arguments) })`)
return named
}

export function decorateAction(decorators) {
Expand Down