feat: add rangeRead
This commit is contained in:
parent
fee46c3710
commit
c0e6012d2f
33
packages/cwait/src/onchain/ops/rangeRead.ts
Normal file
33
packages/cwait/src/onchain/ops/rangeRead.ts
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import { Claim, ClaimKey, constructRangeRead, extractRead, isResolvedRead } from '@coinweb/contract-kit';
|
||||||
|
import { ClaimRange } from '@coinweb/contract-kit/dist/types/operations/read';
|
||||||
|
|
||||||
|
import { TypedClaim } from '../../types';
|
||||||
|
import { context } from '../context';
|
||||||
|
|
||||||
|
import { pushAwaitedOp } from './awaited';
|
||||||
|
import { shiftResolvedOp } from './resolved';
|
||||||
|
|
||||||
|
export const rangeReadOp = <TClaims extends Claim[] = TypedClaim[]>(
|
||||||
|
firstPart: ClaimKey['first_part'],
|
||||||
|
range: ClaimRange,
|
||||||
|
maxCount: number
|
||||||
|
) => {
|
||||||
|
return new Promise<TClaims | null>((resolve, reject) => {
|
||||||
|
try {
|
||||||
|
const { op, isOp } = shiftResolvedOp();
|
||||||
|
|
||||||
|
if (!isOp) {
|
||||||
|
pushAwaitedOp(constructRangeRead(context.issuer, firstPart, range, maxCount));
|
||||||
|
} else {
|
||||||
|
if (op && !isResolvedRead(op)) {
|
||||||
|
throw new Error('Read operation not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
const claim = op && ((extractRead(op)?.map((result) => result.content) ?? null) as TClaims | null);
|
||||||
|
resolve(claim);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
@ -18,7 +18,7 @@ export const readOp = <TClaim extends Claim = TypedClaim>(key: ClaimKey) => {
|
|||||||
throw new Error('Read operation not found');
|
throw new Error('Read operation not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
const claim = op && ((extractRead(op)?.[0].content ?? null) as TClaim | null);
|
const claim = op && ((extractRead(op)?.[0]?.content ?? null) as TClaim | null);
|
||||||
resolve(claim);
|
resolve(claim);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user