如何获取AWS Amplify数据存储的当前状态?

时间:2020-06-08 12:53:13

标签: aws-amplify

使用AWS Amplify数据存储区时,很难知道数据存储区将具有哪种行为。数据存储区已同步还是未同步。数据存储区API没有获取当前状态的API方法。

1 个答案:

答案 0 :(得分:1)

在数据存储版本2.2.2中,您可以收听事件中心。

频道为datastore

注册到频道

import { Hub } from '@aws-amplify/core';

Hub.listen ("datastore", (data) => {
        console.log ('A new event has happened:', data);
})

示例输出

await DataStore.clear();
await DataStore.start();

生成以下输出:

A new event has happened:  {"channel":"datastore","payload":{"event":"storageSubscribed"},"source":"","patternInfo":[]}

A new event has happened:  {"channel":"datastore","payload":{"event":"networkStatus","data":{"active":true}},"source":"","patternInfo":[]}

A new event has happened:  {"channel":"datastore","payload":{"event":"outboxStatus","data":{"isEmpty":true}},"source":"","patternInfo":[]}

A new event has happened:  {"channel":"datastore","payload":{"event":"subscriptionsEstablished"},"source":"","patternInfo":[]}

A new event has happened:  {"channel":"datastore","payload":{"event":"syncQueriesStarted","data":{"models":["ModelX","ModelY","ModelLala"]}},"source":"","patternInfo":[]}

A new event has happened:  {"channel":"datastore","payload":{"event":"modelSynced","data":{"isFullSync":true,"isDeltaSync":false,"counts":{"new":5,"updated":0,"deleted":2}}},"source":"","patternInfo":[]}

A new event has happened:  {"channel":"datastore","payload":{"event":"modelSynced","data":{"isFullSync":true,"isDeltaSync":false,"counts":{"new":296,"updated":0,"deleted":2}}},"source":"","patternInfo":[]}

A new event has happened:  {"channel":"datastore","payload":{"event":"modelSynced","data":{"isFullSync":true,"isDeltaSync":false,"counts":{"new":8155,"updated":0,"deleted":0}}},"source":"","patternInfo":[]}

A new event has happened:  {"channel":"datastore","payload":{"event":"syncQueriesReady"},"source":"","patternInfo":[]}

A new event has happened:  {"channel":"datastore","payload":{"event":"ready"},"source":"","patternInfo":[]}

另请参见:

https://github.com/aws-amplify/amplify-js/pull/5942

https://github.com/aws-amplify/amplify-js/issues/4808

https://docs.amplify.aws/lib/utilities/hub/q/platform/js#channels