Commit 4d9f743
fix(listeners): use singleton native listener to prevent iOS removeAll() bug (#3164)
## Summary
- Fix critical bug where iOS `removePurchaseUpdatedListener` called
`removeAll()` instead of removing only the specific listener, silently
wiping ALL registered listeners
- Replace per-listener native registration with singleton native handler
+ JS-level `Set` fan-out for all 5 listener types
- `remove()` now only deletes from the JS Set — other listeners remain
intact regardless of iOS native behavior
## Root Cause
When multiple `useIAP` hooks were active (e.g., a persistent top-level
component + a screen-level component), unmounting one component called
`removePurchaseUpdatedListener` on iOS which triggered
`purchaseUpdatedListeners.removeAll()`. This wiped **all** listeners
including the ones from the still-mounted component, causing
`onPurchaseSuccess` to silently stop firing.
Android was unaffected because it correctly used `.remove(listener)`
instead of `.removeAll()`.
### Before (broken)
```
Component A: useIAP → registers listener A
Component B: useIAP → registers listener B
Component B unmounts → iOS removeAll() → listener A also gone ❌
User purchases → no listener to receive → purchase lost
```
### After (fixed)
```
Component A: useIAP → adds cbA to JS Set
Component B: useIAP → adds cbB to JS Set
(native singleton already attached, no duplicate registration)
Component B unmounts → Set.delete(cbB) → cbA still in Set ✅
User purchases → native singleton → fans out to cbA ✅
```
## Changes
### `src/index.ts`
- Replace `WeakMap` per-listener tracking with module-level `Set` per
event type
- Register a single native handler that fans out to all JS listeners in
the Set
- `remove()` only deletes from JS Set (never calls native remove)
- Add `resetListenerState()` called in `endConnection()` for clean
re-registration
- Applied to all 5 listener types: `purchaseUpdated`, `purchaseError`,
`promotedProduct`, `userChoiceBilling`, `developerProvidedBilling`
### `src/__tests__/index.test.ts`
- Update tests to verify singleton native registration (1 call, not N)
- Verify JS-level removal: listener stops receiving after `remove()`
- Verify independent removal: removing one listener doesn't affect
others
## Test plan
- [x] `yarn typecheck` passes
- [x] `yarn lint` passes
- [x] `yarn jest` — 251 tests pass
Closes #3150
🤖 Generated with [Claude Code](https://claude.ai/code)
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Added a public way to fully clear and reset event-listener state to
ensure clean reconnections.
* **Refactor**
* Centralized event dispatch so a single native callback fans out to
multiple JS listeners for consistent broadcasting and error handling.
* **Tests**
* Updated tests to validate single-handler broadcasting, JS-level
removal behavior, reconnection/re-registration, and normalized error
forwarding.
* **Documentation**
* Clarified PR review workflow to require explicit POST when replying to
individual review comments.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent e2acf36 commit 4d9f743
File tree
3 files changed
+259
-211
lines changed- .claude/commands
- src
- __tests__
3 files changed
+259
-211
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| 88 | + | |
88 | 89 | | |
89 | | - | |
| 90 | + | |
90 | 91 | | |
91 | | - | |
| 92 | + | |
| 93 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
133 | | - | |
| 133 | + | |
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
143 | | - | |
144 | | - | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
145 | 147 | | |
146 | 148 | | |
147 | 149 | | |
148 | 150 | | |
149 | 151 | | |
150 | 152 | | |
151 | 153 | | |
152 | | - | |
| 154 | + | |
153 | 155 | | |
154 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
155 | 160 | | |
156 | 161 | | |
157 | 162 | | |
| |||
160 | 165 | | |
161 | 166 | | |
162 | 167 | | |
163 | | - | |
164 | | - | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
165 | 171 | | |
166 | 172 | | |
167 | 173 | | |
| |||
170 | 176 | | |
171 | 177 | | |
172 | 178 | | |
173 | | - | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
174 | 183 | | |
175 | 184 | | |
176 | 185 | | |
| |||
198 | 207 | | |
199 | 208 | | |
200 | 209 | | |
201 | | - | |
202 | | - | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
203 | 214 | | |
204 | 215 | | |
205 | 216 | | |
206 | 217 | | |
207 | | - | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
208 | 222 | | |
209 | 223 | | |
210 | 224 | | |
| |||
215 | 229 | | |
216 | 230 | | |
217 | 231 | | |
218 | | - | |
| 232 | + | |
219 | 233 | | |
220 | 234 | | |
221 | 235 | | |
222 | 236 | | |
223 | 237 | | |
224 | | - | |
| 238 | + | |
| 239 | + | |
225 | 240 | | |
226 | 241 | | |
227 | 242 | | |
| |||
232 | 247 | | |
233 | 248 | | |
234 | 249 | | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
239 | 253 | | |
240 | 254 | | |
241 | 255 | | |
| |||
248 | 262 | | |
249 | 263 | | |
250 | 264 | | |
251 | | - | |
| 265 | + | |
252 | 266 | | |
| 267 | + | |
253 | 268 | | |
254 | | - | |
255 | 269 | | |
256 | | - | |
| 270 | + | |
257 | 271 | | |
258 | 272 | | |
259 | 273 | | |
| |||
263 | 277 | | |
264 | 278 | | |
265 | 279 | | |
266 | | - | |
| 280 | + | |
| 281 | + | |
267 | 282 | | |
268 | 283 | | |
269 | | - | |
270 | | - | |
271 | 284 | | |
272 | 285 | | |
273 | | - | |
| 286 | + | |
274 | 287 | | |
275 | 288 | | |
276 | 289 | | |
277 | 290 | | |
278 | 291 | | |
279 | | - | |
| 292 | + | |
| 293 | + | |
280 | 294 | | |
281 | | - | |
282 | | - | |
| 295 | + | |
283 | 296 | | |
284 | | - | |
285 | | - | |
| 297 | + | |
286 | 298 | | |
287 | 299 | | |
288 | 300 | | |
| |||
295 | 307 | | |
296 | 308 | | |
297 | 309 | | |
298 | | - | |
| 310 | + | |
299 | 311 | | |
300 | 312 | | |
301 | 313 | | |
302 | | - | |
303 | | - | |
| 314 | + | |
| 315 | + | |
304 | 316 | | |
305 | 317 | | |
306 | | - | |
307 | | - | |
308 | 318 | | |
309 | 319 | | |
310 | 320 | | |
| |||
322 | 332 | | |
323 | 333 | | |
324 | 334 | | |
325 | | - | |
| 335 | + | |
326 | 336 | | |
327 | | - | |
| 337 | + | |
| 338 | + | |
328 | 339 | | |
329 | 340 | | |
330 | 341 | | |
| |||
336 | 347 | | |
337 | 348 | | |
338 | 349 | | |
339 | | - | |
| 350 | + | |
340 | 351 | | |
341 | 352 | | |
342 | | - | |
| 353 | + | |
343 | 354 | | |
344 | 355 | | |
345 | 356 | | |
| |||
349 | 360 | | |
350 | 361 | | |
351 | 362 | | |
352 | | - | |
| 363 | + | |
353 | 364 | | |
354 | | - | |
| 365 | + | |
| 366 | + | |
355 | 367 | | |
356 | 368 | | |
357 | | - | |
| 369 | + | |
358 | 370 | | |
359 | 371 | | |
360 | 372 | | |
| |||
377 | 389 | | |
378 | 390 | | |
379 | 391 | | |
380 | | - | |
| 392 | + | |
381 | 393 | | |
382 | | - | |
| 394 | + | |
383 | 395 | | |
384 | 396 | | |
385 | 397 | | |
| |||
1777 | 1789 | | |
1778 | 1790 | | |
1779 | 1791 | | |
1780 | | - | |
1781 | | - | |
1782 | | - | |
| 1792 | + | |
| 1793 | + | |
| 1794 | + | |
| 1795 | + | |
1783 | 1796 | | |
1784 | 1797 | | |
1785 | 1798 | | |
| |||
0 commit comments