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

FIX addon-actions performance issue #7256

Merged
merged 1 commit into from
Jul 2, 2019
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
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