saga for meteor connect

This commit is contained in:
Guilherme Gazzo 2017-08-16 22:06:31 -03:00
parent f269a6d36b
commit 0986e75779
No known key found for this signature in database
GPG Key ID: 1F85C9AD922D0829
10 changed files with 96 additions and 32 deletions

View File

@ -2,15 +2,16 @@
const REQUEST = 'REQUEST'; const REQUEST = 'REQUEST';
const SUCCESS = 'SUCCESS'; const SUCCESS = 'SUCCESS';
const FAILURE = 'FAILURE'; const FAILURE = 'FAILURE';
const defaultTypes = [REQUEST, SUCCESS, FAILURE];
function createRequestTypes(base) { function createRequestTypes(base, types = defaultTypes) {
const res = {}; const res = {};
[REQUEST, SUCCESS, FAILURE].forEach(type => res[type] = `${ base }_${ type }`); types.forEach(type => res[type] = `${ base }_${ type }`);
return res; return res;
} }
// Login events // Login events
export const LOGIN = createRequestTypes('LOGIN'); export const LOGIN = createRequestTypes('LOGIN');
export const METEOR = createRequestTypes('METEOR_CONNECT');
export const LOGOUT = 'LOGOUT'; // logout is always success export const LOGOUT = 'LOGOUT'; // logout is always success
export const INCREMENT = 'INCREMENT'; export const INCREMENT = 'INCREMENT';

20
app/actions/connect.js Normal file
View File

@ -0,0 +1,20 @@
import * as types from './actionsTypes';
export function connectRequest() {
return {
type: types.METEOR.REQUEST
};
}
export function connectSuccess() {
return {
type: types.METEOR.SUCCESS
};
}
export function connectFailure(err) {
return {
type: types.METEOR.FAILURE,
err
};
}

View File

