WritableMap values can be arbitrarily changed in JS side, hence the next listener will received a modified value instead of the original values from Native Event
Description
WritableMap(and I should say WritableArray as well) are mutable on the JS side.
React Native version:
0.64.1
Steps To Reproduce
Provide a detailed list of steps that reproduce the issue.
- Create a Native event and fire it from native side. Ensure that you are passing a reference argument(like WritableMap or WritableArray). let's say that native returns { "a" : 1, "b": 1 }
- Create 2 listeners.
- First Listener does this
eventEmitter.addListener("myEvent", (obj) => {
console.log("obj: ", obj)
obj.a = 2
})
- Second Listener does this
eventEmitter.addListener("myEvent", (obj) => {
console.log("obj: ", obj)
obj.b = 2
})
- Depending on the listener invocation sequence, either you see
{ a: 1, b: 2 }or{ a: 2, b 1 }
Expected Results
Function listeners should not be able to interfere with another listener from the mutation of the argument. The simplest fix for this is to ensure that each listener should be receiving a copy of the WritableMap or WritableArray coming from native, to preserve the integrity of the listeners
Workaround
eventEmitter.addListener("myEvent", (origObj) => {
const copy = _.cloneDeep(origObj) // lodash's clone deep or the equivalent javascript code
copy.b = 2 // does not interefere with other listeners
})
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.
This issue was closed because it has been stalled for 7 days with no activity.