How to use the fast-json-patch.generate function in fast-json-patch

To help you get started, we’ve selected a few fast-json-patch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github FabricLabs / fabric / types / service.js View on Github external
}

    if (this.store) {
      // TODO: add robust + convenient database opener
      try {
        await this.store.batch(ops, function shareChanges () {
          // TODO: notify status?
        });
      } catch (E) {
        console.error('[FABRIC:SERVICE]', 'Threw Exception:', E);
      }
    }

    if (self.observer) {
      try {
        let patches = manager.generate(self.observer);
        if (patches.length) self.emit('patches', patches);
        if (patches.length) self.emit('message', {
          '@type': 'Commit',
          '@data': patches
        });
      } catch (E) {
        console.error('Could not generate patches:', E);
      }
    }

    return this;
  }
github freeCodeCamp / meeting-for-good / client / components / AvailabilityGrid / AvailabilityGrid.js View on Github external
// need to call the full event to edit... since he dosn't have the
    // info that maybe have a guest "deleted"
    try {
      const eventFull = await loadEvent(event._id, true);
      const observerEvent = jsonpatch.observe(eventFull);
      // find for curUser at the array depends if is a participant
      // yet or not
      const curParticipant =
        isCurParticipantUpsert(curUser, eventFull, availabilityCurUserinQuarters.length);
      // because the patch jsonpatch dosent work as espected when you have a arrays of arrays
      // we need to generate a patch to delete all availability and then add ther availability again
      // then merge both patchs arrays.
      curParticipant.availability = [];
      const patchforDelete = jsonpatch.generate(observerEvent);
      curParticipant.availability = availabilityReducer(availabilityCurUserinQuarters);
      const patchesforAdd = jsonpatch.generate(observerEvent);
      const patches = _.concat(patchforDelete, patchesforAdd);
      await this.props.submitAvail(patches);
    } catch (err) {
      console.log('err at submit avail', err);
    }
  }
github freeCodeCamp / meeting-for-good / client / components / AvailabilityGrid / AvailabilityGrid.js View on Github external
// construct the avaqilabily for the cur user from grid
    const availabilityCurUserinQuarters = AvaliabilityCurUserFromGrid(grid, curUser);
    // need to call the full event to edit... since he dosn't have the
    // info that maybe have a guest "deleted"
    try {
      const eventFull = await loadEvent(event._id, true);
      const observerEvent = jsonpatch.observe(eventFull);
      // find for curUser at the array depends if is a participant
      // yet or not
      const curParticipant =
        isCurParticipantUpsert(curUser, eventFull, availabilityCurUserinQuarters.length);
      // because the patch jsonpatch dosent work as espected when you have a arrays of arrays
      // we need to generate a patch to delete all availability and then add ther availability again
      // then merge both patchs arrays.
      curParticipant.availability = [];
      const patchforDelete = jsonpatch.generate(observerEvent);
      curParticipant.availability = availabilityReducer(availabilityCurUserinQuarters);
      const patchesforAdd = jsonpatch.generate(observerEvent);
      const patches = _.concat(patchforDelete, patchesforAdd);
      await this.props.submitAvail(patches);
    } catch (err) {
      console.log('err at submit avail', err);
    }
  }
github FabricLabs / fabric / types / transition.js View on Github external
_applyTo (state) {
    if (!state) throw new Error('State must be provided.');
    if (!(state instanceof Entity)) throw new Error('State not of known Entity type.');

    let instance = Object.assign({}, state);
    let observer = monitor.observe(instance);

    try {
      monitor.applyPatch(instance, this._state.changes);
    } catch (E) {
      console.error('Could not apply changes:', E);
    }

    let changes = monitor.generate(observer);
    // console.log('changes:', changes);
    return instance;
  }
github colyseus / colyseus / src / serializer / JSONPatchSerializer.ts View on Github external
public hasChanged(newState: any) {
    this.patches = jsonpatch.generate(this.observer);

    const changed = (this.patches.length > 0);

    if (changed) {

      //
      // debugging
      //
      if (debugPatch.enabled) {
        debugPatch('%d bytes, %j', this.patches.length, this.patches);
      }

      this.state = newState;
    }

    return changed;
github FabricLabs / doorman / lib / service.js View on Github external
return this.store.batch(ops, function shareChanges () {
    let patches = manager.generate(self.observer);
    if (patches.length) self.emit('patches', patches);
  });
};
github FabricLabs / fabric / types / state.js View on Github external
commit () {
    ++this.clock;

    this['@parent'] = this.id;
    this['@preimage'] = this.toString();
    this['@constructor'] = this.constructor;

    if (this.observer) {
      this['@changes'] = monitor.generate(this.observer);
    }

    this['@id'] = this.id;

    if (this['@changes'] && this['@changes'].length) {
      this.emit('changes', this['@changes']);
      this.emit('state', this['@state']);
      this.emit('message', {
        '@type': 'Transaction',
        '@data': {
          'changes': this['@changes'],
          'state': this['@changes']
        }
      });
    }
github FabricLabs / fabric / types / machine.js View on Github external
commit () {
    let self = this;
    if (!self.observer) return false;

    let changes = monitor.generate(self.observer);

    if (changes && changes.length) {
      let vector = new State({
        '@type': 'Change',
        '@data': changes,
        method: 'patch',
        parent: self.id,
        params: changes
      });

      if (!self.history) self.history = [];
      self.history.push(vector);

      self.emit('transaction', vector);
    }
github zarxor / IdentityServer4.OpenAdmin / src / UI / ClientApp / pages / Editor.vue View on Github external
saveItem() {
                var url = this.editorOptions.url.replace("[id]", this.itemId);
                this.form.isPosting = true;
                var patchData = jsonpatch.generate(this.itemObserver);

                this.$http
                    .patch(url, patchData)
                    .then(response => {
                        this.item = response.data;
                        this.itemObserver = jsonpatch.observe(this.item);

                        this.snackbar.text = "Saved successfully!"
                        this.snackbar.enabled = true;

                        this.form.hasChanged = false;
                        this.form.isPosting = false;
                    }, response => {
                        this.snackbar.text = "Error saving: " + response.status;
                        this.snackbar.enabled = true;
github colyseus / colyseus / src / State / Observer.js View on Github external
getPatches () {
    if (!this.previousState) {
      return jsonpatch.generate(this.patchObserver)

    } else {
      Object.assign(this.previousState, this.getState())
      return jsonpatch.generate(this.patchObserver)
    }
  }