Skip to content

Commit

Permalink
chore: Убраны window
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniysemin committed Oct 4, 2023
1 parent 3828937 commit cd8a8e0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/assistantSdk/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const promiseTimeout = <T>(promise: Promise<T>, timeout: number): Promise<T> =>
return Promise.race([
promise.then((v) => {
if (timeoutId) {
window.clearTimeout(timeoutId);
clearTimeout(timeoutId);
}
return v;
}),
Expand Down Expand Up @@ -405,7 +405,7 @@ export const createAssistant = ({
const { command } = items[i];

if (typeof command !== 'undefined') {
window.setTimeout(() => emit('command', command));
setTimeout(() => emit('command', command));

if (command.type === 'start_music_recognition') {
voice.shazam();
Expand Down
7 changes: 5 additions & 2 deletions src/assistantSdk/client/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export const createProtocol = (

status = 'connected';

window.clearTimeout(clearReadyTimer);
clearTimeout(clearReadyTimer);

/// считаем коннект = ready, если по истечении таймаута сокет не был разорван
/// т.к бек может разрывать сокет, если с settings что-то не так
Expand Down Expand Up @@ -343,7 +343,10 @@ export const createProtocol = (
},
init: () => {
// в отличии от reconnect не обрывает коннект если он в порядке
if (status === 'ready' && window.navigator.onLine) {
if (
(status === 'ready' && typeof window !== 'undefined' && window.navigator.onLine) ||
typeof window === 'undefined'
) {
return Promise.resolve();
}

Expand Down
23 changes: 14 additions & 9 deletions src/assistantSdk/client/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C
const { on, emit } = createNanoEvents<TransportEvents>();

let hasCert = !checkCertUrl;
let retryTimeoutId = -1;
let retryTimeoutId: unknown = -1;
let retries = 0;
let status: 'closed' | 'closing' | 'connecting' | 'open' = 'closed';
let webSocket: WebSocket;
let stopped = true;

const checkCert = (checkUrl: string) =>
new Promise<boolean>((resolve) => {
const checkCert = (checkUrl: string) => {
if (typeof window === 'undefined') {
return Promise.resolve(true);
}

return new Promise<boolean>((resolve) => {
window
.fetch(checkUrl)
.then(() => resolve(true))
.catch(() => resolve(false));
});
};

const close = () => {
stopped = true;
Expand All @@ -46,7 +51,7 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C
status = 'connecting';
emit('connecting');

if (!hasCert && window.navigator.onLine) {
if ((!hasCert && typeof window !== 'undefined' && window.navigator.onLine) || typeof window === 'undefined') {
const okay = await checkCert(checkCertUrl!);

if (!okay) {
Expand All @@ -70,7 +75,7 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C
return;
}

window.clearTimeout(retryTimeoutId);
clearTimeout(retryTimeoutId as number);

retries = 0;

Expand All @@ -90,10 +95,10 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C

// пробуем переподключаться, если возникла ошибка при коннекте
if (!webSocket || (webSocket.readyState === 3 && !stopped)) {
window.clearTimeout(retryTimeoutId);
clearTimeout(retryTimeoutId as number);

if (retries < 2) {
retryTimeoutId = window.setTimeout(() => {
retryTimeoutId = setTimeout(() => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
open(url);

Expand Down Expand Up @@ -129,7 +134,7 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C
return;
}

window.setTimeout(() => reconnect(url));
setTimeout(() => reconnect(url));

close();
};
Expand All @@ -141,7 +146,7 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C
return {
close,
get isOnline() {
return window.navigator.onLine;
return (typeof window !== 'undefined' && window.navigator.onLine) || typeof window === 'undefined';
},
on,
open,
Expand Down
13 changes: 7 additions & 6 deletions src/assistantSdk/voice/audioContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ export const isAudioSupported = typeof window !== 'undefined' && (window.AudioCo
* @returns AudioContext
*/
export const createAudioContext = (options?: AudioContextOptions): AudioContext => {
if (window.AudioContext) {
return new AudioContext(options);
if (!isAudioSupported) {
throw new Error('Audio not supported');
}

if (window.webkitAudioContext) {
// eslint-disable-next-line new-cap
return new window.webkitAudioContext();
if (window.AudioContext) {
return new AudioContext(options);
}

throw new Error('Audio not supported');
// @ts-ignore
// eslint-disable-next-line new-cap
return new window.webkitAudioContext();
};

interface ContextEvents {
Expand Down
2 changes: 1 addition & 1 deletion src/assistantSdk/voice/listener/navigatorAudioProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export const createNavigatorAudioProvider = (cb: (buffer: ArrayBuffer, last: boo
return createAudioRecorder(stream, cb);
})
.catch((err) => {
if (window.location.protocol === 'http:') {
if (typeof window !== 'undefined' && window.location.protocol === 'http:') {
throw new Error('Audio is supported only on a secure connection');
}

Expand Down

0 comments on commit cd8a8e0

Please sign in to comment.