Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Touch RxJs #3

Open
renjie1996 opened this issue Jan 5, 2018 · 0 comments
Open

Touch RxJs #3

renjie1996 opened this issue Jan 5, 2018 · 0 comments

Comments

@renjie1996
Copy link
Owner

renjie1996 commented Jan 5, 2018

hello world

a = 1
b = 2
 
c = a + b // current value:c = 3
 
a = 2 // current value still:c = 3 

when use RxJs

a$ = 1
b$ = 2
 
c$ = a$ + b$ // current value:3
 
a$ = 2 // current value like vue computed:4

Concept

Observable

Observable is actually an asynchronous array

Observable = TimeLine + Array

when using Observable

We can make a function return multiple times, which is very different from the original javascript function.

let foo = Rx.Observable.create(observer => {
  observer.next('hello, rxjs');
});
 
foo.subscribe(x => console.log(x)); //  'hello, rxjs'
foo.subscribe(y => console.log(y)); //  'hello, rxjs'

You can also create an event stream that prints the event object when the user clicks it.

let clicks = Rx.Observable.fromEvent(document, 'click');
clicks.subscribe(x => console.log(x));

observer

observer is an Object .By default, the callback in subscribe will be placed in next.

let observer = {
    next() {},
    complete() {},
    error() {},
};

Remember the promise we used so often, two states, resolve and reject. In observer we can manually execute Observer.complete () and Observer.error () to control the process.

let foo = Rx.Observable.create(observer => {
  observer.next(123);
  observer.next(456);
  observer.complete();
  observer.next(789);
});
 
foo.subscribe(x => console.log(x)); // 123 456, then nothing

subscription

Subscription is the return value after subscribe

let observable = Rx.Observable.interval(1000);
let subscription = observable.subscribe(x => console.log(x));
 
setTimeout(() => {
  subscription.unsubscribe();
}, 3100)

like can Interrupting Promise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant