Skip to content

Commit

Permalink
feat(bounties): make watch API singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
voliva committed Jan 15, 2025
1 parent 95b4db8 commit 86e44b8
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/sdk-governance/src/bounties/bounties-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export function createBountiesSdk(typedApi: BountiesSdkTypedApi): BountiesSdk {
}

return {
watchBounties,
watch: watchBounties(),
getBounties,
getBounty,
getProposedBounty,
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk-governance/src/bounties/child-bounties-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ChildBounty,
GenericChildBounty,
} from "./child-sdk-types"
import { keyedMemo } from "@/util/memo"

export function createChildBountiesSdk(
typedApi: ChildBountiesSdkTypedApi,
Expand Down Expand Up @@ -180,7 +181,7 @@ export function createChildBountiesSdk(
}

return {
watchChildBounties,
watch: keyedMemo(watchChildBounties, new Map()),
getChildBounty,
}
}
2 changes: 1 addition & 1 deletion packages/sdk-governance/src/bounties/child-sdk-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export type ChildBounty =
| PendingPayoutChildBounty

export interface ChildBountiesSdk {
watchChildBounties(parentId: number): {
watch(parentId: number): {
bounties$: Observable<Map<number, ChildBounty>>
bountyIds$: Observable<number[]>
getBountyById$: (key: number) => Observable<ChildBounty>
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk-governance/src/bounties/find-referenda.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OngoingReferendum } from "@/referenda/sdk-types"
import { weakMemo } from "@/util/memo"
import { keyedMemo } from "@/util/memo"
import { MultiAddress } from "./descriptors"

const spenderOrigins = [
Expand All @@ -11,7 +11,7 @@ const spenderOrigins = [
"BigTipper",
]

const getDecodedSpenderReferenda = weakMemo(
const getDecodedSpenderReferenda = keyedMemo(
async (ongoingReferenda: OngoingReferendum[]) => {
const spenderReferenda = ongoingReferenda.filter(
(ref) =>
Expand All @@ -35,6 +35,7 @@ const getDecodedSpenderReferenda = weakMemo(
)
return response.filter((v) => !!v)
},
new WeakMap(),
)

export async function findApprovingReferenda(
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-governance/src/bounties/sdk-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export type Bounty =
| PendingPayoutBounty

export interface BountiesSdk {
watchBounties(): {
watch: {
bounties$: Observable<Map<number, Bounty>>
bountyIds$: Observable<number[]>
getBountyById$: (key: number) => Observable<Bounty>
Expand Down
10 changes: 6 additions & 4 deletions packages/sdk-governance/src/util/memo.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
export const weakMemo = <Arg extends [object], R>(fn: (...arg: Arg) => R) => {
const cache = new WeakMap<Arg[0], R>()
return (...arg: Arg) => {
export const keyedMemo =
<Arg extends [any], R>(
fn: (...arg: Arg) => R,
cache: Map<Arg[0], R> | WeakMap<Arg[0], R>,
) =>
(...arg: Arg) => {
if (cache.has(arg[0])) return cache.get(arg[0])!
const result = fn(...arg)
cache.set(arg[0], result)
return result
}
}

export const memo = <Arg extends Array<unknown>, R>(fn: (...arg: Arg) => R) => {
let cachedKey: Arg | null = null
Expand Down

0 comments on commit 86e44b8

Please sign in to comment.