Skip to content

Latest commit

 

History

History
504 lines (397 loc) · 19.4 KB

API-INTERNAL.md

File metadata and controls

504 lines (397 loc) · 19.4 KB

Internal API Reference

Functions

getMergeQueue()

Getter - returns the merge queue.

getMergeQueuePromise()

Getter - returns the merge queue promise.

getDefaultKeyStates()

Getter - returns the default key states.

getDeferredInitTask()

Getter - returns the deffered init task.

getEvictionBlocklist()

Getter - returns the eviction block list.

initStoreValues(keys, initialKeyStates, safeEvictionKeys)

Sets the initial values for the Onyx store

maybeFlushBatchUpdates()

We are batching together onyx updates. This helps with use cases where we schedule onyx updates after each other. This happens for example in the Onyx.update function, where we process API responses that might contain a lot of update operations. Instead of calling the subscribers for each update operation, we batch them together which will cause react to schedule the updates at once instead of after each other. This is mainly a performance optimization.

reduceCollectionWithSelector()

Takes a collection of items (eg. {testKey_1:{a:'a'}, testKey_2:{b:'b'}}) and runs it through a reducer function to return a subset of the data according to a selector. The resulting collection will only contain items that are returned by the selector.

get()

Get some data from the store

storeKeyBySubscriptions(subscriptionID, key)

Stores a subscription ID associated with a given key.

deleteKeyBySubscriptions(subscriptionID)

Deletes a subscription ID associated with its corresponding key.

getAllKeys()

Returns current key names stored in persisted storage

getCollectionKeys()

Returns set of all registered collection keys

isCollectionKey()

Checks to see if the subscriber's supplied key is associated with a collection of keys.

splitCollectionMemberKey(key)

Splits a collection member key into the collection key part and the ID part.

isKeyMatch()

Checks to see if a provided key is the exact configured key of our connected subscriber or if the provided key is a collection member key (in case our configured key is a "collection key")

isSafeEvictionKey()

Checks to see if this key has been flagged as safe for removal.

getCollectionKey(key)

Extracts the collection identifier of a given collection member key.

For example:

  • getCollectionKey("report_123") would return "report_"
  • getCollectionKey("report_") would return "report_"
  • getCollectionKey("report_-1_something") would return "report_"
  • getCollectionKey("sharedNVP_user_-1_something") would return "sharedNVP_user_"
tryGetCachedValue()

Tries to get a value from the cache. If the value is not present in cache it will return the default value or undefined. If the requested key is a collection, it will return an object with all the collection members.

removeLastAccessedKey()

Remove a key from the recently accessed key list.

addLastAccessedKey()

Add a key to the list of recently accessed keys. The least recently accessed key should be at the head and the most recently accessed key at the tail.

addAllSafeEvictionKeysToRecentlyAccessedList()

Take all the keys that are safe to evict and add them to the recently accessed list when initializing the app. This enables keys that have not recently been accessed to be removed.

keysChanged()

When a collection of keys change, search for any callbacks matching the collection key and trigger those callbacks

keyChanged()

When a key change happens, search for any callbacks matching the key or collection key and trigger those callbacks

sendDataToConnection()

Sends the data obtained from the keys to the connection. It either: - sets state on the withOnyxInstances - triggers the callback function

addKeyToRecentlyAccessedIfNeeded()

We check to see if this key is flagged as safe for eviction and add it to the recentlyAccessedKeys list so that when we run out of storage the least recently accessed key can be removed.

getCollectionDataAndSendAsObject()

Gets the data for a given an array of matching keys, combines them into an object, and sends the result back to the subscriber.

scheduleSubscriberUpdate()

