forked from DesignmanIO/react-native-meteor-offline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectMeteorRedux.js
190 lines (178 loc) · 5.96 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
/**
* Created by Julian on 12/30/16.
*/
import Meteor, {
getData
} from 'react-native-meteor';
import {
createStore
} from 'redux';
import _ from 'lodash';
import EventEmitter from 'events';
import nextFrame from 'next-frame';
const meteorReduxReducers = (state = {}, action) => {
const {type, collection, id, fields} = action;
switch (type) {
case 'ADDED':
// collection doesn't exist yet, add it
if (!state[collection]) {
return {
...state,
[collection]: {
[id]: fields,
},
};
// no doc with _id exists yet
} else if (!state[collection][id]) {
return {
...state,
[collection]: {
...state[collection],
[id]: fields,
},
};
// duplicate found, update it
} else if (state[collection] && state[collection][id]){
// console.warn(`${id} not added to ${collection}, duplicate found`);
return {
...state,
[collection]: {
...state[collection],
[id]: {...fields, ...state[collection][id]},
}
};
}
return state;
case 'CHANGED':
return {
...state,
[collection]: {
...state[collection],
[id]: _.merge(state[collection][id], fields),
}
};
case 'REMOVED':
if (state[collection][id]) {
const withoutDoc = {...state};
delete withoutDoc.collection.id;
return withoutDoc;
return withoutDoc;
}
// console.error(`Couldn't remove ${id}, not found in ${collection} collection`);
return state;
case 'SET_READY':
return {
...state,
ready: action.ready,
}
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 = (preloadedState = undefined, enhancer = undefined) => {
// console.log(preloadedState, enhancer);
const MeteorStore = createStore(meteorReduxReducers, 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', async (obj) => {
const {collection, id} = obj;
const fields = obj.fields || {};
await nextFrame();
MeteorStore.dispatch({type: 'REMOVED', collection, id, fields});
});
Meteor.ddp.on('changed', async (obj) => {
const {collection, id} = obj;
const fields = obj.fields || {};
await nextFrame();
MeteorStore.dispatch({type: 'CHANGED', collection, id, fields});
});
Meteor.ddp.on('added', async (obj) => {
const {collection, id} = obj;
const fields = obj.fields || {};
fields._id = id;
const getCollection = MeteorStore.getState()[collection];
if(
!getCollection ||
!getCollection[id] ||
!_.isEqual(getCollection[id], fields)
){
MeteorStore.dispatch({type: 'ADDED', collection, id, fields});
}
});
}
});
return MeteorStore;
};
class MeteorStore {
constructor(props) {
}
}
const subscribeCached = (store, name, ...args) => {
Meteor.waitDdpConnected(() => {
if (Meteor.ddp.status === 'connected') {
return Meteor.subscribe(name, ...args);
}
});
// fallback if store not initialized
if (!store) return Meteor.subscribe(name, ...args);
// if callback exists, run it
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,
};
};
returnCached = (cursor, store, collectionName, doDisable) => {
if (Meteor.ddp && Meteor.ddp.status === 'disconnected') {
return store.getState()[collectionName] || [];
}
return cursor;
}
export {
meteorReduxReducers,
subscribeCached,
returnCached
};
export default initMeteorRedux;
// export default connectMeteorRedux;