Skip to content

Commit

Permalink
adapt to new implementation w/out direct and added nodes arg
Browse files Browse the repository at this point in the history
  • Loading branch information
elidoran committed May 19, 2017
1 parent 43bbb64 commit 85288c7
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 144 deletions.
16 changes: 8 additions & 8 deletions examples/buffers/json-payload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ console.log('example buffers/json')
// first added will be the start node.
// this decodes a length header specifying the number
// of bytes in the following JSON content.
nodes.add('header', function header(control) {
nodes.add('header', function header(control, N) {

if ((this.input.length - this.index) < 4) {
control.wait()
} else {
this.contentLength = this.input.readUInt32BE(this.index)
this.index += 4
control.next('content')
control.next(N.content)
}

})

// this gathers buffers until it has enough to extract
// the JSON content; according to length header.
nodes.add('content', function content(control) {
nodes.add('content', function content(control, N) {

if (this.buffers
&& (this.buffersLength + this.input.length >= this.contentLength)) {
Expand All @@ -34,13 +34,13 @@ nodes.add('content', function content(control) {
this.content = Buffer.concat(this.buffers)
this.buffers = null
this.buffersLength = 0
control.next('parse', 'header')
control.next(N.parse, N.header)
}

else if ((this.index + this.contentLength) <= this.input.length) {
this.content = this.input.slice(this.index, this.index + this.contentLength)
this.index += this.contentLength
control.next('parse', 'header')
control.next(N.parse, N.header)
}

else {
Expand All @@ -55,14 +55,14 @@ nodes.add('content', function content(control) {
})

// once content is availble we parse the JSON.
nodes.add('parse', function parse(control) {
nodes.add('parse', function parse(control, N) {
this.value = JSON.parse(this.content)
this.content = null
control.next('report')
control.next(N.report)
})

// report it
nodes.add('report', function report(control) {
nodes.add('report', function report(control, N) {
console.log('report:\n ', this.value)
this.value = null
control.next()
Expand Down
14 changes: 7 additions & 7 deletions examples/strings/counter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ console.log('example strings/counter\n------------------------------------------
// add our two nodes
nodes.addAll({

count: function count(control) {
count: function count(control, N) {

if (!this.input || this.input.length < 1) {
return control.next(this.value.count > 0 ? 'use' : 'stop')
return control.next(this.value.count > 0 ? N.use : N.stop)
} else {
this.input = this.trim(0, this.input.length - 1)
}
Expand Down Expand Up @@ -37,11 +37,11 @@ nodes.addAll({
if (index >= this.input.length) {
control.wait()
} else {
control.next('use')
control.next(N.use)
}
},

use: function use(control, context) {
use: function use(control, N, context) {

// use our value
console.log(context.value.ch, '->', context.value.count)
Expand All @@ -51,13 +51,13 @@ nodes.addAll({
this.value.count = 0

// go back to count another character
control.next('count')
control.next(N.count)
},

stop: function stop(control) {
stop: function stop(control, N) {

if (this.input && this.input.length > 0) {
control.next('count')
control.next(N.count)
} else {
control.wait()
}
Expand Down

0 comments on commit 85288c7

Please sign in to comment.