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

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
harrysolovay committed Dec 21, 2022
1 parent a94fcd6 commit e736141
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 119 deletions.
7 changes: 3 additions & 4 deletions codegen/typeVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>) =>
(...args: any) => {
return `\
const fallback = (key: keyof M.TyVisitorMethods<string>) => (...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<${
Expand Down
21 changes: 10 additions & 11 deletions effects/blockWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@ export function blockWatch<Client extends Z.$<rpc.Client>>(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<Z.T<CreateListener>>
.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<Z.T<CreateListener>>
}
}, k0_)
return chain.subscribeNewHeads(client)([], createListenerMapped)
}
}
Expand Down
14 changes: 6 additions & 8 deletions examples/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
28 changes: 12 additions & 16 deletions examples/multisig_transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
})
}
14 changes: 6 additions & 8 deletions examples/polkadot_js_signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
18 changes: 8 additions & 10 deletions examples/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()))

Expand Down
108 changes: 54 additions & 54 deletions rpc/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,64 +86,64 @@ export class Client<
Params extends unknown[] = any[],
NotificationData = any,
>(): SubscriptionFactory<Params, NotificationData, SendErrorData, HandlerErrorData> =>
(
subscribeMethod,
unsubscribeMethod,
params,
createListener,
) => {
const id = this.providerRef.nextId()
const waiter = deferred<any>() // 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<any>() // 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<any, any, SendErrorData, HandlerErrorData>,
) {
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<any, any, SendErrorData, HandlerErrorData>,
) {
if (maybeEnd instanceof U.End) {
stop(maybeEnd.value)
} else if (event instanceof Error) {
stop()
}
}
}

discard = () => {
this.pendingCalls = {}
Expand Down
14 changes: 6 additions & 8 deletions test_util/extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ export function collectExtrinsicEvents<
>(extrinsic: SignedExtrinsic<Client, Props, Sign>) {
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_)
}

Expand Down

0 comments on commit e736141

Please sign in to comment.