Hello, friends!
I'm new here and this is my first post.
My question is: What are your opinions on using Observables for event handlers, now available in .NET 4.0?
As an example take the DocData class inspired by Glenn's RefUcsSpy and Tony's subsequent comments.
Alas, it is not C#, it's in F#; but should be straightforward to port.
type DocData(doc: Document) =
let obs =
doc.CommandWillStart
|> Observable.filter
(fun e ->
DocDataModule.Contains e.GlobalCommandName &&
not acadApp.WcsCurrent &&
not acadApp.ScriptActive &&
DocDataModule.RequestUserConfirmation() )
|> Observable.subscribe
(fun _ ->
let ucsFollow = acadApp.UcsFollow
let currentUCS = doc.Editor.CurrentUserCoordinateSystem
let commandEnded = ref null
commandEnded :=
doc.CommandCancelled
|> Observable.merge doc.CommandEnded
|> Observable.merge doc.CommandFailed
|> Observable.subscribe
(fun _ ->
acadApp.UcsFollow <- ucsFollow
doc.Editor.CurrentUserCoordinateSystem <- currentUCS
(!commandEnded).Dispose() )
acadApp.UcsFollow <- false
doc.Editor.CurrentUserCoordinateSystem <-
Matrix3d.Identity )
member x.Stop() =
obs.Dispose()
Look ma, no mutable state.