feat: add commitment param to subscription apis

This commit is contained in:
Justin Starry
2020-05-21 12:17:38 +08:00
committed by Michael Vines
parent 803910bf08
commit 722adb66c2
4 changed files with 35 additions and 4 deletions

3
web3.js/module.d.ts vendored
View File

@ -255,11 +255,13 @@ declare module '@solana/web3.js' {
onAccountChange( onAccountChange(
publickey: PublicKey, publickey: PublicKey,
callback: AccountChangeCallback, callback: AccountChangeCallback,
commitment?: Commitment,
): number; ): number;
removeAccountChangeListener(id: number): Promise<void>; removeAccountChangeListener(id: number): Promise<void>;
onProgramAccountChange( onProgramAccountChange(
programId: PublicKey, programId: PublicKey,
callback: ProgramAccountChangeCallback, callback: ProgramAccountChangeCallback,
commitment?: Commitment,
): number; ): number;
removeProgramAccountChangeListener(id: number): Promise<void>; removeProgramAccountChangeListener(id: number): Promise<void>;
onSlotChange(callback: SlotChangeCallback): number; onSlotChange(callback: SlotChangeCallback): number;
@ -267,6 +269,7 @@ declare module '@solana/web3.js' {
onSignature( onSignature(
signature: TransactionSignature, signature: TransactionSignature,
callback: SignatureResultCallback, callback: SignatureResultCallback,
commitment?: Commitment,
): number; ): number;
removeSignatureListener(id: number): Promise<void>; removeSignatureListener(id: number): Promise<void>;
onRootChange(callback: RootChangeCallback): number; onRootChange(callback: RootChangeCallback): number;

View File

@ -268,11 +268,13 @@ declare module '@solana/web3.js' {
onAccountChange( onAccountChange(
publickey: PublicKey, publickey: PublicKey,
callback: AccountChangeCallback, callback: AccountChangeCallback,
commitment: ?Commitment,
): number; ): number;
removeAccountChangeListener(id: number): Promise<void>; removeAccountChangeListener(id: number): Promise<void>;
onProgramAccountChange( onProgramAccountChange(
programId: PublicKey, programId: PublicKey,
callback: ProgramAccountChangeCallback, callback: ProgramAccountChangeCallback,
commitment: ?Commitment,
): number; ): number;
removeProgramAccountChangeListener(id: number): Promise<void>; removeProgramAccountChangeListener(id: number): Promise<void>;
onSlotChange(callback: SlotChangeCallback): number; onSlotChange(callback: SlotChangeCallback): number;
@ -280,6 +282,7 @@ declare module '@solana/web3.js' {
onSignature( onSignature(
signature: TransactionSignature, signature: TransactionSignature,
callback: SignatureResultCallback, callback: SignatureResultCallback,
commitment: ?Commitment,
): number; ): number;
removeSignatureListener(id: number): Promise<void>; removeSignatureListener(id: number): Promise<void>;
onRootChange(callback: RootChangeCallback): number; onRootChange(callback: RootChangeCallback): number;

View File

@ -759,6 +759,7 @@ type SubscriptionId = 'subscribing' | number;
type AccountSubscriptionInfo = { type AccountSubscriptionInfo = {
publicKey: string, // PublicKey of the account as a base 58 string publicKey: string, // PublicKey of the account as a base 58 string
callback: AccountChangeCallback, callback: AccountChangeCallback,
commitment: ?Commitment,
subscriptionId: ?SubscriptionId, // null when there's no current server subscription id subscriptionId: ?SubscriptionId, // null when there's no current server subscription id
}; };
@ -776,6 +777,7 @@ export type ProgramAccountChangeCallback = (
type ProgramAccountSubscriptionInfo = { type ProgramAccountSubscriptionInfo = {
programId: string, // PublicKey of the program as a base 58 string programId: string, // PublicKey of the program as a base 58 string
callback: ProgramAccountChangeCallback, callback: ProgramAccountChangeCallback,
commitment: ?Commitment,
subscriptionId: ?SubscriptionId, // null when there's no current server subscription id subscriptionId: ?SubscriptionId, // null when there's no current server subscription id
}; };
@ -806,6 +808,7 @@ export type SignatureResultCallback = (
type SignatureSubscriptionInfo = { type SignatureSubscriptionInfo = {
signature: TransactionSignature, // TransactionSignature as a base 58 string signature: TransactionSignature, // TransactionSignature as a base 58 string
callback: SignatureResultCallback, callback: SignatureResultCallback,
commitment: ?Commitment,
subscriptionId: ?SubscriptionId, // null when there's no current server subscription id subscriptionId: ?SubscriptionId, // null when there's no current server subscription id
}; };
@ -1753,12 +1756,20 @@ export class Connection {
for (let id of accountKeys) { for (let id of accountKeys) {
const sub = this._accountChangeSubscriptions[id]; const sub = this._accountChangeSubscriptions[id];
this._subscribe(sub, 'accountSubscribe', [sub.publicKey]); this._subscribe(
sub,
'accountSubscribe',
this._argsWithCommitment([sub.publicKey], sub.commitment),
);
} }
for (let id of programKeys) { for (let id of programKeys) {
const sub = this._programAccountChangeSubscriptions[id]; const sub = this._programAccountChangeSubscriptions[id];
this._subscribe(sub, 'programSubscribe', [sub.programId]); this._subscribe(
sub,
'programSubscribe',
this._argsWithCommitment([sub.programId], sub.commitment),
);
} }
for (let id of slotKeys) { for (let id of slotKeys) {
@ -1768,7 +1779,11 @@ export class Connection {
for (let id of signatureKeys) { for (let id of signatureKeys) {
const sub = this._signatureSubscriptions[id]; const sub = this._signatureSubscriptions[id];
this._subscribe(sub, 'signatureSubscribe', [sub.signature]); this._subscribe(
sub,
'signatureSubscribe',
this._argsWithCommitment([sub.signature], sub.commitment),
);
} }
for (let id of rootKeys) { for (let id of rootKeys) {
@ -1810,18 +1825,21 @@ export class Connection {
/** /**
* Register a callback to be invoked whenever the specified account changes * Register a callback to be invoked whenever the specified account changes
* *
* @param publickey Public key of the account to monitor * @param publicKey Public key of the account to monitor
* @param callback Function to invoke whenever the account is changed * @param callback Function to invoke whenever the account is changed
* @param commitment Specify the commitment level account changes must reach before notification
* @return subscription id * @return subscription id
*/ */
onAccountChange( onAccountChange(
publicKey: PublicKey, publicKey: PublicKey,
callback: AccountChangeCallback, callback: AccountChangeCallback,
commitment: ?Commitment,
): number { ): number {
const id = ++this._accountChangeSubscriptionCounter; const id = ++this._accountChangeSubscriptionCounter;
this._accountChangeSubscriptions[id] = { this._accountChangeSubscriptions[id] = {
publicKey: publicKey.toBase58(), publicKey: publicKey.toBase58(),
callback, callback,
commitment,
subscriptionId: null, subscriptionId: null,
}; };
this._updateSubscriptions(); this._updateSubscriptions();
@ -1887,16 +1905,19 @@ export class Connection {
* *
* @param programId Public key of the program to monitor * @param programId Public key of the program to monitor
* @param callback Function to invoke whenever the account is changed * @param callback Function to invoke whenever the account is changed
* @param commitment Specify the commitment level account changes must reach before notification
* @return subscription id * @return subscription id
*/ */
onProgramAccountChange( onProgramAccountChange(
programId: PublicKey, programId: PublicKey,
callback: ProgramAccountChangeCallback, callback: ProgramAccountChangeCallback,
commitment: ?Commitment,
): number { ): number {
const id = ++this._programAccountChangeSubscriptionCounter; const id = ++this._programAccountChangeSubscriptionCounter;
this._programAccountChangeSubscriptions[id] = { this._programAccountChangeSubscriptions[id] = {
programId: programId.toBase58(), programId: programId.toBase58(),
callback, callback,
commitment,
subscriptionId: null, subscriptionId: null,
}; };
this._updateSubscriptions(); this._updateSubscriptions();
@ -2011,16 +2032,19 @@ export class Connection {
* *
* @param signature Transaction signature string in base 58 * @param signature Transaction signature string in base 58
* @param callback Function to invoke on signature notifications * @param callback Function to invoke on signature notifications
* @param commitment Specify the commitment level signature must reach before notification
* @return subscription id * @return subscription id
*/ */
onSignature( onSignature(
signature: TransactionSignature, signature: TransactionSignature,
callback: SignatureResultCallback, callback: SignatureResultCallback,
commitment: ?Commitment,
): number { ): number {
const id = ++this._signatureSubscriptionCounter; const id = ++this._signatureSubscriptionCounter;
this._signatureSubscriptions[id] = { this._signatureSubscriptions[id] = {
signature, signature,
callback, callback,
commitment,
subscriptionId: null, subscriptionId: null,
}; };
this._updateSubscriptions(); this._updateSubscriptions();

View File

@ -1840,6 +1840,7 @@ test('account change notification', async () => {
const subscriptionId = connection.onAccountChange( const subscriptionId = connection.onAccountChange(
programAccount.publicKey, programAccount.publicKey,
mockCallback, mockCallback,
'recent',
); );
const balanceNeeded = Math.max( const balanceNeeded = Math.max(