Skip to content
Draft
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
27 changes: 2 additions & 25 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ This method will be deprecated soon. Please use <code>Onyx.connectWithoutView()<
<dt><a href="#connectWithoutView">connectWithoutView(connectOptions)</a> ⇒</dt>
<dd><p>Connects to an Onyx key given the options passed and listens to its changes.</p>
</dd>
<dt><a href="#disconnect">disconnect(connection)</a></dt>
<dd><p>Disconnects and removes the listener from the Onyx key.</p>
</dd>
<dt><a href="#set">set(key, value, options)</a></dt>
<dd><p>Write a value to our store with the given key</p>
</dd>
Expand Down Expand Up @@ -91,7 +88,7 @@ Connects to an Onyx key given the options passed and listens to its changes.
This method will be deprecated soon. Please use `Onyx.connectWithoutView()` instead.

**Kind**: global function
**Returns**: The `Connection` handle to use when calling `Onyx.disconnect()`.
**Returns**: The `Connection` handle whose `unsubscribe()` stops this subscription.

| Param | Description |
| --- | --- |
Expand All @@ -118,7 +115,7 @@ cache after any same-tick writes have applied.
Connects to an Onyx key given the options passed and listens to its changes.

**Kind**: global function
**Returns**: The `Connection` handle to use when calling `Onyx.disconnect()`.
**Returns**: The `Connection` handle whose `unsubscribe()` stops this subscription.

| Param | Description |
| --- | --- |
Expand All @@ -139,26 +136,6 @@ object whenever any member changes; signature `(collection, collectionKey)`.
For any other key, the callback fires with the value at that key; signature
`(value, key)`. Initial fire is deferred via `scheduleInitialSubscriberNotification` so it reads
cache after any same-tick writes have applied.
<a name="disconnect"></a>

## disconnect(connection)
Disconnects and removes the listener from the Onyx key.

**Kind**: global function

| Param | Description |
| --- | --- |
| connection | Connection object returned by calling `Onyx.connect()` or `Onyx.connectWithoutView()`. |

**Example**
```ts
const connection = Onyx.connectWithoutView({
key: ONYXKEYS.SESSION,
callback: onSessionChange,
});

Onyx.disconnect(connection);
```
<a name="set"></a>

