2 people like it.

Resource cleanup event combinator

Declares an event combinator 'Event.using' that automatically releases resources allocated by a previous event occurence. Each event occurence creates a value using a function specified by the user and automatically calls 'Dispose' when generating a new value.

Declaration of the using combinator

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
open System

module Event = 
  /// Generates new values using the specified function 'f' (just like
  /// 'Event.map'), but automatically diposes of the previous value when 
  /// a new value is generated.
  let using f evt = 
    evt |> Event.scan (fun st inp ->
      // Dipose of the previous value if it is not 'null'
      if st <> Unchecked.defaultof<_> then 
        (st :> IDisposable).Dispose()
      f inp ) Unchecked.defaultof<_>

Windows Forms example

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
open System.Drawing
open System.Windows.Forms

let colors = (...)
let frm = (...)

// Turns colors into brushes, but automatically disposes 
// of the previous brush when creating a new one
colors 
  |> Event.using (fun clr -> new SolidBrush(clr))
  |> Event.add (fun br -> frm.BackBrush <- br)
namespace System
Multiple items
module Event

from Microsoft.FSharp.Control

--------------------
type Event<'T> =
  new : unit -> Event<'T>
  member Trigger : arg:'T -> unit
  member Publish : IEvent<'T>

Full name: Microsoft.FSharp.Control.Event<_>

--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate)> =
  new : unit -> Event<'Delegate,'Args>
  member Trigger : sender:obj * args:'Args -> unit
  member Publish : IEvent<'Delegate,'Args>

Full name: Microsoft.FSharp.Control.Event<_,_>

--------------------
new : unit -> Event<'T>

--------------------
new : unit -> Event<'Delegate,'Args>
val using : f:('a -> 'b) -> evt:IEvent<'c,'a> -> IEvent<'b> (requires equality and 'b :> IDisposable and delegate and 'c :> Delegate)

Full name: Script.Event.using


 Generates new values using the specified function 'f' (just like
 'Event.map'), but automatically diposes of the previous value when
 a new value is generated.
val f : ('a -> 'b) (requires equality and 'b :> IDisposable)
val evt : IEvent<'c,'a> (requires delegate and 'c :> Delegate)
val scan : collector:('U -> 'T -> 'U) -> state:'U -> sourceEvent:IEvent<'Del,'T> -> IEvent<'U> (requires delegate and 'Del :> Delegate)

Full name: Microsoft.FSharp.Control.Event.scan
val st : 'b (requires equality and 'b :> IDisposable)
val inp : 'a
module Unchecked

from Microsoft.FSharp.Core.Operators
val defaultof<'T> : 'T

Full name: Microsoft.FSharp.Core.Operators.Unchecked.defaultof
type IDisposable =
  member Dispose : unit -> unit

Full name: System.IDisposable
namespace System.Drawing
namespace System.Windows
namespace System.Windows.Forms
val colors : IEvent<Color>

Full name: Script.colors
failwith "Not implemented" : IEvent<Color>
val frm : Form

Full name: Script.frm
failwith "Not implemented" : Form
Multiple items
module Event

from Script

--------------------
module Event

from Microsoft.FSharp.Control

--------------------
type Event<'T> =
  new : unit -> Event<'T>
  member Trigger : arg:'T -> unit
  member Publish : IEvent<'T>

Full name: Microsoft.FSharp.Control.Event<_>

--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate)> =
  new : unit -> Event<'Delegate,'Args>
  member Trigger : sender:obj * args:'Args -> unit
  member Publish : IEvent<'Delegate,'Args>

Full name: Microsoft.FSharp.Control.Event<_,_>

--------------------
new : unit -> Event<'T>

--------------------
new : unit -> Event<'Delegate,'Args>
val clr : Color
Multiple items
type SolidBrush =
  inherit Brush
  new : color:Color -> SolidBrush
  member Clone : unit -> obj
  member Color : Color with get, set

Full name: System.Drawing.SolidBrush

--------------------
SolidBrush(color: Color) : unit
val add : callback:('T -> unit) -> sourceEvent:IEvent<'Del,'T> -> unit (requires delegate and 'Del :> Delegate)

Full name: Microsoft.FSharp.Control.Event.add
val br : SolidBrush
Raw view Test code New version

More information

Link:http://fssnip.net/43
Posted:13 years ago
Author:Tomas Petricek
Tags: disposable , idisposable , events , reactive , event