Re-use bagList types (#4730)
This commit is contained in:
@@ -2,11 +2,11 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Observable } from 'rxjs';
|
||||
import type { Option, u64 } from '@polkadot/types';
|
||||
import type { Option } from '@polkadot/types';
|
||||
import type { PalletBagsListListBag } from '@polkadot/types/lookup';
|
||||
import type { BN } from '@polkadot/util';
|
||||
import type { DeriveApi } from '../types';
|
||||
import type { BagBase, BagList, BagListEntry } from './types';
|
||||
import type { Bag } from './types';
|
||||
|
||||
import { map, of, switchMap } from 'rxjs';
|
||||
|
||||
@@ -14,20 +14,15 @@ import { BN_ZERO, bnToBn } from '@polkadot/util';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
function unwrap (id: BN, opt: Option<PalletBagsListListBag>): BagBase {
|
||||
return {
|
||||
bag: opt.unwrapOr(null),
|
||||
id,
|
||||
key: id.toString()
|
||||
};
|
||||
}
|
||||
|
||||
function orderBags (ids: u64[], bags: Option<PalletBagsListListBag>[]): BagList {
|
||||
function orderBags (ids: BN[], bags: Option<PalletBagsListListBag>[]): Bag[] {
|
||||
const sorted = ids
|
||||
.map((id, index) => unwrap(id, bags[index]))
|
||||
.filter(({ bag }) => bag)
|
||||
.map((id, index) => ({
|
||||
bag: bags[index].unwrapOr(null),
|
||||
id,
|
||||
key: id.toString()
|
||||
}))
|
||||
.sort((a, b) => b.id.cmp(a.id))
|
||||
.map((base, index): BagListEntry => ({
|
||||
.map((base, index): Bag => ({
|
||||
...base,
|
||||
bagLower: BN_ZERO,
|
||||
bagUpper: base.id,
|
||||
@@ -43,30 +38,35 @@ function orderBags (ids: u64[], bags: Option<PalletBagsListListBag>[]): BagList
|
||||
);
|
||||
}
|
||||
|
||||
export function all (instanceId: string, api: DeriveApi): () => Observable<BagList> {
|
||||
return memo(instanceId, (): Observable<BagList> =>
|
||||
export function _getIds (instanceId: string, api: DeriveApi): (ids: (BN | number)[]) => Observable<Bag[]> {
|
||||
return memo(instanceId, (_ids: (BN | number)[]): Observable<Bag[]> => {
|
||||
const ids = _ids.map((id) => bnToBn(id));
|
||||
|
||||
return ids.length
|
||||
? api.query.listBags.multi<Option<PalletBagsListListBag>[]>(ids).pipe(
|
||||
map((bags) => orderBags(ids, bags))
|
||||
)
|
||||
: of([]);
|
||||
});
|
||||
}
|
||||
|
||||
export function all (instanceId: string, api: DeriveApi): () => Observable<Bag[]> {
|
||||
return memo(instanceId, (): Observable<Bag[]> =>
|
||||
api.query.bagsList.listBags.keys().pipe(
|
||||
switchMap((keys) => {
|
||||
if (!keys.length) {
|
||||
return of([]);
|
||||
}
|
||||
|
||||
const ids = keys.map(({ args: [id] }) => id);
|
||||
|
||||
return api.query.listBags.multi<Option<PalletBagsListListBag>[]>(ids).pipe(
|
||||
map((bags) => orderBags(ids, bags))
|
||||
);
|
||||
})
|
||||
switchMap((keys) =>
|
||||
api.derive.bagsList._getIds(keys.map(({ args: [id] }) => id))
|
||||
),
|
||||
map((list) =>
|
||||
list.filter(({ bag }) => bag)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
export function get (instanceId: string, api: DeriveApi): (id: BN | number) => Observable<BagBase> {
|
||||
return memo(instanceId, (_id: BN | number): Observable<BagBase> => {
|
||||
const id = bnToBn(_id);
|
||||
|
||||
return api.query.bagsList.listBags(id).pipe(
|
||||
map((o) => unwrap(id, o))
|
||||
);
|
||||
});
|
||||
export function get (instanceId: string, api: DeriveApi): (id: BN | number) => Observable<Bag> {
|
||||
return memo(instanceId, (id: BN | number): Observable<Bag> =>
|
||||
api.derive.bagsList._getIds([bnToBn(id)]).pipe(
|
||||
map((bags) => bags[0])
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -4,20 +4,15 @@
|
||||
import type { PalletBagsListListBag, PalletBagsListListNode } from '@polkadot/types/lookup';
|
||||
import type { BN } from '@polkadot/util';
|
||||
|
||||
export interface BagBase {
|
||||
export interface Bag {
|
||||
bag: PalletBagsListListBag | null;
|
||||
bagUpper: BN;
|
||||
bagLower: BN;
|
||||
id: BN;
|
||||
index: number;
|
||||
key: string;
|
||||
}
|
||||
|
||||
export interface Bag extends BagBase {
|
||||
export interface BagExpanded extends Bag {
|
||||
nodes: PalletBagsListListNode[];
|
||||
}
|
||||
|
||||
export interface BagListEntry extends BagBase {
|
||||
bagUpper: BN;
|
||||
bagLower: BN;
|
||||
index: number;
|
||||
}
|
||||
|
||||
export type BagList = BagListEntry[];
|
||||
|
||||
Reference in New Issue
Block a user