Loading...
Loading...
Compare original and translation side by side
The initialization of the client is synchronous. The async work is stored as a property you can await, while passing the reference around.
客户端的初始化是同步的。异步操作被存储为一个可等待的属性,同时可以传递对象引用。
awaitawait getX()awaitawait getX()await// This doesn't work
export const client = await createClient(); // Top-level await breaks bundlerslet client: Client | null = null;
export async function getClient() {
if (!client) {
client = await createClient();
}
return client;
}
// Every consumer must await
const client = await getClient();await// 这种写法无法正常工作
export const client = await createClient(); // 顶层await会导致打包工具出错let client: Client | null = null;
export async function getClient() {
if (!client) {
client = await createClient();
}
return client;
}
// 所有使用方都必须添加await
const client = await getClient();await// client.ts
export const client = createClient();
// Sync access works immediately
client.save(data);
client.load(id);
// Await the async work when you need to
await client.whenSynced;whenSynced// client.ts
export const client = createClient();
// 可立即进行同步访问
client.save(data);
client.load(id);
// 需要时再等待异步操作完成
await client.whenSynced;whenSynced<!-- +layout.svelte -->
<script>
import { client } from '$lib/client';
</script>
{#await client.whenSynced}
<LoadingSpinner />
{:then}
{@render children?.()}
{/await}<!-- +layout.svelte -->
<script>
import { client } from '$lib/client';
</script>
{#await client.whenSynced}
<LoadingSpinner />
{:then}
{@render children?.()}
{/await}withCapabilities()function createClient() {
const state = initializeSyncState();
return {
save(data) {
/* sync method */
},
load(id) {
/* sync method */
},
withCapabilities({ persistence }) {
const whenSynced = persistence(state);
return Object.assign(this, { whenSynced });
},
};
}
// Usage
export const client = createClient().withCapabilities({
persistence: (state) => loadFromIndexedDB(state),
});withCapabilities()function createClient() {
const state = initializeSyncState();
return {
save(data) {
/* 同步方法 */
},
load(id) {
/* 同步方法 */
},
withCapabilities({ persistence }) {
const whenSynced = persistence(state);
return Object.assign(this, { whenSynced });
},
};
}
// 使用示例
export const client = createClient().withCapabilities({
persistence: (state) => loadFromIndexedDB(state),
});| Aspect | Async Construction | Sync + whenSynced |
|---|---|---|
| Module export | Can't export directly | Export the object |
| Consumer code | | Direct import, sync use |
| UI integration | Awkward promise handling | Single |
| Type signature | | |
| 维度 | 异步构造 | 同步构造+whenSynced |
|---|---|---|
| 模块导出 | 无法直接导出 | 可直接导出对象 |
| 消费端代码 | 到处都是 | 直接导入,同步使用 |
| UI集成 | 繁琐的Promise处理 | 单个 |
| 类型签名 | | 带 |
const provider = new IndexeddbPersistence('my-db', doc);
// Constructor returns immediately
provider.on('update', handleUpdate); // Sync access works
await provider.whenSynced; // Wait when you need toconst provider = new IndexeddbPersistence('my-db', doc);
// 构造函数立即返回
provider.on('update', handleUpdate); // 可直接同步访问
await provider.whenSynced; // 需要时再等待