Skip to content

Angular support #382

@smnbbrv

Description

@smnbbrv

Hi guys,

it would be really nice to have a deep Angular support.

One can say it is supported, and, well, one can use it with Angular. However, it currently feels like an alien bird in the nest because

  1. two-way binding and export message as "normal" JavaScript-friendly Object is not possible and sorry, but to me it is not usable, see Angular support #382 (comment)
  2. dependency injection is not anyhow taken into account. Ideally every service client could become a native angular service
  3. the methods are not compatible with neither RX Observables / native Promises. With Promise it is quite clear it cannot be supported because of non-unary methods, but RX is fully compliant here.

This is what I came to after a couple of attempts

export function fromClient<T>(c: T) {
  return <any>autoBind(<any>c) as T;
}

export function fromUnary<T extends Message>(clientMethod: (...p) => UnaryResponse, request: Message, meta?: Metadata) {
  return new Observable<T>(obs => {
    const req = clientMethod(request || null, meta || null, (err: ServiceError, response: T) => {
      if (err) {
        obs.error(err);
      }

      obs.next(response);
      obs.complete();
    });

    return () => req.cancel();
  });
}

and usage:

const client = fromClient(new HelloServiceClient(environment.grpc));
const obs = fromUnary<Hello>(client.getHello, new HelloRequest());

This code is far away from ideal. It is actually ugly, because I need to monkey-bind the methods and status is not returned anyhow yet (however the status part is quite solvable with pipe > filter and some boolean parameter in observable response).

Finally, the question: do you plan to support Angular in this / related repository? Should @angular/cli care of this instead? Or it is up to the end user to decide how to solve this problem?

I am ready to participate / support here (if possible).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions