Skip to content

Commit

Permalink
chore: iframe отдельно
Browse files Browse the repository at this point in the history
  • Loading branch information
sasha-tlt committed Jul 22, 2021
1 parent d88e5db commit c23e200
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 67 deletions.
67 changes: 67 additions & 0 deletions src/iframe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { AssistantPostMessage } from './typings';

const inIframe = () => {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
};

if (typeof window !== 'undefined' && inIframe()) {
const postMessage = (action: AssistantPostMessage) => {
window.top.postMessage(JSON.stringify(action), '*');
};

window.AssistantHost = {
sendDataContainer(json: string) {
postMessage({ type: 'sendDataContainer', payload: json });
},
close() {
postMessage({ type: 'close' });
},
sendData(json: string) {
postMessage({ type: 'sendData', payload: json });
},
setSuggest(suggests: string) {
postMessage({ type: 'setSuggest', payload: suggests });
},
ready() {
postMessage({ type: 'ready' });
},
};

window.addEventListener('message', (e) => {
try {
if (typeof e.data === 'string') {
const data = JSON.parse(e.data);

switch (data.type) {
case 'onData':
window.AssistantClient?.onData?.(data.payload);
break;
case 'onRequestState': {
const state = window.AssistantClient?.onRequestState?.();
postMessage({ type: 'state', payload: state, requestId: data.requestId });
break;
}
case 'onRequestRecoveryState': {
const recoverystate = window.AssistantClient?.onRequestRecoveryState?.();
postMessage({ type: 'recoveryState', payload: recoverystate });
break;
}
case 'onStart':
window.AssistantClient?.onStart?.();
break;
default:
// eslint-disable-next-line no-console
console.error(e, 'Unknown parsed message');
break;
}
}
} catch (err) {
// eslint-disable-next-line no-console
console.error(err, 'Unknown message');
}
});
}
68 changes: 1 addition & 67 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
AssistantClientCommand,
AssistantSmartAppError,
AssistantSmartAppCommand,
AssistantPostMessage,
} from './typings';
import { createNanoEvents } from './nanoevents';
import { initializeAssistantSDK, InitializeAssistantSDKParams } from './dev';
import { createNanoObservable, ObserverFunc } from './nanoobservable';
import './iframe';

export interface AssistantEvents<A extends AssistantSmartAppData> {
start: () => void;
Expand All @@ -34,72 +34,6 @@ export interface SendDataParams {
requestId?: string;
}

function inIframe() {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}

if (typeof window !== 'undefined' && inIframe()) {
const postMessage = (action: AssistantPostMessage) => {
window.top.postMessage(JSON.stringify(action), '*');
};

window.AssistantHost = {
sendDataContainer(json: string) {
postMessage({ type: 'sendDataContainer', payload: json });
},
close() {
postMessage({ type: 'close' });
},
sendData(json: string) {
postMessage({ type: 'sendData', payload: json });
},
setSuggest(suggests: string) {
postMessage({ type: 'setSuggest', payload: suggests });
},
ready() {
postMessage({ type: 'ready' });
},
};

window.addEventListener('message', (e) => {
try {
if (typeof e.data === 'string') {
const data = JSON.parse(e.data);

switch (data.type) {
case 'onData':
window.AssistantClient?.onData?.(data.payload);
break;
case 'onRequestState': {
const state = window.AssistantClient?.onRequestState?.();
postMessage({ type: 'state', payload: state, requestId: data.requestId });
break;
}
case 'onRequestRecoveryState': {
const recoverystate = window.AssistantClient?.onRequestRecoveryState?.();
postMessage({ type: 'recoveryState', payload: recoverystate });
break;
}
case 'onStart':
window.AssistantClient?.onStart?.();
break;
default:
// eslint-disable-next-line no-console
console.error(e, 'Unknown parsed message');
break;
}
}
} catch (err) {
// eslint-disable-next-line no-console
console.error(err, 'Unknown message');
}
});
}

export const createAssistant = <A extends AssistantSmartAppData>({
getState,
getRecoveryState,
Expand Down

0 comments on commit c23e200

Please sign in to comment.