forked from DesignmanIO/react-native-meteor-offline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectMeteorRedux.js
314 lines (298 loc) · 9.26 KB
/
connectMeteorRedux.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* Created by Julian on 12/30/16.
*/
import Meteor, { getData } from 'react-native-meteor';
import { createStore, combineReducers } from 'redux';
import { AsyncStorage } from 'react-native';
import _ from 'lodash';
import EventEmitter from 'events';
import { persistStore, autoRehydrate, createTransform } from 'redux-persist';
const meteorReduxReducers = (
state = { reactNativeMeteorOfflineRecentlyAdded: [] },
action
) => {
const { type, collection, id, fields, cleared } = action;
// console.log(type);
switch (type) {
case 'SET_USERID': {
return { ...state, userId: id };
}
case 'RECENTLY_ADDED': {
return {
...state,
reactNativeMeteorOfflineRecentlyAdded: [
...(state.reactNativeMeteorOfflineRecentlyAdded || []),
id,
],
};
}
case 'ADDED': {
// doc and/or collection don't exist yet, add them
if (_.isEqual(_.get(state, `${collection}.${id}`, {}), fields)) return state;
return {
...state,
[collection]: { ...state[collection], [id]: fields },
};
}
case 'CHANGED': {
// something's changed, add/update
if (cleared.length) {
const nextDoc = _.omit(state[collection][id], cleared);
return { ...state, [collection]: { ...state[collection], [id]: nextDoc } };
} else if (_.isEqual(_.get(state, `${collection}.${id}`), fields)) return state;
return { ...state, [collection]: { ...state[collection], [id]: fields } };
}
case 'REMOVED':
if (state[collection][id]) {
const newState = _.clone(state);
delete newState[collection][id];
return newState;
}
// console.error(`Couldn't remove ${id}, not found in ${collection} collection`);
return state;
case 'SET_READY':
// todo: check for removed docs
return {
...state,
ready: action.ready,
};
case 'REMOVE_AFTER_RECONNECT':
// todo: check for removed docs
const { removed } = action;
const withoutRemoved = _.omit(
state.reactNativeMeteorOfflineRecentlyAdded,
removed
);
getData().db[collection].remove({ _id: { $in: removed } });
return {
...state,
reactNativeMeteorOfflineRecentlyAdded: withoutRemoved,
};
case 'persist/REHYDRATE':
if (
typeof Meteor.ddp === 'undefined' ||
Meteor.ddp.status === 'disconnected'
) {
return action.payload;
}
return state;
case 'HARDRESET':
console.log('hard reset');
return {};
default:
return state;
}
};
const meteorReduxEmitter = new EventEmitter();
const initMeteorRedux = (
customDebugger = undefined,
preloadedState = undefined,
enhancer = undefined,
customReducers = undefined
) => {
// console.log(preloadedState, enhancer)
const newReducers = customReducers !== undefined
? combineReducers({ ...customReducers, meteorReduxReducers })
: meteorReduxReducers;
const MeteorStore = createStore(
newReducers,
customDebugger,
preloadedState,
enhancer
);
MeteorStore.loaded = () => {
meteorReduxEmitter.emit('rehydrated');
};
meteorReduxEmitter.once('rehydrated', () => {
// restore collections to minimongo
_.each(MeteorStore.getState(), (collection, key) => {
const correctedCollection = _.chain(collection)
.map((doc) => doc)
.filter('_id')
.value();
// add the collection if it doesn't exist
if (!getData().db[key]) {
// add collection to minimongo
getData().db.addCollection(key);
}
// only upsert if the data doesn't match
if (!_.isEqual(getData().db[key], collection)) {
// add documents to collection
getData().db[key].upsert(correctedCollection);
}
});
MeteorStore.dispatch({ type: 'SET_READY', ready: true });
});
Meteor.waitDdpConnected(() => {
// return false;
// question: do I need to check for disconnection?
let connected = true;
Meteor.ddp.on('disconnected', () => {
connected = false;
});
if (connected) {
Meteor.ddp.on('removed', ({ collection, id, fields = {} }) => {
MeteorStore.dispatch({ type: 'REMOVED', collection, id, fields });
});
Meteor.ddp.on('changed', ({ collection, id, fields = {}, cleared = [] }) => {
MeteorStore.dispatch({ type: 'CHANGED', collection, id, fields, cleared });
});
Meteor.ddp.on('added', (obj, ...args) => {
const { collection, id } = obj;
// console.log('added', obj, args);
const fields = obj.fields || {};
fields._id = id;
const getCollection = MeteorStore.getState()[collection];
if (
!getCollection ||
!getCollection[id] ||
!_.isEqual(getCollection[id], fields)
) {
// don't insert if it exists
MeteorStore.dispatch({ type: 'ADDED', collection, id, fields });
}
MeteorStore.dispatch({ type: 'RECENTLY_ADDED', id });
});
}
});
return MeteorStore;
};
const subscribeCached = (store, name, ...args) => {
let offline = true;
const subHandle = Meteor.subscribe(name, ...args);
Meteor.waitDdpConnected(() => {
if (Meteor.ddp.status === 'connected') {
offline = false;
}
});
if (!store || !offline) return subHandle;
if (typeof args[args.length - 1] === 'function' && store.getState().ready) {
const callback = _.once(args[args.length - 1]);
callback();
}
return {
ready: () => {
return store.getState().ready || false;
},
offline: true,
};
};
const returnCached = (cursor, store, collection, doDisable) => {
console.warn('returnCached is deprecated and will be removed soon');
if (Meteor.ddp && Meteor.ddp.status === 'disconnected') {
return store.getState()[collection] || [];
}
return cursor;
};
class MeteorOffline {
constructor(options = {}) {
this.offline = true;
// first time connecting since app open or connection restored
this.firstConnection = true;
this.subscriptions = [];
this.collections = [];
if (!options.store) {
this.store = initMeteorRedux(options.debugger || undefined, undefined, autoRehydrate());
}
this.persistor = persistStore(
this.store,
{
storage: AsyncStorage,
debounce: options.debounce || 1000,
blacklist: ['reactNativeMeteorOfflineRecentlyAdded'],
},
() => {
this.store.loaded();
}
);
console.log('initializing');
Meteor.waitDdpConnected(() => {
if (Meteor.ddp.status === 'connected') {
this.offline = false;
} else {
this.offline = true;
this.firstConnection = false;
}
});
}
subReady (uniqueName) {
return this.subscriptions[uniqueName].ready && !this.offline;
}
user() {
if (Meteor.user()) {
this.store.dispatch({ type: 'SET_USERID', id: Meteor.userId() });
return Meteor.user();
}
const { userId } = this.store.getState();
return Meteor.collection('users').findOne(userId);
}
reset() {
this.store.dispatch({ type: 'HARDRESET' });
this.persistor.purge();
//console.log('performed meteor offline hard reset');
}
subscribe(uniqueName, name, ...params) {
const hasCallback = typeof params[params.length - 1] === 'function';
const justParams = params.slice(0, params.length - 1);
_.set(this.subscriptions, `${uniqueName}.${name}`, name);
_.set(
this.subscriptions,
`${uniqueName}.${params}`,
JSON.stringify(justParams)
);
let subHandle = Meteor.subscribe(name, ...params);
if (this.offline) {
subHandle = {
ready: () => {
// ready === rehydrated
return this.store.getState().ready || false;
},
offline: this.offline,
};
}
// run callback if it's offline and ready for the first time
if (
this.offline &&
hasCallback &&
this.store.getState().ready &&
!this.subscriptions[uniqueName].ready
) {
// handled by meteor.subscribe if online
const callback = _.once(params[params.length - 1]);
callback();
}
this.subscriptions[uniqueName].ready = subHandle.ready();
return subHandle;
}
collection(collection, subscriptionName) {
if (
Meteor.status().connected &&
this.firstConnection &&
_.get(this.subscriptions, `${subscriptionName}.ready`)
) {
this.firstConnection = false;
const t = new Date();
const recentlyAddedIds = this.store.getState()
.reactNativeMeteorOfflineRecentlyAdded;
const cachedIds = _.sortBy(_.keys(this.store.getState()[collection]));
// console.log(`got cached in ${new Date() - t}ms`);
const removed = _.sortBy(_.difference(cachedIds, recentlyAddedIds)) || [];
// console.log(
// `got difference in ${new Date() - t}ms`,
// recentlyAddedIds,
// cachedIds,
// removed,
// this.store.getState().reactNativeMeteorOfflineRecentlyAdded
// );
this.store.dispatch({
type: 'REMOVE_AFTER_RECONNECT',
collection,
removed,
});
}
this.collections = _.uniq([...this.collections, collection]);
return Meteor.collection(collection);
}
}
export { meteorReduxReducers, subscribeCached, returnCached, MeteorOffline };
export default initMeteorRedux;