## set(key, value, options)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ const connection = Onyx.connectWithoutView({
});
```

To teardown the subscription call `Onyx.disconnect()` with the connection returned from `Onyx.connectWithoutView()`. It's recommended to clean up subscriptions anytime you are connecting from within a function to prevent memory leaks.
To teardown the subscription call `unsubscribe()` on the connection returned from `Onyx.connectWithoutView()`. It's recommended to clean up subscriptions anytime you are connecting from within a function to prevent memory leaks.

```javascript
Onyx.disconnect(connection);
connection.unsubscribe();
```

We can also access values inside React function components via the `useOnyx()` [hook](https://react.dev/reference/react/hooks). When the data changes the component will re-render.
Expand Down
27 changes: 2 additions & 25 deletions lib/Onyx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function init({
* @param connectOptions The options object that will define the behavior of the connection.
* @param connectOptions.key The Onyx key to subscribe to.
* @param connectOptions.callback A function that will be called when the Onyx data we are subscribed changes.
* @returns The `Connection` handle to use when calling `Onyx.disconnect()`.
* @returns The `Connection` handle whose `unsubscribe()` stops this subscription.
*/
function connect<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKey>): Connection {
const {key, callback} = connectOptions;
Expand Down Expand Up @@ -236,34 +236,12 @@ function connect<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKey>): Co
* @param connectOptions The options object that will define the behavior of the connection.
* @param connectOptions.key The Onyx key to subscribe to.
* @param connectOptions.callback A function that will be called when the Onyx data we are subscribed changes.
* @returns The `Connection` handle to use when calling `Onyx.disconnect()`.
* @returns The `Connection` handle whose `unsubscribe()` stops this subscription.
*/
function connectWithoutView<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKey>): Connection {
return connect(connectOptions);
}

/**
* Disconnects and removes the listener from the Onyx key.
*
* @example
* ```ts
* const connection = Onyx.connectWithoutView({
* key: ONYXKEYS.SESSION,
* callback: onSessionChange,
* });
*
* Onyx.disconnect(connection);
* ```
*
* @param connection Connection object returned by calling `Onyx.connect()` or `Onyx.connectWithoutView()`.
*/
function disconnect(connection: Connection): void {
if (!connection) {
return;
}
connection.unsubscribe();
}

/**
* Write a value to our store with the given key
*
Expand Down Expand Up @@ -801,7 +779,6 @@ const Onyx = {
METHOD: OnyxUtils.METHOD,
connect,
connectWithoutView,
disconnect,
set,
multiSet,
merge,
Expand Down
2 changes: 1 addition & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ type MixedOperationsQueue = {

/**
* Represents a connection to an Onyx key, returned by `Onyx.connect()`/`Onyx.connectWithoutView()`.
* Pass it to `Onyx.disconnect()` to stop receiving callbacks for this subscription.
* Call its `unsubscribe()` to stop receiving callbacks for this subscription.
*/
type Connection = {
/** Unsubscribe this connection. Idempotent. */
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/onyxClearNativeStorageTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Set data while storage is clearing', () => {

afterEach(() => {
if (connection) {
Onyx.disconnect(connection);
connection.unsubscribe();
}
return Onyx.clear();
});
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/onyxClearWebStorageTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Set data while storage is clearing', () => {

afterEach(() => {
if (connection) {
Onyx.disconnect(connection);
connection.unsubscribe();
}
return Onyx.clear();
});
Expand Down Expand Up @@ -229,7 +229,7 @@ describe('Set data while storage is clearing', () => {
// When onyx is cleared
.then(() => Onyx.clear())
.then(() => {
Onyx.disconnect(testConnection);
testConnection.unsubscribe();
})
.then(() => {
// Then the collection callback should only have been called three times:
Expand Down
44 changes: 22 additions & 22 deletions tests/unit/onyxTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('Onyx', () => {

afterEach(() => {
if (connection) {
Onyx.disconnect(connection);
connection.unsubscribe();
}
return Onyx.clear();
});
Expand Down Expand Up @@ -354,7 +354,7 @@ describe('Onyx', () => {
// Expect that the connection to a key with a default value that wasn't changed is not called on clear
expect(mockCallback).toHaveBeenCalledTimes(0);

return Onyx.disconnect(otherTestConnection);
return otherTestConnection.unsubscribe();
});
});

Expand Down Expand Up @@ -400,7 +400,7 @@ describe('Onyx', () => {
.then(() => {
expect(testKeyValue).toBe('test');
if (connection) {
Onyx.disconnect(connection);
connection.unsubscribe();
}
return Onyx.set(ONYX_KEYS.TEST_KEY, 'test updated');
})
Expand Down Expand Up @@ -1279,7 +1279,7 @@ describe('Onyx', () => {
// We set an initial value of 42 for ONYX_KEYS.OTHER_TEST in Onyx.init()
expect(otherTestCallback).toHaveBeenNthCalledWith(1, 42, ONYX_KEYS.OTHER_TEST);
expect(otherTestCallback).toHaveBeenNthCalledWith(2, 'pizza', ONYX_KEYS.OTHER_TEST);
for (const id of connections) Onyx.disconnect(id);
for (const id of connections) id.unsubscribe();
}),
);
});
Expand Down Expand Up @@ -1538,7 +1538,7 @@ describe('Onyx', () => {
expect(dogCallback).toHaveBeenNthCalledWith(1, undefined, dog);
expect(dogCallback).toHaveBeenLastCalledWith({name: 'Rex'}, dog);

connections.map((id) => Onyx.disconnect(id));
connections.map((id) => id.unsubscribe());
});
});

Expand Down Expand Up @@ -1769,7 +1769,7 @@ describe('Onyx', () => {
ONYX_KEYS.COLLECTION.ROUTES,
);

connections.map((id) => Onyx.disconnect(id));
connections.map((id) => id.unsubscribe());
});
});

Expand Down Expand Up @@ -1856,7 +1856,7 @@ describe('Onyx', () => {
ONYX_KEYS.COLLECTION.PEOPLE,
);

connections.map((id) => Onyx.disconnect(id));
connections.map((id) => id.unsubscribe());
});
});

Expand Down Expand Up @@ -2427,8 +2427,8 @@ describe('Onyx', () => {
expect(individualCallback1).toHaveBeenCalledWith({id: '1', name: 'Updated Item 1'}, key1);
expect(individualCallback2).toHaveBeenCalledWith({id: '2', name: 'Updated Item 2'}, key2);

Onyx.disconnect(connection1);
Onyx.disconnect(connection2);
connection1.unsubscribe();
connection2.unsubscribe();
});

it('should not save a RAM-only collection to storage', async () => {
Expand Down Expand Up @@ -4048,7 +4048,7 @@ describe('RAM-only keys should not read from storage', () => {
expect(receivedValue).toBeUndefined();
expect(cache.get(ONYX_KEYS.RAM_ONLY_TEST_KEY)).toBeUndefined();

Onyx.disconnect(connection);
connection.unsubscribe();
});

it('should not return stale storage data for RAM-only collection members via multiGet', async () => {
Expand Down Expand Up @@ -4078,7 +4078,7 @@ describe('RAM-only keys should not read from storage', () => {
expect(cache.get(collectionMember1)).toBeUndefined();
expect(cache.get(collectionMember2)).toBeUndefined();

Onyx.disconnect(connection);
connection.unsubscribe();
});

it('should not include stale RAM-only keys in getAllKeys results', async () => {
Expand Down Expand Up @@ -4159,7 +4159,7 @@ describe('RAM-only keys should not read from storage', () => {
// Should never receive the stale value
expect(receivedValues.every((v) => v === undefined || v === null)).toBe(true);

Onyx.disconnect(connection);
connection.unsubscribe();
});

it('should still work correctly for normal keys when RAM-only keys have stale storage data', async () => {
Expand Down Expand Up @@ -4195,8 +4195,8 @@ describe('RAM-only keys should not read from storage', () => {
// RAM-only key should NOT read stale value from storage
expect(ramOnlyValue).toBeUndefined();

Onyx.disconnect(connection1);
Onyx.disconnect(connection2);
connection1.unsubscribe();
connection2.unsubscribe();
});

it('should not sync RAM-only keys from other instances via keepInstancesSync', async () => {
Expand Down Expand Up @@ -4243,8 +4243,8 @@ describe('RAM-only keys should not read from storage', () => {

expect(normalValue).toEqual('synced_normal_value');

Onyx.disconnect(connection);
Onyx.disconnect(connection2);
connection.unsubscribe();
connection2.unsubscribe();
});

it('should notify collection-root and collection member subscribers when a collection member syncs from another instance', async () => {
Expand Down Expand Up @@ -4291,8 +4291,8 @@ describe('RAM-only keys should not read from storage', () => {
// The collection member subscriber must receive the synced data.
expect(collectionMember2).toEqual({name: 'entry 2 changed'});

Onyx.disconnect(collectionConn);
Onyx.disconnect(collectionMember2Conn);
collectionConn.unsubscribe();
collectionMember2Conn.unsubscribe();
});

it('should notify a collection-root subscriber once when multiple members sync from another instance', async () => {
Expand Down Expand Up @@ -4333,7 +4333,7 @@ describe('RAM-only keys should not read from storage', () => {
expect(collection[`${ONYX_KEYS.COLLECTION.TEST_KEY}2`]).toEqual({name: 'entry 2'});
expect(collection[`${ONYX_KEYS.COLLECTION.TEST_KEY}3`]).toEqual({name: 'entry 3 changed'});

Onyx.disconnect(connection);
connection.unsubscribe();
});

it('should notify subscribers with undefined when a collection member is removed in another instance', async () => {
Expand Down Expand Up @@ -4378,8 +4378,8 @@ describe('RAM-only keys should not read from storage', () => {
// The collection-root subscriber must receive the collection without the removed member.
expect(collection).toEqual({[`${ONYX_KEYS.COLLECTION.TEST_KEY}1`]: {name: 'entry 1'}});

Onyx.disconnect(collectionConn);
Onyx.disconnect(collectionMember2Conn);
collectionConn.unsubscribe();
collectionMember2Conn.unsubscribe();
});

it('should serve RAM-only keys from cache and normal keys from storage in multiGet', async () => {
Expand Down Expand Up @@ -4437,7 +4437,7 @@ describe('RAM-only keys should not read from storage', () => {
const storageValue = await StorageMock.getItem(ONYX_KEYS.RAM_ONLY_TEST_KEY);
expect(storageValue).toEqual('stale_value');

Onyx.disconnect(connection);
connection.unsubscribe();
});
});

Expand Down
Loading
Loading