Comments (9)
In fact it already does that, but it's complicated... When it's online, it persists data with every DDP change. When it's offline, the persisted data is reinserted into minimongo. So the data is there, even after the connection resumes. The difficulty is knowing when to tell your component when the subscription is 'ready', and when to return the Meteor subscription, as opposed to the 'redux subscription' (it's not really a redux subscription, but I mean the redux data that's persisted). If you ignore the ready
function entirely, you would probably see the data load into your components as soon as the persisted data restores, but as the subscription is doing stuff, it might be unresponsive. So... I'm not sure where to go from here. Here's the current logic:
const subscribeCached = (store, name, ...args) => {
// if connected, return normal subscription
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();
}
// not connected (yet), use offline collection
return {
ready: () => {return store.getState().ready || false},
offline: true,
};
};
from react-native-meteor-offline.
@JulianKingman As far as I understand, now it uses store to actually get data only while offline (only then docs from store get injected to minimongo). If some docs were inserted to minimongo from store, and, later, while having internet connection, the subscription to these docs was stopped (e.g. due to component unmount) the docs would be removed from minimongo, nevertheless there're still such docs in store, wouldn't they? And, when after that you run the subscription again, while having internet connection, you'd have to wait for subscription (from meteor server, not from store) to become ready to show those docs once again. What I was talking about is ability to show docs from store, even online, while having to wait for 'real' docs from meteor server. Do you see some ways to achieve it? Can you think of any major caveats of always pulling data from the store only, ignoring Meteor's ready
? I mean smth like that:
const subscribeCached = (store, name, ...args) => {
if (!store) return Meteor.subscribe(name, ...args);
if(typeof args[args.length - 1] === 'function' && store.getState().ready){
const callback = _.once(args[args.length - 1]);
callback();
}
const handle = Meteor.subscribe(name, ...args);
return {
ready: () => {return store.getState().ready || handle.ready() || false},
offline: true,
};
};
In theory that would make us subscribe, but use data only from store, not from 'real' collections, and when the sub is ready data in the store would become updated as well
from react-native-meteor-offline.
Actually, it looks like my 'removed' reducer wasn't working... oops! But if it was, then whenever a doc was removed via ddp, it would be removed from the persisted collection. Unfortunately there's no way to know (via the DDP api) whether an object is removed intentionally, or by removing a subscription, or even by disconnection (a problem I avoid by only listening while there's a connection).
from react-native-meteor-offline.
So, looks like it requires porting subsmanager
from react-native-meteor-offline.
Is there still trouble with the 'REMOVED' reducer in the current version?
Shouldn't it be like
case 'REMOVED':
if (state[collection][id]) {
return {
...state,
[collection]: _.omit(state[collection], [ id ])
};
}
return state;
?
from react-native-meteor-offline.
Something like that. The version on NPM is right, I just pushed it up now.
from react-native-meteor-offline.
Btw, sorry for writing here but didn't find any other way to reach you - is that fall-through in switch intentional? Looks like persist/REHYDRATE
is dispatched by redux-persist
, right? Is it on purpose that in such case we fall through to HARDRESET
(no break
or return
in case
, if we don't follow if
branch)? Where is HARDRESET
used except of this case?
case 'persist/REHYDRATE':
if (typeof Meteor.ddp === 'undefined' || Meteor.ddp.status === 'disconnected') {
return action.payload;
}
case 'HARDRESET':
console.log('hard reset');
return {};
default:
return state;
from react-native-meteor-offline.
Good catch. I'll add a return state
to the persist action. Basically the HARDRESET
is just there in case you need to clear the store out, which I've had to do a couple times while debugging.
from react-native-meteor-offline.
The only thing you need to accomplish this is to not wait for handle.ready()
for displaying your data. The store will be ready almost instantly, so documents will show up before they come from the server, and when docs are finally finished being added
, things should be synced up. I'd recommend somehow showing that documents are offline, though, as elements could change or be removed and a user might not know why.
from react-native-meteor-offline.
Related Issues (20)
- Last Redux State is not written to disk HOT 5
- _.clone(state) or _.cloneDeep(state.prop) HOT 4
- How to write record HOT 2
- [Error] Expected listener to be a function HOT 2
- Crash when subscribing HOT 1
- How to sync inserts/updates after resuming connection HOT 3
- Package not bundling HOT 3
- Unable to resolve module 'redux-persist' when trying to npm link HOT 1
- 'cleared' field ignored in ddp event listeners HOT 3
- Can't get online insert to work HOT 1
- Create user issues HOT 1
- Subsciptions keep on reloading HOT 1
- MO.user() after successful Meteor.logout() then go offline.
- TypeError: undefined is not an object (evaluating '(0, _reactNativeMeteor.getData)().db[collection].remove') HOT 1
- Cleanup method needed when user logged out and new one connected HOT 3
- undefined is not an object (evaluating 'state[collection][id]') HOT 2
- offline property of MeteorOffline is always false
- with expo sdk32 : invalid attempt to spread non-iterable instance HOT 6
- Can you give me the example of a function component? HOT 1
- Upgrade to meteorrn/core HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-meteor-offline.