cache settings

This commit is contained in:
Guilherme Gazzo 2017-11-19 00:44:55 -02:00
parent 654967ccf9
commit cfd90b14f9
2 changed files with 25 additions and 25 deletions

View File

@ -50,37 +50,17 @@ const RocketChat = {
const url = `${ _url }/websocket`;
Meteor.connect(url, { autoConnect: true, autoReconnect: true });
Meteor.ddp.on('disconnected', () => {
reduxStore.dispatch(disconnect());
});
Meteor.ddp.on('connected', () => {
reduxStore.dispatch(connectSuccess());
resolve();
});
Meteor.ddp.on('connected', () => {
Meteor.call('public-settings/get', (err, data) => {
if (err) {
console.error(err);
}
const settings = {};
realm.write(() => {
data.forEach((item) => {
const setting = {
_id: item._id
};
setting._server = { id: reduxStore.getState().server.server };
if (settingsType[item.type]) {
setting[settingsType[item.type]] = item.value;
realm.create('settings', setting, true);
}
settings[item._id] = item.value;
});
});
reduxStore.dispatch(actions.setAllSettings(settings));
});
Meteor.ddp.on('connected', async() => {
Meteor.ddp.on('changed', (ddbMessage) => {
if (ddbMessage.collection === 'stream-room-messages') {
realm.write(() => {
@ -99,11 +79,12 @@ const RocketChat = {
});
}
});
RocketChat.getSettings();
});
})
.catch(e => console.error(e));
},
login(params, callback) {
return new Promise((resolve, reject) => {
Meteor._startLoggingIn();
@ -389,7 +370,22 @@ const RocketChat = {
Meteor.disconnect();
AsyncStorage.removeItem(TOKEN_KEY);
AsyncStorage.removeItem(`${ TOKEN_KEY }-${ server }`);
}
},
async getSettings() {
const temp = realm.objects('settings').sorted('_updatedAt', true)[0];
const result = await (!temp ? call('public-settings/get') : call('public-settings/get', new Date(temp._updatedAt)));
const settings = temp ? result.update : result;
const filteredSettings = RocketChat._filterSettings(settings);
realm.write(() => {
filteredSettings.forEach(setting => realm.create('settings', setting, true));
});
reduxStore.dispatch(actions.setAllSettings(RocketChat.parseSettings(filteredSettings)));
},
parseSettings: settings => settings.reduce((ret, item) => {
ret[item._id] = item[settingsType[item.type]] || item.valueAsString;
return ret;
}, {}),
_filterSettings: settings => settings.filter(setting => settingsType[setting.type])
};
export default RocketChat;

View File

@ -4,6 +4,8 @@ import * as actions from '../actions';
import { setServer } from '../actions/server';
import { restoreToken } from '../actions/login';
import { APP } from '../actions/actionsTypes';
import realm from '../lib/realm';
import RocketChat from '../lib/rocketchat';
const restore = function* restore() {
try {
@ -16,6 +18,8 @@ const restore = function* restore() {
const currentServer = yield call([AsyncStorage, 'getItem'], 'currentServer');
if (currentServer) {
yield put(setServer(currentServer));
const tmp = realm.objects('settings');
yield put(actions.setAllSettings(RocketChat.parseSettings(tmp.slice(0, tmp.length))));
}
yield put(actions.appReady({}));