Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stuck when trying to send response back to message Bus #25

Closed
hanneslehmann opened this issue Jun 19, 2017 · 2 comments
Closed

Stuck when trying to send response back to message Bus #25

hanneslehmann opened this issue Jun 19, 2017 · 2 comments

Comments

@hanneslehmann
Copy link

Hi,
I have modified the example and wanted to send the response of the calculator back on the bus, on a different topic. I would like to chain different functions to react on events down a chain. When I run "go run" process is stuck, nothing happens.

package main
import (
    "fmt"
    "github.com/asaskevich/EventBus"
    "time"
)

var (
    bus  EventBus.Bus;
)

func calculator1(a int, b int) {
    fmt.Printf("calc1: %d\n", a + b)
    bus.Publish("print", "calc1 calculated : %d\n", a + b)
}

func printer(s string) {
	fmt.Println(s)
}

func main() {
	bus = EventBus.New();
    bus.Subscribe("calc", calculator1);
	bus.Subscribe("print", printer);
	sum := 1
    for sum < 10 {
        fmt.Println(sum)
        bus.Publish("calc", sum, sum);
        time.Sleep(1000 * time.Millisecond)
        sum += 1
    }
    bus.Unsubscribe("calc", calculator1);
    bus.Unsubscribe("print", printer);
}

Output

> go run poc3.go
1
calc1: 2
^Csignal: interrupt

I am on linux 64 bit.

@stackus
Copy link

stackus commented Aug 25, 2017

Happened upon this issue while evaluating the library for some work. Firing off new events from functions called from other events is something we'll need so I tried your example locally to see if I could get it to work.

Two changes are needed to make this work for me (macOS Sierra).

  1. Use fmt.Sprintf on the arguments "calc1 calculated : %d\n", a+b. printer is only expecting a single parameter.
  2. Use SubscribeAsync for the subscription to calc events. Transactional parameter can be either true or false.

@bennAH
Copy link
Contributor

bennAH commented Jan 3, 2018

@hanneslehmann Closing this issue. The problem is you have a subscribed synchronously therefore the callback has to complete first before the process can continue. The issue is the first call back never completes. @stackus 2nd suggestion is correct that you should SubscribeAsync if you want this behaviour.

@bennAH bennAH closed this as completed Jan 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants