Annotation Interface Callable
Marks an annotated method as a method which can be called from browsers.
import org.wcardinal.controller.annotation.Callable;
import org.wcardinal.controller.annotation.Controller;
@Controller
class MyController {
@Callable
String hello(String name) {
return "Hello, " + name + "!";
}
}
In browsers, MyController#hello(String) can be called as follows:
<script src="my-controller"></script> <script> console.log(await myController.hello("Cardinal")); // Prints "Hello, Cardinal!" </script>
Thread Safety
When a method is called, a controller owing the method is locked. Accessing fields of the controller owing the method, thus, is thread safe. This default behavior can be changed. Please refer to@Unlocked.
Type Declaration for TypeScript
In the TypeScript projects, the type declaration ofMyController shown in above will look like this.
import { controller } from "@wcardinal/wcardinal";
interface MyController extends controller.Controller {
hello: controller.Callable<string, [name: string]>;
}
If methods like `controller.Controller#on(string, function): this` and
`controller.Callable#timeout(number)` aren't mandatory, the declaration
can be simplified to:
interface MyController {
hello(name: string): string;
}- 関連項目: