swarm icon indicating copy to clipboard operation
swarm copied to clipboard

How to get new item from list ?

Open riston opened this issue 9 years ago • 7 comments

I am using following model:

ItemList is a vector, which consists of model items. When listen to the "items" changes I get back the reference to object but the object itself is empty.

This case occurs when User A adds object the other User B session listens the event but it's empty or not yet synchronized. How to fix such issue ?

var items = host.get("/ItemList#items");

items.on("init", ....)
items.on(function (spec, val, source) {
   // This gets the correct type of object but, the properties are default values
  var item = items.getObject(val);
});

riston avatar Apr 26 '15 19:04 riston

Try using onObjectStateReady(cb) instead of on("init", cb) to catch the moment when vector is synchronized. Use onObjectEvent(cb) to handle all list items' events.

abalandin avatar Apr 27 '15 09:04 abalandin

@abalandin thank you for the tip! One scenario more, when the server is down and for example on restart or crash the clients try to reconnect. Server is back online and then the "reon" event is thrown, should the items.onObjectEvent(cb) be also called ?

riston avatar May 01 '15 07:05 riston

A

  • Shane Sizer

On Fri, May 1, 2015 at 2:35 AM, Risto Novik [email protected] wrote:

@abalandin thank you for the tip! One scenario more, when the server is down and for example on restart or crash the clients try to reconnect. Server is back online and then the "reon" event is thrown, should the items.onObjectEvent(cb) be also called ?

Reply to this email directly or view it on GitHub: https://github.com/gritzko/swarm/issues/54#issuecomment-98066528

sksizer avatar May 01 '15 12:05 sksizer

Guys, if you have any suggestions regarding the API, it is the right time.

gritzko avatar May 01 '15 13:05 gritzko

@riston Events are emitted once operations arrive. Even if the server is dead, ops still may arrive. For example, from another tab in the same browser. (Not sure I fully understand the question.)

gritzko avatar May 01 '15 13:05 gritzko

@gritzko, @abalandin could you explain more about the flow behind when and what event is thrown ?

For the collection there is a mixin called CollectionMethodsMixin with following methods:

  • onObjectEvent - Subscribe on collections entries events
  • offObjectEvent - Unsubscribe from collections entries' events
  • onObjectStateReady - Waits for collection to receive state from cache or uplink and then invokes passed callback

When the server crashes or client side user is in offline state. Later when the connection is restored or the server is restarted. What are the events which are thrown on the client, how to get list of objects that have changed ?

The Host module has listeners for state changes reon``,reoff,on,off```, what about the collection based objects ?

riston avatar May 03 '15 12:05 riston

Regarding server crash, that is no different from a regular case. The RTT is a bit longer, of course, but op delivery is asynchronous in either case. I mean, it may take 1 second or 1 hour to deliver an event, but the process is the same. On reconnection, everything that arrived will be emitted. 0.4 has a planned feature of delivering such event bursts in batches, like

    model.on('set+', function (ev) {
        if (ev.constructor===Array) {
            // that is an event batch
        }
    });

gritzko avatar May 14 '15 06:05 gritzko