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

The way to stop observers periodically #5

Open
Antonboom opened this issue Nov 19, 2020 · 0 comments
Open

The way to stop observers periodically #5

Antonboom opened this issue Nov 19, 2020 · 0 comments

Comments

@Antonboom
Copy link

Hello.
Thanks for the interesting library!

We were faced with the need for a mechanism for stopping observers without stopping the property:

package main

import (
	"fmt"
	"math/rand"
	"time"

	"github.com/imkira/go-observer"
)

type stop struct{}

func main() {
	obs := observer.NewProperty(0)

	go func() {
		t := time.Tick(time.Second * 3)

		for {
			select {
			case <-t:
				obs.Update(stop{}) // obs.Interrupt()
			default:
				obs.Update(rand.Int())
			}
			time.Sleep(time.Second)
		}
	}()

	stream := obs.Observe()
	for {
		<-stream.Changes()
		switch i := stream.Next().(type) {
		case stop:
			fmt.Println("exit")
		case int:
			fmt.Println(i)
		}
		/*
			select {
			case <-stream.Changes():
			case <-stream.Interrupts():
			}
		*/
	}
}

In the comments I suggested a new possible observer API.
What do you think about that?

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

1 participant