Disconnected Action
This commit is contained in:
parent
a518395375
commit
46025e0e14
|
@ -26,8 +26,18 @@ export default function Actions(central, TYPES) {
|
|||
},
|
||||
});
|
||||
|
||||
const disconnectStore = () => ({
|
||||
type: TYPES.BLUETOOTH_DISCONNECT_REQUEST,
|
||||
request: (dispatch) => {
|
||||
dispatch({ type: TYPES.BLUETOOTH_DISCONNECTING });
|
||||
central.disconnect();
|
||||
dispatch({ type: TYPES.BLUETOOTH_DISCONNECTED });
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
connectStore,
|
||||
disconnectStore,
|
||||
syncState,
|
||||
sendAction,
|
||||
};
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
export const BLUETOOTH_CONNECTING = '@@bluetooth/CONNECTING';
|
||||
export const BLUETOOTH_CONNECTED = '@@bluetooth/CONNECTED';
|
||||
export const BLUETOOTH_DISCONNECTING = '@@bluetooth/DISCONNECTING';
|
||||
export const BLUETOOTH_DISCONNECTED = '@@bluetooth/DISCONNECTED';
|
||||
export const BLUETOOTH_ERROR = '@@bluetooth/ERROR';
|
||||
export const BLUETOOTH_READ = '@@bluetooth/READ';
|
||||
export const BLUETOOTH_SYNC = '@@bluetooth/SYNC';
|
||||
export const BLUETOOTH_SEND = '@@bluetooth/SEND';
|
||||
|
||||
export const BLUETOOTH_CONNECT_REQUEST = '@@bluetooth/CONNECT_REQUEST';
|
||||
export const BLUETOOTH_DISCONNECT_REQUEST = '@@bluetooth/DISCONNECT_REQUEST';
|
||||
export const BLUETOOTH_SEND_REQUEST = '@@bluetooth/SEND_REQUEST';
|
||||
export const BLUETOOTH_SYNC_REQUEST = '@@bluetooth/SYNC_REQUEST';
|
||||
|
|
|
@ -13,11 +13,16 @@ export default function Central(
|
|||
id,
|
||||
};
|
||||
|
||||
const isConnected = () => state.server && state.server.connected;
|
||||
|
||||
const connect = name => bluetooth
|
||||
.requestDevice({
|
||||
filters: [{ services: [SERVICE_UUID], name }],
|
||||
})
|
||||
.then(device => device.gatt.connect())
|
||||
.then((device) => {
|
||||
state.device = device;
|
||||
return device.gatt.connect();
|
||||
})
|
||||
.then((server) => {
|
||||
state.server = server;
|
||||
return server.getPrimaryService(SERVICE_UUID);
|
||||
|
@ -27,6 +32,12 @@ export default function Central(
|
|||
state.characteristic = characteristic;
|
||||
});
|
||||
|
||||
const disconnect = () => {
|
||||
if (isConnected()) {
|
||||
state.device.gatt.disconnect();
|
||||
}
|
||||
};
|
||||
|
||||
const listener = callback => (event) => {
|
||||
const chunk = decode(event.target.value);
|
||||
const message = `${state.message}${chunk}`;
|
||||
|
@ -78,8 +89,9 @@ export default function Central(
|
|||
};
|
||||
|
||||
return {
|
||||
connected: state.server && state.server.connected,
|
||||
connected: isConnected(),
|
||||
connect,
|
||||
disconnect,
|
||||
handler,
|
||||
write,
|
||||
listener,
|
||||
|
|
|
@ -5,11 +5,11 @@ import MIDDLEWARE from './middleware';
|
|||
import REDUCERS from './reducers';
|
||||
import STORE from './store';
|
||||
|
||||
const { connectStore } = ACTIONS;
|
||||
const { connectStore, disconnectStore } = ACTIONS;
|
||||
|
||||
export const types = TYPES;
|
||||
export const status = STATUS;
|
||||
export const actions = { connectStore };
|
||||
export const actions = { connectStore, disconnectStore };
|
||||
export const reducers = REDUCERS;
|
||||
export const middleware = MIDDLEWARE;
|
||||
export const createSyncStore = STORE;
|
||||
|
|
|
@ -5,11 +5,13 @@ const { sendAction } = ACTIONS;
|
|||
|
||||
const {
|
||||
BLUETOOTH_CONNECT_REQUEST,
|
||||
BLUETOOTH_DISCONNECT_REQUEST,
|
||||
BLUETOOTH_SEND_REQUEST,
|
||||
} = TYPES;
|
||||
|
||||
const REQUESTS = [
|
||||
BLUETOOTH_CONNECT_REQUEST,
|
||||
BLUETOOTH_DISCONNECT_REQUEST,
|
||||
BLUETOOTH_SEND_REQUEST,
|
||||
];
|
||||
|
||||
|
|
|
@ -3,15 +3,26 @@ import * as STATUS from '../central/status';
|
|||
|
||||
import initial from './initial';
|
||||
|
||||
export default (autosync = true) => (state = initial, { type, payload }) => {
|
||||
export function status(state = initial.status, { type }) {
|
||||
switch (type) {
|
||||
case TYPES.BLUETOOTH_CONNECTING:
|
||||
return Object.assign({}, state, { status: STATUS.CONNECTING });
|
||||
return STATUS.CONNECTING;
|
||||
case TYPES.BLUETOOTH_CONNECTED:
|
||||
return Object.assign({}, state, { status: STATUS.CONNECTED });
|
||||
case TYPES.BLUETOOTH_SYNC:
|
||||
return autosync ? Object.assign({}, state, { store: payload }) : state;
|
||||
return STATUS.CONNECTED;
|
||||
case TYPES.BLUETOOTH_DISCONNECTING:
|
||||
return STATUS.DISCONNECTING;
|
||||
case TYPES.BLUETOOTH_DISCONNECTED:
|
||||
return STATUS.DISCONNECTED;
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function remote(state = null, { type, payload }) {
|
||||
switch (type) {
|
||||
case TYPES.BLUETOOTH_SYNC:
|
||||
return payload;
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
/* global window */
|
||||
import { createStore, applyMiddleware, compose } from 'redux';
|
||||
import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
|
||||
|
||||
import middleware from '../middleware';
|
||||
import reducers from '../reducers';
|
||||
import { status, remote } from '../reducers';
|
||||
|
||||
export default (actions) => {
|
||||
export default (actions, reducer = () => true) => {
|
||||
const middlewares = [middleware(actions)];
|
||||
const enhancers = [applyMiddleware(...middlewares)];
|
||||
|
||||
|
@ -14,5 +14,12 @@ export default (actions) => {
|
|||
}
|
||||
/* eslint-enable */
|
||||
|
||||
return createStore(reducers(), compose(...enhancers));
|
||||
return createStore(
|
||||
combineReducers({
|
||||
status,
|
||||
remote,
|
||||
local: reducer,
|
||||
}),
|
||||
compose(...enhancers),
|
||||
);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue