Skip to content

Commit 1626711

Browse files
authored
Merge pull request #43 from blinklabs-io/feat/output-embedded
feat: embedded output plugin
2 parents b7af053 + 3184a86 commit 1626711

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

filter/filter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package filter
1616

17+
// We import the various plugins that we want to be auto-registered
1718
import (
1819
_ "github.com/blinklabs-io/snek/filter/chainsync"
1920
_ "github.com/blinklabs-io/snek/filter/event"

input/input.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package input
1616

17+
// We import the various plugins that we want to be auto-registered
1718
import (
1819
_ "github.com/blinklabs-io/snek/input/chainsync"
1920
)

output/embedded/embedded.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 embedded
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/blinklabs-io/snek/event"
21+
)
22+
23+
type CallbackFunc func(event.Event) error
24+
25+
type EmbeddedOutput struct {
26+
errorChan chan error
27+
eventChan chan event.Event
28+
callbackFunc CallbackFunc
29+
outputChan chan event.Event
30+
}
31+
32+
func New(options ...EmbeddedOptionFunc) *EmbeddedOutput {
33+
e := &EmbeddedOutput{
34+
errorChan: make(chan error),
35+
eventChan: make(chan event.Event, 10),
36+
}
37+
for _, option := range options {
38+
option(e)
39+
}
40+
return e
41+
}
42+
43+
// Start the embedded output
44+
func (e *EmbeddedOutput) Start() error {
45+
go func() {
46+
for {
47+
evt, ok := <-e.eventChan
48+
// Channel has been closed, which means we're shutting down
49+
if !ok {
50+
return
51+
}
52+
if e.callbackFunc != nil {
53+
if err := e.callbackFunc(evt); err != nil {
54+
e.errorChan <- fmt.Errorf("callback function error: %s", err)
55+
return
56+
}
57+
}
58+
if e.outputChan != nil {
59+
e.outputChan <- evt
60+
}
61+
}
62+
}()
63+
return nil
64+
}
65+
66+
// Stop the embedded output
67+
func (e *EmbeddedOutput) Stop() error {
68+
close(e.eventChan)
69+
close(e.errorChan)
70+
if e.outputChan != nil {
71+
close(e.outputChan)
72+
}
73+
return nil
74+
}
75+
76+
// ErrorChan returns the input error channel
77+
func (e *EmbeddedOutput) ErrorChan() chan error {
78+
return e.errorChan
79+
}
80+
81+
// InputChan returns the input event channel
82+
func (e *EmbeddedOutput) InputChan() chan<- event.Event {
83+
return e.eventChan
84+
}
85+
86+
// OutputChan always returns nil
87+
func (e *EmbeddedOutput) OutputChan() <-chan event.Event {
88+
return nil
89+
}

output/embedded/options.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 embedded
16+
17+
import "github.com/blinklabs-io/snek/event"
18+
19+
type EmbeddedOptionFunc func(*EmbeddedOutput)
20+
21+
// WithCallbackFunc specifies a callback function for events
22+
func WithCallbackFunc(callbackFunc CallbackFunc) EmbeddedOptionFunc {
23+
return func(o *EmbeddedOutput) {
24+
o.callbackFunc = callbackFunc
25+
}
26+
}
27+
28+
// WithOutputChan specifies an event.Event channel to use for events
29+
func WithOutputChan(outputChan chan event.Event) EmbeddedOptionFunc {
30+
return func(o *EmbeddedOutput) {
31+
o.outputChan = outputChan
32+
}
33+
}

output/output.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package output
1616

17+
// We import the various plugins that we want to be auto-registered
1718
import (
1819
_ "github.com/blinklabs-io/snek/output/log"
1920
)

0 commit comments

Comments
 (0)