From 6c0fe35ee143f6a5990f906095463eadd97badf2 Mon Sep 17 00:00:00 2001 From: Craig Beck Date: Wed, 22 May 2024 10:54:15 -0700 Subject: [PATCH 1/4] Pass id of newly created doc in callback form add and resolve with id for addPromised --- src/Model/mutators.ts | 30 ++++++++++++++++++++---------- test/Model/mutators.js | 23 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 test/Model/mutators.js diff --git a/src/Model/mutators.ts b/src/Model/mutators.ts index b9029ade..76365e70 100644 --- a/src/Model/mutators.ts +++ b/src/Model/mutators.ts @@ -10,6 +10,8 @@ var RemoveEvent = mutationEvents.RemoveEvent; var MoveEvent = mutationEvents.MoveEvent; var promisify = util.promisify; +type ValueCallback = ((error: Error | null | undefined, value?: T) => void); + declare module './Model' { interface Model { _mutate(segments, fn, cb): void; @@ -43,11 +45,11 @@ declare module './Model' { createNullPromised(subpath: Path, value: any): Promise; _createNull(segments: Segments, value: any, cb?: ErrorCallback): void; - add(value: any, cb?: ErrorCallback): string; - add(subpath: Path, value: any, cb?: ErrorCallback): string; + add(value: any, cb?: ValueCallback): string; + add(subpath: Path, value: any, cb?: ValueCallback): string; addPromised(value: any): Promise; addPromised(subpath: Path, value: any): Promise; - _add(segments: Segments, value: any, cb?: ErrorCallback): string; + _add(segments: Segments, value: any, cb?: ValueCallback): string; /** * Deletes the value at this model's path or a relative subpath. @@ -404,20 +406,23 @@ Model.prototype.add = function() { var segments = this._splitPath(subpath); return this._add(segments, value, cb); }; -Model.prototype.addPromised = promisify(Model.prototype.add); +Model.prototype.addPromised = promisify(Model.prototype.add); Model.prototype._add = function(segments, value, cb) { if (typeof value !== 'object') { var message = 'add requires an object value. Invalid value: ' + value; cb = this.wrapCallback(cb); - return cb(new Error(message)); + cb(new Error(message)); + return; } - var id = value.id || this.id(); + + const id = value.id || this.id(); value.id = id; segments = this._dereference(segments.concat(id)); - var model = this; + const model = this; + function add(doc, docSegments, fnCb) { - var previous; + let previous; if (docSegments.length) { previous = doc.set(docSegments, value, fnCb); } else { @@ -426,10 +431,15 @@ Model.prototype._add = function(segments, value, cb) { // it being stored in the database by ShareJS value = doc.get(); } - var event = new ChangeEvent(value, previous, model._pass); + const event = new ChangeEvent(value, previous, model._pass); model._emitMutation(segments, event); } - this._mutate(segments, add, cb); + + const callbackWithId = (cb != null) + ? (err: Error) => { cb(err, id); } + : null; + + this._mutate(segments, add, callbackWithId); return id; }; diff --git a/test/Model/mutators.js b/test/Model/mutators.js new file mode 100644 index 00000000..6d8493c2 --- /dev/null +++ b/test/Model/mutators.js @@ -0,0 +1,23 @@ +const {expect} = require('chai'); +const {RootModel} = require('../../lib/Model'); + +describe('mutatotrs', () => { + describe('add', () => { + const guidRegExp = new RegExp(/[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}/); + it('returns created id in callback', () => { + const model = new RootModel(); + model.add('_test_doc', {name: 'foo'}, (error, id) => { + expect(error).to.not.exist; + expect(id).not.to.be.undefined; + expect(id).to.match(guidRegExp, 'Excpected a GUID-like Id'); + }); + }); + + it('resolves promised add with id', async () => { + const model = new RootModel(); + const id = await model.addPromised('_test_doc', {name: 'bar'}); + expect(id).not.to.be.undefined; + expect(id).to.match(guidRegExp, 'Excpected a GUID-like Id'); + }); + }); +}); From f1d9dc76aa98f4d19ca86599db83aff788503bb1 Mon Sep 17 00:00:00 2001 From: Craig Beck Date: Thu, 23 May 2024 13:25:16 -0700 Subject: [PATCH 2/4] Typo fixes --- test/Model/mutators.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Model/mutators.js b/test/Model/mutators.js index 6d8493c2..589ba3d9 100644 --- a/test/Model/mutators.js +++ b/test/Model/mutators.js @@ -1,7 +1,7 @@ const {expect} = require('chai'); const {RootModel} = require('../../lib/Model'); -describe('mutatotrs', () => { +describe('mutators', () => { describe('add', () => { const guidRegExp = new RegExp(/[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}/); it('returns created id in callback', () => { @@ -9,7 +9,7 @@ describe('mutatotrs', () => { model.add('_test_doc', {name: 'foo'}, (error, id) => { expect(error).to.not.exist; expect(id).not.to.be.undefined; - expect(id).to.match(guidRegExp, 'Excpected a GUID-like Id'); + expect(id).to.match(guidRegExp, 'Expected a GUID-like Id'); }); }); @@ -17,7 +17,7 @@ describe('mutatotrs', () => { const model = new RootModel(); const id = await model.addPromised('_test_doc', {name: 'bar'}); expect(id).not.to.be.undefined; - expect(id).to.match(guidRegExp, 'Excpected a GUID-like Id'); + expect(id).to.match(guidRegExp, 'Expected a GUID-like Id'); }); }); }); From 8ec99d1825149938db77e1e429a52fa868ef31fc Mon Sep 17 00:00:00 2001 From: Craig Beck Date: Thu, 23 May 2024 13:35:26 -0700 Subject: [PATCH 3/4] Change to use let --- src/Model/mutators.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/mutators.ts b/src/Model/mutators.ts index 76365e70..789659b2 100644 --- a/src/Model/mutators.ts +++ b/src/Model/mutators.ts @@ -410,7 +410,7 @@ Model.prototype.addPromised = promisify(Model.prototype.add); Model.prototype._add = function(segments, value, cb) { if (typeof value !== 'object') { - var message = 'add requires an object value. Invalid value: ' + value; + let message = 'add requires an object value. Invalid value: ' + value; cb = this.wrapCallback(cb); cb(new Error(message)); return; From 4e19a9ca9ca7315ab305fe72885413c0bd51ff9e Mon Sep 17 00:00:00 2001 From: Craig Beck Date: Thu, 23 May 2024 16:34:57 -0700 Subject: [PATCH 4/4] Change ValueCallback value to non-optional; rename wrapped callback in _add --- src/Model/mutators.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Model/mutators.ts b/src/Model/mutators.ts index 789659b2..55b4789e 100644 --- a/src/Model/mutators.ts +++ b/src/Model/mutators.ts @@ -10,7 +10,7 @@ var RemoveEvent = mutationEvents.RemoveEvent; var MoveEvent = mutationEvents.MoveEvent; var promisify = util.promisify; -type ValueCallback = ((error: Error | null | undefined, value?: T) => void); +type ValueCallback = ((error: Error | null | undefined, value: T) => void); declare module './Model' { interface Model { @@ -411,8 +411,8 @@ Model.prototype.addPromised = promisify(Model.prototype.add); Model.prototype._add = function(segments, value, cb) { if (typeof value !== 'object') { let message = 'add requires an object value. Invalid value: ' + value; - cb = this.wrapCallback(cb); - cb(new Error(message)); + const errorCallback = this.wrapCallback(cb); + errorCallback(new Error(message)); return; }