Skip to content

Commit

Permalink
show full change details
Browse files Browse the repository at this point in the history
  • Loading branch information
0x8890 committed Jan 24, 2016
1 parent 3fe7733 commit 329526b
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 76 deletions.
5 changes: 5 additions & 0 deletions doc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog


##### 1.12.0 (2016-01-24)
- Feature: include entire payloads in change event.
- Fix: update objects should show resulting operations.


##### 1.11.5 (2016-01-24)
- Polish: use `msgpack` to store data in IndexedDB instead of JSON, which allows use of Transferable & ArrayBuffer.
- Polish: improve IndexedDB error handling.
Expand Down
10 changes: 5 additions & 5 deletions lib/core.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

var EventEmitter = require('events')
var EventLite = require('event-lite')

// Local modules.
var memoryAdapter = require('./adapter/adapters/memory')
Expand Down Expand Up @@ -29,8 +29,8 @@ var plainObject = {}

/**
* This is the default export of the `fortune` module. The Fortune class
* subclasses the built-in `EventEmitter` class, and it has a few static
* properties attached to it that may be useful to access:
* is an event emitter that implements a subset of `EventEmitter`, and it has
* a few static properties attached to it that may be useful to access:
*
* - `Adapter`: abstract base class for the Adapter.
* - `adapters`: included adapters, defaults to memory adapter. Note that the
Expand Down Expand Up @@ -61,8 +61,8 @@ function Fortune (options) {
}


// Inherit from EventEmitter class.
Fortune.prototype = assign(Object.create(EventEmitter.prototype))
// Inherit from EventLite class.
Fortune.prototype = assign(Object.create(EventLite.prototype))


/**
Expand Down
15 changes: 3 additions & 12 deletions lib/dispatch/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,12 @@ module.exports = function (context) {
var eventData = {}, currentType

eventData[createMethod] = {}
eventData[createMethod][type] = map(records, function (record) {
return record[primaryKey]
})
eventData[createMethod][type] = records

for (currentType in updates) {
if (!updates[currentType].length) continue
if (!(updateMethod in eventData))
eventData[updateMethod] = {}
eventData[updateMethod][currentType] =
map(updates[currentType], mapId)
if (!(updateMethod in eventData)) eventData[updateMethod] = {}
eventData[updateMethod][currentType] = updates[currentType]
}

// Summarize changes during the lifecycle of the request.
Expand All @@ -190,8 +186,3 @@ module.exports = function (context) {
return context
})
}


function mapId (update) {
return update[primaryKey]
}
11 changes: 2 additions & 9 deletions lib/dispatch/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,8 @@ module.exports = function (context) {

for (currentType in updates) {
if (!updates[currentType].length) continue
if (!(updateMethod in eventData))
eventData[updateMethod] = {}
eventData[updateMethod][currentType] =
map(updates[currentType], mapId)
if (!(updateMethod in eventData)) eventData[updateMethod] = {}
eventData[updateMethod][currentType] = updates[currentType]
}

// Summarize changes during the lifecycle of the request.
Expand All @@ -149,8 +147,3 @@ module.exports = function (context) {
return context
})
}


function mapId (update) {
return update[primaryKey]
}
53 changes: 32 additions & 21 deletions lib/dispatch/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var BadRequestError = errors.BadRequestError
var find = require('../common/array/find')
var includes = require('../common/array/includes')
var map = require('../common/array/map')
var unique = require('../common/array/unique')

var constants = require('../common/constants')
var changeEvent = constants.change
Expand Down Expand Up @@ -247,22 +246,42 @@ module.exports = function (context) {

ids = id

// Initialize array.
if (!update.push) update.push = {}
if (!update.pull) update.pull = {}
update.push[field] = []
update.pull[field] = []

// Compute differences, and mutate the update.
// Compute differences for push, and mutate the update.
for (k = 0, l = ids.length; k < l; k++) {
id = ids[k]
if (!includes(record[field], id)) update.push[field].push(id)
if (!includes(record[field], id)) {
if (!('push' in update)) update.push = {}
if (field in update.push) {
if (Array.isArray(update.push[field])) {
update.push[field].push(id)
continue
}
update.push[field] = [ update.push[field], id ]
continue
}
update.push[field] = [ id ]
}
}

// Compute differences for pull, and mutate the update.
for (k = 0, l = record[field].length; k < l; k++) {
id = record[field][k]
if (!includes(ids, id)) update.pull[field].push(id)
if (!includes(ids, id)) {
if (!('pull' in update)) update.pull = {}
if (field in update.pull) {
if (Array.isArray(update.pull[field])) {
update.pull[field].push(id)
continue
}
update.pull[field] = [ update.pull[field], id ]
continue
}
update.pull[field] = [ id ]
}
}

// Delete the original replace, since it is no longer valid.
delete update.replace[field]
}

if (update.push && update.push[field]) {
Expand Down Expand Up @@ -333,20 +352,17 @@ module.exports = function (context) {
var eventData = {}, linkedType

eventData[updateMethod] = {}
eventData[updateMethod][type] =
map(transformedUpdates, mapPrimaryKey)
eventData[updateMethod][type] = transformedUpdates

for (linkedType in relatedUpdates) {
if (!relatedUpdates[linkedType].length) continue

if (linkedType !== type)
eventData[updateMethod][linkedType] =
map(relatedUpdates[linkedType], mapPrimaryKey)
eventData[updateMethod][linkedType] = relatedUpdates[linkedType]

// Get the union of update IDs.
else eventData[updateMethod][type] =
unique(eventData[updateMethod][type].concat(
map(relatedUpdates[type], mapPrimaryKey)))
eventData[updateMethod][type].concat(relatedUpdates[type])
}

// Summarize changes during the lifecycle of the request.
Expand Down Expand Up @@ -387,8 +403,3 @@ function dropFields (update, fields) {
for (field in update.pull)
if (!(field in fields)) delete update.pull[field]
}


function mapPrimaryKey (update) {
return update[primaryKey]
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fortune",
"description": "Application middleware for Node.js and web browsers.",
"version": "1.11.5",
"version": "1.12.0",
"license": "MIT",
"author": {
"email": "0x8890@airmail.cc",
Expand Down Expand Up @@ -30,6 +30,7 @@
"chalk": "^1.1.1",
"deep-equal": "^1.0.1",
"error-class": "^2.0.0",
"event-lite": "^0.1.0",
"msgpack-lite": "^0.1.15",
"negotiator": "^0.6.0",
"tapdance": "^4.1.1"
Expand Down
6 changes: 4 additions & 2 deletions test/integration/methods/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ run(() => {
store = instance

store.on(changeEvent, data => {
ok(deepEqual(data[createMethod].user.sort((a, b) => a - b),
ok(deepEqual(data[createMethod].user
.map(x => x.id).sort((a, b) => a - b),
[ 4 ]), 'change event shows created IDs')
ok(deepEqual(data[updateMethod].user.sort((a, b) => a - b),
ok(deepEqual(data[updateMethod].user
.map(x => x.id).sort((a, b) => a - b),
[ 1, 3 ]), 'change event shows updated IDs')
})

Expand Down
3 changes: 2 additions & 1 deletion test/integration/methods/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ run(() => {
store.on(changeEvent, data => {
ok(find(data[deleteMethod].user, id => id === 3),
'change event shows deleted ID')
ok(deepEqual(data[updateMethod].user.sort((a, b) => a - b),
ok(deepEqual(data[updateMethod].user
.map(x => x.id).sort((a, b) => a - b),
[ 1, 2 ]), 'change event shows updated IDs')
})

Expand Down
Loading

0 comments on commit 329526b

Please sign in to comment.