Skip to content

Commit

Permalink
Disputable Voting: partial application subscription methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bpierre committed Aug 24, 2020
1 parent 0e77afb commit 2076bc4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 47 deletions.
15 changes: 7 additions & 8 deletions packages/connect-voting-disputable/src/models/CastVote.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {
SubscriptionCallback,
SubscriptionHandler,
} from '@aragon/connect-types'

import Voter from './Voter'
import { SubscriptionCallback, SubscriptionResult } from '@aragon/connect-types'
import { subscription } from '@aragon/connect-core'
import { CastVoteData, IDisputableVotingConnector } from '../types'
import Voter from './Voter'

export default class CastVote {
#connector: IDisputableVotingConnector
Expand Down Expand Up @@ -33,7 +30,9 @@ export default class CastVote {
return this.#connector.voter(this.voterId)
}

onVoter(callback: SubscriptionCallback<Voter>): SubscriptionHandler {
return this.#connector.onVoter(this.voterId, callback)
onVoter(callback?: SubscriptionCallback<Voter>): SubscriptionResult<Voter> {
return subscription<Voter>(callback, (callback) =>
this.#connector.onVoter(this.voterId, callback)
)
}
}
60 changes: 36 additions & 24 deletions packages/connect-voting-disputable/src/models/DisputableVoting.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
Address,
SubscriptionCallback,
SubscriptionHandler,
SubscriptionResult,
} from '@aragon/connect-types'

import { subscription } from '@aragon/connect-core'
import { IDisputableVotingConnector } from '../types'
import Vote from './Vote'
import Voter from './Voter'
import Setting from './Setting'
import { IDisputableVotingConnector } from '../types'

