From 0bc9a3255d942e000aadb53c451fed5b8a8d738a Mon Sep 17 00:00:00 2001 From: Evgeniy Semin Date: Wed, 20 Oct 2021 17:44:24 +0300 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=BE=20=D1=81=D0=BE=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=B5=20everything,=20=D1=81=D1=80=D0=B0=D0=B1=D0=B0=D1=82?= =?UTF-8?q?=D1=8B=D0=B2=D0=B0=D1=8E=D1=89=D0=B5=D0=B5=20=D0=BF=D1=80=D0=B8?= =?UTF-8?q?=20=D0=BB=D1=8E=D0=B1=D0=BE=D0=BC=20=D1=81=D0=BE=D0=B1=D1=8B?= =?UTF-8?q?=D1=82=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/assistant/assistant.ts | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/assistant/assistant.ts b/src/assistant/assistant.ts index 6477850..cf91a74 100644 --- a/src/assistant/assistant.ts +++ b/src/assistant/assistant.ts @@ -94,6 +94,7 @@ export type AssistantEvents = { actionCommand: (event: ActionCommandEvent) => void; status: (status: OriginalMessageType['status']) => void; error: (error: AssistantError) => void; + everything: AssistantEvents[keyof Omit]; }; export interface CreateAssistantDevOptions { @@ -103,6 +104,11 @@ export interface CreateAssistantDevOptions { export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration & CreateAssistantDevOptions) => { const { on, emit } = createNanoEvents(); + const emitEvent: typeof emit = (event, ...args) => { + emit('everything', ...args); + emit(event, ...args); + }; + const subscriptions: Array<() => void> = []; // хеш [messageId]: requestId, где requestId - пользовательский ид экшена @@ -151,7 +157,7 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration const client = createClient(protocol, metaProvider); const voice = createVoice( client, - (event) => emit('assistant', event), + (event) => emitEvent('assistant', event), () => { voiceReady = true; // когда голос готов, возвращаем первоначальное состояние @@ -168,7 +174,7 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration }; if (!isDefaultApp(current.info)) { - emit('app', { type: 'close', app: current.info }); + emitEvent('app', { type: 'close', app: current.info }); } }; @@ -204,33 +210,33 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration client.sendData(data, 'SERVER_ACTION'); }; - subscriptions.push(protocol.on('ready', () => emit('vps', { type: 'ready' }))); + subscriptions.push(protocol.on('ready', () => emitEvent('vps', { type: 'ready' }))); // при неудачном переподключении к сокету subscriptions.push( transport.on('error', (error: Event) => { - emit('vps', { type: 'error', error }); + emitEvent('vps', { type: 'error', error }); }), ); // обработка исходящих сообщений subscriptions.push( protocol.on('outcoming', (message: OriginalMessageType) => { - emit('vps', { type: 'outcoming', message }); + emitEvent('vps', { type: 'outcoming', message }); }), ); // обработка ошибок subscriptions.push( protocol.on('error', (error: ProtocolError) => { - emit('error', error); + emitEvent('error', error); }), ); // оповещение о статусах subscriptions.push( client.on('status', (status) => { - emit('status', status); + emitEvent('status', status); }), ); @@ -241,11 +247,11 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration const { activate_app_info, items, app_info: mesAppInfo, character } = systemMessage; if (character) { - emit('assistant', { character: character.id }); + emitEvent('assistant', { character: character.id }); } if (mesAppInfo && activate_app_info) { - emit('app', { type: 'run', app: mesAppInfo }); + emitEvent('app', { type: 'run', app: mesAppInfo }); } if (items) { @@ -268,7 +274,7 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration } if (command.type === 'action') { - emit('actionCommand', { + emitEvent('actionCommand', { type: 'command', command: command as ActionCommand, }); @@ -276,7 +282,7 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration if ((command.type === 'smart_app_data' || command.type === 'navigation') && mesAppInfo) { // эмитим все команды, т.к бывают фоновые команды - emit('app', { + emitEvent('app', { type: 'command', command: { ...(command as AssistantSmartAppData), @@ -296,7 +302,7 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration } } - emit('vps', { type: 'incoming', systemMessage, originalMessage }); + emitEvent('vps', { type: 'incoming', systemMessage, originalMessage }); } }), ); From d65518f5c4fb9c989be8921d6a604176f77b3b2c Mon Sep 17 00:00:00 2001 From: Evgeniy Semin Date: Tue, 26 Oct 2021 17:07:27 +0300 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=BE=20=D1=81=D0=BE=D0=B1=D1=8B=D1=82=D0=B8?= =?UTF-8?q?=D0=B5=20everything=20=D0=B2=20createAssistant.on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/assistant/assistant.ts | 30 ++++++++++--------------- src/createAssistant.ts | 46 +++++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/assistant/assistant.ts b/src/assistant/assistant.ts index cf91a74..6477850 100644 --- a/src/assistant/assistant.ts +++ b/src/assistant/assistant.ts @@ -94,7 +94,6 @@ export type AssistantEvents = { actionCommand: (event: ActionCommandEvent) => void; status: (status: OriginalMessageType['status']) => void; error: (error: AssistantError) => void; - everything: AssistantEvents[keyof Omit]; }; export interface CreateAssistantDevOptions { @@ -104,11 +103,6 @@ export interface CreateAssistantDevOptions { export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration & CreateAssistantDevOptions) => { const { on, emit } = createNanoEvents(); - const emitEvent: typeof emit = (event, ...args) => { - emit('everything', ...args); - emit(event, ...args); - }; - const subscriptions: Array<() => void> = []; // хеш [messageId]: requestId, где requestId - пользовательский ид экшена @@ -157,7 +151,7 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration const client = createClient(protocol, metaProvider); const voice = createVoice( client, - (event) => emitEvent('assistant', event), + (event) => emit('assistant', event), () => { voiceReady = true; // когда голос готов, возвращаем первоначальное состояние @@ -174,7 +168,7 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration }; if (!isDefaultApp(current.info)) { - emitEvent('app', { type: 'close', app: current.info }); + emit('app', { type: 'close', app: current.info }); } }; @@ -210,33 +204,33 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration client.sendData(data, 'SERVER_ACTION'); }; - subscriptions.push(protocol.on('ready', () => emitEvent('vps', { type: 'ready' }))); + subscriptions.push(protocol.on('ready', () => emit('vps', { type: 'ready' }))); // при неудачном переподключении к сокету subscriptions.push( transport.on('error', (error: Event) => { - emitEvent('vps', { type: 'error', error }); + emit('vps', { type: 'error', error }); }), ); // обработка исходящих сообщений subscriptions.push( protocol.on('outcoming', (message: OriginalMessageType) => { - emitEvent('vps', { type: 'outcoming', message }); + emit('vps', { type: 'outcoming', message }); }), ); // обработка ошибок subscriptions.push( protocol.on('error', (error: ProtocolError) => { - emitEvent('error', error); + emit('error', error); }), ); // оповещение о статусах subscriptions.push( client.on('status', (status) => { - emitEvent('status', status); + emit('status', status); }), ); @@ -247,11 +241,11 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration const { activate_app_info, items, app_info: mesAppInfo, character } = systemMessage; if (character) { - emitEvent('assistant', { character: character.id }); + emit('assistant', { character: character.id }); } if (mesAppInfo && activate_app_info) { - emitEvent('app', { type: 'run', app: mesAppInfo }); + emit('app', { type: 'run', app: mesAppInfo }); } if (items) { @@ -274,7 +268,7 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration } if (command.type === 'action') { - emitEvent('actionCommand', { + emit('actionCommand', { type: 'command', command: command as ActionCommand, }); @@ -282,7 +276,7 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration if ((command.type === 'smart_app_data' || command.type === 'navigation') && mesAppInfo) { // эмитим все команды, т.к бывают фоновые команды - emitEvent('app', { + emit('app', { type: 'command', command: { ...(command as AssistantSmartAppData), @@ -302,7 +296,7 @@ export const createAssistant = ({ getMeta, ...configuration }: VpsConfiguration } } - emitEvent('vps', { type: 'incoming', systemMessage, originalMessage }); + emit('vps', { type: 'incoming', systemMessage, originalMessage }); } }), ); diff --git a/src/createAssistant.ts b/src/createAssistant.ts index faed1b9..e6959aa 100644 --- a/src/createAssistant.ts +++ b/src/createAssistant.ts @@ -24,6 +24,16 @@ export interface AssistantEvents { ) => void; } +interface EverythingEvent { + everything: (param: { + type: keyof AssistantEvents | 'sendDataContainer' | 'sendData'; + payload: + | Parameters[keyof AssistantEvents]>[0] + | SendDataParams + | (Omit & { data: SendDataParams['action']; message_name: string }); + }) => void; +} + export interface SendDataParams { action: AssistantServerAction; name?: string; @@ -116,17 +126,31 @@ export const createAssistant = ({ let isInitialCommandsEmitted = false; let receivedAppInitialData: AssistantClientCommand[] = []; const stackAppInitialData: AssistantClientCommand[] = [...(window.appInitialData || [])]; - const { on, emit } = createNanoEvents>(); const observables = new Map; requestId?: string }>(); + const { on, emit } = createNanoEvents & EverythingEvent>(); const emitCommand = (command: AssistantClientCustomizedCommand) => { 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); }; @@ -237,10 +261,11 @@ export const createAssistant = ({ ): (() => 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 () => {}; } @@ -250,11 +275,11 @@ export const createAssistant = ({ const { subscribe } = createNanoObservable(({ 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 }); }); @@ -266,6 +291,7 @@ export const createAssistant = ({ throw new Error('Не поддерживается в данной версии клиента'); } + emit('everything', { type: 'sendData', payload: { action, name, requestId } }); window.AssistantHost?.sendData(JSON.stringify(action), name || null); return () => {};