WebAdvertisementReceivedEvent.withMemory constructor
WebAdvertisementReceivedEvent.withMemory(
- WebAdvertisementReceivedEvent memory,
- WebAdvertisementReceivedEvent newEvent
Not every device sends all the fields in each advertisement packet. (probably has something to do with the maximum size of such a packet).
This however makes it more difficult for the developer to keep track of
the most up-to-date data. This factory creates a new instance of the
event but fills in the missing data from newEvent with the information
that was stored form the last event that is in memory.
Below in an example how you could implement this memorization.
WebAdvertisementReceivedEvent? memory;
stream.map((event) {
final convertedEvent =
WebAdvertisementReceivedEvent.fromJSObject(event, device);
var combined = convertedEvent;
if (memory != null) {
combined = WebAdvertisementReceivedEvent.withMemory(memory!, convertedEvent);
}
memory = combined;
return combined;
});
Implementation
factory WebAdvertisementReceivedEvent.withMemory(
final WebAdvertisementReceivedEvent memory,
final WebAdvertisementReceivedEvent newEvent) {
assert(memory.device == newEvent.device,
"The device from memory should be the same as from the new event");
final List<String> uuids = newEvent.uuids;
final UnmodifiableMapView<int, ByteData> manufacturerData =
newEvent.manufacturerData;
final UnmodifiableMapView<String, ByteData> serviceData =
newEvent.serviceData;
late String? name;
if (newEvent.name != null) {
name = newEvent.name;
} else {
name = memory.name;
}
final int? rssi = newEvent.rssi;
final int? txPower = newEvent.txPower;
late int? appearance;
if (newEvent.appearance != null) {
appearance = newEvent.appearance;
} else {
appearance = memory.appearance;
}
return WebAdvertisementReceivedEvent._(
uuids: uuids,
manufacturerData: manufacturerData,
serviceData: serviceData,
name: name,
rssi: rssi,
txPower: txPower,
appearance: appearance,
device: newEvent.device);
}