-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do not store subscribers by function name #156
Comments
Please provide an example of client code with use case for the same function subscribed more than once. |
Okay, here is an elementary React example of what I mean: const List = ({ nexus, onSelectRow }) => {
const [records, setRecords] = React.useState([])
React.useEffect(() => {
const update = () => setRecords(nexus.getRecordSet())
const token = nexus.subscribe(update) // token === 'update'
return () => nexus.unsubscribe(token)
}, [nexus])
return (/* records rendered as a list with pagination controls */)
}
const Form = ({ nexus, onClose }) => {
const [model, setModel] = React.useState({})
React.useEffect(() => {
const update = ({ controls, methods }) => {
setModel(nexus.getCurrentRecordModel())
}
const token = nexus.subscribe(update) // token === 'update'
return () => nexus.unsubscribe(token)
}, [nexus])
return (/* form that allows to view and edit specific record */)
}
const ViewApp = () => {
const nexus = NexusFactory('example')
const [showForm, toggleForm] = React.useState(false)
const handleSelectRow = index => {
nexus.positionOnRow(index)
toggleForm(true)
}
return (
<>
<List nexus={nexus} onSelectRow={handleSelectRow} />
{showForm && <Form nexus={nexus} onClose={() => toggleForm(false)} />}
</>
)
} The point is that two different components create two subscriptions to the same Nexus instance. As soon as Form is mounted its subscription replaces the earlier subscription created by List because both components use subscriber functions with the same name So storing subscriptions using function names basically is not a good idea. |
Ok, I see now the named function is stored by its name, e.g.
Basically we need to remove tokens and store function objects directly:
React example from above should be covered in this case. |
It leads to a weird situation when different closures attach two different subscribers with the same name. In this case, the second attached subscriber replaces the first one leading to unsubscribing in the first closure.
Possible fixes
Just remove the check if the subscriber is named function or store subscribers as function instances.
Possible workaround until the bug is fixed
Pass anonymous subscribers to the
.subscribe
method likeThe text was updated successfully, but these errors were encountered: