findAndModify alternative for latency compensation


#1

For an app I’m building, I want users to be able to select items. When an item is selected, it increments the strength by 1. The range of strength they have is from 0 to 2. If the strength hits 3, it resets itself back down to 0. The package I’m using for findAndModify in meteorjs doesn’t work on the client, which is preventing me from using MeteorJS’s latency compensation. I was wondering if anyone knew an alternative way to write these findAndModify calls to allow for quicker feedback to users

The Meteor Method is here:

    // If an edge exists and the strength is less than or equal to the maximum strength
    // for a label connection, increment it by one
    // increment will equal null if no edge is found
    var increment = Collections.Responses.findAndModify({
        query: {
            _courseID: doc._courseID,
            _resourceID: doc._resourceID,
            _studentID: Meteor.userId(),
            "edges.fromID": doc._resourceID,
            "edges.toID": doc._labelID,
            "edges.strength": {$lte: 2}
        },
        update: {$inc: {'edges.$.strength': 1}}
    });

    // If an edge exists and is incremented past the maximum,
    // set back to 0
    // maxed will equal null if no edge is found
    var maxed = Collections.Responses.findAndModify({
        query: {
            _courseID: doc._courseID,
            _resourceID: doc._resourceID,
            _studentID: Meteor.userId(),
            "edges.fromID": doc._resourceID,
            "edges.toID": doc._labelID,
            "edges.strength": {$eq: 3}
        },
        update: {$inc: {'edges.$.strength': -3}}
    });

Schema Here:

// ==== Response Schema ==== //
Schemas.Response = new SimpleSchema({
    _id: {
        type: String
    },
    edges: {
        type: [Schemas.edge],
        optional: true
    }
});

// ==== Connection Schema ==== //
Schemas.edge = new SimpleSchema({
    fromID: {
        type: String
    },
    toID: {
        type: String
    },
    strength: {
        type: Number
    }
});