Skip to content

help about: "copying value lets Flow guarantee the pointer isn't modified elsewhere" #443

@ghost

Description

I would be obliged if one explain AddUniqueOp.applyTo comment:
https://github.com/parse-community/Parse-SDK-JS/blob/master/src/ParseOp.js#L199

applyTo(value: mixed | Array<mixed>): Array<mixed> {
    if (value == null) {
            return this._value || [];
    }
    if (Array.isArray(value)) {
      // copying value lets Flow guarantee the pointer isn't modified elsewhere
      var valueCopy = value;
      var toAdd = [];
      this._value.forEach((v) => {
        if (v instanceof ParseObject) {
          if (!arrayContainsObject(valueCopy, v)) {
            toAdd.push(v);
          }
        } else {
          if (valueCopy.indexOf(v) < 0) {
            toAdd.push(v);
          }
        }
      })
      return value.concat(toAdd);
    }
    throw new Error('Cannot add elements to a non-array value');
}

why we need valueCopy reference?

var valueCopy = value;

and why we need copy technique?

thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions