Skip to content

Commit f9254ba

Browse files
Fix types
1 parent b2d0034 commit f9254ba

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

lib/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Onyx, {OnyxUpdate, ConnectOptions} from './Onyx';
2-
import {CustomTypeOptions, OnyxCollection, OnyxEntry} from './types';
2+
import {CustomTypeOptions, OnyxCollection, OnyxEntry, ComputedKey} from './types';
33
import withOnyx from './withOnyx';
44

55
export default Onyx;
6-
export {CustomTypeOptions, OnyxCollection, OnyxEntry, OnyxUpdate, withOnyx, ConnectOptions};
6+
export {CustomTypeOptions, OnyxCollection, OnyxEntry, OnyxUpdate, withOnyx, ConnectOptions, ComputedKey};

lib/types.d.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ type CollectionKey = `${CollectionKeyBase}${string}`;
9999
*/
100100
type OnyxKey = Key | CollectionKey;
101101

102+
/**
103+
* A computed key is a key that is not stored in the database, but instead is computed from other keys
104+
* and cached in memory. This is useful for expensive computations that are used in multiple places.
105+
*/
106+
type ComputedKey<DependenciesT, ValueT> = {
107+
/**
108+
* The cache key of the computed value.
109+
*/
110+
cacheKey: string;
111+
/**
112+
* Keys that this computed key depends on. The values of these keys will be passed to the compute function.
113+
* This will also cause the key to be recomputed whenever any of the dependencies value change.
114+
*/
115+
dependencies?: {[KeyT in keyof DependenciesT]: OnyxKey | ComputedKey<any, unknown>};
116+
/**
117+
* Compute the value for this computed key.
118+
*/
119+
compute: (params: DependenciesT) => ValueT;
120+
};
121+
102122
/**
103123
* Represents a mapping of Onyx keys to values, where keys are either normal or collection Onyx keys
104124
* and values are the corresponding values in Onyx's state.
@@ -226,4 +246,4 @@ type NullishObjectDeep<ObjectType extends object> = {
226246
*/
227247
type WithOnyxInstanceState<TOnyxProps> = (TOnyxProps & {loading: boolean}) | undefined;
228248

229-
export {CollectionKey, CollectionKeyBase, CustomTypeOptions, DeepRecord, Key, KeyValueMapping, OnyxCollection, OnyxEntry, OnyxKey, Selector, NullishDeep, WithOnyxInstanceState};
249+
export {CollectionKey, CollectionKeyBase, ComputedKey, CustomTypeOptions, DeepRecord, Key, KeyValueMapping, OnyxCollection, OnyxEntry, OnyxKey, Selector, NullishDeep, WithOnyxInstanceState};

lib/withOnyx.d.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {IsEqual} from 'type-fest';
2-
import {CollectionKeyBase, KeyValueMapping, OnyxCollection, OnyxEntry, OnyxKey, Selector} from './types';
2+
import {CollectionKeyBase, ComputedKey, KeyValueMapping, OnyxCollection, OnyxEntry, OnyxKey, Selector} from './types';
33

44
/**
55
* Represents the base mapping options between an Onyx key and the component's prop.
@@ -86,6 +86,25 @@ type BaseMappingFunctionKeyAndSelector<TComponentProps, TOnyxProps, TOnyxProp ex
8686
selector: Selector<TOnyxKey, TOnyxProps, TOnyxProps[TOnyxProp]>;
8787
};
8888

89+
/**
90+
* TODO
91+
*
92+
* @example
93+
* ```ts
94+
* // Onyx prop with computed key.
95+
* accountName: {
96+
* key: {
97+
* cacheKey: ONYXKEYS.COMPUTED.ACCOUNT_NAME,
98+
* dependencies: {account: ONYXKEYS.ACCOUNT},
99+
* compute: ({account}) => account.name,
100+
* },
101+
* },
102+
* ```
103+
*/
104+
type BaseMappingComputedKey<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOnyxProps> = {
105+
key: ComputedKey<any, TOnyxProps[TOnyxProp]> | ((props: Omit<TComponentProps, keyof TOnyxProps> & Partial<TOnyxProps>) => ComputedKey<any, TOnyxProps[TOnyxProp]>);
106+
};
107+
89108
/**
90109
* Represents the mapping options between an Onyx key and the component's prop with all its possibilities.
91110
*/
@@ -95,6 +114,7 @@ type Mapping<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOnyxProps, TO
95114
| BaseMappingKey<TComponentProps, TOnyxProps, TOnyxProp, TOnyxKey, OnyxEntry<KeyValueMapping[TOnyxKey]>>
96115
| BaseMappingStringKeyAndSelector<TComponentProps, TOnyxProps, TOnyxProp, TOnyxKey>
97116
| BaseMappingFunctionKeyAndSelector<TComponentProps, TOnyxProps, TOnyxProp, TOnyxKey>
117+
| BaseMappingComputedKey<TComponentProps, TOnyxProps, TOnyxProp>
98118
);
99119

100120
/**
@@ -106,6 +126,7 @@ type CollectionMapping<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOny
106126
| BaseMappingKey<TComponentProps, TOnyxProps, TOnyxProp, TOnyxKey, OnyxCollection<KeyValueMapping[TOnyxKey]>>
107127
| BaseMappingStringKeyAndSelector<TComponentProps, TOnyxProps, TOnyxProp, TOnyxKey>
108128
| BaseMappingFunctionKeyAndSelector<TComponentProps, TOnyxProps, TOnyxProp, TOnyxKey>
129+
| BaseMappingComputedKey<TComponentProps, TOnyxProps, TOnyxProp>
109130
);
110131

111132
/**

0 commit comments

Comments
 (0)