Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get optimistic updates to work #1

Open
rclai opened this issue Jan 15, 2016 · 5 comments
Open

Get optimistic updates to work #1

rclai opened this issue Jan 15, 2016 · 5 comments
Assignees

Comments

@rclai
Copy link
Owner

rclai commented Jan 15, 2016

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 } });
        }       
    }
});
@rclai rclai self-assigned this Jan 15, 2016
@jbaum98
Copy link

jbaum98 commented Aug 21, 2016

Could you maybe use require() as suggested in the Meteor docs?

Meteor.methods({
    'updateScore': function(playerId) {
        if (this.isSimulation) {
            const { simulateUpdate } = require('/path/to/simulateUpdate.js');
            const { dispatch, getState } = require('/path/to/store.js');
            const { collections: { players }} = getState();
            dispatch(simulateUpdate(
                'players', 
                playerId, 
                { score: players[playerId].score + 5 }
            ));
        } else {
            const Players = require('/path/to/players.js');
            Players.update(playerId, { $inc: { score: 5 } });
        }       
    }
});

@rclai
Copy link
Owner Author

rclai commented Aug 21, 2016

Yeah, that would actually work I think. Also, with the introduction of the reify module it looks like conditional imports are possible now.

I don't think I'll be working on this anytime soon though because the status of whether DDP will still be alive is in the air.

@jbaum98
Copy link

jbaum98 commented Aug 21, 2016

Really? Where does meteor imply they're getting rid of it and what would they be replacing it with?

@rclai
Copy link
Owner Author

rclai commented Aug 22, 2016

They don't say explicitly, but implicitly with the rise of Apollo.

@rclai
Copy link
Owner Author

rclai commented Aug 22, 2016

I'll eventually implement it just for fun even if it goes nowhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants