Title: Improve discoverability and support for (de)registering event handlers · Issue #12926 · PowerShell/PowerShell · GitHub
Open Graph Title: Improve discoverability and support for (de)registering event handlers · Issue #12926 · PowerShell/PowerShell
X Title: Improve discoverability and support for (de)registering event handlers · Issue #12926 · PowerShell/PowerShell
Description: Summary of the new feature/enhancement Currently, finding event metadata is pretty difficult in PS. The event members are deliberately hidden from autocompletion, can't be directly accessed from objects like methods can to reveal metadat...
Open Graph Description: Summary of the new feature/enhancement Currently, finding event metadata is pretty difficult in PS. The event members are deliberately hidden from autocompletion, can't be directly accessed from ob...
X Description: Summary of the new feature/enhancement Currently, finding event metadata is pretty difficult in PS. The event members are deliberately hidden from autocompletion, can't be directly accessed fro...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/12926
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Improve discoverability and support for (de)registering event handlers","articleBody":"## Summary of the new feature/enhancement\r\n\r\nCurrently, finding event metadata is pretty difficult in PS. The event members are deliberately hidden from autocompletion, can't be directly accessed from objects like methods can to reveal metadata which is often useful in determining how you want to work with them, and aren't available for autocompletion.\r\n\r\nAdditionally, while `Register-EngineEvent` and `Register-ObjectEvent` do exist, they won't always work. For example, using some GUI-centric script patterns, many users can run into deadlocks using `Register-ObjectEvent` and some Forms classes.\r\n\r\nTo work around this, many scripters resort to using the hidden `$obj.add_EventName($handler)` methods which are not discoverable, not well-documented (as they are generally considered an implementation detail), and difficult to work with in PS.\r\n\r\nMost documentation around events is C#-centric and will recommend the C# syntax for registering and deregistering delegates; `obj.EventName += eventHandler;`\r\n\r\nThis creates friction and confusion; users have neither appropriate access to event metadata to determine how to work with them in PS, nor appropriate documentation to fill in the gaps where Register-ObjectEvent isn't a workable solution. Additionally, even discovering event names for use with Register-ObjectEvent is difficult since the members aren't directly available.\r\n\r\n## Proposed technical implementation details (optional)\r\n\r\n1. Expose event members as actual object members, allowing their names to register for tab completion, and the PSEvent metadata to be shown to users accessing the event.\r\n2. Implement a custom binder for use with `+=` / `-=` which bridges the gap by calling the appropriate `add_*` / `remove_*` method for the targeted event.\r\n3. Add some logic into the Compiler to perform a runtime check for LHS being a PSEvent when handling expressions with `+=` or `-=` operators (this will be dependent on #1 being implemented, or this check cannot be relied on). If the LHS is PSEvent, call out to the binder from #2 and (de)register the event. Naturally, fall back to standard `+=` behaviour if the LHS is not `PSEvent`.\r\n\r\n### Demonstration\r\n\r\n```ps1\r\nPS> $ps = [powershell]::Create()\r\nPS> $handler = { Write-Host \"event triggered!\" }\r\n# Tab completion lists the event member as well\r\nPS> $ps.Invo\r\nInvocationStateInfo Invoke InvokeAsync InvocationStateChanged \r\nPS> $ps.InvocationStateChanged += $handler\r\nPS> $ps.AddScript('Write-Output \"data from pipe\"').Invoke()\r\nevent triggered!\r\nevent triggered!\r\ndata from pipe\r\nPS> $ps.InvocationStateChanged -= $handler\r\nPS> $ps.AddScript('Write-Output \"data from pipe\"').Invoke()\r\ndata from pipe\r\n```\r\n\r\nI have working code for this that I can submit as a PR if there is interest in having this be a part of PowerShell. Just have to put some tests together 😎 \r\n\r\nBelow illustrates the metadata event members would show if users attempt to examine them, once the aforementioned changes are in place.\r\n\r\n```\r\nPS> $ps.InvocationStateChanged\r\n\r\nMemberType : Event\r\nValue : System.EventHandler`1[System.Management.Automation.PSInvocationStateChangedEventArgs] InvocationStateChanged\r\nTypeNameOfValue : System.Management.Automation.PSEvent\r\nName : InvocationStateChanged\r\nIsInstance : True\r\n\r\n\r\nPS /Users/joelfrancis/repos/Github/powershell> $ps.InvocationStateChanged.Value\r\n\r\nMemberType : Event\r\nName : InvocationStateChanged\r\nDeclaringType : System.Management.Automation.PowerShell\r\nReflectedType : System.Management.Automation.PowerShell\r\nMetadataToken : 335544336\r\nModule : System.Management.Automation.dll\r\nAttributes : None\r\nIsSpecialName : False\r\nAddMethod : Void add_InvocationStateChanged(System.EventHandler`1[System.Management.Automation.PSInvocationStateChangedEventArgs])\r\nRemoveMethod : Void remove_InvocationStateChanged(System.EventHandler`1[System.Management.Automation.PSInvocationStateChangedEventArgs])\r\nRaiseMethod : \r\nIsMulticast : True\r\nEventHandlerType : System.EventHandler`1[System.Management.Automation.PSInvocationStateChangedEventArgs]\r\nCustomAttributes : {}\r\nIsCollectible : True\r\n\r\n\r\nPS> $ps.InvocationStateChanged.GetType()\r\n\r\nIsPublic IsSerial Name BaseType\r\n-------- -------- ---- --------\r\nTrue False PSEvent System.Management.Automation.PSMemberInfo\r\n\r\nPS> $ps.InvocationStateChanged.Value.GetType()\r\n\r\nIsPublic IsSerial Name BaseType\r\n-------- -------- ---- --------\r\nFalse False RuntimeEventInfo System.Reflection.EventInfo\r\n```\r\n","author":{"url":"https://github.com/vexx32","@type":"Person","name":"vexx32"},"datePublished":"2020-06-09T14:13:49Z","url":"https://github.com/PowerShell/PowerShell/issues/12926"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:be2c0ace-35f8-ad73-8a5f-2359b98fc1cb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF46:24F1E7:E10425:142A161:6AFDB3E5 |
| html-safe-nonce | 7fbc62b4778209b65ad6e99fb88db5287299e24f59b552f3e46507be46d2a683 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjQ2OjI0RjFFNzpFMTA0MjU6MTQyQTE2MTo2QUZEQjNFNSIsInZpc2l0b3JfaWQiOiI1NjI2MDUwMDY3Mzg0MDg3NTI1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 12980f373155a82eb66ca378567e3e6029ac553704d7752990a193a91b99a62d |
| hovercard-subject-tag | issue:635467452 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/12926/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0d80c51b96c8f65ef07d7a4f0f0d87ebd84ba9b3bb362a9369515a3846effc7b/PowerShell/PowerShell/issues/12926 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0d80c51b96c8f65ef07d7a4f0f0d87ebd84ba9b3bb362a9369515a3846effc7b/PowerShell/PowerShell/issues/12926 |
| og:image:alt | Summary of the new feature/enhancement Currently, finding event metadata is pretty difficult in PS. The event members are deliberately hidden from autocompletion, can't be directly accessed from ob... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vexx32 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3c532f4d069d6fbe9b1837450343d1b9e0619a7d5c4ba0b3b64fd8e0bae67c7e |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5396ad00d1c6d19097aee7fc4d00a2eb03432c8f |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width