Skip to content

Commit 1c64e8e

Browse files
author
James Halliday
committed
.into() FINALLY works
1 parent e80d3c2 commit 1c64e8e

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

index.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,16 @@ exports.stream = function (em, eventName) {
7878
};
7979

8080
self.into = function (key, cb) {
81-
if (!vars.get(key)) {
82-
vars.set(key, {});
83-
}
84-
saw.nest(cb, vars.get(key));
81+
if (!vars.get(key)) vars.set(key, {});
82+
var parent = vars;
83+
vars = Vars(parent.get(key));
84+
85+
saw.nest(function () {
86+
cb.apply(this, arguments);
87+
this.tap(function () {
88+
vars = parent;
89+
});
90+
}, vars.store);
8591
};
8692

8793
self.flush = function () {

test/binary.js

+44-1
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ exports.nested = function () {
701701
}, 50);
702702
};
703703

704-
exports.into = function () {
704+
exports.intoBuffer = function () {
705705
var to = setTimeout(function () {
706706
assert.fail('never finished');
707707
}, 500);
@@ -737,3 +737,46 @@ exports.into = function () {
737737
})
738738
;
739739
}
740+
741+
exports.intoStream = function () {
742+
var to = setTimeout(function () {
743+
assert.fail('never finished');
744+
}, 500);
745+
746+
var digits = [ 1, 2, 3, 4, 5, 6 ];
747+
var stream = new EventEmitter;
748+
var iv = setInterval(function () {
749+
var d = digits.shift();
750+
if (d) stream.emit('data', new Buffer([ d ]))
751+
else clearInterval(iv)
752+
}, 20);
753+
754+
Binary.stream(stream)
755+
.into('moo', function () {
756+
this
757+
.word8('x')
758+
.word8('y')
759+
.word8('z')
760+
;
761+
})
762+
.tap(function (vars) {
763+
assert.eql(vars, { moo : { x : 1, y : 2, z : 3 } });
764+
})
765+
.word8('w')
766+
.tap(function (vars) {
767+
assert.eql(vars, {
768+
moo : { x : 1, y : 2, z : 3 },
769+
w : 4,
770+
});
771+
})
772+
.word8('x')
773+
.tap(function (vars) {
774+
assert.eql(vars, {
775+
moo : { x : 1, y : 2, z : 3 },
776+
w : 4,
777+
x : 5,
778+
});
779+
clearTimeout(to);
780+
})
781+
;
782+
}

0 commit comments

Comments
 (0)