我有以下推文流类。它有 TweetReceived 事件,可以与我系统的其他组件一起使用。
它似乎工作正常,但我觉得它比它应该的更复杂。
是否有任何工具可以为我提供此功能而无需自己实现 mbox/事件机制?
您还建议使用 asyncSeq 而不是 IObservable 吗?
谢谢!
type TweetStream ( cfg:oauth.Config) =
let token = TwitterToken.Token (cfg.accessToken,
cfg.accessTokenSecret,
cfg.appKey,
cfg.appSecret)
let stream = new SimpleStream("https://stream.twitter.com/1.1/statuses/sample.json")
let event = new Event<_>()
let agent = MailboxProcessor.Start(fun (mbox) ->
let rec loop () =
async {
let! msg = mbox.Receive()
do event.Trigger(msg)
return! loop()
}
loop ())
member x.TweetReceived = event.Publish
member x.Start () =
Task.Factory.StartNew(fun () -> stream.StartStream(token, agent.Post))
|> ignore
member x.Stop = stream.StopStream
更新:感谢 Thomas 对第二个问题的快速(一如既往)回答。
我的第一个问题可能有点不清楚,所以我重构了代码以使类 AgentEvent 可见,我重新表述第一个问题:有没有办法更容易地实现 AgentEvent 中的逻辑?这个逻辑是否已经在某个地方实现了?
我问这个是因为它感觉像是一种常见的使用模式。
type AgentEvent<'t>()=
let event = new Event<'t>()
let agent = MailboxProcessor.Start(fun (mbox) ->
let rec loop () =
async {
let! msg = mbox.Receive()
do event.Trigger(msg)
return! loop()
}
loop ())
member x.Event = event.Publish
member x.Post = agent.Post
type TweetStream ( cfg:oauth.Config) =
let token = TwitterToken.Token (cfg.accessToken,
cfg.accessTokenSecret,
cfg.appKey,
cfg.appSecret)
let stream = new SimpleStream("https://stream.twitter.com/1.1/statuses/sample.json")
let agentEvent = AgentEvent()
member x.TweetReceived = agentEvent.Event
member x.Start () =
Task.Factory.StartNew(fun () -> stream.StartStream(token, agentEvent.Post))
|> ignore
member x.Stop = stream.StopStream