Skip to content

Commit

Permalink
Merge pull request #6 from kadirahq/constant-key
Browse files Browse the repository at this point in the history
Use a constant key to identify messages
  • Loading branch information
Muhammed Thanish authored Oct 28, 2016
2 parents fe70080 + 7574f20 commit 9a62220
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import Channel from '@kadira/storybook-channel';
import stringify from 'json-stringify-safe';

export default function createChannel({ key }) {
const transport = new PostmsgTransport({ key });
export const KEY = 'storybook-channel';

export default function createChannel() {
const transport = new PostmsgTransport();
return new Channel({ transport });
}

export class PostmsgTransport {
constructor({ key }) {
this._key = key;
constructor() {
this._buffer = [];
this._handler = null;
window.addEventListener('message', this._handleEvent.bind(this), false);
Expand All @@ -26,7 +27,7 @@ export class PostmsgTransport {
this._buffer.push({ event, resolve, reject });
});
}
const data = stringify({ key: this._key, event });
const data = stringify({ key: KEY, event });
iframeWindow.postMessage(data, '*');
return Promise.resolve(null);
}
Expand Down Expand Up @@ -68,7 +69,7 @@ export class PostmsgTransport {
return null;
}
const { key, event } = data;
if (key === this._key) {
if (key === KEY) {
this._handler(event);
}
}
Expand Down

0 comments on commit 9a62220

Please sign in to comment.