Logs creates a subscription that fires for all new log that match the given filter criteria.
(ctx context.Context, crit FilterCriteria)
| 235 | |
| 236 | // Logs creates a subscription that fires for all new log that match the given filter criteria. |
| 237 | func (api *PublicFilterAPI) Logs(ctx context.Context, crit FilterCriteria) (*rpc.Subscription, error) { |
| 238 | notifier, supported := rpc.NotifierFromContext(ctx) |
| 239 | if !supported { |
| 240 | return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported |
| 241 | } |
| 242 | |
| 243 | var ( |
| 244 | rpcSub = notifier.CreateSubscription() |
| 245 | matchedLogs = make(chan []*types.Log) |
| 246 | ) |
| 247 | |
| 248 | logsSub, err := api.events.SubscribeLogs(cpchain.FilterQuery(crit), matchedLogs) |
| 249 | if err != nil { |
| 250 | return nil, err |
| 251 | } |
| 252 | |
| 253 | go func() { |
| 254 | |
| 255 | for { |
| 256 | select { |
| 257 | case logs := <-matchedLogs: |
| 258 | for _, log := range logs { |
| 259 | notifier.Notify(rpcSub.ID, &log) |
| 260 | } |
| 261 | case <-rpcSub.Err(): // client send an unsubscribe request |
| 262 | logsSub.Unsubscribe() |
| 263 | return |
| 264 | case <-notifier.Closed(): // connection dropped |
| 265 | logsSub.Unsubscribe() |
| 266 | return |
| 267 | } |
| 268 | } |
| 269 | }() |
| 270 | |
| 271 | return rpcSub, nil |
| 272 | } |
| 273 | |
| 274 | // FilterCriteria represents a request to create a new filter. |
| 275 | // Same as ethereum.FilterQuery but with UnmarshalJSON() method. |
no test coverage detected