From 03337111ce50617ec5d21eb691811c93ebd5f986 Mon Sep 17 00:00:00 2001 From: Annie_wang Date: Tue, 13 Sep 2022 20:40:44 +0800 Subject: [PATCH] update docs Signed-off-by: Annie_wang --- .../apis/js-apis-distributed-data.md | 164 +++++++++++------- 1 file changed, 104 insertions(+), 60 deletions(-) diff --git a/en/application-dev/reference/apis/js-apis-distributed-data.md b/en/application-dev/reference/apis/js-apis-distributed-data.md index 5682921bad..a220f98c7c 100644 --- a/en/application-dev/reference/apis/js-apis-distributed-data.md +++ b/en/application-dev/reference/apis/js-apis-distributed-data.md @@ -61,7 +61,7 @@ export default class MyAbilityStage extends AbilityStage { console.log("Failed to create KVManager: " + JSON.stringify(err)); return; } - console.log("Created KVManager"); + console.log("Created KVManager successfully"); kvManager = manager; }); } @@ -89,7 +89,7 @@ export default class MyAbilityStage extends AbilityStage { console.log("Failed to create KVManager: " + JSON.stringify(err)); return; } - console.log("Created KVManager"); + console.log("Created KVManager successfully"); kvManager = manager; }); } @@ -139,7 +139,7 @@ export default class MyAbilityStage extends AbilityStage { console.log("Failed to create KVManager: " + JSON.stringify(err)); return; } - console.log("Created KVManager"); + console.log("Created KVManager successfully"); kvManager = manager; }); } @@ -167,7 +167,7 @@ export default class MyAbilityStage extends AbilityStage { console.log("Failed to create KVManager: " + JSON.stringify(err)); return; } - console.log("Created KVManager"); + console.log("Created KVManager successfully"); kvManager = manager; }); } @@ -606,7 +606,7 @@ try { off(event: 'distributedDataServiceDie', deathCallback?: Callback<void>): void -Unsubscribes from the service status changes. +Unsubscribes from service status changes. **System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore @@ -2393,7 +2393,7 @@ Subscribes to data change notifications of the specified type. | ----- | ------ | ---- | ----------------------- | | event |string | Yes |Event to subscribe to. The value is **dataChange**, which indicates data changes. | | type |[SubscribeType](#subscribetype) | Yes |Type of data changes. | -| listener |Callback<[ChangeNotification](#changenotification)> | Yes |Callback used to return the data changes.| +| listener |Callback<[ChangeNotification](#changenotification)> | Yes |Callback used to return data changes.| **Example** @@ -2442,25 +2442,34 @@ Unsubscribes from data change notifications. | Name | Type| Mandatory | Description | | ----- | ------ | ---- | ----------------------- | | event |string | Yes |Event to unsubscribe from. The value is **dataChange**, which indicates data changes. | -| listener |Callback<[ChangeNotification](#changenotification)> |No |Callback used to return the data changes.| +| listener |Callback<[ChangeNotification](#changenotification)> |No |Callback used to return data changes.| **Example** ```js let kvStore; -kvStore.on('dataChange', function (data) { - console.log("callback call data: " + data); -}); -kvStore.off('dataChange', function (data) { - console.log("callback call data: " + data); -}); +class KvstoreModel { + call(data) { + console.log("dataChange: " + data); + } + subscribeDataChange() { + if (kvStore != null) { + kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_REMOTE, this.call); + } + } + unsubscribeDataChange() { + if (kvStore != null) { + kvStore.off('dataChange', this.call); + } + } +} ``` ### off('syncComplete')9+ off(event: 'syncComplete', syncCallback?: Callback<Array<[string, number]>>): void -Unsubscribes from data change events. This API uses a synchronous callback to return the result. +Unsubscribes from data change events. This API returns the result synchronously. **System capability**: SystemCapability.DistributedDataManager.KVStore.Core @@ -2475,14 +2484,20 @@ Unsubscribes from data change events. This API uses a synchronous callback to re ```js let kvStore; -try { - const func = function (data) { - console.log('syncComplete ' + data) - }; - kvStore.on('syncComplete', func); - kvStore.off('syncComplete', func); -}catch(e) { - console.log('syncComplete e ' + e); +class KvstoreModel { + call(data) { + console.log("syncComplete: " + data); + } + subscribeSyncComplete() { + if (kvStore != null) { + kvStore.on('syncComplete', this.call); + } + } + unsubscribeSyncComplete() { + if (kvStore != null) { + kvStore.off('syncComplete', this.call); + } + } } ``` @@ -4047,7 +4062,7 @@ try { on(event: 'syncComplete', syncCallback: Callback<Array<[string, number]>>): void -Subscribes to the data synchronization complete events. +Subscribes to data synchronization complete events. **System capability**: SystemCapability.DistributedDataManager.KVStore.Core @@ -4056,7 +4071,7 @@ Subscribes to the data synchronization complete events. | Name | Type| Mandatory | Description | | ----- | ------ | ---- | ----------------------- | | event |string | Yes |Event to subscribe to. The value is **syncComplete**, which indicates completion of a data synchronization. | -| syncCallback |Callback<Array<[string, number]>> | Yes |Callback called to return the synchronization result. | +| syncCallback |Callback<Array<[string, number]>> | Yes |Callback used to return the synchronization result. | **Example** @@ -4083,7 +4098,7 @@ try { off(event: 'syncComplete', syncCallback?: Callback<Array<[string, number]>>): void -Unsubscribes from the data synchronization complete events. +Unsubscribes from data synchronization complete events. **System capability**: SystemCapability.DistributedDataManager.KVStore.Core @@ -4098,14 +4113,20 @@ Unsubscribes from the data synchronization complete events. ```js let kvStore; -try { - const func = function (data) { - console.log('syncComplete ' + data) - }; - kvStore.on('syncComplete', func); - kvStore.off('syncComplete', func); -}catch(e) { - console.log('syncComplete e ' + e); +class KvstoreModel { + call(data) { + console.log("syncComplete: " + data); + } + subscribeSyncComplete() { + if (kvStore != null) { + kvStore.on('syncComplete', this.call); + } + } + unsubscribeSyncComplete() { + if (kvStore != null) { + kvStore.off('syncComplete', this.call); + } + } } ``` @@ -4139,7 +4160,7 @@ kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_LOCAL, fun off(event:'dataChange', listener?: Callback<ChangeNotification>): void -Unsubscribes from the data change events. This API returns the result synchronously. +Unsubscribes from data change events. This API returns the result synchronously. **System capability**: SystemCapability.DistributedDataManager.KVStore.Core @@ -4154,12 +4175,21 @@ Unsubscribes from the data change events. This API returns the result synchronou ```js let kvStore; -kvStore.on('dataChange', function (data) { - console.log("callback call data: " + data); -}); -kvStore.off('dataChange', function (data) { - console.log("callback call data: " + data); -}); +class KvstoreModel { + call(data) { + console.log("dataChange: " + data); + } + subscribeDataChange() { + if (kvStore != null) { + kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_REMOTE, this.call); + } + } + unsubscribeDataChange() { + if (kvStore != null) { + kvStore.off('dataChange', this.call); + } + } +} ``` ### sync7+ @@ -5542,7 +5572,7 @@ try { on(event: 'syncComplete', syncCallback: Callback<Array<[string, number]>>): void -Subscribes to the data synchronization complete events. +Subscribes to data synchronization complete events. **System capability**: SystemCapability.DistributedDataManager.KVStore.Core @@ -5578,7 +5608,7 @@ try { off(event: 'syncComplete', syncCallback?: Callback<Array<[string, number]>>): void -Unsubscribes from the synchronization complete events. This API returns the result synchronously. +Unsubscribes from synchronization complete events. This API returns the result synchronously. **System capability**: SystemCapability.DistributedDataManager.KVStore.Core @@ -5593,14 +5623,20 @@ Unsubscribes from the synchronization complete events. This API returns the resu ```js let kvStore; -try { - const func = function (data) { - console.log('syncComplete ' + data) - }; - kvStore.on('syncComplete', func); - kvStore.off('syncComplete', func); -}catch(e) { - console.log('syncComplete e ' + e); +class KvstoreModel { + call(data) { + console.log("syncComplete: " + data); + } + subscribeSyncComplete() { + if (kvStore != null) { + kvStore.on('syncComplete', this.call); + } + } + unsubscribeSyncComplete() { + if (kvStore != null) { + kvStore.off('syncComplete', this.call); + } + } } ``` @@ -5618,7 +5654,7 @@ Subscribes to data changes of the specified type. This API returns the result sy | ----- | ------ | ---- | ----------------------- | | event |string | Yes |Event to subscribe to. The value is **dataChange**, which indicates data changes. | | type |[SubscribeType](#subscribetype) | Yes |Subscription type. | -| listener |Callback<[ChangeNotification](#changenotification)> | Yes |Callback used to return the result.| +| listener |Callback<[ChangeNotification](#changenotification)> | Yes |Callback used to return data changes.| **Example** @@ -5634,7 +5670,7 @@ kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_LOCAL, fun off(event:'dataChange', listener?: Callback<ChangeNotification>): void -Unsubscribes from the data change events. This API returns the result synchronously. +Unsubscribes from data change events. This API returns the result synchronously. **System capability**: SystemCapability.DistributedDataManager.KVStore.Core @@ -5643,18 +5679,27 @@ Unsubscribes from the data change events. This API returns the result synchronou | Name | Type| Mandatory | Description | | ----- | ------ | ---- | ----------------------- | | event |string | Yes |Event to unsubscribe from. The value is **dataChange**, which indicates data changes. | -| listener |Callback<[ChangeNotification](#changenotification)> |No |Callback used to return the result.| +| listener |Callback<[ChangeNotification](#changenotification)> |No |Callback used to return data changes.| **Example** ```js let kvStore; -kvStore.on('dataChange', function (data) { - console.log("callback call data: " + data); -}); -kvStore.off('dataChange', function (data) { - console.log("callback call data: " + data); -}); +class KvstoreModel { + call(data) { + console.log("dataChange: " + data); + } + subscribeDataChange() { + if (kvStore != null) { + kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_REMOTE, this.call); + } + } + unsubscribeDataChange() { + if (kvStore != null) { + kvStore.off('dataChange', this.call); + } + } +} ``` ## SyncMode @@ -5668,4 +5713,3 @@ Enumerates the synchronization modes. | PULL_ONLY |0 |Pull data from the peer end to the local end only.| | PUSH_ONLY |1 |Push data from the local end to the peer end only.| | PUSH_PULL |2 |Push data from the local end to the peer end and then pull data from the peer end to the local end.| - -- GitLab