Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
49 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,36 @@ | ||
var sm = require('../lib/statemutate.js'); | ||
var sm = require('../lib/statemutate.js'), | ||
fa = require('./fileadapter.js'); | ||
|
||
var projection = new sm.Projections(); | ||
|
||
var model = { | ||
// prepare data file | ||
// fa.save('data.json',[{payload : 'One'}, {payload:'Two'}, {payload:'Three',value : 3}, {payload:'Four'}]); | ||
var simpleModel = { | ||
text: '', | ||
valueSum: 0 | ||
} | ||
var data = projection | ||
.arrange([{payload : 'One'}, new projection.Message('','Two'), {payload:'Three', value : 3},{payload:'Four'}]) | ||
.append({payload : 'Last', value : -1}) | ||
.for(model) | ||
.with(function(state, message) { | ||
state.text += message.payload + ' '; | ||
state.valueSum += message.value || 0; | ||
return state; | ||
}) | ||
.perform(function(message){ | ||
console.log('Step: '); | ||
console.log(message); | ||
}); | ||
console.log('Final result:') | ||
console.log(data); | ||
|
||
performSimpleProjection(simpleModel, function(model) { | ||
console.log('Final result:'); | ||
console.log(model); | ||
}); | ||
|
||
|
||
function performSimpleProjection(model, callback){ | ||
var projection = new sm.Projections(); | ||
|
||
fa.load('./data.json', function(data) { | ||
projection | ||
.arrange(data) | ||
.append({payload : 'Last', value : -1}) | ||
.for(model) | ||
.with(function(state, message) { | ||
state.text += message.payload + ' '; | ||
state.valueSum += message.value || 0; | ||
return state; | ||
}) | ||
.perform(function(message){ | ||
console.log('Step: '); | ||
console.log(message); | ||
}); | ||
callback(model); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"payload":"One"},{"payload":"Two"},{"payload":"Three","value":3},{"payload":"Four"}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var fs = require('fs'); | ||
|
||
function save(filename, data) { | ||
fs.writeFile(filename, JSON.stringify(data), function(err) { | ||
if(err) throw err; | ||
}); | ||
} | ||
|
||
function load(filename, callback) { | ||
fs.readFile(filename, function (err, data) { | ||
if (err) throw err; | ||
callback(JSON.parse(data)); | ||
}); | ||
} | ||
exports.save = save; | ||
exports.load = load; |