Skip to content

Commit 95a6e4a

Browse files
authored
Merge pull request #29 from OpenZeppelin/debounce-compile-calls
debounce calls to compiler api
2 parents e6ff053 + 9a444d1 commit 95a6e4a

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

src/lib/utils/helpers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@ export const isUpgradeable = (sources?: ContractSources) => {
2626
if (!sources) return false;
2727
return Object.keys(sources).some((path) => path.includes('@openzeppelin/contracts-upgradeable'));
2828
}
29+
30+
export const debouncer = (fn: (...args: any[]) => void, delay: number) => {
31+
let timeout: NodeJS.Timeout;
32+
return (...args: any[]) => {
33+
clearTimeout(timeout);
34+
timeout = setTimeout(() => fn(...args), delay);
35+
};
36+
}

src/lib/wizard/components/Deploy.svelte

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
import { addAPToDropdown, findDeploymentEnvironment, globalState } from "$lib/state/state.svelte";
1010
import { attempt } from "$lib/utils/attempt";
1111
import { encodeConstructorArgs, getConstructorInputsWizard, getContractBytecode } from "$lib/utils/contracts";
12-
import { isMultisig, isUpgradeable } from "$lib/utils/helpers";
12+
import { debouncer, isMultisig, isUpgradeable } from "$lib/utils/helpers";
1313
import Button from "./shared/Button.svelte";
1414
import Input from "./shared/Input.svelte";
1515
import Message from "./shared/Message.svelte";
1616
17+
// debounce the compile call to avoid sending too many requests while the user is editing.
18+
const compileDebounced = debouncer(compile, 600);
19+
1720
let inputsWithValue = $state<Record<string, string | number | boolean>>({});
18-
let busy = $state(false);
21+
let isDeploying = $state(false);
1922
let successMessage = $state<string>("");
2023
let errorMessage = $state<string>("");
2124
let compilationError = $state<string>("");
@@ -24,6 +27,7 @@
2427
let deploymentResult = $state<DeploymentResult | undefined>(undefined);
2528
let isDeterministic = $state(false);
2629
let salt: string = $state("");
30+
let isCompiling = $state(false);
2731
2832
let contractBytecode = $derived.by(() => {
2933
if (!globalState.contract?.target || !compilationResult) return;
@@ -61,12 +65,12 @@
6165
: undefined
6266
);
6367
64-
6568
let inputs: ABIParameter[] = $state([]);
6669
6770
$effect(() => {
6871
if (globalState.contract?.source?.sources) {
69-
compile();
72+
isCompiling = true;
73+
compileDebounced();
7074
}
7175
});
7276
@@ -75,7 +79,7 @@
7579
inputsWithValue[target.name] = target.value;
7680
}
7781
78-
async function compile() {
82+
async function compile(): Promise<void> {
7983
const sources = globalState.contract?.source?.sources;
8084
if (!sources) {
8185
return;
@@ -94,6 +98,7 @@
9498
if (globalState.contract?.target && compilationResult) {
9599
inputs = getConstructorInputsWizard(globalState.contract.target, compilationResult.output.contracts);
96100
}
101+
isCompiling = false;
97102
}
98103
99104
function displayMessage(message: string, type: "success" | "error") {
@@ -304,9 +309,9 @@
304309
};
305310
306311
async function triggerDeploy() {
307-
busy = true;
312+
isDeploying = true;
308313
await deploy();
309-
busy = false;
314+
isDeploying = false;
310315
}
311316
312317
</script>
@@ -316,7 +321,9 @@
316321
<Message type="warn" message="Upgradable contracts are not yet fully supported. This action will only deploy the implementation contract without initializing. <br />We recommend using <u><a href='https://github.com/OpenZeppelin/openzeppelin-upgrades' target='_blank'>openzeppelin-upgrades</a></u> package instead." />
317322
{/if}
318323

319-
{#if inputs.length > 0}
324+
{#if isCompiling}
325+
<Message type="loading" message="Compiling..." />
326+
{:else if inputs.length > 0}
320327
<h6 class="text-sm">Constructor Arguments</h6>
321328
{#each inputs as input}
322329
<Input name={input.name} placeholder={`${input.name} (${input.type})`} onchange={handleInputChange} value={''} type="text"/>
@@ -352,7 +359,7 @@
352359
<Message message={compilationError} type="error" />
353360
{/if}
354361

355-
<Button disabled={!globalState.authenticated || busy} loading={busy} label="Deploy" onClick={triggerDeploy} />
362+
<Button disabled={!globalState.authenticated || isDeploying || isCompiling} loading={isDeploying} label="Deploy" onClick={triggerDeploy} />
356363

357364
{#if successMessage || errorMessage}
358365
<Message message={successMessage || errorMessage} type={successMessage ? "success" : "error"} />

src/lib/wizard/components/shared/Message.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
type Props = {
33
message: string;
4-
type: 'success' | 'error' | 'warn' | 'info';
4+
type: 'success' | 'error' | 'warn' | 'info' | 'loading';
55
};
66
77
let { message, type }: Props = $props();
@@ -27,5 +27,10 @@
2727
<i class={`fa fa-info-circle text-blue-600`}></i>
2828
<div class="text-xs text-blue-600">{@html message}</div>
2929
</div>
30+
{:else if type === 'loading'}
31+
<div class="flex flex-row items-center gap-2">
32+
<i class={"fa fa-circle-o-notch fa-spin text-blue-600"}></i>
33+
<div class="text-xs text-blue-600">{@html message}</div>
34+
</div>
3035
{/if}
3136

0 commit comments

Comments
 (0)