|
| 1 | +// Copyright 2023 Blink Labs, LLC. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package notify |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/blinklabs-io/snek/event" |
| 21 | + "github.com/blinklabs-io/snek/input/chainsync" |
| 22 | + "github.com/gen2brain/beeep" |
| 23 | +) |
| 24 | + |
| 25 | +type NotifyOutput struct { |
| 26 | + errorChan chan error |
| 27 | + eventChan chan event.Event |
| 28 | +} |
| 29 | + |
| 30 | +func New(options ...NotifyOptionFunc) *NotifyOutput { |
| 31 | + n := &NotifyOutput{ |
| 32 | + errorChan: make(chan error), |
| 33 | + eventChan: make(chan event.Event, 10), |
| 34 | + } |
| 35 | + for _, option := range options { |
| 36 | + option(n) |
| 37 | + } |
| 38 | + return n |
| 39 | +} |
| 40 | + |
| 41 | +// Start the notify output |
| 42 | +func (n *NotifyOutput) Start() error { |
| 43 | + go func() { |
| 44 | + for { |
| 45 | + evt, ok := <-n.eventChan |
| 46 | + // Channel has been closed, which means we're shutting down |
| 47 | + if !ok { |
| 48 | + return |
| 49 | + } |
| 50 | + switch evt.Type { |
| 51 | + case "chainsync.block": |
| 52 | + payload := evt.Payload |
| 53 | + if payload == nil { |
| 54 | + panic(fmt.Errorf("ERROR: %v", payload)) |
| 55 | + } |
| 56 | + |
| 57 | + be := payload.(chainsync.BlockEvent) |
| 58 | + err := beeep.Notify( |
| 59 | + "Snek", |
| 60 | + fmt.Sprintf("New Block!\nBlockNumber: %d, SlotNumber: %d\nHash: %s", |
| 61 | + be.BlockNumber, |
| 62 | + be.SlotNumber, |
| 63 | + be.BlockHash, |
| 64 | + ), |
| 65 | + "assets/snek-icon.png", |
| 66 | + ) |
| 67 | + if err != nil { |
| 68 | + panic(err) |
| 69 | + } |
| 70 | + default: |
| 71 | + err := beeep.Notify( |
| 72 | + "Snek", |
| 73 | + fmt.Sprintf("New Event!\nEvent: %v", evt), |
| 74 | + "assets/snek-icon.png", |
| 75 | + ) |
| 76 | + if err != nil { |
| 77 | + panic(err) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + }() |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +// Stop the embedded output |
| 86 | +func (n *NotifyOutput) Stop() error { |
| 87 | + close(n.eventChan) |
| 88 | + close(n.errorChan) |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +// ErrorChan returns the input error channel |
| 93 | +func (n *NotifyOutput) ErrorChan() chan error { |
| 94 | + return n.errorChan |
| 95 | +} |
| 96 | + |
| 97 | +// InputChan returns the input event channel |
| 98 | +func (n *NotifyOutput) InputChan() chan<- event.Event { |
| 99 | + return n.eventChan |
| 100 | +} |
| 101 | + |
| 102 | +// OutputChan always returns nil |
| 103 | +func (n *NotifyOutput) OutputChan() <-chan event.Event { |
| 104 | + return nil |
| 105 | +} |
0 commit comments