Comments (9)
@bangarharshit in Go speak, Single
seems like a good candidate for an interface type that with Observable
and Observer
mixins:
// Imaginary unicorn
type Single interface {
rx.Observable
rx.Observer
}
Most foreseeable goal is to make base interfaces like rx.Observable
(not yet existing) which is implemented by observable.Observable
(a "variant" struct of rx.Observable
). This will mean more extensibility for developers (anyone can whip up an observable based on anything from slice to channel) while keeping the core library lean.
Maybe a Slack or Gitter for RxGo is in order. I'll open a separate issue about this.
from rxgo.
Can you give a hypothetical use case in code? I'm trying to understand what you're explaining.
from rxgo.
@jochasinga the use-case I am envisioning is when a go client needs to talk to multiple micro-service
backends to fulfill a user-request. The psuedo-code would be something like:
func processUserRequest(obj interface{}) {
observer = {OnSuccess: func() {
// Make a REST call to another micro-service
}, OnError: func() {
// Return a 5xx error to caller.
}};
//Create Single of `obj`
single.Start(func() {
//Create a Single of `obj`
}).Subscribe(observer)
}
Basically the single pattern will allow us make the whole request process in a micro-services architecture asynchronous while maintaining the readability of the code.
from rxgo.
afaik Single
type implements both Observer
and Observable
which means it can both emit values or act like an observer. In your case, it seems you are using it as an observable with a single item waiting to emit. Would this suffice?
observable.Start(func() interface{} {
res, err := http.Get("somewhere")
if err != nil {
return err
}
return res
}).Subscribe(observer.Observer{
NextHandler: func(item interface{}) {
fmt.Printf("Processing: %v\n", item)
res, ok := item.(*http.Response); ok {
if res.statusCode == 200 {
// Make a call to another service
}
}
},
ErrHandler: func(err error) {
fmt.Printf("Encountered error: %v\n", err)
// Return a 5xx error to caller
},
})
I've tried playing with Single
type, but with Go I haven't found a need for it yet because Go's rather "raw" type system makes it unnecessary to come up with so many abstract types. Maybe this can be included in the next milestone.
I'd love to know if it works for you.
from rxgo.
@jochasinga yeah that's the idiom I currently plan to use. Was just thinking that the Single
might make this a lot more explicit.
from rxgo.
Things like network calls can be single (with single, a large number of Observable related stuff can be migrated to it since it is terminated after an event). It should neither be a subclass of Observable
and Observer
. It is more of a variant of Observable
.
BTW, there are other variants of Observable in RxJava for specific purposes (which may not be relevant for RxGo). Would love to learn and contribute. Do you have a slack or any IRC channel where we can ask questions?
1. Maybe
2. Flowable - Supports backpressure
3. Completable - Just the case if it succeeded or not.
4. ParallelFlowable
from rxgo.
@bangarharshit very good point. A Single
would be really useful in developing an actor
programming model that is pretty prevalent in distributed systems.
from rxgo.
+1 for the Single
interface.
Meanwhile, I tried to propose something regarding the migration of Observer
, Observable
and Connectable
to interfaces: #90
from rxgo.
implemented on RxGo 2 (in development) #95
from rxgo.
Related Issues (20)
- when to support the generic type HOT 1
- Why I can not find "Throttle" operator?
- [Support] Leak goroutine with rxgo.Merge HOT 1
- SwitchMap operator
- New maintainer? HOT 11
- Sometimes First().Get() not returning a context canceled error HOT 2
- Go 1.18 Generics Support
- RX driver for SQL databases
- Counting items of a Connectable Observable
- Cold & Hot Observables HOT 1
- Add `Subject` to rxgo
- There are some examples.
- Custom Producer HOT 1
- How cancel subscription ?
- How to do recursion?
- Is this still mantained? HOT 2
- RxGo Rest api example HOT 1
- mapOperator missing res when return error in "map" function HOT 1
- Adding Subjects
- Real-World Example in doc HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rxgo.