ZachBray/FunScript — pull request #8 by endeavour (2012-12-02 18:55:02+00:00)
Title: F# Events

I've implemented the basics of events/observables. None of the module are yet done (filter, map etc), nor any Rx modules.

Also needs tests to be added.

As a (contrived) example:

[]
module Program

open FunJS
open FunJS.TypeScript

[]
let log (x : string) : unit = failwith "never"

let main() =
let event = FunJS.Core.Events.Event()

let pub1 = event.Publish
let pub2 = event.Publish
let pub3 = event.Publish

let handler1 (x:int) = log ("ONE:" + x.ToString())

pub1.AddHandler(handler1)
pub2.AddHandler(fun x -> log ("TWO:" + x.ToString()))
pub1.RemoveHandler(handler1)

let observer1 = FunJS.Core.Events.ActionObserver((fun x -> log ("THREE:" + x.ToString())), (fun e -> ()), (fun()->()))

let sub3 = pub3.Subscribe(observer1)
sub3.Dispose()

let observer2 = FunJS.Core.Events.ActionObserver((fun x -> log ("FOUR:" + x.ToString())))

let sub4 = pub3.Subscribe(observer2)
sub4.Dispose()

event.Trigger(123)