@sanity/mutator
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -60,7 +60,7 @@ 'use strict'; | ||
this.doc = new _Document2.default(doc); | ||
this.doc.onMutation = function (msg) { | ||
this.document = new _Document2.default(doc); | ||
this.document.onMutation = function (msg) { | ||
return _this.handleDocMutation(msg); | ||
}; | ||
this.doc.onRebase = function (msg) { | ||
this.document.onRebase = function (msg) { | ||
return _this.handleDocRebase(msg); | ||
@@ -84,2 +84,3 @@ }; | ||
this.mutations.push(mutation); | ||
var oldLocal = this.LOCAL; | ||
this.LOCAL = mutation.apply(this.LOCAL); | ||
@@ -112,3 +113,5 @@ if (this.onMutation) { | ||
} | ||
// Collect current staged mutations into a commit and ... | ||
this.commits.push(new Commit(this.mutations)); | ||
// ... clear the table for the next commit. | ||
this.mutations = []; | ||
@@ -149,3 +152,3 @@ this.performCommits(); | ||
var squashed = commit.squash(this.LOCAL); | ||
var docResponder = this.doc.stage(squashed); | ||
var docResponder = this.document.stage(squashed, true); | ||
@@ -170,3 +173,7 @@ var responder = { | ||
}; | ||
this.commitHandler(squashed.mutations, responder); | ||
this.commitHandler({ | ||
mutation: squashed, | ||
success: responder.success, | ||
failure: responder.failure | ||
}); | ||
} | ||
@@ -183,3 +190,3 @@ }, { | ||
if (this.commits.length == 0 && this.mutations.length == 0) { | ||
this.LOCAL = this.doc.EDGE; | ||
this.LOCAL = this.document.EDGE; | ||
if (this.onMutation) { | ||
@@ -198,6 +205,6 @@ this.onMutation(msg); | ||
return commit.apply(doc); | ||
}, this.doc.EDGE); | ||
}, this.document.EDGE); | ||
this.LOCAL = _Mutation2.default.applyAll(this.LOCAL, this.mutations); | ||
// Copy over rev, since we don't care if it changed, we only care about the content | ||
oldLocal._rev = this.EDGE._rev; | ||
oldLocal._rev = this.LOCAL._rev; | ||
var changed = !(0, _isEqual3.default)(this.LOCAL, oldLocal); | ||
@@ -204,0 +211,0 @@ if (changed && this.onRebase) { |
@@ -89,3 +89,3 @@ 'use strict'; | ||
key: 'stage', | ||
value: function stage(mutation) { | ||
value: function stage(mutation, silent) { | ||
var _this = this; | ||
@@ -101,3 +101,3 @@ | ||
if (this.onMutation) { | ||
if (this.onMutation && !silent) { | ||
this.onMutation({ | ||
@@ -104,0 +104,0 @@ mutation: mutation, |
@@ -13,5 +13,5 @@ 'use strict'; | ||
function s4() { | ||
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); | ||
return Math.floor((1 + Math.random()) * 0x10000).toString(36).substring(1); | ||
} | ||
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); | ||
} |
@@ -130,3 +130,3 @@ 'use strict'; | ||
var squashed = mutations.reduce(function (result, mutation) { | ||
return result.concat.apply(result, _toConsumableArray(mutations.mutations)); | ||
return result.concat.apply(result, _toConsumableArray(mutation.mutations)); | ||
}, []); | ||
@@ -133,0 +133,0 @@ return new Mutation({ mutations: squashed }); |
@@ -6,2 +6,7 @@ 'use strict'; | ||
}); | ||
var _compact2 = require('lodash/compact'); | ||
var _compact3 = _interopRequireDefault(_compact2); | ||
exports.default = extract; | ||
@@ -61,3 +66,3 @@ | ||
} | ||
return result; | ||
return (0, _compact3.default)(result); | ||
} |
{ | ||
"name": "@sanity/mutator", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"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", |
@@ -39,5 +39,5 @@ // A wrapper for Document that allows the client to gather mutations on the client side and commit them | ||
constructor(doc) { | ||
this.doc = new Document(doc) | ||
this.doc.onMutation = msg => this.handleDocMutation(msg) | ||
this.doc.onRebase = msg => this.handleDocRebase(msg) | ||
this.document = new Document(doc) | ||
this.document.onMutation = msg => this.handleDocMutation(msg) | ||
this.document.onRebase = msg => this.handleDocRebase(msg) | ||
this.LOCAL = doc | ||
@@ -51,2 +51,3 @@ this.mutations = [] | ||
this.mutations.push(mutation) | ||
const oldLocal = this.LOCAL | ||
this.LOCAL = mutation.apply(this.LOCAL) | ||
@@ -73,3 +74,5 @@ if (this.onMutation) { | ||
} | ||
// Collect current staged mutations into a commit and ... | ||
this.commits.push(new Commit(this.mutations)) | ||
// ... clear the table for the next commit. | ||
this.mutations = [] | ||
@@ -102,3 +105,3 @@ this.performCommits() | ||
const squashed = commit.squash(this.LOCAL) | ||
const docResponder = this.doc.stage(squashed) | ||
const docResponder = this.document.stage(squashed, true) | ||
@@ -121,3 +124,7 @@ const responder = { | ||
} | ||
this.commitHandler(squashed.mutations, responder) | ||
this.commitHandler({ | ||
mutation: squashed, | ||
success: responder.success, | ||
failure: responder.failure | ||
}) | ||
} | ||
@@ -132,3 +139,3 @@ | ||
if (this.commits.length == 0 && this.mutations.length == 0) { | ||
this.LOCAL = this.doc.EDGE | ||
this.LOCAL = this.document.EDGE | ||
if (this.onMutation) { | ||
@@ -144,6 +151,6 @@ this.onMutation(msg) | ||
const oldLocal = this.LOCAL | ||
this.LOCAL = this.commits.reduce((doc, commit) => commit.apply(doc), this.doc.EDGE) | ||
this.LOCAL = this.commits.reduce((doc, commit) => commit.apply(doc), this.document.EDGE) | ||
this.LOCAL = Mutation.applyAll(this.LOCAL, this.mutations) | ||
// Copy over rev, since we don't care if it changed, we only care about the content | ||
oldLocal._rev = this.EDGE._rev | ||
oldLocal._rev = this.LOCAL._rev | ||
const changed = !isEqual(this.LOCAL, oldLocal) | ||
@@ -150,0 +157,0 @@ if (changed && this.onRebase) { |
@@ -73,3 +73,3 @@ // @flow | ||
// submission. | ||
stage(mutation : Mutation) : SubmissionResponder { | ||
stage(mutation : Mutation, silent? : bool) : SubmissionResponder { | ||
if (!mutation.transactionId) { | ||
@@ -83,3 +83,3 @@ throw new Error('Mutations _must_ have transactionId when submitted') | ||
if (this.onMutation) { | ||
if (this.onMutation && !silent) { | ||
this.onMutation({ | ||
@@ -86,0 +86,0 @@ mutation, |
@@ -8,3 +8,3 @@ // Locally unique id's. We use this to generate transaction ids, and they don't have to be cryptographically | ||
return Math.floor((1 + Math.random()) * 0x10000) | ||
.toString(16) | ||
.toString(36) | ||
.substring(1); | ||
@@ -11,0 +11,0 @@ } |
@@ -85,5 +85,5 @@ // @flow | ||
static squash(document : Object, mutations : Array<Mutation>) : Mutation { | ||
const squashed = mutations.reduce((result, mutation) => result.concat(...mutations.mutations), []) | ||
const squashed = mutations.reduce((result, mutation) => result.concat(...mutation.mutations), []) | ||
return new Mutation({mutations: squashed}) | ||
} | ||
} |
// @flow | ||
import PlainProbe from './PlainProbe' | ||
import {Expression, Matcher} from '../jsonpath' | ||
import {compact} from 'lodash' | ||
@@ -42,3 +43,3 @@ export default function extract(path : string, value : Object) : Array<any> { | ||
} | ||
return result | ||
return compact(result) | ||
} |
// @flow | ||
import {test} from 'tap' | ||
import DocTester from './util/DocTester' | ||
import DocumentTester from './util/DocumentTester' | ||
test('simple remote mutation', tap => { | ||
(new DocTester(tap, { | ||
(new DocumentTester(tap, { | ||
_rev: '1', | ||
@@ -29,3 +29,3 @@ title: 'Hello' | ||
test('simple local mutation arrives', tap => { | ||
(new DocTester(tap, { | ||
(new DocumentTester(tap, { | ||
_rev: '1', | ||
@@ -69,3 +69,3 @@ title: 'Hello' | ||
test('local mutation submitted, but remote mutation wins the race and causes a rebase', tap => { | ||
(new DocTester(tap, { | ||
(new DocumentTester(tap, { | ||
_rev: '1', | ||
@@ -112,3 +112,3 @@ count: 1 | ||
test('simple local mutation failing', tap => { | ||
(new DocTester(tap, { | ||
(new DocumentTester(tap, { | ||
_rev: '1', | ||
@@ -144,3 +144,3 @@ title: 'Hello' | ||
test('simple local mutation arriving out of order', tap => { | ||
(new DocTester(tap, { | ||
(new DocumentTester(tap, { | ||
_rev: '1', | ||
@@ -147,0 +147,0 @@ numbers: [] |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
306462
88
6387