Skip to content

Commit 2310ec1

Browse files
J1-takaivasfvitor
andauthored
i18n(ja): Add Japanese Texts to Develop secton (part 1) (#3268)
Co-authored-by: Vitor Ayres <[email protected]> Co-authored-by: Vitor Ayres <[email protected]>
1 parent aab7860 commit 2310ec1

File tree

11 files changed

+2350
-3
lines changed

11 files changed

+2350
-3
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
title: Rust からフロントエンドを呼び出す
3+
sidebar:
4+
order: 1
5+
---
6+
7+
`@tauri-apps/api` NPM パッケージは、グローバル・イベントと Webview 固有のイベントの両方を検知(リッスン)するための API を提供しています。
8+
9+
- グローバル・イベントの検知
10+
11+
```ts
12+
import { listen } from '@tauri-apps/api/event';
13+
14+
type DownloadStarted = {
15+
url: string;
16+
downloadId: number;
17+
contentLength: number;
18+
};
19+
20+
listen<DownloadStarted>('download-started', (event) => {
21+
console.log(
22+
`downloading ${event.payload.contentLength} bytes from ${event.payload.url}`
23+
);
24+
});
25+
```
26+
27+
- Webview 固有イベントの検知
28+
29+
```ts
30+
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow';
31+
32+
const appWebview = getCurrentWebviewWindow();
33+
appWebview.listen<string>('logged-in', (event) => {
34+
localStorage.setItem('session-token', event.payload);
35+
});
36+
```
37+
38+
The `listen` 関数は、アプリケーションの全「[ライフタイム](https://doc.rust-lang.org/rust-by-example/ja/scope/lifetime.html)」期間中、イベント・リスナーの登録は維持されたままです。
39+
イベントの検知(リッスン)を停止するには、`listen` 関数によって返される `unlisten` 関数を使用できます:
40+
41+
```js
42+
import { listen } from '@tauri-apps/api/event';
43+
44+
const unlisten = await listen('download-started', (event) => {});
45+
unlisten();
46+
```
47+
48+
:::note
49+
コンポーネントがアンマウントされるときなど、実行コンテキストがスコープ(有効範囲)外になる場合は、常に `unlisten` 関数を使用してください。
50+
51+
ページが再読み込みされるか、別の URL に移動すると、リスナーは自動的に登録解除されます。
52+
ただし、この仕組みは「シングル・ページ・アプリケーション(SPA)」ルーターには適用されません。
53+
54+
> > > 《訳注》 **シングル・ページ・アプリケーション** 一つのページでコンテンツの切り替えを行なうアプリケーションや Web ページの構造。詳しくは [Wikipedia](https://ja.wikipedia.org/wiki/シングルページアプリケーション) を参照してください。
55+
> > > :::
56+
57+
さらに、Tauri ではイベントを一度だけ検知(リッスン)するためのユーティリティ関数を提供しています:
58+
59+
```js
60+
import { once } from '@tauri-apps/api/event';
61+
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow';
62+
63+
once('ready', (event) => {});
64+
65+
const appWebview = getCurrentWebviewWindow();
66+
appWebview.once('ready', () => {});
67+
```
68+
69+
:::note
70+
フロントエンドで発行されたイベントは、上記の API によって登録されたリスナーもトリガーします。
71+
詳細については、次章 [フロントエンドから Rust を呼び出す] の説明を参照してください。
72+
:::
73+
74+
#### Rust のイベントを検知
75+
76+
グローバル・イベントも Webview 固有のイベントも、Rust に登録されたリスナーに配信されます。
77+
78+
- グローバル・イベントの検知
79+
80+
```rust title="src-tauri/src/lib.rs"
81+
use tauri::Listener;
82+
83+
#[cfg_attr(mobile, tauri::mobile_entry_point)]
84+
pub fn run() {
85+
tauri::Builder::default()
86+
.setup(|app| {
87+
app.listen("download-started", |event| {
88+
if let Ok(payload) = serde_json::from_str::<DownloadStarted>(&event.payload()) {
89+
println!("downloading {}", payload.url);
90+
}
91+
});
92+
Ok(())
93+
})
94+
.run(tauri::generate_context!())
95+
.expect("error while running tauri application");
96+
}
97+
```
98+
99+
- Webview 固有イベントの検知
100+
101+
```rust title="src-tauri/src/lib.rs"
102+
use tauri::{Listener, Manager};
103+
104+
#[cfg_attr(mobile, tauri::mobile_entry_point)]
105+
pub fn run() {
106+
tauri::Builder::default()
107+
.setup(|app| {
108+
let webview = app.get_webview_window("main").unwrap();
109+
webview.listen("logged-in", |event| {
110+
let session_token = event.data;
111+
// save token..
112+
});
113+
Ok(())
114+
})
115+
.run(tauri::generate_context!())
116+
.expect("error while running tauri application");
117+
}
118+
```
119+
120+
The `listen` 関数は、アプリケーションの全「ライフタイム」期間中、イベント・リスナーの登録は維持されたままです。
121+
イベントの検知(リッスン)を停止するには、`listen` 関数によって返される `unlisten` 関数を使用できます:
122+
123+
```rust
124+
// unlisten outside of the event handler scope:
125+
let event_id = app.listen("download-started", |event| {});
126+
app.unlisten(event_id);
127+
128+
// unlisten when some event criteria is matched
129+
let handle = app.handle().clone();
130+
app.listen("status-changed", |event| {
131+
if event.data == "ready" {
132+
handle.unlisten(event.id);
133+
}
134+
});
135+
```
136+
137+
さらに、Tauri はイベントを一度だけ検知(リッスン)するためのユーティリティ関数を提供しています:
138+
139+
```rust
140+
app.once("ready", |event| {
141+
println!("app is ready");
142+
});
143+
```
144+
145+
この場合、イベント・リスナーは最初のトリガー後にすぐに登録が解除されます。
146+
147+
[フロントエンドから Rust を呼び出す]: /ja/develop/calling-rust/

0 commit comments

Comments
 (0)