export default class DisputableVoting {
#address: Address
Expand Down Expand Up @@ -46,9 +46,11 @@ export default class DisputableVoting {
}

onCurrentSetting(
callback: SubscriptionCallback<Setting>
): SubscriptionHandler {
return this.#connector.onCurrentSetting(this.#address, callback)
callback?: SubscriptionCallback<Setting>
): SubscriptionResult<Setting> {
return subscription<Setting>(callback, (callback) =>
this.#connector.onCurrentSetting(this.#address, callback)
)
}

async setting(settingNumber: string): Promise<Setting> {
Expand All @@ -57,9 +59,11 @@ export default class DisputableVoting {

onSetting(
settingNumber: string,
callback: SubscriptionCallback<Setting>
): SubscriptionHandler {
return this.#connector.onSetting(this.settingId(settingNumber), callback)
callback?: SubscriptionCallback<Setting>
): SubscriptionResult<Setting> {
return subscription<Setting>(callback, (callback) =>
this.#connector.onSetting(this.settingId(settingNumber), callback)
)
}

async settings({ first = 1000, skip = 0 } = {}): Promise<Setting[]> {
Expand All @@ -68,9 +72,11 @@ export default class DisputableVoting {

onSettings(
{ first = 1000, skip = 0 } = {},
callback: SubscriptionCallback<Setting[]>
): SubscriptionHandler {
return this.#connector.onSettings(this.#address, first, skip, callback)
callback?: SubscriptionCallback<Setting[]>
): SubscriptionResult<Setting[]> {
return subscription<Setting[]>(callback, (callback) =>
this.#connector.onSettings(this.#address, first, skip, callback)
)
}

async vote(voteId: string): Promise<Vote> {
Expand All @@ -79,9 +85,11 @@ export default class DisputableVoting {

onVote(
voteId: string,
callback: SubscriptionCallback<Vote>
): SubscriptionHandler {
return this.#connector.onVote(voteId, callback)
callback?: SubscriptionCallback<Vote>
): SubscriptionResult<Vote> {
return subscription<Vote>(callback, (callback) =>
this.#connector.onVote(voteId, callback)
)
}

async votes({ first = 1000, skip = 0 } = {}): Promise<Vote[]> {
Expand All @@ -90,23 +98,27 @@ export default class DisputableVoting {

onVotes(
{ first = 1000, skip = 0 } = {},
callback: SubscriptionCallback<Vote[]>
): SubscriptionHandler {
return this.#connector.onVotes(this.#address, first, skip, callback)
callback?: SubscriptionCallback<Vote[]>
): SubscriptionResult<Vote[]> {
return subscription<Vote[]>(callback, (callback) =>
this.#connector.onVotes(this.#address, first, skip, callback)
)
}

voterId(voterAddress: string): string {
voterId(voterAddress: Address): string {
return `${this.#address}-voterId-${voterAddress.toLowerCase()}`
}

async voter(voterAddress: string): Promise<Voter> {
async voter(voterAddress: Address): Promise<Voter> {
return this.#connector.voter(this.voterId(voterAddress))
}

onVoter(
voterAddress: string,
callback: SubscriptionCallback<Voter>
): SubscriptionHandler {
return this.#connector.onVoter(this.voterId(voterAddress), callback)
voterAddress: Address,
callback?: SubscriptionCallback<Voter>
): SubscriptionResult<Voter> {
return subscription<Voter>(callback, (callback) =>
this.#connector.onVoter(this.voterId(voterAddress), callback)
)
}
}
37 changes: 22 additions & 15 deletions packages/connect-voting-disputable/src/models/Vote.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { BigNumber } from 'ethers'
import {
Address,
SubscriptionCallback,
SubscriptionHandler,
SubscriptionResult,
} from '@aragon/connect-types'

import { subscription } from '@aragon/connect-core'
import { IDisputableVotingConnector, VoteData } from '../types'
import CastVote from './CastVote'
import CollateralRequirement from './CollateralRequirement'
import { IDisputableVotingConnector, VoteData } from '../types'

export default class Vote {
#connector: IDisputableVotingConnector
Expand Down Expand Up @@ -83,19 +84,21 @@ export default class Vote {
.toString()
}

castVoteId(voterAddress: string): string {
castVoteId(voterAddress: Address): string {
return `${this.id}-cast-${voterAddress.toLowerCase()}`
}

async castVote(voterAddress: string): Promise<CastVote | null> {
async castVote(voterAddress: Address): Promise<CastVote | null> {
return this.#connector.castVote(this.castVoteId(voterAddress))
}

onCastVote(
voterAddress: string,
callback: SubscriptionCallback<CastVote | null>
): SubscriptionHandler {
return this.#connector.onCastVote(this.castVoteId(voterAddress), callback)
voterAddress: Address,
callback?: SubscriptionCallback<CastVote | null>
): SubscriptionResult<CastVote | null> {
return subscription<CastVote | null>(callback, (callback) =>
this.#connector.onCastVote(this.castVoteId(voterAddress), callback)
)
}

async castVotes({ first = 1000, skip = 0 } = {}): Promise<CastVote[]> {
Expand All @@ -104,18 +107,22 @@ export default class Vote {

onCastVotes(
{ first = 1000, skip = 0 } = {},
callback: SubscriptionCallback<CastVote[]>
): SubscriptionHandler {
return this.#connector.onCastVotes(this.id, first, skip, callback)
callback?: SubscriptionCallback<CastVote[]>
): SubscriptionResult<CastVote[]> {
return subscription<CastVote[]>(callback, (callback) =>
this.#connector.onCastVotes(this.id, first, skip, callback)
)
}

async collateralRequirement(): Promise<CollateralRequirement> {
return this.#connector.collateralRequirement(this.id)
}

onCollateralRequirement(
callback: SubscriptionCallback<CollateralRequirement>
): SubscriptionHandler {
return this.#connector.onCollateralRequirement(this.id, callback)
callback?: SubscriptionCallback<CollateralRequirement>
): SubscriptionResult<CollateralRequirement> {
return subscription<CollateralRequirement>(callback, (callback) =>
this.#connector.onCollateralRequirement(this.id, callback)
)
}
}

0 comments on commit 2076bc4

Please sign in to comment.