can-define-stream
Add useful stream conversion methods to a supplied can-define/map/map or can-define/list/list constructor using a stream interface such as can-stream-kefir.
canDefineStream(streamInterface)
The can-define-stream
module exports a function that takes a streamInterface and returns a function that takes a DefineMap.prototype or DefineList.prototype and uses the supplied stream interface to create streamed property definitions.
var canStream = require("can-stream-kefir");
var canDefineStream = require("can-define-stream");
var DefineMap = require("can-define/map/map");
var Person = DefineMap.extend({
first: "string",
last: "string",
fullName: {
get: function() {
return this.first + " " + this.last;
}
},
fullNameChangeCount: {
stream: function() {
return this.toStream(".fullName").scan(function(last) {
return last + 1;
}, 0);
}
}
});
canDefineStream(canStream)(Person);
var me = new Person({name: "Justin", last: "Meyer"});
me.on("fullNameChangeCount", function(ev, newVal) {
console.log(newVal);
});
me.fullNameChangeCount //-> 0
me.first = "Obaid"; //-> console.logs 1
me.last = "Ahmed"; //-> console.logs 2
Parameters
- streamInterface
{streamInterface()}
:A streamInterface function. See can-stream-kefir for implementation.
Use
The toStream method has shorthands for all of the other methods:
toStream("eventName") //-> stream
toStream(".propName") //-> stream
toStream(".propName eventName") //-> stream
For example:
Update map property based on stream value
var DefineMap = require('can-define/map/map');
var canStream = require("can-stream-kefir");
var canDefineStream = require("can-define-stream");
var Person = DefineMap.extend({
name: "string",
lastValidName: {
stream: function() {
return this.toStream(".name").filter(function(name) { // using propName
return name.indexOf(" ") >= 0;
});
}
}
});
canDefineStream(canStream)(Person);
var me = new Person({name: "James"});
me.on("lastValidName", function(lastValid) {});
me.name = "JamesAtherton"; //lastValidName -> undefined
me.name = "James Atherton"; //lastValidName -> James Atherton
Stream on DefineList
var DefineList = require('can-define/list/list');
var canStream = require("can-stream-kefir");
var canDefineStream = require("can-define-stream");
var PeopleList = DefineList.extend({});
canDefineStream(canStream)(PeopleList);
var people = new PeopleList([
{ first: "Justin", last: "Meyer" },
{ first: "Paula", last: "Strozak" }
]);
var stream = people.toStream('length'); // using eventName
stream.onValue(function(val) {
val //-> 2, 3
});
people.push({
first: 'Obaid',
last: 'Ahmed'
}); //-> stream.onValue -> 3