Skip to content

Commit

Permalink
Fixes issue with cached values
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mujica committed Dec 22, 2017
1 parent d2a151d commit 61789c2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
48 changes: 47 additions & 1 deletion can-kefir-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var QUnit = require("steal-qunit");
var Kefir = require("can-kefir");
var QUnit = require("steal-qunit");
var queues = require("can-queues");
var canReflect = require("can-reflect");

QUnit.module("can-kefir");
Expand Down Expand Up @@ -73,6 +74,51 @@ QUnit.test("properties can be read without binding", function() {
);
});

QUnit.test("properties caches value/error correctly when unbound", function(assert) {
var emitter;

var stream = Kefir.stream(function(e) {
emitter = e;
}).toProperty();

var handler = function noop() {};
canReflect.onKeyValue(stream, "value", handler);
emitter.value(10);
canReflect.offKeyValue(stream, "value", handler);

assert.equal(canReflect.getKeyValue(stream, "value"), 10);
assert.equal(canReflect.getKeyValue(stream, "error"), undefined);

canReflect.onKeyValue(stream, "value", handler);
assert.equal(canReflect.getKeyValue(stream, "value"), 10, "should be cached");
canReflect.offKeyValue(stream, "value", handler);
});

QUnit.test("callbacks are within a batch", function(assert) {
var emitter;
assert.expect(2);

var stream = Kefir.stream(function(e) {
emitter = e;
});

var valueChangeCounter = 0;
canReflect.onKeyValue(stream, "value", function onValueChange() {
valueChangeCounter += 1;
});

queues.batch.start();
emitter.value(1);
assert.equal(
valueChangeCounter,
0,
"handler should not be called while flushing is prevented"
);
queues.batch.stop();

assert.equal(valueChangeCounter, 1);
});

QUnit.test("Kefir.emitterProperty", function() {
var stream = new Kefir.emitterProperty();

Expand Down
2 changes: 1 addition & 1 deletion can-kefir.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ canReflect.assignSymbols(Kefir.Observable.prototype, {
var currentValue = getCurrentValue(stream, key);

// save current value so we won't through events if we provided a value
meta.value = currentValue;
meta[key] = currentValue;

return currentValue;
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"kefir": "^3.5.1"
},
"devDependencies": {
"can-queues": "^0.3.1",
"detect-cyclic-packages": "^1.1.0",
"jshint": "^2.9.1",
"steal": "^1.3.1",
Expand Down

0 comments on commit 61789c2

Please sign in to comment.