locka99 / opcua

A client and server implementation of the OPC UA specification written in Rust
Mozilla Public License 2.0
475 stars 128 forks source link

feat(subscription): Allow to detect when all data_value event are sent #325

Open tinou98 opened 1 month ago

tinou98 commented 1 month ago

This allows to write code like that :

struct NotificationHandler {
    buffer: Chunk,
    sender: Sender,
}

impl OnSubscriptionNotification for NotificationHandler {
    fn on_data_value(&mut self, val: DataValue, item: &MonitoredItem) {
        let Some(value) = val.value else {
            return;
        };

        self.buffer.add_point(item.item_to_monitor().node_id, value);
    }

    fn on_packet_handled(&mut self) {
        self.sender.send(std::mem::take(&mut self.buffer));
    }
}

I didn't add the entry in SubscriptionCallbacks, because the main purpose of this “packet handled” event is to flush something, which require shared state (which is hard in different FnMut), and also because it would be a breaking change.