X Tutup
The Wayback Machine - https://web.archive.org/web/20200628004212/https://github.com/angular/angular/issues/35183
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

operators definition conflicting with ReactiveX/rxjs definition. "They return a function" vs "should return an observable" #35183

Open
ghuser opened this issue Feb 6, 2020 · 4 comments

Comments

@ghuser
Copy link

@ghuser ghuser commented Feb 6, 2020

📚 Docs or angular.io bug report

Description

In the section https://angular.io/guide/rx-library#operators there is a definition of an operator:

..Operators take configuration options, and they return a function that takes a source observable.

But in rxjs guidelines about operation creation they say:

Operators should always return an Observable. [...]If you create a method that returns something other than an Observable, it's not an operator, and that's fine.

Clearly there is a conflicting definition: "they return a function"" vs "should always return an observable".

🔬 Minimal Reproduction

What's the affected URL?**

https://angular.io/guide/rx-library#operators

Reproduction Steps**

Expected vs Actual Behavior**

📷Screenshot

🔥 Exception or Error





🌍 Your Environment

Browser info

Anything else relevant?

@ngbot ngbot bot added this to the needsTriage milestone Feb 6, 2020
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Feb 6, 2020
@kapunahelewong kapunahelewong added this to Need Assistance from Eng. in docs Feb 6, 2020
@BioPhoton
Copy link
Contributor

@BioPhoton BioPhoton commented Feb 6, 2020

Dear @ghuser

If you create a method that returns something other than an Observable, it's not an operator, and that's fine.

IMHO the whole sentence is useless. :D

What you should fouse about is:

  • creation of Observable
  • creation of OperatorFunction

Observable:

Static:

const arr = [1,2,3,4];
const o = new Observable((subscriber => {
  arr.foeEach(n => subscriber.next(n))
  subscriber.complete()
}));

Creation Function:

const arr = [1,2,3,4];
function createObserfableFromArray(arr) => {
  return new Observable((subscriber => {
    arr.foeEach(n => subscriber.next(n))
    subscriber.complete()
  }));
}
const o =createObserfableFromArray = (arr) ;

Operators

Static:

function log(observable) => {
  return observable.pipe(tap(value => console.log(value)));
}

observable.pipe(
  log
)

Creation Function:

function logCreation(param) {
  return function(observable) => {
    return observable.pipe(
     tap(value => console.log(param, value))
   );
  }
}

observable.pipe(
  logCreation('value: ')
)

To sum up:

Operators are functions that take an Observable and return an observable:

  • (o: Observable) => Observable
    If you wrap them in a function to create them they are configurable:
  • (cfg) => (o: Observable) => Observable

Observables can be created by using instantiating a new one:

  • new Observable
    or by a function:
  • (arg?: any) => Observable

Let me know if this helps.

Best,
Michael

@kapunahelewong
Copy link
Contributor

@kapunahelewong kapunahelewong commented Feb 6, 2020

Thank you so much, @BioPhoton!! @ghuser, does that help you? We can also clear up the docs on this point.

@kapunahelewong kapunahelewong moved this from Need Assistance from Eng. to Committed - Selected for development in docs Feb 6, 2020
@ghuser
Copy link
Author

@ghuser ghuser commented Feb 17, 2020

@BioPhoton

It is more clear now, though this syntax confuses me. Is it valid js?:

function createObserfableFromArray(arr) => {

and

const o =createObserfableFromArray = (arr) ;

@kapunahelewong
Copy link
Contributor

@kapunahelewong kapunahelewong commented May 27, 2020

Status: Docs Team can use the excellent explanation by @BioPhoton to update the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
docs
Committed - Selected for development
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.
X Tutup