Report duplicate ids in `split` as a warning in dev mode
See original GitHub issueHowdy!
Even though I understand that the proper usage of split
on a Signal[List[_]]
expects to have a unique id per element in a list, if you mistakenly have duplicated ids the split bit crashes with an obscure ObserverError: null
error.
From that point on, the component starts behaving weirdly, say, if you change the list of items. Also, the stacktrace doesn’t seem to point back to that execution so the error is quite hard to track back to the usage of split
and the reason why the error is happening.
Our workaround for now is just to de-duplicate before calling split
, however this could be a problem that affects other people. Perhaps doing the same process of deduplication, and/or showing a warning in case there is a duplicated item in the input Signal[List[_]]
is a positive change?
Here is a quick snippet to replicate:
import org.scalajs.dom
import com.raquo.laminar.api.L._
// NOTE: There is a duplicated element in this list
val signal = EventStream.empty.toSignal(List(1, 2, 3, 3, 4, 5))
val view: Div = {
div(
children <-- signal.split(_.toString) {
case (id: String, value: Int, valueSignal: Signal[Int]) =>
div(id)
}
)
}
render(
dom.document.body,
view
)
https://scalafiddle.io/sf/lq5Urp7/1
JS Cosole log:
VM140:5978 ObserverError: null
com.raquo.airstream.core.Observer$$anon$1.onNext(eval at <anonymous> (https://scalafiddle.io/resultframe?theme=light:46:37), <anonymous>:4513:92)
eval(eval at <anonymous> (https://scalafiddle.io/resultframe?theme=light:46:37), <anonymous>:4540:14)
scala.scalajs.runtime.AnonFunction1.apply(eval at <anonymous> (https://scalafiddle.io/resultframe?theme=light:46:37), <anonymous>:6678:45)
scala.util.Success.fold(eval at <anonymous> (https://scalafiddle.io/resultframe?theme=light:46:37), <anonymous>:9832:17)
com.raquo.airstream.core.Observer$$anon$1.onTry(eval at <anonymous> (https://scalafiddle.io/resultframe?theme=light:46:37), <anonymous>:4536:15)
com.raquo.airstream.signal.Signal.onAddedExternalObserver(eval at <anonymous> (https://scalafiddle.io/resultframe?theme=light:46:37), <anonymous>:4776:12)
com.raquo.airstream.signal.SignalFromEventStream.onAddedExternalObserver(eval at <anonymous> (https://scalafiddle.io/resultframe?theme=light:46:37), <anonymous>:8943:5)
com.raquo.airstream.core.Observable.addObserver(eval at <anonymous> (https://scalafiddle.io/resultframe?theme=light:46:37), <anonymous>:859:9)
com.raquo.airstream.core.Observable.foreach(eval at <anonymous> (https://scalafiddle.io/resultframe?theme=light:46:37), <anonymous>:851:10)
eval(eval at <anonymous> (https://scalafiddle.io/resultframe?theme=light:46:37), <anonymous>:1894:14)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:5 (4 by maintainers)
Top GitHub Comments
The upcoming Airstream 15.0.0 will now detect duplicate keys in
split
and print warnings to the browser console listing the offending keys as well as the parent observable’s displayName. This is enabled by default to help debugging. To avoid the overhead of checking for duplicates, you might want to disable it either:a) globally “in production” by setting
DuplicateKeysConfig.default.shouldWarn = true
in your main App.scala, orb) for specific large-list splits in your main App.scala by passing the optional
duplicateKeys = DuplicateKeysConfig.noWarnings
to thesplit
operator (it defaults toDuplicateKeysConfig.default
)For now, the warnings are just that – warnings. They don’t change the behaviour and don’t throw exceptions. This implementation is my initial take to provide some improvement to the dev experience. I might add more advanced configuration in a future version. I’m just not sure what features will be needed yet.
Cool yeah, I think this is what I’ll need, thanks @ngbinh!