GithubHelp home page GithubHelp logo

Optimistic updates about horizon HOT 13 OPEN

rethinkdb avatar rethinkdb commented on June 14, 2024 2
Optimistic updates

from horizon.

Comments (13)

marshall007 avatar marshall007 commented on June 14, 2024

@dalanmiller correct me if I'm wrong, but it seems like this could be accomplished simply by merging the response from collection.store(...) into the original object?

var todo = {
  task: "babelify everything",
  completed: false
};

todos.store(todo).then((result) => {
  Object.assign(todo, result);
  console.log(todo.id) // -> <uuid_from_server>
})

Given that this is so easy to implement in user-land, I kinda don't think we shouldn't do it by default. Especially since it relies on the assumption that people aren't reusing the object they originally passed in for other purposes.

That said, in framework-specific wrappers like for Angular or React, it probably does make sense. I just don't think we should in the core Fusion client.

from horizon.

coffeemug avatar coffeemug commented on June 14, 2024

I think the real problem with generating the ID on the server is that it makes optimistic updates really hard (though I'm not 100% sure if there might be an easy way around this).

from horizon.

dalanmiller avatar dalanmiller commented on June 14, 2024

Can you elucidate that @coffeemug ?

from horizon.

deontologician avatar deontologician commented on June 14, 2024

If the id is created on the server, you have to wait for the round trip in order to find out what the id was, or you have to be able to predict what the id will be (maybe a hash of some information the client has... some other hare-brained scheme)

from horizon.

dalanmiller avatar dalanmiller commented on June 14, 2024

Ahh sorry, I read @coffeemug's comment backwards before ingesting caffeine, I read creating ids on the client.

But if we do have a means of creating an id on the client with a wide enough key space that collisions aren't a realistic problem, this wouldn't be such an issue right? This is what I've been doing in the quick examples I created under the /examples dir using a javascript UUID generator snippet I found in the React docs.

from horizon.

deontologician avatar deontologician commented on June 14, 2024

I agree, client UUIDs shouldn't be a problem, and it is the easiest way to allow optimistic updates

from horizon.

deontologician avatar deontologician commented on June 14, 2024

We could codify this when we implement #52, we either use the id that the user created or we generate a new one client-side when creating a new document. Something like:

var myDoc = {foo: bar, id: 123}
var myDoc2 = {foo: 'bar'}
fusion.createAndKeepUpdated(myDoc) // id: 123 inserted
fusion.createAndKeepUpdated(myDoc2) // client-side uuid generated

(totally bogus hypothetical api notwithstanding)

from horizon.

dalanmiller avatar dalanmiller commented on June 14, 2024

That's a good point @deontologician. What if I want to "insert and attach` versus just sync between db and view which is the functionality suggested in #52.

// Insert a document and keep it updated
d = {id: 1, text: "Hello"};
collection.insert(d).attach(); // Modifications on the server come back and update `d` 

// versus

d = {id: 1, text: "Hello"};
collection.insert(d);
collection.find(d).subscribe().attach(d);

from horizon.

deontologician avatar deontologician commented on June 14, 2024

Originally posted by @danielcompton in issue #42

Firebase, Relay, and Meteor have built in latency compensation in their client side libraries. Updates are optimistically applied, and then 'committed' client-side once the confirmation comes back from the server.

As we discovered in our RethinkDB websockets project (pre-fusion), the richer your query model, the more difficult latency compensation is. We were running full REQL queries on the client, it is very difficult to simulate updates without something like Reqlite. Firebase has the easiest time of it because their query model is so impoverished.

Related links:

http://info.meteor.com/blog/optimistic-ui-with-meteor-latency-compensation
https://github.com/neumino/reqlite
https://facebook.github.io/relay/docs/guides-mutations.html#optimistic-updates
https://www.firebase.com/docs/web/guide/offline-capabilities.html

from horizon.

deontologician avatar deontologician commented on June 14, 2024

For email people: title changed from "Document state boomerang? (Optimistic updates)" to "Optimistic updates"

from horizon.

segphault avatar segphault commented on June 14, 2024

I built a simple example of an app with optimistic updates using Horizon and FreezerJS: https://github.com/segphault/rethinkdb-optimistic-demo/blob/horizon/client/tundra.js

I needed to set the id for new objects on the client side, as expected. I used a simple third-party uuid library from npm for that.

from horizon.

dalanmiller avatar dalanmiller commented on June 14, 2024

Are we still down for adding horizon.uuid() to the client API?

from horizon.

deontologician avatar deontologician commented on June 14, 2024

I don't think this by itself solves optimistic updates quite yet, at least with the default .watch({ rawChanges: false }) setting. Once we have document versioning and timestamps in changefeeds, we should be able to cook something up.

I am ambivalent about putting .uuid() in. Essentially we'll just be re-exporting a dependency, so I don't know if there's any good reason to put it into the Horizon object. Though I don't have a strong opinion

from horizon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.