@sanity/mutator
Advanced tools
Comparing version 0.104.44 to 0.104.45
@@ -162,15 +162,14 @@ 'use strict'; | ||
if (this.HEAD) { | ||
(function () { | ||
var updatedAt = new Date(_this2.HEAD._updatedAt); | ||
if (_this2.incoming.find(function (mut) { | ||
return mut.timestamp && mut.timestamp < updatedAt; | ||
})) { | ||
_this2.incoming = _this2.incoming.filter(function (mut) { | ||
return mut.timestamp < updatedAt; | ||
}); | ||
} | ||
})(); | ||
var updatedAt = new Date(this.HEAD._updatedAt); | ||
if (this.incoming.find(function (mut) { | ||
return mut.timestamp && mut.timestamp < updatedAt; | ||
})) { | ||
this.incoming = this.incoming.filter(function (mut) { | ||
return mut.timestamp < updatedAt; | ||
}); | ||
} | ||
} | ||
// Keep applying mutations as long as any apply | ||
var protect = 0; | ||
do { | ||
@@ -190,2 +189,5 @@ // Find next mutation that can be applied to HEAD (if any) | ||
mustRebase = mustRebase || this.applyIncoming(nextMut); | ||
if (protect++ > 100) { | ||
throw new Error('Mutator stuck flusing incoming mutations. Probably stuck here:', JSON.stringify(nextMut)); | ||
} | ||
} while (nextMut); | ||
@@ -366,3 +368,5 @@ | ||
// Copy over rev, since we don't care if it changed, we only care about the content | ||
oldEdge._rev = this.EDGE._rev; | ||
if (oldEdge !== null && this.EDGE !== null) { | ||
oldEdge._rev = this.EDGE._rev; | ||
} | ||
var changed = !(0, _isEqual3.default)(this.EDGE, oldEdge); | ||
@@ -369,0 +373,0 @@ if (changed && this.onRebase) { |
@@ -50,3 +50,3 @@ 'use strict'; | ||
if (firstMut) { | ||
this._appliesToMissingDocument = firstMut.create || firstMut.createIfNotExist || firstMut.createOrReplace; | ||
this._appliesToMissingDocument = firstMut.create || firstMut.createIfNotExists || firstMut.createOrReplace; | ||
} else { | ||
@@ -62,15 +62,18 @@ this._appliesToMissingDocument = true; | ||
value: function compile() { | ||
var _this = this; | ||
var operations = []; | ||
this.mutations.forEach(function (mutation) { | ||
if (mutation.create) { | ||
// TODO: Fail entire patch if document did exist | ||
operations.push(function (doc) { | ||
return doc === null ? mutation.create : doc; | ||
return doc === null ? Object.assign(mutation.create, { _createdAt: _this.params.timestamp }) : doc; | ||
}); | ||
} else if (mutation.createIfNotExist) { | ||
} else if (mutation.createIfNotExists) { | ||
operations.push(function (doc) { | ||
return doc === null ? mutation.createIfNotExist : doc; | ||
return doc === null ? Object.assign(mutation.createIfNotExists, { _createdAt: _this.params.timestamp }) : doc; | ||
}); | ||
} else if (mutation.createOrReplace) { | ||
operations.push(function () { | ||
return mutation.createOrReplace; | ||
return Object.assign(mutation.createOrReplace, { _createdAt: _this.params.timestamp }); | ||
}); | ||
@@ -82,8 +85,6 @@ } else if (mutation.delete) { | ||
} else if (mutation.patch) { | ||
(function () { | ||
var patch = new _patch.Patcher(mutation.patch); | ||
operations.push(function (doc) { | ||
return patch.apply(doc); | ||
}); | ||
})(); | ||
var patch = new _patch.Patcher(mutation.patch); | ||
operations.push(function (doc) { | ||
return patch.apply(doc); | ||
}); | ||
} else { | ||
@@ -93,2 +94,10 @@ throw new Error('Unsupported mutation ' + JSON.stringify(mutation, null, 2)); | ||
}); | ||
if (typeof this.params.timestamp === 'string') { | ||
operations.push(function (doc) { | ||
if (doc) { | ||
return Object.assign(doc, { _updatedAt: _this.params.timestamp }); | ||
} | ||
return doc; | ||
}); | ||
} | ||
var prevRev = this.previousRev; | ||
@@ -95,0 +104,0 @@ var rev = this.resultRev || this.transactionId; |
@@ -157,12 +157,10 @@ 'use strict'; | ||
if (descender.tail) { | ||
(function () { | ||
// Not arrived yet | ||
var matcher = new Matcher(descender.descend(), _this2); | ||
descender.head.toFieldReferences().forEach(function (field) { | ||
leads.push({ | ||
target: descender.head, | ||
matcher: matcher | ||
}); | ||
// Not arrived yet | ||
var matcher = new Matcher(descender.descend(), _this2); | ||
descender.head.toFieldReferences().forEach(function (field) { | ||
leads.push({ | ||
target: descender.head, | ||
matcher: matcher | ||
}); | ||
})(); | ||
}); | ||
} else { | ||
@@ -176,22 +174,20 @@ // arrived | ||
if (this.hasRecursives()) { | ||
(function () { | ||
// The recustives matcher will have no active set, only inherit recursives from this | ||
var recursivesMatcher = new Matcher([], _this2); | ||
if (probe.containerType() == 'array') { | ||
var length = probe.length(); | ||
for (var i = 0; i < length; i++) { | ||
leads.push({ | ||
target: _Expression2.default.indexReference(i), | ||
matcher: recursivesMatcher | ||
}); | ||
} | ||
} else if (probe.containerType() == 'object') { | ||
probe.attributeKeys().forEach(function (name) { | ||
leads.push({ | ||
target: _Expression2.default.attributeReference(name), | ||
matcher: recursivesMatcher | ||
}); | ||
// The recustives matcher will have no active set, only inherit recursives from this | ||
var recursivesMatcher = new Matcher([], this); | ||
if (probe.containerType() == 'array') { | ||
var length = probe.length(); | ||
for (var i = 0; i < length; i++) { | ||
leads.push({ | ||
target: _Expression2.default.indexReference(i), | ||
matcher: recursivesMatcher | ||
}); | ||
} | ||
})(); | ||
} else if (probe.containerType() == 'object') { | ||
probe.attributeKeys().forEach(function (name) { | ||
leads.push({ | ||
target: _Expression2.default.attributeReference(name), | ||
matcher: recursivesMatcher | ||
}); | ||
}); | ||
} | ||
} | ||
@@ -198,0 +194,0 @@ |
@@ -7,4 +7,2 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -140,34 +138,26 @@ | ||
value: function tokenizeQuoted() { | ||
var _this3 = this; | ||
var quote = this.peek(); | ||
if (quote == "'" || quote == '"') { | ||
var _ret2 = function () { | ||
_this3.consume(quote); | ||
var escape = false; | ||
var inner = _this3.takeWhile(function (char) { | ||
if (escape) { | ||
escape = false; | ||
return char; | ||
} | ||
if (char == '\\') { | ||
escape = true; | ||
return ''; | ||
} | ||
if (char != quote) { | ||
return char; | ||
} | ||
return null; | ||
}); | ||
_this3.consume(quote); | ||
return { | ||
v: { | ||
type: 'quoted', | ||
value: inner, | ||
quote: quote == '"' ? 'double' : 'single' | ||
} | ||
}; | ||
}(); | ||
if ((typeof _ret2 === 'undefined' ? 'undefined' : _typeof(_ret2)) === "object") return _ret2.v; | ||
this.consume(quote); | ||
var _escape = false; | ||
var inner = this.takeWhile(function (char) { | ||
if (_escape) { | ||
_escape = false; | ||
return char; | ||
} | ||
if (char == '\\') { | ||
_escape = true; | ||
return ''; | ||
} | ||
if (char != quote) { | ||
return char; | ||
} | ||
return null; | ||
}); | ||
this.consume(quote); | ||
return { | ||
type: 'quoted', | ||
value: inner, | ||
quote: quote == '"' ? 'double' : 'single' | ||
}; | ||
} | ||
@@ -228,3 +218,3 @@ return null; | ||
value: function tokenizeSymbol() { | ||
var _this4 = this; | ||
var _this3 = this; | ||
@@ -235,3 +225,3 @@ var result = null; | ||
var found = patterns.find(function (pattern) { | ||
return _this4.tryConsume(pattern); | ||
return _this3.tryConsume(pattern); | ||
}); | ||
@@ -238,0 +228,0 @@ if (found) { |
{ | ||
"name": "@sanity/mutator", | ||
"version": "0.104.44", | ||
"version": "0.104.45", | ||
"description": "A set of models to make it easier to utilize the powerful real time collaborative features of Sanity", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -138,2 +138,3 @@ // @flow | ||
// Keep applying mutations as long as any apply | ||
let protect = 0 | ||
do { | ||
@@ -149,2 +150,5 @@ // Find next mutation that can be applied to HEAD (if any) | ||
mustRebase = mustRebase || this.applyIncoming(nextMut) | ||
if (protect++ > 100) { | ||
throw new Error('Mutator stuck flusing incoming mutations. Probably stuck here:', JSON.stringify(nextMut)) | ||
} | ||
} while (nextMut) | ||
@@ -297,3 +301,5 @@ | ||
// Copy over rev, since we don't care if it changed, we only care about the content | ||
oldEdge._rev = this.EDGE._rev | ||
if (oldEdge !== null && this.EDGE !== null) { | ||
oldEdge._rev = this.EDGE._rev | ||
} | ||
const changed = !isEqual(this.EDGE, oldEdge) | ||
@@ -300,0 +306,0 @@ if (changed && this.onRebase) { |
// @flow | ||
import {Patcher} from '../patch' | ||
import { Patcher } from '../patch' | ||
import luid from './luid' | ||
@@ -12,35 +12,36 @@ import debug from './debug' | ||
export default class Mutation { | ||
params : { | ||
transactionId : string, | ||
transition : string, | ||
identity : string, | ||
previousRev : string, | ||
resultRev : string, | ||
mutations : Array<Object>, | ||
timestamp: String, | ||
params: { | ||
transactionId: string, | ||
transition: string, | ||
identity: string, | ||
previousRev: string, | ||
resultRev: string, | ||
mutations: Array<Object>, | ||
timestamp: String | ||
} | ||
compiled : Function | ||
constructor(options : Object) { | ||
compiled: Function | ||
_appliesToMissingDocument: boolean | ||
constructor(options: Object) { | ||
this.params = options | ||
} | ||
get transactionId() : string { | ||
get transactionId(): string { | ||
return this.params.transactionId | ||
} | ||
get transition() : string { | ||
get transition(): string { | ||
return this.params.transition | ||
} | ||
get identity() : string { | ||
get identity(): string { | ||
return this.params.identity | ||
} | ||
get previousRev() : string { | ||
get previousRev(): string { | ||
return this.params.previousRev | ||
} | ||
get resultRev() : string { | ||
get resultRev(): string { | ||
return this.params.resultRev | ||
} | ||
get mutations() : Array<Object> { | ||
get mutations(): Array<Object> { | ||
return this.params.mutations | ||
} | ||
get timestamp() : Date { | ||
get timestamp(): Date { | ||
if (typeof this.params.timestamp == 'string') { | ||
@@ -61,3 +62,6 @@ return new Date(this.params.timestamp) | ||
if (firstMut) { | ||
this._appliesToMissingDocument = (firstMut.create || firstMut.createIfNotExist || firstMut.createOrReplace) | ||
this._appliesToMissingDocument = | ||
firstMut.create || | ||
firstMut.createIfNotExists || | ||
firstMut.createOrReplace | ||
} else { | ||
@@ -73,7 +77,12 @@ this._appliesToMissingDocument = true | ||
if (mutation.create) { | ||
operations.push(doc => (doc === null ? mutation.create : doc)) | ||
} else if (mutation.createIfNotExist) { | ||
operations.push(doc => (doc === null ? mutation.createIfNotExist : doc)) | ||
// TODO: Fail entire patch if document did exist | ||
operations.push( | ||
doc => (doc === null ? Object.assign(mutation.create, {_createdAt: this.params.timestamp}) : doc) | ||
) | ||
} else if (mutation.createIfNotExists) { | ||
operations.push( | ||
doc => (doc === null ? Object.assign(mutation.createIfNotExists, {_createdAt: this.params.timestamp}) : doc) | ||
) | ||
} else if (mutation.createOrReplace) { | ||
operations.push(() => mutation.createOrReplace) | ||
operations.push(() => Object.assign(mutation.createOrReplace, {_createdAt: this.params.timestamp})) | ||
} else if (mutation.delete) { | ||
@@ -85,5 +94,15 @@ operations.push(() => null) | ||
} else { | ||
throw new Error(`Unsupported mutation ${JSON.stringify(mutation, null, 2)}`) | ||
throw new Error( | ||
`Unsupported mutation ${JSON.stringify(mutation, null, 2)}` | ||
) | ||
} | ||
}) | ||
if (typeof this.params.timestamp === 'string') { | ||
operations.push(doc => { | ||
if (doc) { | ||
return Object.assign(doc, {_updatedAt: this.params.timestamp}) | ||
} | ||
return doc | ||
}) | ||
} | ||
const prevRev = this.previousRev | ||
@@ -93,5 +112,10 @@ const rev = this.resultRev || this.transactionId | ||
if (prevRev && prevRev != doc._rev) { | ||
throw new Error(`Previous revision for this mutation was ${prevRev}, but the document revision is ${doc._rev}`) | ||
throw new Error( | ||
`Previous revision for this mutation was ${prevRev}, but the document revision is ${doc._rev}` | ||
) | ||
} | ||
const result = operations.reduce((revision, operation) => operation(revision), doc) | ||
const result = operations.reduce( | ||
(revision, operation) => operation(revision), | ||
doc | ||
) | ||
if (result && rev) { | ||
@@ -103,4 +127,6 @@ result._rev = rev | ||
} | ||
apply(document : Object) : Object { | ||
debug(`Applying mutation ${JSON.stringify(this.mutations)} to document ${JSON.stringify(document)}`) | ||
apply(document: Object): Object { | ||
debug( | ||
`Applying mutation ${JSON.stringify(this.mutations)} to document ${JSON.stringify(document)}` | ||
) | ||
if (!this.compiled) { | ||
@@ -113,3 +139,3 @@ this.compile() | ||
} | ||
static applyAll(document : Object, mutations : Array<Mutation>) : Object { | ||
static applyAll(document: Object, mutations: Array<Mutation>): Object { | ||
return mutations.reduce((doc, mutation) => mutation.apply(doc), document) | ||
@@ -121,6 +147,9 @@ } | ||
// TOOO: Optimize mutations, eliminating mutations that overwrite themselves! | ||
static squash(document : Object, mutations : Array<Mutation>) : Mutation { | ||
const squashed = mutations.reduce((result, mutation) => result.concat(...mutation.mutations), []) | ||
return new Mutation({mutations: squashed}) | ||
static squash(document: Object, mutations: Array<Mutation>): Mutation { | ||
const squashed = mutations.reduce( | ||
(result, mutation) => result.concat(...mutation.mutations), | ||
[] | ||
) | ||
return new Mutation({ mutations: squashed }) | ||
} | ||
} |
348071
100
7775