DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
  • Ecosystem
    • can-construct-super
    • can-define-stream
    • can-fixture
    • can-fixture-socket
    • can-jquery
    • can-stache-converters
    • can-stream
      • Methods
        • toStream
        • toStreamFromCompute
        • toStreamFromEvent
        • toStreamFromProperty
    • can-vdom
    • can-view-import
    • can-zone
    • steal-stache
  • Infrastructure
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Ecosystem
  • /
  • can-stream
  • / On this page
    • can-stream

      module

      Convert observable values into streams. Kefir is used internally to provide the stream functionality.

      • npm package badge
      • Star
      • source

      Object

      The can-stream module exports methods useful for converting observable values like can-computes or can-define/map/map properties into streams.

      var canStream = require("can-stream");
      var DefineMap = require("can-define/map/map");
      
      var me = new DefineMap({name: "Justin"});
      
      var nameStream = canStream.toStream(me,".name");
      
      
      nameStream.onValue(function(name){
          // name -> "Obaid";
      });
      
      me.name = "Obaid";
      

      Usage

      The toStream method has shorthands for all of the other methods:

      var canStream = require("can-stream");
      
      canStream.toStream(compute)                    //-> stream
      canStream.toStream(map, "eventName")           //-> stream
      canStream.toStream(map, ".propName")           //-> stream
      canStream.toStream(map, ".propName eventName") //-> stream
      

      For example:

      Converting a compute to a stream

      var canCompute = require("can-compute");
      var canStream = require("can-stream");
      
      var compute = canCompute(0);
      var stream = canStream.toStream(compute);
      
      stream.onValue(function(newVal){
          console.log(newVal);
      });
      
      compute(1);
      //-> console.logs 1
      

      Converting an event to a stream

      var DefineList = require('can-define/list/list');
      var canStream = require('can-stream');
      
      var hobbies = new DefineList(["js","kayaking"]);
      
      var changeCount = canStream.toStream(obs, "length").scan(function(prev){
          return prev + 1;
      }, 0);
      changeCount.onValue(function(event) {
          console.log(event);
      });
      
      hobbies.push("bball")
      //-> console.logs {type: "add", args: [2,["bball"]]}
      hobbies.shift()
      //-> console.logs {type: "remove", args: [0,["js"]]}
      

      Converting a property value to a stream

      var canStream = require('can-stream');
      var DefineMap = require("can-define/map/map");
      
      var person = new DefineMap({
          first: "Justin",
          last: "Meyer"
      });
      
      var first = canStream.toStream(person, '.first'),
          last = canStream.toStream(person, '.last');
      
      var fullName = Kefir.combine(first, last, function(first, last){
          return first + last;
      });
      
      fullName.onValue(function(newVal){
          console.log(newVal);
      });
      
      map.first = "Payal"
      //-> console.logs "Payal Meyer"
      

      Converting an event on a nested object into a stream

      var canStream = require('can-stream');
      var DefineMap = require("can-define/map/map");
      var DefineList = require("can-define/list/list");
      
      var me = new DefineMap({
          todos: ["mow lawn"]
      });
      
      var addStream = canStream.toStream(me, ".todos add");
      
      addStream.onValue(function(event){
          console.log(event);
      });
      
      map.todos.push("do dishes");
      //-> console.logs {type: "add", args: [1,["do dishes"]]}
      

      CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.0.0.