Unfortunately, getting optimistic updates was a fail because I tried to simulate the update before the method was called as you can see in my code that I commented out, but turns out that a client-side method stub needs to exist and the simulated call needs to take place inside the method stub in order to adhere to how the Meteor optimistic updates function under the hood. I had saveOriginals and retrieveOriginals implemented but deleted the code. This is still doable and I didn't want to move forward until I get your feedback on you would want to consume it. So based on what I just said here are the issues:
You need to create a client file only for the method stubs. This means you'd need the same method in a separate file in the server for the real updates. You can't put it in shared code space, otherwise, that means that the Players Mongo collection object would have to be imported into the client (unless you can import modules conditionally for server only and client only?), which we don't want and can't have because we created a DDP store definition by that name already. Also you would have to import the Redux store into the method, which looks kind of lame.
// client-only file method declaration,
// there would be another one too on the server, with the database implementation
import store from './wherever/my/store/is/exported';
import { simulateUpdate } from './whereever/redux-ddp/is'
Meteor.methods({
'updateScore': function(playerId) {
const { dispatch, getState } = store;
const { collections: { players } } = getState();
dispatch(simulateUpdate(
'players',
playerId,
{ score: players[playerId].score + 5 }
));
}
});
All in all, I'm not so sure this is desirable.
I initially imagined this, but I don't think this is possible?
EDIT: With reify (Meteor 1.3.3) it should now be possible to do the below:
// shared code space, one method declaration
// How can we import modules depending on server or client environment??
if (Meteor.isClient) {
import store from ...
import { simulateUpdate } from ...
} else if (Meteor.isServer) {
import Players from ...
}
Meteor.methods({
'updateScore': function(playerId) {
if (this.isSimulation) {
const { dispatch, getState } = store;
const { collections: { players } = getState();
dispatch(simulateUpdate(
'players',
playerId,
{ score: players[playerId].score + 5 }
));
} else {
Players.update(playerId, { $inc: { score: 5 } });
}
}
});
Unfortunately, getting optimistic updates was a fail because I tried to simulate the update before the method was called as you can see in my code that I commented out, but turns out that a client-side method stub needs to exist and the simulated call needs to take place inside the method stub in order to adhere to how the Meteor optimistic updates function under the hood. I had
saveOriginalsandretrieveOriginalsimplemented but deleted the code. This is still doable and I didn't want to move forward until I get your feedback on you would want to consume it. So based on what I just said here are the issues:You need to create a client file only for the method stubs. This means you'd need the same method in a separate file in the server for the real updates. You can't put it in shared code space, otherwise, that means that the
PlayersMongo collection object would have to be imported into the client (unless you can import modules conditionally for server only and client only?), which we don't want and can't have because we created a DDP store definition by that name already. Also you would have to import the Redux store into the method, which looks kind of lame.All in all, I'm not so sure this is desirable.
I initially imagined this, but I don't think this is possible?
EDIT: With
reify(Meteor 1.3.3) it should now be possible to do the below: