From cfd90b14f96c355a1a1323c4b239c037066c865e Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Sun, 19 Nov 2017 00:44:55 -0200 Subject: [PATCH] cache settings --- app/lib/rocketchat.js | 46 ++++++++++++++++++++----------------------- app/sagas/init.js | 4 ++++ 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index a26432b31..34d3cd9dd 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -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; diff --git a/app/sagas/init.js b/app/sagas/init.js index c86b5caad..66b5ea43f 100644 --- a/app/sagas/init.js +++ b/app/sagas/init.js @@ -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({}));