ostgals Github contribution chart
ostgals Github Stats
ostgals Most Used Languages

Activity

09 Sep 2022

Ostgals

started

Started On 09 Sep 2022 at 10:03:15
Issue Comment

Ostgals

Do not store subscribers by function name

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 like

nb.subscribe(() => update()) // not nb.subscribe(update) 

Forked On 26 Jul 2022 at 08:16:26

Ostgals

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 update! As a result, the List is never updated anymore.

So storing subscriptions using function names basically is not a good idea.

Commented On 26 Jul 2022 at 08:16:26