Skip to content

feat: Добавлено событие everything, срабатывающее при любом событии #217

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
46 changes: 36 additions & 10 deletions src/createAssistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ export interface AssistantEvents<A extends AssistantSmartAppData> {
) => void;
}

interface EverythingEvent<A extends AssistantSmartAppData> {
everything: (param: {
type: keyof AssistantEvents<A> | 'sendDataContainer' | 'sendData';
payload:
| Parameters<AssistantEvents<A>[keyof AssistantEvents<A>]>[0]
| SendDataParams
| (Omit<SendDataParams, 'action' | 'name'> & { data: SendDataParams['action']; message_name: string });
}) => void;
}

export interface SendDataParams {
action: AssistantServerAction;
name?: string;
Expand Down Expand Up @@ -116,17 +126,31 @@ export const createAssistant = <A extends AssistantSmartAppData>({
let isInitialCommandsEmitted = false;
let receivedAppInitialData: AssistantClientCommand[] = [];
const stackAppInitialData: AssistantClientCommand[] = [...(window.appInitialData || [])];
const { on, emit } = createNanoEvents<AssistantEvents<A>>();
const observables = new Map<string, { next: ObserverFunc<A | AssistantSmartAppError>; requestId?: string }>();
const { on, emit } = createNanoEvents<AssistantEvents<A> & EverythingEvent<A>>();
const emitCommand = (command: AssistantClientCustomizedCommand<A>) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

emitCommand не случится, если есть подписка на ответ - эмитить everything нужно в любом случае
https://github.com/sberdevices/assistant-client/pull/217/files#diff-4a8e2af747f7708c3f686add51592371c9d7ffd3a905ff4b25fcfa1859e31e10L209

if (command.type === 'smart_app_data') {
emit('command', command.smart_app_data as AssistantSmartAppCommand['smart_app_data']);
const smartAppData = command.smart_app_data as AssistantSmartAppCommand['smart_app_data'];

emit('command', smartAppData);
emit('everything', {
type: 'command',
payload: smartAppData,
});
}

if (command.type === 'smart_app_error') {
emit('everything', {
type: 'error',
payload: command.smart_app_error,
});
emit('error', command.smart_app_error);
}

emit('everything', {
type: 'data',
payload: command as A,
});
return emit('data', command as A);
};

Expand Down Expand Up @@ -237,10 +261,11 @@ export const createAssistant = <A extends AssistantSmartAppData>({
): (() => void) => {
if (window.AssistantHost?.sendDataContainer) {
if (onData == null) {
window.AssistantHost?.sendDataContainer(
/* eslint-disable-next-line @typescript-eslint/camelcase */
JSON.stringify({ data: action, message_name: name || '', requestId }),
);
/* eslint-disable-next-line @typescript-eslint/camelcase */
const data = { data: action, message_name: name || '', requestId };

emit('everything', { type: 'sendDataContainer', payload: data });
window.AssistantHost?.sendDataContainer(JSON.stringify(data));
return () => {};
}

Expand All @@ -250,11 +275,11 @@ export const createAssistant = <A extends AssistantSmartAppData>({

const { subscribe } = createNanoObservable<A | AssistantSmartAppError>(({ next }) => {
const realRequestId = requestId || v4();
/* eslint-disable-next-line @typescript-eslint/camelcase */
const data = { data: action, message_name: name || '', requestId: realRequestId };

window.AssistantHost?.sendDataContainer(
/* eslint-disable-next-line @typescript-eslint/camelcase */
JSON.stringify({ data: action, message_name: name || '', requestId: realRequestId }),
);
emit('everything', { type: 'sendDataContainer', payload: data });
window.AssistantHost?.sendDataContainer(JSON.stringify(data));

observables.set(realRequestId, { next, requestId });
});
Expand All @@ -266,6 +291,7 @@ export const createAssistant = <A extends AssistantSmartAppData>({
throw new Error('Не поддерживается в данной версии клиента');
}

emit('everything', { type: 'sendData', payload: { action, name, requestId } });
window.AssistantHost?.sendData(JSON.stringify(action), name || null);

return () => {};
Expand Down