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

[RFC] midway controller supports returning Observable #274

Open
waitingsong opened this issue Jul 10, 2019 · 9 comments
Open

[RFC] midway controller supports returning Observable #274

waitingsong opened this issue Jul 10, 2019 · 9 comments
Labels
status: Holding Same features may be hold, and implements layer.

Comments

@waitingsong
Copy link
Member

Midway 的 controller 能否支持直接返回 (rxjs 的) Observable 对象。
nestjs 支持,感觉是比较方便的。

@waitingsong waitingsong changed the title [RFC] midway controller supports return Observable [RFC] midway controller supports returning Observable Jul 10, 2019
@czy88840616
Copy link
Member

粗略想了一下。。没理解。。。有示例么?

@waitingsong
Copy link
Member Author

waitingsong commented Jul 10, 2019

大致这样:

import { Context, config, controller, get, provide } from 'midway'
import { Observable, timer } from 'rxjs'
import { map } from 'rxjs/operators'


@provide()
@controller('/')
export class HomeController {

  constructor(
    @config() private readonly welcomeMsg: string,
  ) {}

  @get('/', { middleware: ['apiMiddleware'] })
  public index(ctx: Context): Observable<string> {
    return timer(5000).pipe(
      map(() => {
        return `${ this.welcomeMsg } - ${ ctx.api.reqTimeStr }`
      })
    )
  }

}

返回的是 Observable<string> , 相当于不用在这儿进行 ctx.body 赋值。
好处是 Service 可以返回 Observable 对象,然后可以利用 rxjs 丰富的 operators 操作符在 controller 里面对数据流进行各种合并、变形、转换、延时、防抖等等处理后直接丢出流。
这个需求可能受众不大。

@czy88840616
Copy link
Member

这个貌似没见过。。。虽然看过nest有这样搞。。不过他是配合他的整个流程,加上拦截器,包了 express,而koa 这边因为本身自带了洋葱模型,所以再写一个就很奇怪。可能 Observable 可以作为核心能力的一部分,甚至单独出一个 Midway-Observable 来尝试写法都可以。

@waitingsong
Copy link
Member Author

waitingsong commented Jul 10, 2019

是的,这个改动会比较大。
如果仅仅是判断 controller 返回的是 Observable 然后 obv.toPromise() 就可解决。不过意义不大,要发挥 rxjs 的优势得全生命周期支持。

@czy88840616
Copy link
Member

所以可能得来个 midway for rxjs ?

@waitingsong
Copy link
Member Author

估计 midway grpc 优先级还高些。

@czy88840616
Copy link
Member

😄 GRPC。。在做faas的时候用到了,发现和java交互有点问题。。

@waitingsong
Copy link
Member Author

和 golang 交互没问题吧。那锅就是 java 的了嘛……

@czy88840616
Copy link
Member

没错,就是java的锅。

@czy88840616 czy88840616 added the status: Holding Same features may be hold, and implements layer. label Jul 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: Holding Same features may be hold, and implements layer.
Development

No branches or pull requests

2 participants