8000 Switch to passing only the current-change in 'changes' emit · js-data/js-data@d0935c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit d0935c6

Browse files
committed
Switch to passing only the current-change in 'changes' emit
* ref: 372
1 parent fe9d4cb commit d0935c6

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/Schema.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,17 +802,23 @@ const makeDescriptor = function (prop, schema, opts) {
802802
// TODO: Make it so tracking can be turned on for all properties instead of
803803
// only per-property
804804
if (track && !_get(creatingPath)) {
805+
/* previous is versioned on database commit
806+
* props are versioned on set()
807+
* TODO? Option to disable props versioning (commit only versioning)
808+
*/
805809
const previous = _get(previousPath)
806810
const current = _get(keyPath)
807811
let changing = _get(changingPath)
808812
let changed = _get(changedPath)
809813

814+
// IS it possible for changing to be defined but changed undefined?
810815
if (!changing) {
811816
// Track properties that are changing in the current event loop
812817
changed = []
813818
}
814819

815820
// Add changing properties to this array once at most
821+
// Consider making changed into a Set ?
816822
const index = changed.indexOf(prop)
817823
if (current !== value && index === -1) {
818824
changed.push(prop)
@@ -853,7 +859,8 @@ const makeDescriptor = function (prop, schema, opts) {
853859
for (i = 0; i < changed.length; i++) {
854860
this.emit('change:' + changed[i], this, utils.get(this, changed[i]))
855861
}
856-
const changes = this.changes()
862+
863+
const changes = utils.diffObjects({ [prop] : value }, { [prop] : current })
857864
const changeRecord = utils.plainCopy(changes)
858865
changeRecord.timestamp = new Date().getTime()
859866
const changeHistory = _get(changeHistoryPath) || []

test/unit/record/on.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { assert, JSData, sinon } from '../../_setup'
2+
3+
describe("Record#on('changes')", function() {
4+
it('Tracking changes to a single property', function(done) {
5+
const Store = new JSData.DataStore()
6+
const Foo = Store.defineMapper('foo', {
7+
schema: {
8+
properties: {
9+
id: { type: 'number' },
10+
name: { type: 'string', track: true }
11+
}
12+
},
13+
})
14+
const foo = Foo.createRecord()
15+
const listener = sinon.stub()
16+
foo.on('change', listener)
17+
foo.name = 'new foo'
18+
const secondSpec = function() {
19+
foo.name = 'updated foo'
20+
setTimeout(function () {
21+
const [record, changes] = listener.args[1]
22+
assert.equal(foo, record, "on 'change' listener called with record which was modified")
23+
assert.isTrue(listener.calledTwice, "on 'change' listener was called when modifying a property")
24+
assert.equal(Object.keys(changes.added).length, 0)
25+
assert.equal(Object.keys(changes.removed).length, 0)
26+
assert.equal(changes.changed.name, 'updated foo', "Only the property changed was emitted in the changeSet")
27+
done()
28+
}, 5)
29+
}
30+
setTimeout(function () {
31+
const [record, changes] = listener.args[0]
32+
assert.equal(foo, record, "on 'change' listener called with record which was modified")
33+
assert.isTrue(listener.calledOnce, "on 'change' listener was called when modifying a property")
34+
assert.equal(Object.keys(changes.changed).length, 0)
35+
assert.equal(Object.keys(changes.removed).length, 0)
36+
assert.equal(changes.added.name, 'new foo', "Only the property changed was emitted in the changeSet")
37+
secondSpec()
38+
}, 5)
39+
})
40+
})

0 commit comments

Comments
 (0)
0