@sanity/mutator
Advanced tools
Comparing version 0.105.0-drafts.0 to 0.105.0-drafts.2
@@ -173,2 +173,3 @@ 'use strict'; | ||
// Keep applying mutations as long as any apply | ||
var protect = 0; | ||
do { | ||
@@ -188,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); | ||
@@ -364,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); | ||
@@ -367,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 }); | ||
}); | ||
@@ -90,2 +93,10 @@ } else if (mutation.delete) { | ||
}); | ||
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; | ||
@@ -92,0 +103,0 @@ var rev = this.resultRev || this.transactionId; |
{ | ||
"name": "@sanity/mutator", | ||
"version": "0.105.0-drafts.0", | ||
"version": "0.105.0-drafts.2", | ||
"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 }) | ||
} | ||
} |
348079
100
7775