Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: store both recurring/one-time grants & spents; add state #379

Merged
merged 24 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
821d623
feat(background): store both recurring/one-time grants & balances
sidvishnoi Jun 28, 2024
0514f88
add `state`; -hasHostPermissions -> +state=`missing_host_permissions`
sidvishnoi Jun 28, 2024
b5dd9c0
package.json: loosen up `engines.node` (matters only for dev)
sidvishnoi Jun 28, 2024
aca5097
nit: reduce number of variable assignments
sidvishnoi Jun 28, 2024
e61cc6f
fix: store `transformedAmount` instead of `amount` as balance
sidvishnoi Jun 28, 2024
21debf0
show combined balance in Settings
sidvishnoi Jun 28, 2024
40e07a0
lint
sidvishnoi Jun 28, 2024
a831a03
Merge branch 'main' into storage-migration-1
sidvishnoi Jul 1, 2024
cd87323
add comment
sidvishnoi Jul 1, 2024
87058e8
run migrate only on update
sidvishnoi Jul 1, 2024
22507a3
add todo comment on revoking grants
sidvishnoi Jul 1, 2024
87038e0
rename `ok` to `hasPermissions`
sidvishnoi Jul 1, 2024
9435de4
use AmountValue in Amount and AccessToken also
sidvishnoi Jul 1, 2024
67cea99
nit: format comment [ci skip]
sidvishnoi Jul 1, 2024
7b387c8
nit: move a comment
sidvishnoi Jul 1, 2024
e94b8de
feat(background/storage): add `setRemainingBalance` helper
sidvishnoi Jul 1, 2024
7af2c8f
instead of balance, store amountSpent (which the API returns)
sidvishnoi Jul 2, 2024
8a99905
remove Storage.setSpentAmount, DebounceWithQueue for now
sidvishnoi Jul 2, 2024
46fa29e
Merge branch 'main' into storage-migration-1
sidvishnoi Jul 2, 2024
25a77ea
nit: Storage.getBalance
sidvishnoi Jul 2, 2024
3e3872a
Merge branch 'storage-migration-1' of github.com:interledger/web-mone…
sidvishnoi Jul 2, 2024
85e43a8
nit: WalletInformation
sidvishnoi Jul 2, 2024
c372788
nit: WalletInformation
sidvishnoi Jul 2, 2024
b0be514
fix computeBalance logic
sidvishnoi Jul 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/background/services/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
} from '@/shared/types'
import { type Browser } from 'webextension-polyfill'
import { EventsService } from './events'
import { DebounceWithQueue } from '@/shared/helpers'

const defaultStorage = {
/**
Expand All @@ -31,10 +32,25 @@ const defaultStorage = {
} satisfies Omit<Storage, 'publicKey' | 'privateKey' | 'keyId'>

export class StorageService {
private setRemainingBalanceRecurring: DebounceWithQueue<[amount: string]>
private setRemainingBalanceOneTime: DebounceWithQueue<[amount: string]>

constructor(
private browser: Browser,
private events: EventsService
) {}
) {
const bigIntMin = (a: string, b: string) => (BigInt(a) < BigInt(b) ? a : b)
this.setRemainingBalanceRecurring = new DebounceWithQueue(
(amount: string) => this.set({ recurringGrantRemainingBalance: amount }),
(args) => [args.reduce((m, [e]) => (!m ? e : bigIntMin(m, e)), '')],
sidvishnoi marked this conversation as resolved.
Show resolved Hide resolved
1000
)
this.setRemainingBalanceOneTime = new DebounceWithQueue(
(amount: string) => this.set({ oneTimeGrantRemainingBalance: amount }),
(args) => [args.reduce((m, [e]) => (!m ? e : bigIntMin(m, e)), '')],
1000
)
}

async get<TKey extends StorageKey>(
keys?: TKey[]
Expand Down Expand Up @@ -137,6 +153,14 @@ export class StorageService {
return true
}

setRemainingBalance(grant: GrantDetails['type'], balance: string) {
if (grant === 'recurring') {
this.setRemainingBalanceRecurring.enqueue(balance)
} else if (grant === 'one-time') {
this.setRemainingBalanceOneTime.enqueue(balance)
}
}

async updateRate(rate: string): Promise<void> {
await this.set({ rateOfPay: rate })
this.events.emit('storage.rate_of_pay_update', { rate })
Expand Down
42 changes: 42 additions & 0 deletions src/shared/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,48 @@ export function debounceAsync<T extends unknown[], R extends Promise<unknown>>(
}
}

/**
* Debounce a function, while allowing the queued arguments to be reduced before
* the function is called. With args reducer, we can call the debounced function
* with first/last/merged arguments etc.
*
* @example
* ```ts
* const debounceWithQueue = new DebounceWithQueue(
* (total: number) => saveToStorage(total),
* (collectedArgs) => collectedArgs.reduce(total, [val] => total + val, 0),
* wait
* )
* debounceWithQueue.enqueue(10)
* debounceWithQueue.enqueue(15)
* // results in saveToStorage(25)
* ```
*/
export class DebounceWithQueue<Args extends unknown[]> {
sidvishnoi marked this conversation as resolved.
Show resolved Hide resolved
private argsList: Args[] = []
private func: () => Promise<unknown>

constructor(
func: (...args: Args) => Promise<unknown>,
argsReducer: (args: Args[]) => [...Args],
wait: number
) {
this.func = debounceAsync(() => {
if (this.argsList.length > 0) {
const args = argsReducer(this.argsList.slice())
this.argsList = []
return func(...args)
}
return Promise.resolve()
}, wait)
}

enqueue(...data: Args) {
this.argsList.push(data)
void this.func()
}
}

export function debounceSync<T extends unknown[], R>(
func: (...args: T) => R,
wait: number
Expand Down
Loading