Skip to content

Commit

Permalink
FIX addon-actions performance issue (#7256)
Browse files Browse the repository at this point in the history
FIX addon-actions performance issue
  • Loading branch information
ndelangen authored Jul 2, 2019
2 parents 4922763 + 1f12e4a commit 3b2ef9b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
1 change: 1 addition & 0 deletions addons/actions/src/models/ActionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export interface ActionOptions {
depth?: number;
clearOnStoryChange?: boolean;
limit?: number;
allowFunction?: boolean;
}
59 changes: 59 additions & 0 deletions addons/actions/src/preview/__tests__/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,62 @@ describe('Depth config', () => {
});
});
});

describe('allowFunction config', () => {
it('with global allowFunction false', () => {
const channel = createChannel();

const allowFunction = false;

configureActions({
allowFunction,
});

action('test-action')({
root: {
one: {
a: 1,
b: () => 'foo',
},
},
});

expect(getChannelData(channel)[0]).toEqual({
root: {
one: {
a: 1,
b: expect.any(Function),
},
},
});
});

// TODO: this test is pretty pointless, as the real channel isn't used, nothing is changed
it('with global allowFunction true', () => {
const channel = createChannel();

const allowFunction = true;

configureActions({
allowFunction,
});

action('test-action')({
root: {
one: {
a: 1,
b: () => 'foo',
},
},
});

expect(getChannelData(channel)[0]).toEqual({
root: {
one: {
a: 1,
b: expect.any(Function),
},
},
});
});
});
1 change: 1 addition & 0 deletions addons/actions/src/preview/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function action(name: string, options: ActionOptions = {}): HandlerFuncti
options: {
...actionOptions,
depth: minDepth + (actionOptions.depth || 3),
allowFunction: actionOptions.allowFunction || false,
},
};
channel.emit(EVENT_ID, actionDisplayToEmit);
Expand Down
6 changes: 4 additions & 2 deletions examples/official-storybook/stories/addon-actions.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ export const allTypes = () => {
<Button onClick={() => action('Boolean')(false)}>Boolean</Button>
<Button onClick={() => action('Empty Object')({})}>Empty Object</Button>
<Button onClick={() => action('File')(file)}>File</Button>
<Button onClick={() => action('Function')(A)}>Function A</Button>
<Button onClick={() => action('Function (bound)')(bound)}>Bound Function B</Button>
<Button onClick={() => action('Function', { allowFunction: true })(A)}>Function A</Button>
<Button onClick={() => action('Function (bound)', { allowFunction: true })(bound)}>
Bound Function B
</Button>
<Button onClick={() => action('Infinity')(Infinity)}>Infinity</Button>
<Button onClick={() => action('-Infinity')(-Infinity)}>-Infinity</Button>
<Button onClick={() => action('NaN')(NaN)}>NaN</Button>
Expand Down
8 changes: 7 additions & 1 deletion lib/channel-postmessage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ export class PostmsgTransport {
});
}
let depth = 15;
let allowFunction = true;

if (options && typeof options.allowFunction === 'boolean') {
// eslint-disable-next-line prefer-destructuring
allowFunction = options.allowFunction;
}
if (options && Number.isInteger(options.depth)) {
// eslint-disable-next-line prefer-destructuring
depth = options.depth;
}

const data = stringify({ key: KEY, event }, { maxDepth: depth });
const data = stringify({ key: KEY, event }, { maxDepth: depth, allowFunction });

// TODO: investigate http://blog.teamtreehouse.com/cross-domain-messaging-with-postmessage
// might replace '*' with document.location ?
Expand Down

1 comment on commit 3b2ef9b

@vercel
Copy link

@vercel vercel bot commented on 3b2ef9b Jul 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.