Skip to content

Commit 513b3ef

Browse files
committed
Remove Onyx.disconnect()
`connect()`/`connectWithoutView()` already return a `Connection` handle whose `unsubscribe()` tears the subscription down, so `disconnect()` is a redundant wrapper around it. Call `connection.unsubscribe()` directly instead.
1 parent e8bc8d6 commit 513b3ef

8 files changed

Lines changed: 48 additions & 94 deletions

File tree

‎API.md‎

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ This method will be deprecated soon. Please use <code>Onyx.connectWithoutView()<
1515
<dt><a href="#connectWithoutView">connectWithoutView(connectOptions)</a> ⇒</dt>
1616
<dd><p>Connects to an Onyx key given the options passed and listens to its changes.</p>
1717
</dd>
18-
<dt><a href="#disconnect">disconnect(connection)</a></dt>
19-
<dd><p>Disconnects and removes the listener from the Onyx key.</p>
20-
</dd>
2118
<dt><a href="#set">set(key, value, options)</a></dt>
2219
<dd><p>Write a value to our store with the given key</p>
2320
</dd>
@@ -91,7 +88,7 @@ Connects to an Onyx key given the options passed and listens to its changes.
9188
This method will be deprecated soon. Please use `Onyx.connectWithoutView()` instead.
9289

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

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

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

123120
| Param | Description |
124121
| --- | --- |
@@ -139,26 +136,6 @@ object whenever any member changes; signature `(collection, collectionKey)`.
139136
For any other key, the callback fires with the value at that key; signature
140137
`(value, key)`. Initial fire is deferred via `scheduleInitialSubscriberNotification` so it reads
141138
cache after any same-tick writes have applied.
142-
<a name="disconnect"></a>
143-
144-
## disconnect(connection)
145-
Disconnects and removes the listener from the Onyx key.
146-
147-
**Kind**: global function
148-
149-
| Param | Description |
150-
| --- | --- |
151-
| connection | Connection object returned by calling `Onyx.connect()` or `Onyx.connectWithoutView()`. |
152-
153-
**Example**
154-
```ts
155-
const connection = Onyx.connectWithoutView({
156-
key: ONYXKEYS.SESSION,
157-
callback: onSessionChange,
158-
});
159-
160-
Onyx.disconnect(connection);
161-
```
162139
<a name="set"></a>
163140

