Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

feat: rpc subscription refactor + returning results #413

Merged
merged 14 commits into from
Nov 23, 2022

Conversation

harrysolovay
Copy link
Contributor

This PR refactors RPC subscriptions / the Capi-wide listener interface such that one can return (and infer types from) results.

Previously...

const root = C.chain.unsubscribeNewHeads(T.polkadot)(
  C.chain.subscribeNewHeads(T.polkadot)([], function(header) {
    if (someCondition)
      return this.stop()
  }),
)

With these changes...

const root = C.chain.subscribeNewHeads(T.polkadot)([], (ctx) =>
  (header) => {
    if (someCondition)
      return ctx.stop()
  })

The calling of a corresponding unsubscribe call method is now taken care of at the RPC level. This means we no longer need to return the subscription ID from RPC subscription effects, which allows us to return other values.

const header = C.chain.subscribeNewHeads(T.polkadot)([], (ctx) =>
  (header) => {
    if (header.number > THRESHOLD)
      return ctx.stop(header)
  })

header can be utilized as any other effect. This enables myriad new possibilities for modeling effects that observe and react to on-chain events.

@harrysolovay harrysolovay force-pushed the subscription-results branch 2 times, most recently from 367b2e8 to 6328dbf Compare November 19, 2022 21:12
@harrysolovay harrysolovay requested a review from kratico November 19, 2022 21:16
tjjfvi
tjjfvi previously approved these changes Nov 22, 2022
util/mapping.ts Outdated Show resolved Hide resolved
examples/raw_rpc_client_subscription.ts Show resolved Hide resolved
kratico
kratico previously approved these changes Nov 22, 2022
rpc/client.test.ts Outdated Show resolved Hide resolved
rpc/client.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@kratico kratico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@harrysolovay harrysolovay merged commit d7fcd39 into main Nov 23, 2022
@harrysolovay harrysolovay deleted the subscription-results branch November 23, 2022 13:57
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants