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

[zh-cn]: translation for background sync api #19593

Merged
merged 17 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
92 changes: 92 additions & 0 deletions files/zh-cn/web/api/background_synchronization_api/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
title: Background Synchronization API
slug: Web/API/Background_Synchronization_API
l10n:
sourceCommit: 56df677713fecf43ec0eb8862cb91c141aaa0005
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
---

{{DefaultAPISidebar("Background Sync")}}{{Securecontext_Header}}{{AvailableInWorkers}}

**Background Synchronization API** 使 Web 应用程序能够推迟任务,以便一旦用户拥有稳定的网络连接,它们就可以在 [Service Worker](/zh-CN/docs/Web/API/Service_Worker_API) 中运行。

## 概念和用法

如果设备离线,Background Synchronization API 允许 Web 应用程序将服务器同步工作交给 Service Worker 延迟处理。用途可能包括在后台发送请求,如果在使用应用程序时无法发送它们。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

例如,电子邮件客户端应用程序也可以允许其用户撰写和发送消息在任意时间,即使设备没有网络连接。应用程序前端仅注册一个同步请求,并且当网络再次存在时,Service Worker 会收到警报并处理同步。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

{{domxref('SyncManager')}} 接口允许通过 {{domxref('ServiceWorkerRegistration.sync')}} 使用。使用唯一的标识符来设置同步事件的“名称”, 然后可以在 {{domxref('ServiceWorker')}} 脚本中监听。一旦收到事件,你可以运行任何可用的功能,比如向服务器发送请求。

由于此 API 依赖于 Service Worker,因此此 API 提供的功能仅在安全上下文(HTTPS)中可用。

## 接口

- {{domxref('SyncManager')}} {{Experimental_Inline}}
- : 注册网络恢复后需要在 Service Worker 中运行的任务。这些任务称为 _后台同步请求(background sync requests)_。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
- {{domxref('SyncEvent')}} {{Experimental_Inline}}
- : 代表同步事件,发送到 {{domxref('ServiceWorker')}} 的[全局作用域](/zh-CN/docs/Web/API/ServiceWorkerGlobalScope)。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

### 其他接口的扩展
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

[Service Worker API](/zh-CN/docs/Web/API/Service_Worker_API) 的以下新增内容提供了用于设置后台同步。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

- {{domxref("ServiceWorkerRegistration.sync")}} {{ReadOnlyInline}}
- : 返回 {{domxref("SyncManager")}} 的引用,用于注册在设备具有网络连接后运行的任务。
- {{domxref("ServiceWorkerGlobalScope/sync_event", "sync")}} 事件
- : 一旦网络可用,会立刻触发 {{domxref("ServiceWorkerGlobalScope/sync_event", "sync")}} 的 handler 执行工作。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

## 示例

以下示例展示了如何使用该接口。

### 请求后台同步

以下异步函数在浏览器的上下文注册后台同步任务:

```js
async function syncMessagesLater() {
const registration = await navigator.serviceWorker.ready;
try {
await registration.sync.register("sync-messages");
} catch {
console.log("Background Sync could not be registered!");
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
}
}
```

### 通过标签校验后台同步是否已请求

此代码检查是否注册了给定标签的后台同步任务。

```js
navigator.serviceWorker.ready.then((registration) => {
registration.sync.getTags().then((tags) => {
if (tags.includes("sync-messages"))
console.log("Messages sync already requested");
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
});
});
```

### 在 Service Worker 中监听后台同步

以下示例显示了如何在 Service Worker 中响应后台同步事件。

```js
self.addEventListener("sync", (event) => {
if (event.tag === "sync-messages") {
event.waitUntil(sendOutboxMessages());
}
});
```

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}

## 参见

- [Introducing Background Sync](https://developer.chrome.com/blog/background-sync/)
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
title: "ServiceWorkerGlobalScope: sync event"
short-title: sync
slug: Web/API/ServiceWorkerGlobalScope/sync_event
l10n:
sourceCommit: 56df677713fecf43ec0eb8862cb91c141aaa0005
---
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

{{APIRef("Background Sync")}}{{SecureContext_Header}}{{AvailableInWorkers("service")}}

当页面(或 worker)使用 {{domxref('SyncManager')}} 注册的事件正在运行,并且一旦网络连接可用时,将触发 {{domxref("ServiceWorkerGlobalScope")}} 接口的 **`sync`** 事件。

此事件不能取消,也不会冒泡。

## 语法

像 {{domxref("EventTarget.addEventListener", "addEventListener()")}} 一样在方法中使用事件名称,或设置事件处理器属性。

```js
addEventListener("sync", (event) => {});

onsync = (event) => {};
```

## 事件类型

{{domxref("SyncEvent")}} 继承自 {{domxref("ExtendableEvent")}} 和 {{domxref("Event")}}。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

{{InheritanceDiagram("SyncEvent")}}

## 事件属性

_继承属性自其父级 {{domxref("ExtendableEvent")}} 和 {{domxref("Event")}}_。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

- {{domxref("SyncEvent.tag")}} {{ReadOnlyInline}}
- : 返回此 `SyncEvent` 的开发人员定义的标识符。
- {{domxref("SyncEvent.lastChance")}} {{ReadOnlyInline}}
- : 如果用户代理在当前尝试之后不再进行进一步的同步尝试,则返回 `true`
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

## 示例

以下示例显示了如何在 Service Worker 中响应同步事件。

```js
self.addEventListener("sync", (event) => {
if (event.tag === "sync-messages") {
event.waitUntil(sendOutboxMessages());
}
});
```

你也可以通过 `onsync` 属性设置事件处理器:

```js
self.onsync = (event) => {
// ...
};
```

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}

## See also
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

- [Richer offline experiences with the Periodic Background Sync API](https://developer.chrome.com/docs/capabilities/periodic-background-sync)
- [A Periodic Background Sync demo app](https://webplatformapis.com/periodic_sync/periodicSync_improved.html)
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions files/zh-cn/web/api/serviceworkerregistration/sync/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: "ServiceWorkerRegistration: sync 属性"
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
short-title: sync
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
slug: Web/API/ServiceWorkerRegistration/sync
l10n:
sourceCommit: 56df677713fecf43ec0eb8862cb91c141aaa0005
---

{{APIRef("Background Sync")}}{{SecureContext_Header}}{{AvailableInWorkers}}

{{domxref("ServiceWorkerRegistration")}} 接口的只读属性 **`sync`**,返回 {{domxref("SyncManager")}} 的引用,该接口管理后台同步执行。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

## 值

{{domxref("SyncManager")}} 对象。

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}
38 changes: 38 additions & 0 deletions files/zh-cn/web/api/syncevent/index.md
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: SyncEvent
slug: Web/API/SyncEvent
l10n:
sourceCommit: 56df677713fecf43ec0eb8862cb91c141aaa0005
---

{{APIRef("Background Sync")}}{{AvailableInWorkers("service")}}

{{domxref("Background Synchronization API", "", "", "nocode")}} 的 **`SyncEvent`** 接口,表示在 ServiceWorker 的 {{domxref("ServiceWorkerGlobalScope")}} 上分派的同步操作。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

此接口继承自 {{domxref("ExtendableEvent")}} 接口。

{{InheritanceDiagram}}

## 构造函数

- {{domxref("SyncEvent.SyncEvent", "SyncEvent()")}}
- : 创建一个新的 `SyncEvent` 对象。

## 实例属性

_继承属性自其父级 {{domxref("ExtendableEvent")}} 和 {{domxref("Event")}}_。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

- {{domxref("SyncEvent.tag")}} {{ReadOnlyInline}}
- : 返回此 `SyncEvent` 的开发人员定义的标识符。
- {{domxref("SyncEvent.lastChance")}} {{ReadOnlyInline}}
- : 如果用户代理在当前尝试之后不再进行进一步的同步尝试,则返回 `true`
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

## 实例方法

_继承方法自其父级 {{domxref("ExtendableEvent")}} 和 {{domxref("Event")}}_。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

None.
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

## 浏览器兼容性

{{Compat}}
24 changes: 24 additions & 0 deletions files/zh-cn/web/api/syncevent/lastchance/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: "SyncEvent: lastChance 属性"
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
short-title: lastChance
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
slug: Web/API/SyncEvent/lastChance
l10n:
sourceCommit: 56df677713fecf43ec0eb8862cb91c141aaa0005
---

{{APIRef("Background Sync")}}{{AvailableInWorkers("service")}}

如果用户代理在当前尝试之后不会进行进一步的同步尝试,接口 {{domxref("SyncEvent")}} 的只读属性 **`lastChance`** 将返回 `true`。
这是通过 {{domxref("SyncEvent.SyncEvent","SyncEvent()")}} 构造函数的 `lastChance` 参数中传递值。
jasonren0403 marked this conversation as resolved.
Show resolved Hide resolved

## 值

一个布尔值,指示用户代理是否不会进一步在当前尝试后继续尝试同步。

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}
40 changes: 40 additions & 0 deletions files/zh-cn/web/api/syncevent/syncevent/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: "SyncEvent: SyncEvent() 构造函数"
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
short-title: SyncEvent()
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
slug: Web/API/SyncEvent/SyncEvent
l10n:
sourceCommit: 56df677713fecf43ec0eb8862cb91c141aaa0005
---

{{APIRef("Background Sync")}}{{AvailableInWorkers("service")}}

**`SyncEvent()`** 构造函数创建一个新的 {{domxref("SyncEvent")}} 对象。

## 语法

```js-nolint
new SyncEvent(type, options)
```

### 参数

- `type`
- : 字符串,用于事件名称。它区分大小写,浏览器总是将其设置为 `sync`。
- `options`
- : 一个 object 对象,除了 {{domxref("ExtendableEvent/ExtendableEvent", "ExtendableEvent()")}} 中定义的属性外,还可以包含以下属性:
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
- `tag`
- : 开发人员为此 `SyncEvent` 定义的标识符。
- `lastChance` {{optional_inline}}
- : 一个布尔值,指示用户代理在当前尝试之后不会进行进一步的同步尝试。默认为 `false`。

### 返回值

一个新的 {{domxref("SyncEvent")}} 对象。

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}
24 changes: 24 additions & 0 deletions files/zh-cn/web/api/syncevent/tag/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: "SyncEvent: tag 属性"
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
short-title: tag
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
slug: Web/API/SyncEvent/tag
l10n:
sourceCommit: 56df677713fecf43ec0eb8862cb91c141aaa0005
---

{{APIRef("Background Sync")}}{{AvailableInWorkers("service")}}

接口 {{domxref("SyncEvent")}} 的只读属性 **`tag`** 将返回开发人员为此 `SyncEvent` 定义的标识符。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
这是通过 {{domxref("SyncEvent.SyncEvent","SyncEvent()")}} 构造函数的 `tag` 参数中传递值。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

## 值

开发人员为此 `SyncEvent` 定义的标识符。

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}
33 changes: 33 additions & 0 deletions files/zh-cn/web/api/syncmanager/gettags/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: "SyncManager: getTags() method"
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
short-title: getTags()
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
slug: Web/API/SyncManager/getTags
l10n:
sourceCommit: 56df677713fecf43ec0eb8862cb91c141aaa0005
---

{{APIRef("Background Sync")}}{{AvailableInWorkers}}

{{domxref("SyncManager")}} 接口的 **`getTags()`** 方法返回开发人员定义的用于 `SyncManager` 注册的标识符列表。

## 语法

```js-nolint
getTags()
```

### 参数

None.
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

### 返回值

一个 {{jsxref("Promise")}},resolve 为包含开发人员定义的用于 `SyncManager` 注册的标识符的字符串数组。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}
29 changes: 29 additions & 0 deletions files/zh-cn/web/api/syncmanager/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
title: SyncManager
slug: Web/API/SyncManager
l10n:
sourceCommit: 56df677713fecf43ec0eb8862cb91c141aaa0005
---

{{APIRef("Background Sync")}}{{AvailableInWorkers}}

{{domxref("Background Synchronization API", "", "", "nocode")}} 的 **`SyncManager`** 接口,提供了用于注册和列出同步注册的功能。
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

## 实例属性

None.
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

## 示例方法
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved

- {{domxref("SyncManager.register()")}}
- : 创建一个新的同步注册并返回 {{jsxref("Promise")}}.
xyy94813 marked this conversation as resolved.
Show resolved Hide resolved
- {{domxref("SyncManager.getTags()")}}
- : 返回开发人员定义的 `SyncManager` 注册标识符列表。

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}
Loading