Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
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;
}
// 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);
}
}
// 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);
}
}
_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;
}
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;
return this.store.batch(ops, function shareChanges () {
let patches = manager.generate(self.observer);
if (patches.length) self.emit('patches', patches);
});
};
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']
}
});
}
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);
}
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;
getPatches () {
if (!this.previousState) {
return jsonpatch.generate(this.patchObserver)
} else {
Object.assign(this.previousState, this.getState())
return jsonpatch.generate(this.patchObserver)
}
}