Add observer/observable pattern to device#7
Draft
bruno-f-cruz wants to merge 4 commits into
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Leaving this as a draft as it is a bit opinionated, but since I had this convo with @Poofjunior and @glopesdev separately, just putting it on a PR for future reference. This is how I would like to handle events from devices :P
I added a quick example that compares the two APIs
Summary
tldr: push over pull, reactive framework rocks.
Replaces the single overridable
_on_eventhook with a proper push-subscription API for device-emitted messages, delivered off the reader thread, plus an optional ReactiveX adapter.What changed
Device.subscribe(register, handler, *, message_types=Event)— register a callback that receives a typed, parsedParsedHarpMessage. Filters by message type (default: unsolicitedEvents only; opt intoRead/Writereplies). Returns aSubscriptionhandle withunsubscribe()(also a context manager).Device.subscribe_all(handler, ...)— catch-all across every address for logging/forwarding._dispatchcorrelates request/reply replies on the fast path (O(1), non-blocking) and fans every message onto a dedicated event thread. Handlers run there, so a slow handler can't stall synchronousread()/write(), and handlers may safely call back into the device.close()clears subscriptions so a closed/reopened device retains no stale handlers.harp.device.rx) —observe()/observe_all()return hot, ref-counted (ops.share) Observables backed by a single underlying subscription. Gated behind theharp-device[rx]extra; importing the module withoutreactivexraises a clearImportError. If i had it my way, we would only have this, but for now leaving it optional.scripts/event_monitor.pyprints all events from a device on COM3.Notes
ops.observe_on(scheduler).