From e736141023efc192d02b2f83fac1a49ef7b478b7 Mon Sep 17 00:00:00 2001 From: Harry Solovay Date: Wed, 21 Dec 2022 12:05:05 -0800 Subject: [PATCH] formatting --- codegen/typeVisitor.ts | 7 +-- effects/blockWatch.ts | 21 +++---- examples/batch.ts | 14 ++--- examples/multisig_transfer.ts | 28 ++++----- examples/polkadot_js_signer.ts | 14 ++--- examples/transfer.ts | 18 +++--- rpc/client.ts | 108 ++++++++++++++++----------------- test_util/extrinsic.ts | 14 ++--- 8 files changed, 105 insertions(+), 119 deletions(-) diff --git a/codegen/typeVisitor.ts b/codegen/typeVisitor.ts index 9fc8e593f..b7d260a42 100644 --- a/codegen/typeVisitor.ts +++ b/codegen/typeVisitor.ts @@ -201,13 +201,12 @@ export function createTypeVisitor(props: CodegenProps, files: Files) { const name = path.slice(path.lastIndexOf(".") + 1) const docs = makeDocComment(ty.docs) - const fallback = (key: keyof M.TyVisitorMethods) => - (...args: any) => { - return `\ + const fallback = (key: keyof M.TyVisitorMethods) => (...args: any) => { + return `\ ${docs} export type ${name} = ${(visitor[key] as any)!(...args)} ` - } + } const codec = ty.type === "Compact" ? "" : `\ export const $${name[0]!.toLowerCase()}${name.slice(1)}: $.Codec<${ diff --git a/effects/blockWatch.ts b/effects/blockWatch.ts index a2cb78a31..90d7fb170 100644 --- a/effects/blockWatch.ts +++ b/effects/blockWatch.ts @@ -12,18 +12,17 @@ export function blockWatch>(client: Client) { >(createListener: CreateListener) => { const createListenerMapped = Z .ls(createListener, Z.env) - .next(([createListener, env]) => - (ctx: rpc.ClientSubscriptionContext) => { - const inner = createListener({ ...ctx, env }) - return async (header: rpc.known.Header) => { - const blockHash = chain.getBlockHash(client)(header.number) - const block = await chain.getBlock(client)(blockHash).bind(env)() - if (block instanceof Error) { - return ctx.end(block) - } - return inner(block) as U.InnerEnd> + .next(([createListener, env]) => (ctx: rpc.ClientSubscriptionContext) => { + const inner = createListener({ ...ctx, env }) + return async (header: rpc.known.Header) => { + const blockHash = chain.getBlockHash(client)(header.number) + const block = await chain.getBlock(client)(blockHash).bind(env)() + if (block instanceof Error) { + return ctx.end(block) } - }, k0_) + return inner(block) as U.InnerEnd> + } + }, k0_) return chain.subscribeNewHeads(client)([], createListenerMapped) } } diff --git a/examples/batch.ts b/examples/batch.ts index a5f01dd7e..585770706 100644 --- a/examples/batch.ts +++ b/examples/batch.ts @@ -28,14 +28,12 @@ const tx = extrinsic({ }), }) .signed(T.alice.sign) - .watch((ctx) => - (status) => { - console.log(status) - if (C.rpc.known.TransactionStatus.isTerminal(status)) { - return ctx.end() - } - return + .watch((ctx) => (status) => { + console.log(status) + if (C.rpc.known.TransactionStatus.isTerminal(status)) { + return ctx.end() } - ) + return + }) U.throwIfError(await tx.run()) diff --git a/examples/multisig_transfer.ts b/examples/multisig_transfer.ts index 777132296..f002465d9 100644 --- a/examples/multisig_transfer.ts +++ b/examples/multisig_transfer.ts @@ -29,15 +29,13 @@ const existentialDeposit = extrinsic({ }), }) .signed(T.alice.sign) - .watch(({ end }) => - (status) => { - console.log(`Existential deposit:`, status) - if (C.rpc.known.TransactionStatus.isTerminal(status)) { - return end() - } - return + .watch(({ end }) => (status) => { + console.log(`Existential deposit:`, status) + if (C.rpc.known.TransactionStatus.isTerminal(status)) { + return end() } - ) + return + }) // First approval root const proposal = createOrApproveMultisigProposal("Proposal", T.alice) @@ -97,13 +95,11 @@ function createOrApproveMultisigProposal< })), }) .signed(pair.sign) - .watch(({ end }) => - (status) => { - console.log(`${label}:`, status) - if (C.rpc.known.TransactionStatus.isTerminal(status)) { - return end() - } - return + .watch(({ end }) => (status) => { + console.log(`${label}:`, status) + if (C.rpc.known.TransactionStatus.isTerminal(status)) { + return end() } - ) + return + }) } diff --git a/examples/polkadot_js_signer.ts b/examples/polkadot_js_signer.ts index 04e076e60..0b76dcd39 100644 --- a/examples/polkadot_js_signer.ts +++ b/examples/polkadot_js_signer.ts @@ -27,14 +27,12 @@ const root = C.extrinsic(T.westend)({ ) }, }) - .watch((ctx) => - (status) => { - console.log(status) - if (C.rpc.known.TransactionStatus.isTerminal(status)) { - return ctx.end() - } - return + .watch((ctx) => (status) => { + console.log(status) + if (C.rpc.known.TransactionStatus.isTerminal(status)) { + return ctx.end() } - ) + return + }) U.throwIfError(await root.run()) diff --git a/examples/transfer.ts b/examples/transfer.ts index 3fc3c539c..2d287762c 100644 --- a/examples/transfer.ts +++ b/examples/transfer.ts @@ -14,17 +14,15 @@ const tx = extrinsic({ }) .signed(T.alice.sign) -const finalizedIn = tx.watch(({ end }) => - (status) => { - console.log(status) - if (typeof status !== "string" && status.finalized) { - return end(status.finalized) - } else if (C.rpc.known.TransactionStatus.isTerminal(status)) { - return end(new NeverFinalized()) - } - return +const finalizedIn = tx.watch(({ end }) => (status) => { + console.log(status) + if (typeof status !== "string" && status.finalized) { + return end(status.finalized) + } else if (C.rpc.known.TransactionStatus.isTerminal(status)) { + return end(new NeverFinalized()) } -) + return +}) console.log(U.throwIfError(await C.events(tx, finalizedIn).run())) diff --git a/rpc/client.ts b/rpc/client.ts index a21d39483..eaedbd9f7 100644 --- a/rpc/client.ts +++ b/rpc/client.ts @@ -86,64 +86,64 @@ export class Client< Params extends unknown[] = any[], NotificationData = any, >(): SubscriptionFactory => - ( - subscribeMethod, - unsubscribeMethod, - params, - createListener, - ) => { - const id = this.providerRef.nextId() - const waiter = deferred() // TODO: type this narrowly - const stop = async (value?: unknown) => { - delete this.pendingSubscriptions[id] - const activeSubscriptionId = this.activeSubscriptionByMessageId[id] - if (activeSubscriptionId) { - delete this.activeSubscriptions[activeSubscriptionId] - await this.call( - this.providerRef.nextId(), - unsubscribeMethod, - [activeSubscriptionId], - ) - } - delete this.activeSubscriptionByMessageId[id] - waiter.resolve(value) + ( + subscribeMethod, + unsubscribeMethod, + params, + createListener, + ) => { + const id = this.providerRef.nextId() + const waiter = deferred() // TODO: type this narrowly + const stop = async (value?: unknown) => { + delete this.pendingSubscriptions[id] + const activeSubscriptionId = this.activeSubscriptionByMessageId[id] + if (activeSubscriptionId) { + delete this.activeSubscriptions[activeSubscriptionId] + await this.call( + this.providerRef.nextId(), + unsubscribeMethod, + [activeSubscriptionId], + ) } - const listener = createListener({ - end(value = undefined!) { - return new U.End(value) - }, - endIfError(value) { - return (value instanceof Error ? new U.End(value) : undefined) as any - }, - }) - this.pendingSubscriptions[id] = (event) => { - const result = listener(event) - if (result instanceof Promise) { - result.then((resultR) => maybeStop(resultR, event)) - } else { - maybeStop(result, event) - } + delete this.activeSubscriptionByMessageId[id] + waiter.resolve(value) + } + const listener = createListener({ + end(value = undefined!) { + return new U.End(value) + }, + endIfError(value) { + return (value instanceof Error ? new U.End(value) : undefined) as any + }, + }) + this.pendingSubscriptions[id] = (event) => { + const result = listener(event) + if (result instanceof Promise) { + result.then((resultR) => maybeStop(resultR, event)) + } else { + maybeStop(result, event) } - ;(async () => { - const maybeError = await this.call(id, subscribeMethod, params) - if (maybeError instanceof Error || maybeError.error) { - listener(maybeError) - stop() - } - })() - return waiter - - function maybeStop( - maybeEnd: U.ListenerResult, - event: ClientSubscriptionEvent, - ) { - if (maybeEnd instanceof U.End) { - stop(maybeEnd.value) - } else if (event instanceof Error) { - stop() - } + } + ;(async () => { + const maybeError = await this.call(id, subscribeMethod, params) + if (maybeError instanceof Error || maybeError.error) { + listener(maybeError) + stop() + } + })() + return waiter + + function maybeStop( + maybeEnd: U.ListenerResult, + event: ClientSubscriptionEvent, + ) { + if (maybeEnd instanceof U.End) { + stop(maybeEnd.value) + } else if (event instanceof Error) { + stop() } } + } discard = () => { this.pendingCalls = {} diff --git a/test_util/extrinsic.ts b/test_util/extrinsic.ts index 0b69fd86e..0fd03851c 100644 --- a/test_util/extrinsic.ts +++ b/test_util/extrinsic.ts @@ -15,15 +15,13 @@ export function collectExtrinsicEvents< >(extrinsic: SignedExtrinsic) { const events: rpc.known.TransactionStatus[] = [] return extrinsic - .watch(({ end }) => - (status) => { - events.push(status) - if (rpc.known.TransactionStatus.isTerminal(status)) { - return end() - } - return + .watch(({ end }) => (status) => { + events.push(status) + if (rpc.known.TransactionStatus.isTerminal(status)) { + return end() } - ) + return + }) .next(() => events, k0_) }