@ -154,7 +154,7 @@ const messagesSchema = {
// } // }
}; };
// Realm.clearTestState(); Realm.clearTestState();
const realm = new Realm({ const realm = new Realm({
schema: [settingsSchema, serversSchema, subscriptionSchema, messagesSchema, usersSchema, attachment] schema: [settingsSchema, serversSchema, subscriptionSchema, messagesSchema, usersSchema, attachment]

View File

@ -55,8 +55,6 @@ const RocketChat = {
Meteor.connect(url); Meteor.connect(url);
Meteor.ddp.on('connected', () => { Meteor.ddp.on('connected', () => {
console.log('connected');
Meteor.call('public-settings/get', (err, data) => { Meteor.call('public-settings/get', (err, data) => {
if (err) { if (err) {
console.error(err); console.error(err);
@ -79,7 +77,7 @@ const RocketChat = {
}); });
reduxStore.dispatch(actions.setAllSettings(settings)); reduxStore.dispatch(actions.setAllSettings(settings));
if (cb) { if (typeof cb === 'function') {
cb(); cb();
} }
}); });

32
app/reducers/connect.js Normal file
View File

@ -0,0 +1,32 @@
import { METEOR } from '../actions/actionsTypes';
const initialState = {
connecting: false,
connected: false,
errorMessage: '',
failure: false
};
export default function connect(state = initialState, action) {
switch (action.type) {
case METEOR.REQUEST:
return { ...state,
connecting: true
};
case METEOR.SUCCESS:
return { ...state,
connecting: false,
connected: true,
failure: false
};
case METEOR.FAILURE:
return { ...state,
connecting: false,
connected: false,
failure: true,
errorMessage: action.err
};
default:
return state;
}
}

View File

@ -1,9 +1,10 @@
import { combineReducers } from 'redux'; import { combineReducers } from 'redux';
import * as reducers from './reducers'; import * as reducers from './reducers';
import * as login from './login'; import * as login from './login';
import * as connect from './connect';
const rootReducer = combineReducers({ const rootReducer = combineReducers({
...reducers, ...login ...reducers, ...login, ...connect
}); });
export default rootReducer; export default rootReducer;

26
app/sagas/connect.js Normal file
View File

@ -0,0 +1,26 @@
import { take, put, call, fork } from 'redux-saga/effects';
import { METEOR } from '../actions/actionsTypes';
import RocketChat from '../lib/rocketchat';
import { connectSuccess, connectFailure } from '../actions/connect';
function connect(...args) {
return RocketChat.connect(...args);
}
const watchConnect = function* watchConnect() {
while (true) {
yield take(METEOR.REQUEST);
try {
const response = yield call(connect);
yield put(connectSuccess(response));
} catch (err) {
yield put(connectFailure(err.status));
}
}
};
const root = function* root() {
yield fork(watchConnect);
};
export default root;

View File

@ -1,30 +1,12 @@
import { take, fork } from 'redux-saga/effects'; import { fork } from 'redux-saga/effects';
import hello from './hello'; import hello from './hello';
import login from './login'; import login from './login';
import connect from './connect';
const root = function* root() { const root = function* root() {
yield fork(hello); yield fork(hello);
yield fork(login); yield fork(login);
yield fork(connect);
}; };
// Consider using takeEvery // Consider using takeEvery
export default root; export default root;
//
// import { take, fork } from 'redux-saga/effects';
// import 'babel-polyfill';
// import 'regenerator-runtime/runtime';
//
//
// const foreverAlone = function* foreverAlone() {
// yield take('FOI');
// console.log('FOIIIIIII');
// yield take('voa');
// console.log('o');
// };
//
// const root = function* root() {
// yield fork(foreverAlone);
// };
//
// export default root;

View File

@ -10,7 +10,7 @@ function loginCall(...args) {
const watchLoginRequest = function* watchLoginRequest() { const watchLoginRequest = function* watchLoginRequest() {
while (true) { while (true) {
// yield take('METEOR_CONNECTED'); yield take(types.METEOR.SUCCESS);
const payload = yield take(types.LOGIN.REQUEST); const payload = yield take(types.LOGIN.REQUEST);
try { try {
const response = yield call(loginCall, payload); const response = yield call(loginCall, payload);

View File

@ -9,6 +9,7 @@ import Meteor from 'react-native-meteor';
import { bindActionCreators } from 'redux'; import { bindActionCreators } from 'redux';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import * as actions from '../actions'; import * as actions from '../actions';
import * as meteor from '../actions/connect';
import realm from '../lib/realm'; import realm from '../lib/realm';
import RocketChat from '../lib/rocketchat'; import RocketChat from '../lib/rocketchat';
import RoomItem from '../components/RoomItem'; import RoomItem from '../components/RoomItem';
@ -96,7 +97,8 @@ class RoomsListItem extends React.PureComponent {
Site_Url: state.settings.Site_Url Site_Url: state.settings.Site_Url
}), dispatch => ({ }), dispatch => ({
actions: bindActionCreators(actions, dispatch), actions: bindActionCreators(actions, dispatch),
login: () => dispatch(actions.login()) login: () => dispatch(actions.login()),
connect: () => dispatch(meteor.connectRequest())
})) }))
export default class RoomsListView extends React.Component { export default class RoomsListView extends React.Component {
@ -231,6 +233,7 @@ export default class RoomsListView extends React.Component {
subtitle: props.server subtitle: props.server
}); });
this.props.connect();
RocketChat.getUserToken().then((token) => { RocketChat.getUserToken().then((token) => {
if (!token) { if (!token) {
Navigation.showModal({ Navigation.showModal({
@ -238,7 +241,8 @@ export default class RoomsListView extends React.Component {
animationType: 'slide-up' animationType: 'slide-up'
}); });
} }
RocketChat.connect();
// this.props.actions.connect();
const data = realm.objects('subscriptions').filtered('_server.id = $0', props.server).sorted('_updatedAt', true); const data = realm.objects('subscriptions').filtered('_server.id = $0', props.server).sorted('_updatedAt', true);