Skip to content

Commit eb9ba44

Browse files
committed
consider-client-disconnect-disconnect
1 parent 537d8e2 commit eb9ba44

File tree

5 files changed

+31
-24
lines changed

5 files changed

+31
-24
lines changed

index.d.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React from 'react';
2-
import { InitData, FormField, FieldValue, Sender } from 'global-input-message';
3-
export * from 'global-input-message';
2+
import type { InitData, FormField, FieldValue, Sender } from 'global-input-message';
3+
export type * from 'global-input-message';
44
export function useGlobalInputApp(config: ConfigData | (() => ConfigData), canConnect?: boolean, configId?: any): GlobalInputData;
55
export function getGlobalInputState(): GlobalInputState;
66

77
interface GlobalInputState {
88
isLoading: boolean;
99
isReady: boolean;
1010
isError: boolean;
11-
isDisconnected: boolean;
11+
isClosed: boolean;
1212
isConnected: boolean;
1313
isConnectionDenied: boolean;
1414
initData: InitData;
@@ -62,15 +62,17 @@ export interface GlobalInputData {
6262
isLoading: boolean;
6363
isReady: boolean;
6464
isError: boolean;
65-
isDisconnected: boolean;
65+
isClosed: boolean;
6666
isConnected: boolean;
67+
isDisconnected:boolean;
6768
isConnectionDenied: boolean;
6869
initData: InitData;
6970
senders: Sender[];
71+
sender:Sender;
7072
sendValue: SendValueFunction;
7173
sendInitData: SendInitDataFunction;
7274
setOnchange: (onchange: OnchangeFunction) => void;
73-
disconnect: () => void;
75+
close: () => void;
7476
restart: (config?: ConfigData) => void;
7577
}
7678

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "global-input-react",
3-
"version": "4.5.1",
3+
"version": "4.6.0",
44
"description": "global input react component",
55
"main": "./dist/index",
66
"repository": {
@@ -72,4 +72,4 @@
7272
"global-input-message": "^2.1.3",
7373
"qrcode.react": "^1.0.0"
7474
}
75-
}
75+
}

src/__tests__/mobile-app-and-device-app-communicate.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,6 @@ it("Device App and Mobile App should be able to communicate", async function ()
107107
expect(deviceApp.message).toEqual(mobileApp.message);
108108

109109
mobileApp.con.disconnect();
110-
deviceApp.hook.result.current.disconnect();
110+
deviceApp.hook.result.current.close();
111111
deviceApp.hook.unmount();
112112
}, 10000);

src/globalinput.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const ACTION_TYPES = {
1717

1818
const MobileState = {
1919
INITIALIZING: 1,
20-
DISCONNECTED: 2,
20+
CLOSED: 2,
2121
ERROR: 3,
2222
WAITING_FOR_MOBILE: 4,
2323
MOBILE_CONNECTED: 5
@@ -31,9 +31,10 @@ export const initialState = {
3131
isLoading: true,
3232
isReady: false,
3333
isError: false,
34-
isDisconnected: false,
34+
isClosed: false,
3535
isConnected: false,
3636
isConnectionDenied: false,
37+
isDisconnected: false,
3738
initData: null,
3839
connected: []
3940
};
@@ -57,7 +58,8 @@ const setFieldProperties = (fields, values, setters) => {
5758
}
5859

5960

60-
const closeConnection = () => {
61+
62+
export const closeConnection = (notify) => {
6163
if (mobileData.session) {
6264
mobileData.session.disconnect();
6365
mobileData.session = null;
@@ -67,10 +69,7 @@ const closeConnection = () => {
6769
mobileData.values = [];
6870
mobileData.setters = [];
6971
}
70-
};
71-
export const disconnect = (notify) => {
72-
closeConnection();
73-
mobileData.mobileState = MobileState.DISCONNECTED;
72+
mobileData.mobileState = MobileState.CLOSED;
7473
if (notify) {
7574
notify({ type: ACTION_TYPES.CLOSE });
7675
}
@@ -312,10 +311,12 @@ const getStateData = () => {
312311
isLoading: mobileData.mobileState === MobileState.INITIALIZING,
313312
isReady: mobileData.mobileState === MobileState.WAITING_FOR_MOBILE,
314313
isError: mobileData.mobileState === MobileState.ERROR,
315-
isDisconnected: mobileData.mobileState === MobileState.DISCONNECTED,
314+
isClosed: mobileData.mobileState === MobileState.CLOSED,
316315
isConnected: mobileData.mobileState === MobileState.MOBILE_CONNECTED,
316+
isDisconnected: (!!mobileData.sender) && (mobileData.mobileState !== MobileState.MOBILE_CONNECTED),
317317
initData: mobileData.mobileConfig && mobileData.mobileConfig.initData,
318-
senders: mobileData.senders
318+
senders: mobileData.senders,
319+
sender: mobileData.sender
319320
}
320321
}
321322

src/useGlobalInputApp.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ export const useGlobalInputApp = (config, canConnect = true, configId = "") => {
99
isLoading,
1010
isReady,
1111
isError,
12-
isDisconnected,
12+
isClosed,
1313
isConnected,
14+
isDisconnected,
1415
isConnectionDenied,
1516
initData,
16-
senders
17+
senders,
18+
sender
1719
}, dispatch] = useReducer(globalInput.reducer, globalInput.initialState);
1820

1921
const attached = useRef(true);
@@ -62,7 +64,7 @@ export const useGlobalInputApp = (config, canConnect = true, configId = "") => {
6264
configRef.current.initData = configRef.current.initData();
6365
}
6466
}
65-
globalInput.disconnect(notify);
67+
globalInput.closeConnection(notify);
6668
globalInput.startConnect(config ? config : configRef.current, notify);
6769
}, [])
6870

@@ -82,8 +84,8 @@ export const useGlobalInputApp = (config, canConnect = true, configId = "") => {
8284
}, []);
8385

8486

85-
const disconnect = useCallback(() => {
86-
globalInput.disconnect(notify);
87+
const close = useCallback(() => {
88+
globalInput.closeConnection(notify);
8789
}, []);
8890

8991

@@ -113,15 +115,17 @@ export const useGlobalInputApp = (config, canConnect = true, configId = "") => {
113115
isLoading,
114116
isReady,
115117
isError,
116-
isDisconnected,
118+
isClosed,
117119
isConnected,
120+
isDisconnected,
118121
isConnectionDenied,
119122
initData,
120123
senders,
124+
sender,
121125
sendValue,
122126
sendInitData,
123127
setOnchange,
124-
disconnect,
128+
close,
125129
restart,
126130
};
127131
};

0 commit comments

Comments
 (0)