Skip to content

Use Observables instead of callbacks? #5

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

Closed
zolkis opened this issue Feb 28, 2017 · 10 comments
Closed

Use Observables instead of callbacks? #5

zolkis opened this issue Feb 28, 2017 · 10 comments

Comments

@zolkis
Copy link
Contributor

zolkis commented Feb 28, 2017

As described here and agreed on the face to face meeting,

Promises should be used for asynchronous operations with one-shot result (either success or fail), and callbacks (later Observables) should be used for asynchronous operations with multiple-shot results (e.g. open ended protocol requests such as discovery).

This issue is for tracking and discussing the WoT use case for Observables.

@zolkis
Copy link
Contributor Author

zolkis commented Feb 28, 2017

Use case: return an Observable object for open ended [cancelable] asynchronous operations that provides controls over that operation:

  • cancel the operation
  • attach a callback for handling streamed or partial results
  • attach a callback for handling the end of the operation
  • attach an error handler callback.

For example:

let subscription = asyncOperation(parameters).subscribe({
    next(val) { console.log("Received next result: " + val); },
    error(err) { console.log("Received an error: " + err); },
    complete() { console.log("Operation complete"); }
});
// ...
subscription.cancel();

Alternatively, we could say

let subscription = asyncOperation(parameters)
    .on("next", val => { console.log("Received next result: " + val); })
    .on("error", function(err) { console.log("Received an error: " + err); })
    .on("complete", () => { console.log("Operation complete"); })
;

Or,

let subscription = asyncOperation(parameters)
    .next(val => { console.log("Received next result: " + val); })
    .complete( () => { console.log("Operation complete"); })
    .catch(err => { console.log("Received an error: " + err); });
;

@zolkis
Copy link
Contributor Author

zolkis commented Feb 28, 2017

This would be used mainly with discovery operations.

@zolkis
Copy link
Contributor Author

zolkis commented Feb 28, 2017

We could do a polyfill for the above options, or continue to use callbacks for discovery, but without the ability to cancel discovery, or to know when discovery has ended (when these are supported by the underlying protocols).

@h0ru5
Copy link
Contributor

h0ru5 commented Mar 25, 2017

I am +1 on observables for discovery (and maybe also subscription).

I would stick to the current ECMA discussion. I don't know if there is a polyfill already defined, it seems Angular2 used RxJS, which could be an option.

@h0ru5
Copy link
Contributor

h0ru5 commented Mar 27, 2017

Conclusion on call 27-03-2017 is to go for a subset of current ECMA discussion, risking a breakage but betting on promising candidate.
we check the proposal and decide.

@h0ru5
Copy link
Contributor

h0ru5 commented Mar 27, 2017

Link to proposal: https://github.com/tc39/proposal-observable

@zolkis
Copy link
Contributor Author

zolkis commented May 5, 2017

Leaving this open for further feedback.

@mmccool
Copy link
Contributor

mmccool commented Aug 21, 2017

Discussion in the meeting on Aug 21 did not reveal any major issue but decided to leave this open for now.

@danielpeintner
Copy link
Contributor

I think with the latest updates we can close this issue, correct?

@zolkis
Copy link
Contributor Author

zolkis commented Feb 14, 2018

Addressed by #86.

@zolkis zolkis closed this as completed Feb 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants