This commit is contained in:
William Wong 2020-04-17 02:31:05 -07:00
Родитель 9d9ae05e89
Коммит be31132f61
7 изменённых файлов: 45 добавлений и 29 удалений

Просмотреть файл

@ -1,7 +1,7 @@
const { compose } = require('redux');
const observableToArray = require('../__jest__/observableToArray');
const { default: createAdapter, CLOSED, CONNECTING, OPEN } = require('../../src/index');
const { default: createAdapter, applyEgressMiddleware, CLOSED, CONNECTING, OPEN } = require('../../src/index');
const { default: exportDLJSInterface } = require('../../src/enhancers/exportDLJSInterface');
describe('exportDLJSInterface.connectionStatus$', () => {
@ -24,7 +24,7 @@ describe('exportDLJSInterface.connectionStatus$', () => {
);
});
test('"open"/"error" should set connectionStatus$ accordingly', async () => {
test.only('"open"/"error" should set connectionStatus$ accordingly', async () => {
let interims;
const abortController = new AbortController();
@ -60,3 +60,23 @@ describe('exportDLJSInterface.connectionStatus$', () => {
setReadyState(OPEN);
});
});
test('setReadyState should propagate to connectionStatus$ when enhancer is placed before exportDLJSInterface()', async () => {
const adapter = createAdapter(
{},
compose(
applyEgressMiddleware(({ setReadyState }) => next => activity => {
setReadyState(OPEN);
return next(activity);
}),
exportDLJSInterface()
)
);
const connectionStatusPromise = observableToArray(adapter.connectionStatus$, { count: 3 });
adapter.egress(1);
await expect(connectionStatusPromise).resolves.toEqual([0, 1, 2]);
});

Просмотреть файл

@ -9,7 +9,7 @@ describe('exportDLJSInterface.end', () => {
const adapter = createAdapter(
{},
compose(exportDLJSInterface(), () => () => ({ close }))
compose(exportDLJSInterface(), next => options => ({ ...next(options), close }))
);
expect(close).toHaveBeenCalledTimes(0);

Просмотреть файл

@ -15,7 +15,9 @@ describe('exportDLJSInterface.postActivity', () => {
const adapter = createAdapter(
{},
compose(exportDLJSInterface(), () => () => ({
compose(exportDLJSInterface(), next => options => ({
...next(options),
egress: async (activity, { progress }) => {
await checkpoint1.pause();

Просмотреть файл

@ -72,6 +72,7 @@ export default function createAdapter<TActivity>(
}
readyStatePropertyValue = readyState;
eventTarget.dispatchEvent(createEvent(readyState === ReadyState.OPEN ? 'open' : 'error'));
}
};

Просмотреть файл

@ -28,6 +28,16 @@ export default function exportDLJSInterface(): AdapterEnhancer<IDirectLineActivi
const adapter = next(options);
let connectionStatusObserver: Observer<ConnectionStatus>;
adapter.addEventListener('open', () => {
connectionStatusObserver.next(ConnectionStatus.Connected);
});
adapter.addEventListener('error', () => {
connectionStatusObserver.next(
adapter.getReadyState() === ReadyState.CLOSED ? ConnectionStatus.FailedToConnect : ConnectionStatus.Connecting
);
});
return {
...adapter,
@ -47,7 +57,9 @@ export default function exportDLJSInterface(): AdapterEnhancer<IDirectLineActivi
}
})();
return () => abortController.abort();
return () => {
abortController.abort();
};
})
),
@ -74,28 +86,6 @@ export default function exportDLJSInterface(): AdapterEnhancer<IDirectLineActivi
})
.then(() => observer.complete());
});
},
setReadyState(readyState: ReadyState) {
if (!connectionStatusObserver) {
return;
}
switch (readyState) {
case ReadyState.CONNECTING:
connectionStatusObserver.next(ConnectionStatus.Connecting);
break;
case ReadyState.OPEN:
connectionStatusObserver.next(ConnectionStatus.Connected);
break;
case ReadyState.CLOSED:
connectionStatusObserver.next(ConnectionStatus.FailedToConnect);
break;
}
adapter.setReadyState(readyState);
}
};
};

Просмотреть файл

@ -1,12 +1,14 @@
import { Adapter, ReadyState } from './types/AdapterTypes';
import { compose } from 'redux';
import applyEgressMiddleware from './applyEgressMiddleware';
import applyIngressMiddleware from './applyIngressMiddleware';
import createAdapter from './createAdapter';
import exportDLJSInterface from './enhancers/exportDLJSInterface';
export default createAdapter;
const { CLOSED, CONNECTING, OPEN } = ReadyState;
export { applyEgressMiddleware, applyIngressMiddleware, CLOSED, CONNECTING, OPEN };
export { applyEgressMiddleware, applyIngressMiddleware, CLOSED, compose, CONNECTING, exportDLJSInterface, OPEN };
export type { Adapter }
export type { Adapter };

Просмотреть файл

@ -9,6 +9,7 @@ let config = {
mode: 'production',
output: {
filename: '[name].js',
library: 'ChatAdapter',
libraryTarget: 'umd',
path: resolve(__dirname, 'dist')
},