Skip to content

fix: remove warning about multiMerge not being implemented #827

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

Merged
merged 1 commit into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 0 additions & 10 deletions src/AsyncStorage.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ const AsyncStorage = ((): AsyncStorageStatic => {
* Merges an existing `key` value with an input value, assuming both values
* are stringified JSON.
*
* **NOTE:** This is not supported by all native implementations.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#mergeitem
*/
mergeItem: (key, value, callback) => {
Expand Down Expand Up @@ -337,8 +335,6 @@ const AsyncStorage = ((): AsyncStorageStatic => {
* Batch operation to merge in existing and new values for a given set of
* keys. This assumes that the values are stringified JSON.
*
* **NOTE**: This is not supported by all native implementations.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#multimerge
*/
multiMerge: (keyValuePairs, callback) => {
Expand All @@ -357,10 +353,4 @@ const AsyncStorage = ((): AsyncStorageStatic => {
};
})();

// Not all native implementations support merge.
if (!RCTAsyncStorage.multiMerge) {
delete AsyncStorage.mergeItem;
delete AsyncStorage.multiMerge;
}

export default AsyncStorage;
6 changes: 2 additions & 4 deletions src/AsyncStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,8 @@ const AsyncStorage: AsyncStorageStatic = {
* multiMerge([['k1', 'val1'], ['k2', 'val2']])
*/
multiMerge: (keyValuePairs, callback) => {
const promises = keyValuePairs.map(
(item) =>
AsyncStorage.mergeItem?.(item[0], item[1]) ??
Promise.reject('Not implemented')
const promises = keyValuePairs.map((item) =>
AsyncStorage.mergeItem(item[0], item[1])
);
return createPromiseAll(promises, callback);
},
Expand Down
4 changes: 1 addition & 3 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ export function useAsyncStorage(key: string): AsyncStorageHook {
return {
getItem: (...args) => AsyncStorage.getItem(key, ...args),
setItem: (...args) => AsyncStorage.setItem(key, ...args),
mergeItem: (...args) =>
AsyncStorage.mergeItem?.(key, ...args) ??
Promise.reject('Not implemented'),
mergeItem: (...args) => AsyncStorage.mergeItem(key, ...args),
removeItem: (...args) => AsyncStorage.removeItem(key, ...args),
};
}
12 changes: 2 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,9 @@ export type AsyncStorageStatic = {
* Merges an existing `key` value with an input value, assuming both values
* are stringified JSON.
*
* **NOTE:** This is not supported by all native implementations.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#mergeitem
*/
mergeItem?: (
key: string,
value: string,
callback?: Callback
) => Promise<void>;
mergeItem: (key: string, value: string, callback?: Callback) => Promise<void>;

/**
* Erases *all* `AsyncStorage` for all clients, libraries, etc. You probably
Expand Down Expand Up @@ -152,11 +146,9 @@ export type AsyncStorageStatic = {
* Batch operation to merge in existing and new values for a given set of
* keys. This assumes that the values are stringified JSON.
*
* **NOTE**: This is not supported by all native implementations.
*
* See https://react-native-async-storage.github.io/async-storage/docs/api#multimerge
*/
multiMerge?: (
multiMerge: (
keyValuePairs: [string, string][],
callback?: MultiCallback
) => Promise<void>;
Expand Down
19 changes: 1 addition & 18 deletions website/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ static getItem(key: string, [callback]: ?(error: ?Error, result: ?string) => voi
**Example**:

```js

getMyStringValue = async () => {
try {
return await AsyncStorage.getItem('@key')
Expand All @@ -39,12 +38,10 @@ getMyStringValue = async () => {
}

console.log('Done.')

}
```

```js

getMyObject = async () => {
try {
const jsonValue = await AsyncStorage.getItem('@key')
Expand All @@ -54,7 +51,6 @@ getMyObject = async () => {
}

console.log('Done.')

}
```

Expand All @@ -66,7 +62,7 @@ getMyObject = async () => {

## `setItem`

Sets a string `value` for given `key`. This operation can either modify an existing entry, if it did exist for given `key`, or add new one otherwise.
Sets a string `value` for given `key`. This operation can either modify an existing entry, if it did exist for given `key`, or add new one otherwise.

In order to store object value, you need to serialize it, e.g. using `JSON.stringify()`.

Expand All @@ -87,7 +83,6 @@ static setItem(key: string, value: string, [callback]: ?(error: ?Error) => void)
**Example**:

```js

setStringValue = async (value) => {
try {
await AsyncStorage.setItem('key', value)
Expand All @@ -100,7 +95,6 @@ setStringValue = async (value) => {
```

```js

setObjectValue = async (value) => {
try {
const jsonValue = JSON.stringify(value)
Expand All @@ -121,7 +115,6 @@ setObjectValue = async (value) => {
## `mergeItem`

Merges an existing value stored under `key`, with new `value`, assuming both values are **stringified JSON**.
**NOTE**: This is not supported by all native implementations.

**Signature**:

Expand Down Expand Up @@ -180,7 +173,6 @@ mergeUsers = async () => {
// }
}
}

```

<br />
Expand Down Expand Up @@ -242,7 +234,6 @@ static getAllKeys([callback]: ?(error: ?Error, keys: ?Array<string>) => void): P
**Example**:

```js

getAllKeys = async () => {
let keys = []
try {
Expand All @@ -255,7 +246,6 @@ getAllKeys = async () => {
// example console.log result:
// ['@MyApp_user', '@MyApp_key']
}

```

<br />
Expand All @@ -281,7 +271,6 @@ static multiGet(keys: Array<string>, [callback]: ?(errors: ?Array<Error>, result
**Example**:

```js

getMultiple = async () => {

let values
Expand All @@ -295,7 +284,6 @@ getMultiple = async () => {
// example console.log output:
// [ ['@MyApp_user', 'myUserValue'], ['@MyApp_key', 'myKeyValue'] ]
}

```

<br />
Expand All @@ -321,7 +309,6 @@ static multiSet(keyValuePairs: Array<Array<string>>, [callback]: ?(errors: ?Arra
**Example**:

```js

multiSet = async () => {
const firstPair = ["@MyApp_user", "value_1"]
const secondPair = ["@MyApp_key", "value_2"]
Expand All @@ -333,7 +320,6 @@ multiSet = async () => {

console.log("Done.")
}

```

<br />
Expand All @@ -344,7 +330,6 @@ multiSet = async () => {
## `multiMerge`

Multiple merging of existing and new values in a batch. Assumes that values are *stringified JSON*. Once completed, invokes `callback` with errors (if any).
**NOTE**: This is not supported by all native implementations.

**Signature**:

Expand Down Expand Up @@ -430,7 +415,6 @@ mergeMultiple = async () => {
// ]
// ]
}

```


Expand Down Expand Up @@ -501,7 +485,6 @@ clearAll = async () => {

console.log('Done.')
}

```

<!-- ------------------------ HOOKS ------------------------ -->
Expand Down