ReactiveArray icon indicating copy to clipboard operation
ReactiveArray copied to clipboard

How best to update ReactiveArray from autorun?

Open t3db0t opened this issue 8 years ago • 8 comments

I want to synchronize a database array with a client array, so I want to do something like...

Template.dayModal.onCreated(function(){
  this.myAvailabilities = new ReactiveArray();

  var self = this;
  this.autorun(function(){
    var myDateInfo = Groups.findOne etc etc...;
    // ideally:
    self.myAvailabilities.set(myDateInfo.availabilityArray);
  });
});

Should I just clear the ReactiveArray, iterate through the source array and push each onto the ReactiveArray?

t3db0t avatar Oct 13 '16 20:10 t3db0t

I think you should use the collection directly and not through a reactive array. In your case it looks like the array is just a pass-through.

ManuelDeLeon avatar Oct 13 '16 21:10 ManuelDeLeon

That's precisely what I was GOING to do, but I need to update this array (and at least one other reactively) via a click-and-drag user interaction, so there's no way I can do that fast enough with minimongo. Thoughts?

t3db0t avatar Oct 13 '16 21:10 t3db0t

there's no way I can do that fast enough with minimongo

Just how many times per second are you going to insert/update the collection? Why?

ManuelDeLeon avatar Oct 13 '16 21:10 ManuelDeLeon

Heh, I'm saying I would never try to update the collection with that kind of frequency. I need to update the UI reactively, though, while dragging. I could do it by hand (so to speak), but I wanted to leverage the reactive components.

t3db0t avatar Oct 13 '16 21:10 t3db0t

I only need to update the collection on mouseup, but need to update the UI while dragging.

t3db0t avatar Oct 13 '16 21:10 t3db0t

I'm very confused. What happens while you're dragging the mouse? Are you updating the array?

btw, I think the (uninformed) answer to your question is "clear the array and add the values again".

ManuelDeLeon avatar Oct 13 '16 21:10 ManuelDeLeon

onmousemove: calculate a bunch of coordinates and update (reactive) array onmouseup: write final positions to collection

Elsewhere, there are two helpers that each read from this array to draw the UI, and that needs to happen reactively, of course.

I got this working with ReactiveArray, but it's really slow; I haven't audited it yet.

t3db0t avatar Oct 13 '16 21:10 t3db0t

I don't know. I do stuff like that all the time but I never have to insert/update an array on mouse move.

ManuelDeLeon avatar Oct 13 '16 21:10 ManuelDeLeon