164141
## set(key, value, options)

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ const connection = Onyx.connectWithoutView({
130130
});
131131
```
132132

133-
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.
133+
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.
134134

135135
```javascript
136-
Onyx.disconnect(connection);
136+
connection.unsubscribe();
137137
```
138138

139139
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.

‎lib/Onyx.ts‎

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function init({
140140
* @param connectOptions The options object that will define the behavior of the connection.
141141
* @param connectOptions.key The Onyx key to subscribe to.
142142
* @param connectOptions.callback A function that will be called when the Onyx data we are subscribed changes.
143-
* @returns The `Connection` handle to use when calling `Onyx.disconnect()`.
143+
* @returns The `Connection` handle whose `unsubscribe()` stops this subscription.
144144
*/
145145
function connect<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKey>): Connection {
146146
const {key, callback} = connectOptions;
@@ -236,34 +236,12 @@ function connect<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKey>): Co
236236
* @param connectOptions The options object that will define the behavior of the connection.
237237
* @param connectOptions.key The Onyx key to subscribe to.
238238
* @param connectOptions.callback A function that will be called when the Onyx data we are subscribed changes.
239-
* @returns The `Connection` handle to use when calling `Onyx.disconnect()`.
239+
* @returns The `Connection` handle whose `unsubscribe()` stops this subscription.
240240
*/
241241
function connectWithoutView<TKey extends OnyxKey>(connectOptions: ConnectOptions<TKey>): Connection {
242242
return connect(connectOptions);
243243
}
244244

245-
/**
246-
* Disconnects and removes the listener from the Onyx key.
247-
*
248-
* @example
249-
* ```ts
250-
* const connection = Onyx.connectWithoutView({
251-
* key: ONYXKEYS.SESSION,
252-
* callback: onSessionChange,
253-
* });
254-
*
255-
* Onyx.disconnect(connection);
256-
* ```
257-
*
258-
* @param connection Connection object returned by calling `Onyx.connect()` or `Onyx.connectWithoutView()`.
259-
*/
260-
function disconnect(connection: Connection): void {
261-
if (!connection) {
262-
return;
263-
}
264-
connection.unsubscribe();
265-
}
266-
267245
/**
268246
* Write a value to our store with the given key
269247
*
@@ -801,7 +779,6 @@ const Onyx = {
801779
METHOD: OnyxUtils.METHOD,
802780
connect,
803781
connectWithoutView,
804-
disconnect,
805782
set,
806783
multiSet,
807784
merge,

‎lib/types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ type MixedOperationsQueue = {
407407

408408
/**
409409
* Represents a connection to an Onyx key, returned by `Onyx.connect()`/`Onyx.connectWithoutView()`.
410-
* Pass it to `Onyx.disconnect()` to stop receiving callbacks for this subscription.
410+
* Call its `unsubscribe()` to stop receiving callbacks for this subscription.
411411
*/
412412
type Connection = {
413413
/** Unsubscribe this connection. Idempotent. */

‎tests/unit/onyxClearNativeStorageTest.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('Set data while storage is clearing', () => {
4141

4242
afterEach(() => {
4343
if (connection) {
44-
Onyx.disconnect(connection);
44+
connection.unsubscribe();
4545
}
4646
return Onyx.clear();
4747
});

‎tests/unit/onyxClearWebStorageTest.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('Set data while storage is clearing', () => {
4343

4444
afterEach(() => {
4545
if (connection) {
46-
Onyx.disconnect(connection);
46+
connection.unsubscribe();
4747
}
4848
return Onyx.clear();
4949
});
@@ -229,7 +229,7 @@ describe('Set data while storage is clearing', () => {
229229
// When onyx is cleared
230230
.then(() => Onyx.clear())
231231
.then(() => {
232-
Onyx.disconnect(testConnection);
232+
testConnection.unsubscribe();
233233
})
234234
.then(() => {
235235
// Then the collection callback should only have been called three times:

‎tests/unit/onyxTest.ts‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('Onyx', () => {
6161

6262
afterEach(() => {
6363
if (connection) {
64-
Onyx.disconnect(connection);
64+
connection.unsubscribe();
6565
}
6666
return Onyx.clear();
6767
});
@@ -354,7 +354,7 @@ describe('Onyx', () => {
354354
// Expect that the connection to a key with a default value that wasn't changed is not called on clear
355355
expect(mockCallback).toHaveBeenCalledTimes(0);
356356

357-
return Onyx.disconnect(otherTestConnection);
357+
return otherTestConnection.unsubscribe();
358358
});
359359
});
360360

@@ -400,7 +400,7 @@ describe('Onyx', () => {
400400
.then(() => {
401401
expect(testKeyValue).toBe('test');
402402
if (connection) {
403-
Onyx.disconnect(connection);
403+
connection.unsubscribe();
404404
}
405405
return Onyx.set(ONYX_KEYS.TEST_KEY, 'test updated');
406406
})
@@ -1279,7 +1279,7 @@ describe('Onyx', () => {
12791279
// We set an initial value of 42 for ONYX_KEYS.OTHER_TEST in Onyx.init()
12801280
expect(otherTestCallback).toHaveBeenNthCalledWith(1, 42, ONYX_KEYS.OTHER_TEST);
12811281
expect(otherTestCallback).toHaveBeenNthCalledWith(2, 'pizza', ONYX_KEYS.OTHER_TEST);
1282-
for (const id of connections) Onyx.disconnect(id);
1282+
for (const id of connections) id.unsubscribe();
12831283
}),
12841284
);
12851285
});
@@ -1538,7 +1538,7 @@ describe('Onyx', () => {
15381538
expect(dogCallback).toHaveBeenNthCalledWith(1, undefined, dog);
15391539
expect(dogCallback).toHaveBeenLastCalledWith({name: 'Rex'}, dog);
15401540

1541-
connections.map((id) => Onyx.disconnect(id));
1541+
connections.map((id) => id.unsubscribe());
15421542
});
15431543
});
15441544

@@ -1769,7 +1769,7 @@ describe('Onyx', () => {
17691769
ONYX_KEYS.COLLECTION.ROUTES,
17701770
);
17711771

1772-
connections.map((id) => Onyx.disconnect(id));
1772+
connections.map((id) => id.unsubscribe());
17731773
});
17741774
});
17751775

@@ -1856,7 +1856,7 @@ describe('Onyx', () => {
18561856
ONYX_KEYS.COLLECTION.PEOPLE,
18571857
);
18581858

1859-
connections.map((id) => Onyx.disconnect(id));
1859+
connections.map((id) => id.unsubscribe());
18601860
});
18611861
});
18621862

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

2430-
Onyx.disconnect(connection1);
2431-
Onyx.disconnect(connection2);
2430+
connection1.unsubscribe();
2431+
connection2.unsubscribe();
24322432
});
24332433

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

4051-
Onyx.disconnect(connection);
4051+
connection.unsubscribe();
40524052
});
40534053

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

4081-
Onyx.disconnect(connection);
4081+
connection.unsubscribe();
40824082
});
40834083

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

4162-
Onyx.disconnect(connection);
4162+
connection.unsubscribe();
41634163
});
41644164

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

4198-
Onyx.disconnect(connection1);
4199-
Onyx.disconnect(connection2);
4198+
connection1.unsubscribe();
4199+
connection2.unsubscribe();
42004200
});
42014201

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

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

4246-
Onyx.disconnect(connection);
4247-
Onyx.disconnect(connection2);
4246+
connection.unsubscribe();
4247+
connection2.unsubscribe();
42484248
});
42494249

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

4294-
Onyx.disconnect(collectionConn);
4295-
Onyx.disconnect(collectionMember2Conn);
4294+
collectionConn.unsubscribe();
4295+
collectionMember2Conn.unsubscribe();
42964296
});
42974297

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

4336-
Onyx.disconnect(connection);
4336+
connection.unsubscribe();
43374337
});
43384338

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

4381-
Onyx.disconnect(collectionConn);
4382-
Onyx.disconnect(collectionMember2Conn);
4381+
collectionConn.unsubscribe();
4382+
collectionMember2Conn.unsubscribe();
43834383
});
43844384

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

4440-
Onyx.disconnect(connection);
4440+
connection.unsubscribe();
44414441
});
44424442
});
44434443

0 commit comments

Comments
 (0)