react-native-onyx
Version:
State management for React Native
88 lines (87 loc) • 3.56 kB
TypeScript
import type { ConnectOptions } from './Onyx';
import type { OnyxKey } from './types';
/**
* Represents the connection object returned by `Onyx.connect()`.
*/
type Connection = {
/**
* The ID used to identify this particular connection.
*/
id: string;
/**
* The ID of the subscriber's callback that is associated to this connection.
*/
callbackID: string;
};
/**
* Manages Onyx connections of `Onyx.connect()`, `useOnyx()` and `withOnyx()` subscribers.
*/
declare class OnyxConnectionManager {
/**
* A map where the key is the connection ID generated inside `connect()` and the value is the metadata of that connection.
*/
private connectionsMap;
/**
* Stores the last generated callback ID which will be incremented when making a new connection.
*/
private lastCallbackID;
/**
* Stores the last generated session ID for the connection manager. The current session ID
* is appended to the connection IDs and it's used to create new different connections for the same key
* when `refreshSessionID()` is called.
*
* When calling `Onyx.clear()` after a logout operation some connections might remain active as they
* aren't tied to the React's lifecycle e.g. `Onyx.connect()` usage, causing infinite loading state issues to new `useOnyx()` subscribers
* that are connecting to the same key as we didn't populate the cache again because we are still reusing such connections.
*
* To elimitate this problem, the session ID must be refreshed during the `Onyx.clear()` call (by using `refreshSessionID()`)
* in order to create fresh connections when new subscribers connect to the same keys again, allowing them
* to use the cache system correctly and avoid the mentioned issues in `useOnyx()`.
*/
private sessionID;
constructor();
/**
* Generates a connection ID based on the `connectOptions` object passed to the function.
*
* The properties used to generate the ID are handpicked for performance reasons and
* according to their purpose and effect they produce in the Onyx connection.
*/
private generateConnectionID;
/**
* Fires all the subscribers callbacks associated with that connection ID.
*/
private fireCallbacks;
/**
* Connects to an Onyx key given the options passed and listens to its changes.
*
* @param connectOptions The options object that will define the behavior of the connection.
* @returns The connection object to use when calling `disconnect()`.
*/
connect<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKey>): Connection;
/**
* Disconnects and removes the listener from the Onyx key.
*
* @param connection Connection object returned by calling `connect()`.
*/
disconnect(connection: Connection): void;
/**
* Disconnect all subscribers from Onyx.
*/
disconnectAll(): void;
/**
* Refreshes the connection manager's session ID.
*/
refreshSessionID(): void;
/**
* Adds the connection to the eviction block list. Connections added to this list can never be evicted.
* */
addToEvictionBlockList(connection: Connection): void;
/**
* Removes a connection previously added to this list
* which will enable it to be evicted again.
*/
removeFromEvictionBlockList(connection: Connection): void;
}
declare const connectionManager: OnyxConnectionManager;
export default connectionManager;
export type { Connection };