Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hilla: document request options #4142

Merged
merged 3 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions articles/hilla/lit/guides/endpoints.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,31 @@
endif::[]


== Request Options

All regular Hilla requests support some options as the last parameter. They're listed in the sections here.


=== Abort Signal

Check warning on line 425 in articles/hilla/lit/guides/endpoints.adoc

View workflow job for this annotation

GitHub Actions / lint

[vale] reported by reviewdog 🐶 [Vaadin.Terms] Prefer 'stop', 'exit', 'cancel', or 'end' over 'Abort'. Raw Output: {"message": "[Vaadin.Terms] Prefer 'stop', 'exit', 'cancel', or 'end' over 'Abort'.", "location": {"path": "articles/hilla/lit/guides/endpoints.adoc", "range": {"start": {"line": 425, "column": 5}}}, "severity": "WARNING"}

You can pass an https://developer.mozilla.org/en-US/docs/Web/API/AbortController/signal[AbortSignal] object to the call to cancel a request. This is useful when you want to cancel a request, perhaps because you've decide you don't need it after all or because it's taking too long.

[source,typescript]
----
include::{root}{root-fix}/frontend/demo/fusion/options/request-options.ts[tag=abort,indent=0]
----


=== Suppress Connection State

By default, when a request takes too long, Hilla shows a progress bar to the user at the top of the page. You can suppress this by setting the `mute` option to `true`.

[source,typescript]
----
include::{root}{root-fix}/frontend/demo/fusion/options/request-options.ts[tag=mute,indent=0]
----


== Code Completion in IDEs

As you can see in the earlier [filename]`CounterEndpoint.ts` example, the Javadoc for the `@Endpoint` class is copied to the generated TypeScript file, and the type definitions are maintained. This helps code completion to work -- at least in Visual Studio Code and IntelliJ IDEA Ultimate Edition.
Expand Down
23 changes: 23 additions & 0 deletions frontend/demo/fusion/options/request-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { SlowService } from 'Frontend/generated/endpoints';

export function abortCall() {
// tag::abort[]
const controller = new AbortController();

SlowService.takesTime({ signal: controller.signal }).then((msg) => console.log(msg));

// The `AbortController` can be used to abort the request, for example after a timeout
// or an user action like a button click
const timeoutId = setTimeout(() => {
controller.abort();
}, 5000);
// end::abort[]

return () => clearTimeout(timeoutId);
}

export function muteCall() {
// tag::mute[]
SlowService.takesTime({ mute: true }).then((msg) => console.log(msg));
// end::mute[]
}
23 changes: 23 additions & 0 deletions src/main/java/com/vaadin/demo/fusion/options/SlowService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.vaadin.demo.fusion.options;

import org.jspecify.annotations.NonNull;

import com.vaadin.flow.server.auth.AnonymousAllowed;
import com.vaadin.hilla.BrowserCallable;

@BrowserCallable
@AnonymousAllowed
public class SlowService {

@NonNull
public String takesTime() {
try {
Thread.sleep(10000); // Simulate a long process
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return "Process was interrupted";
}
return "Process completed";
}

}