Schedules an update that will be appended to the macro task queue (so it doesn't update the subscribers immediately).

scheduleNotifyCollectionSubscribers()

This method is similar to notifySubscribersOnNextTick but it is built for working specifically with collections so that keysChanged() is triggered for the collection and not keyChanged(). If this was not done, then the subscriber callbacks receive the data in a different format than they normally expect and it breaks code.

remove()

Remove a key from Onyx and update the subscribers

evictStorageAndRetry()

If we fail to set or merge we must handle this by evicting some data from Onyx and then retrying to do whatever it is we attempted to do.

broadcastUpdate()

Notifies subscribers and writes current value to cache

removeNullValues()

Removes a key from storage if the value is null. Otherwise removes all nested null values in objects, if shouldRemoveNestedNulls is true and returns the object.

prepareKeyValuePairsForStorage()

Storage expects array like: [["@MyApp_user", value_1], ["@MyApp_key", value_2]] This method transforms an object like {'@MyApp_user': myUserValue, '@MyApp_key': myKeyValue} to an array of key-value pairs in the above format and removes key-value pairs that are being set to null

applyMerge(changes)

Merges an array of changes with an existing value

initializeWithDefaultKeyStates()

Merge user provided default key value pairs.

isValidNonEmptyCollectionForMerge()

Validate the collection is not empty and has a correct type before applying mergeCollection()

doAllCollectionItemsBelongToSameParent()

Verify if all the collection keys belong to the same parent

subscribeToKey(connectOptions)

Subscribes to an Onyx key and listens to its changes.

unsubscribeFromKey(subscriptionID)

Disconnects and removes the listener from the Onyx key.

getMergeQueue()

Getter - returns the merge queue.

Kind: global function

getMergeQueuePromise()

Getter - returns the merge queue promise.

Kind: global function

getDefaultKeyStates()

Getter - returns the default key states.

Kind: global function

getDeferredInitTask()

Getter - returns the deffered init task.

Kind: global function

getEvictionBlocklist()

Getter - returns the eviction block list.

Kind: global function

initStoreValues(keys, initialKeyStates, safeEvictionKeys)

Sets the initial values for the Onyx store

Kind: global function

Param Description
keys ONYXKEYS constants object from Onyx.init()
initialKeyStates initial data to set when init() and clear() are called
safeEvictionKeys This is an array of keys (individual or collection patterns) that when provided to Onyx are flagged as "safe" for removal.

maybeFlushBatchUpdates()

We are batching together onyx updates. This helps with use cases where we schedule onyx updates after each other. This happens for example in the Onyx.update function, where we process API responses that might contain a lot of update operations. Instead of calling the subscribers for each update operation, we batch them together which will cause react to schedule the updates at once instead of after each other. This is mainly a performance optimization.

Kind: global function

reduceCollectionWithSelector()

Takes a collection of items (eg. {testKey_1:{a:'a'}, testKey_2:{b:'b'}}) and runs it through a reducer function to return a subset of the data according to a selector. The resulting collection will only contain items that are returned by the selector.

Kind: global function

get()

Get some data from the store

Kind: global function

storeKeyBySubscriptions(subscriptionID, key)

Stores a subscription ID associated with a given key.

Kind: global function

Param Description
subscriptionID A subscription ID of the subscriber.
key A key that the subscriber is subscribed to.

deleteKeyBySubscriptions(subscriptionID)

Deletes a subscription ID associated with its corresponding key.

Kind: global function

Param Description
subscriptionID The subscription ID to be deleted.

getAllKeys()

Returns current key names stored in persisted storage

Kind: global function

getCollectionKeys()

Returns set of all registered collection keys

Kind: global function

isCollectionKey()

Checks to see if the subscriber's supplied key is associated with a collection of keys.

Kind: global function

splitCollectionMemberKey(key) ⇒

Splits a collection member key into the collection key part and the ID part.

Kind: global function
Returns: A tuple where the first element is the collection part and the second element is the ID part, or throws an Error if the key is not a collection one.

Param Description
key The collection member key to split.

isKeyMatch()

Checks to see if a provided key is the exact configured key of our connected subscriber or if the provided key is a collection member key (in case our configured key is a "collection key")

Kind: global function

isSafeEvictionKey()

Checks to see if this key has been flagged as safe for removal.

Kind: global function

getCollectionKey(key) ⇒

Extracts the collection identifier of a given collection member key.

For example:

  • getCollectionKey("report_123") would return "report_"
  • getCollectionKey("report_") would return "report_"
  • getCollectionKey("report_-1_something") would return "report_"
  • getCollectionKey("sharedNVP_user_-1_something") would return "sharedNVP_user_"

Kind: global function
Returns: The plain collection key or throws an Error if the key is not a collection one.

Param Description
key The collection key to process.

tryGetCachedValue()

Tries to get a value from the cache. If the value is not present in cache it will return the default value or undefined. If the requested key is a collection, it will return an object with all the collection members.

Kind: global function

removeLastAccessedKey()

Remove a key from the recently accessed key list.

Kind: global function

addLastAccessedKey()

Add a key to the list of recently accessed keys. The least recently accessed key should be at the head and the most recently accessed key at the tail.

Kind: global function

addAllSafeEvictionKeysToRecentlyAccessedList()

Take all the keys that are safe to evict and add them to the recently accessed list when initializing the app. This enables keys that have not recently been accessed to be removed.

Kind: global function

keysChanged()

When a collection of keys change, search for any callbacks matching the collection key and trigger those callbacks

Kind: global function

keysChanged~isSubscribedToCollectionKey

e.g. Onyx.connect({key: ONYXKEYS.COLLECTION.REPORT, callback: ...});

Kind: inner constant of keysChanged

keysChanged~isSubscribedToCollectionMemberKey

e.g. Onyx.connect({key: ${ONYXKEYS.COLLECTION.REPORT}{reportID}, callback: ...});

Kind: inner constant of keysChanged

keyChanged()

When a key change happens, search for any callbacks matching the key or collection key and trigger those callbacks

Kind: global function
Example

keyChanged(key, value, subscriber => subscriber.initWithStoredValues === false)

sendDataToConnection()

Sends the data obtained from the keys to the connection. It either: - sets state on the withOnyxInstances - triggers the callback function

Kind: global function

addKeyToRecentlyAccessedIfNeeded()

We check to see if this key is flagged as safe for eviction and add it to the recentlyAccessedKeys list so that when we run out of storage the least recently accessed key can be removed.

Kind: global function

getCollectionDataAndSendAsObject()

Gets the data for a given an array of matching keys, combines them into an object, and sends the result back to the subscriber.

Kind: global function

scheduleSubscriberUpdate()

Schedules an update that will be appended to the macro task queue (so it doesn't update the subscribers immediately).

Kind: global function
Example

scheduleSubscriberUpdate(key, value, subscriber => subscriber.initWithStoredValues === false)

scheduleNotifyCollectionSubscribers()

This method is similar to notifySubscribersOnNextTick but it is built for working specifically with collections so that keysChanged() is triggered for the collection and not keyChanged(). If this was not done, then the subscriber callbacks receive the data in a different format than they normally expect and it breaks code.

Kind: global function

remove()

Remove a key from Onyx and update the subscribers

Kind: global function

evictStorageAndRetry()

If we fail to set or merge we must handle this by evicting some data from Onyx and then retrying to do whatever it is we attempted to do.

Kind: global function

broadcastUpdate()

Notifies subscribers and writes current value to cache

Kind: global function

removeNullValues() ⇒

Removes a key from storage if the value is null. Otherwise removes all nested null values in objects, if shouldRemoveNestedNulls is true and returns the object.

Kind: global function
Returns: The value without null values and a boolean "wasRemoved", which indicates if the key got removed completely

prepareKeyValuePairsForStorage() ⇒

Storage expects array like: [["@MyApp_user", value_1], ["@MyApp_key", value_2]] This method transforms an object like {'@MyApp_user': myUserValue, '@MyApp_key': myKeyValue} to an array of key-value pairs in the above format and removes key-value pairs that are being set to null

Kind: global function
Returns: an array of key - value pairs <[key, value]>

applyMerge(changes)

Merges an array of changes with an existing value

Kind: global function

Param Description
changes Array of changes that should be applied to the existing value

initializeWithDefaultKeyStates()

Merge user provided default key value pairs.

Kind: global function

isValidNonEmptyCollectionForMerge()

Validate the collection is not empty and has a correct type before applying mergeCollection()

Kind: global function

doAllCollectionItemsBelongToSameParent()

Verify if all the collection keys belong to the same parent

Kind: global function

subscribeToKey(connectOptions) ⇒

Subscribes to an Onyx key and listens to its changes.

Kind: global function
Returns: The subscription ID to use when calling OnyxUtils.unsubscribeFromKey().

Param Description
connectOptions The options object that will define the behavior of the connection.

unsubscribeFromKey(subscriptionID)

Disconnects and removes the listener from the Onyx key.

Kind: global function

Param Description
subscriptionID Subscription ID returned by calling OnyxUtils.subscribeToKey().