Skip to content

Commit 70d181e

Browse files
authored
Remove p-limit from JS client (#24)
1 parent fa2e0db commit 70d181e

File tree

3 files changed

+52
-38
lines changed

3 files changed

+52
-38
lines changed

clients/js/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
"@solana-program/compute-budget": "^0.9.0",
5353
"@solana-program/system": "^0.8.0",
5454
"commander": "^13.0.0",
55-
"p-limit": "^7.1.1",
5655
"pako": "^2.1.0",
5756
"picocolors": "^1.1.1",
5857
"yaml": "^2.7.0"

clients/js/pnpm-lock.yaml

Lines changed: 0 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clients/js/src/internals.ts

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
TransactionPlanner,
2525
TransactionSigner,
2626
} from '@solana/kit';
27-
import { limitFunction } from 'p-limit';
2827
import { findMetadataPda, SeedArgs } from './generated';
2928
import { getProgramAuthority } from './utils';
3029

@@ -88,25 +87,22 @@ export function createDefaultTransactionPlannerAndExecutor(input: {
8887
});
8988

9089
const executor = createTransactionPlanExecutor({
91-
executeTransactionMessage: limitFunction(
92-
async (message, config) => {
93-
const { value: latestBlockhash } = await input.rpc
94-
.getLatestBlockhash()
95-
.send();
96-
const transaction = await pipe(
97-
setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, message),
98-
async (m) => await estimateAndSetCULimit(m, config),
99-
async (m) => await signTransactionMessageWithSigners(await m, config)
100-
);
101-
assertIsSendableTransaction(transaction);
102-
await sendAndConfirmTransaction(transaction, {
103-
...config,
104-
commitment: 'confirmed',
105-
});
106-
return { transaction };
107-
},
108-
{ concurrency: input.concurrency ?? 5 }
109-
),
90+
executeTransactionMessage: limitFunction(async (message, config) => {
91+
const { value: latestBlockhash } = await input.rpc
92+
.getLatestBlockhash()
93+
.send();
94+
const transaction = await pipe(
95+
setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, message),
96+
async (m) => await estimateAndSetCULimit(m, config),
97+
async (m) => await signTransactionMessageWithSigners(await m, config)
98+
);
99+
assertIsSendableTransaction(transaction);
100+
await sendAndConfirmTransaction(transaction, {
101+
...config,
102+
commitment: 'confirmed',
103+
});
104+
return { transaction };
105+
}, input.concurrency ?? 5),
110106
});
111107

112108
return { planner, executor };
@@ -123,3 +119,39 @@ export async function isValidInstructionPlan(
123119
return false;
124120
}
125121
}
122+
123+
function limitFunction<TArguments extends unknown[], TReturnType>(
124+
fn: (...args: TArguments) => PromiseLike<TReturnType>,
125+
concurrency: number
126+
): (...args: TArguments) => Promise<TReturnType> {
127+
let running = 0;
128+
const queue: Array<{
129+
args: TArguments;
130+
resolve: (value: TReturnType) => void;
131+
reject: (reason?: unknown) => void;
132+
}> = [];
133+
134+
function process() {
135+
// Do nothing if we're still running at max concurrency
136+
// or if there's nothing left to process.
137+
if (running >= concurrency || queue.length === 0) return;
138+
139+
running++;
140+
const { args, resolve, reject } = queue.shift()!;
141+
142+
Promise.resolve(fn(...args))
143+
.then(resolve)
144+
.catch(reject)
145+
.finally(() => {
146+
running--;
147+
process();
148+
});
149+
}
150+
151+
return function (...args) {
152+
return new Promise((resolve, reject) => {
153+
queue.push({ args, resolve, reject });
154+
process();
155+
});
156+
};
157+
}

0 commit comments

Comments
 (0)