Matt-Jensen / lnrpc

User-centric Node.js gRPC client for lightningnetwork/lnd ⚡️
MIT License
12 stars 8 forks source link

Make Subscriptions Observables #1

Closed Matt-Jensen closed 6 years ago

Matt-Jensen commented 6 years ago

Model push data for: subscribeInvoices, subscribeTransactions, subscribeChannelGraph, sendPayment, openChannel, and closeChannel like it was already stage 3.

Sudo implementation:

if (isSubscriptionMethod) {
  return (...args) => new Observable(observer => {
    const call = Reflect.apply(method, target, args);
    call.on('status', observer.next.bind(observer, 'status'));
    call.on('data', observer.next.bind(observer, 'data'));
    call.on('end', observer.complete);

    return () => {
      // any cleanup
    };
  });
}

Usage:

lnrpc.openChannel({...}).subscribe({
  next(event, openStatusUpdate) {
    if (event === 'status') {
      console.log('open channel status:', openStatusUpdate);
    }
  }
});
Matt-Jensen commented 6 years ago

Weigh cost benefit of polyfills:

Matt-Jensen commented 6 years ago

In reality decorating response streams with observers doesn't make sense as Observables are for 1 way communication and RPC streams must support 2 way communication. Unfortunately providing an Observer interface to users would only create confusion when it comes time to write to a stream.

Instead this issue is about fixing the promisi-fication of streaming RPC methods, which should instead return their original grpc values.