examples/datasource/webWorkerSource.js
function(action) {
        var self = this;

        // The subscribe function runs when the Observable is observed.
        return Rx.Observable.create(function subscribe(observer) {
            var id = self.id++,

                handler = function(e) {
                    var response = e.data,
                        error,
                        value;

                    // The response is an array like this [id, error, data]
                    if (response[0] === id) {
                        error = response[1];
                        if (error) {
                            observer.onError(error);
                        } else {
                            value = response[2];
                            observer.onNext(value);
                            observer.onCompleted();
                        }
                    }
                };

            // Add the identifier to the front of the message
            action.unshift(id);

            self._worker.postMessage(action);
            self._worker.addEventListener('message', handler);

            // This is the action to perform if the consumer unsubscribes from the observable
            return function() {
                self._worker.removeEventListener('message', handler);
            };
        });
    }
function(action) {
        var self = this;

        // The subscribe function runs when the Observable is observed.
        return Rx.Observable.create(function subscribe(observer) {
            var id = self.id++,

                handler = function(e) {
                    var response = e.data,
                        error,
                        value;

                    // The response is an array like this [id, error, data]
                    if (response[0] === id) {
                        error = response[1];
                        if (error) {
                            observer.onError(error);
                        } else {
                            value = response[2];
                            observer.onNext(value);
                            observer.onCompleted();
                        }
                    }
                };

            // Add the identifier to the front of the message
            action.unshift(id);

            self._worker.postMessage(action);
            self._worker.addEventListener('message', handler);

            // This is the action to perform if the consumer unsubscribes from the observable
            return function() {
                self._worker.removeEventListener('message', handler);
            };
        });
    }
function ( action ) { var self = this ; // The subscribe function runs when the Observable is observed. return Rx . Observable . create ( function subscribe ( observer ) { var id = self . id ++ , handler = function ( e ) { var response = e . data , error , value ; // The response is an array like this [id, error, data] if ( response [ 0 ] === id ) { error = response [ 1 ] ; if ( error ) { observer . onError ( error ) ; } else { value = response [ 2 ] ; observer . onNext ( value ) ; observer . onCompleted ( ) ; } } } ; // Add the identifier to the front of the message action . unshift ( id ) ; self . _worker . postMessage ( action ) ; self . _worker . addEventListener ( 'message' , handler ) ; // This is the action to perform if the consumer unsubscribes from the observable return function ( ) { self . _worker . removeEventListener ( 'message' , handler ) ; } ; } ) ; }
Creates an observable stream that will send a request to a Model server, and retrieve the response. The request and response are correlated using a unique identifier which the client sends with the request and the server echoes back along with the response.
Creates an observable stream that will send a request to a Model server and retrieve the response . The request and response are correlated using a unique identifier which the client sends with the request and the server echoes back along with the response .