OP-Engineering / op-sqlite

Fastest SQLite library for react-native by @ospfranco
MIT License
598 stars 41 forks source link

[Bug]: Weird reactiveExecute behavior in useEffect and useState #177

Closed shovel-kun closed 4 weeks ago

shovel-kun commented 1 month ago

What happened?

Description

I've observed two bugs when using reactiveExecute with useEffect and useState:

  1. unsubscribe does not work when unmounting, but will work elsewhere.
  2. setting unsubscribe to a useState will cause unsubscribe to run.

Motivation

What I want to do is:

Does anyone have a workaround that allows me to accomplish the above?

op-sqlite version

9.2.6

React Native version

New Arch 0.76, but can be reproduced on older RN versions.

Reproducible Example

https://github.com/shovel-kun/op-sqlite-useeffect-repro

See instructions in App.tsx for repro.

Expected logs:

 LOG  Effect mounted
 LOG  Pressed!
 LOG  Callback ran!
 LOG  Effect unmounting
 LOG  Effect mounted
 LOG  Pressed!
 LOG  Callback ran!
 LOG  Effect unmounting
 LOG  Effect mounted
 LOG  Pressed!
 LOG  Callback ran!

Actual logs:

 LOG  Effect mounted
 LOG  Pressed!
 LOG  Callback ran!
 LOG  Effect unmounting
 LOG  Effect mounted
 LOG  Pressed!
 LOG  Callback ran!
 LOG  Callback ran!
 LOG  Effect unmounting
 LOG  Effect mounted
 LOG  Pressed!
 LOG  Callback ran!
 LOG  Callback ran!
 LOG  Callback ran!
ospfranco commented 1 month ago

You have to await the execute

https://github.com/shovel-kun/op-sqlite-useeffect-repro/blob/main/App.tsx#L77

shovel-kun commented 1 month ago

@ospfranco Added the fix, but the reactiveExecute still doesn't unsubscribe on unmount.

 LOG  Effect mounted
 LOG  Pressed!
 LOG  Callback ran!
 LOG  Effect unmounting
 LOG  Effect mounted
 LOG  Pressed!
 LOG  Callback ran!
 LOG  Callback ran!
 LOG  Effect unmounting
 LOG  Effect mounted
 LOG  Pressed!
 LOG  Callback ran!
 LOG  Callback ran!
 LOG  Callback ran!
ospfranco commented 1 month ago

ah, I see it now. Give me some minutes I'll take a look.

ospfranco commented 1 month ago

Found the issue, will publish a new version soon