Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

This is RxJS v 4. Find the latest version here

Rx.Observable.prototype.transduce(transducer)

Executes a transducer to transform the observable sequence.

Transducers are composable algorithmic transformations. They are independent from the context of their input and output sources and specify only the essence of the transformation in terms of an individual element. Because transducers are decoupled from input or output sources, they can be used in many different processes such as Observable sequences. Transducers compose directly, without awareness of input or creation of intermediate aggregates.

Such examples of transducers libraries are transducers-js from Cognitect and transducers.js from James Long.

In order for this operator to work, the transducers library must follow the following contract:

return {
  '@@transducer/init': function() {
    // Return the item
    return observer;
  },
  '@@transducer/step': function(obs, input) {
    // Process next item
    return obs.onNext(input);
  },
  '@@transducer/result': function(obs) {
    // Mark completion
    return obs.onCompleted();
  }
};

Arguments

  1. transducer (Transducer): A transducer to execute.

Returns

(Observable): An observable sequence that results from the transducer execution.

Example

Below is an example using transducers-js.

function even (x) { return x % 2 === 0; }
function mul10(x) { return x * 10; }

var t = transducers

var source = Rx.Observable.range(1, 5)
  .transduce(t.comp(t.filter(even), t.map(mul10)));

var subscription = source.subscribe(
  function (x) {
    console.log('Next: %s', x);
  },
  function (err) {
    console.log('Error: %s', err);
  },
  function () {
      console.log('Completed');
  });

// => Next: 20
// => Next: 40
// => Completed

Location

File:

Dist:

NPM Packages:

NuGet Packages:

Unit Tests: