0

I've got a subscriptionObserver including the following:

return update(
    previousResult,
    {
        ApptsForCurrentUser: {
            $push: [newAppt],
        },
    }
);

The new item that arrives via the subscription needs to be inserted into the ApptsForCurrentUser array in date-sorted order. It's a multi-dimensional array, and I can sort it using an $apply function.

Is there syntax to $push newAppt to the array prior to handing the array off to the $apply function that will sort it?

Alternatively, should I do something like this?

(Not yet tested):

var newResult = clonedeep(previousResult);  //lodash
newResult.push(newAppt);
newResult.sort(myCustomSortFunction);
const newResultAsAConst = clonedeep(newResult); 
return update(
    previousResult, newResultAsAConst
);
VikR
  • 4,818
  • 8
  • 51
  • 96
  • That's what I'm doing (push and then sort), using https://github.com/kofrasa/mingo. – Sacha Dec 28 '16 at 01:21
  • Can you post the lines that do the push and then sort, as an answer that I can accept? TIA! – VikR Dec 28 '16 at 02:15

1 Answers1

0

Building on advice from @Sacha, I was able to solve this via this code:

import update from 'immutability-helper';
[.....]

const resultWithNewApptAdded = update(previousResult, {getAllApptsForCurrentUser: {$push: [newAppt]}});
//getAllApptsForCurrentUser contains unsorted list of appts
resultWithNewApptAdded.getAllApptsForCurrentUser = resultWithNewApptAdded.getAllApptsForCurrentUser.sort(mySortFunction);
return resultWithNewApptAdded;
VikR
  • 4,818
  • 8
  • 51
  • 96