GithubHelp home page GithubHelp logo

meta's Introduction

This is a proof of concept implementation of signals and slots in Go.

GoDoc

Signals and slots is a language construct introduced in Qt for communication between objects, which makes it easy to implement the Observer pattern while avoiding boilerplate code. The concept is that GUI widgets can send signals containing event information which can be received by other controls using special functions known as slots. Similarly, the signal/slot system can be used for other non-GUI usages, for example asynchronous I/O (including sockets, pipes, serial devices, etc.) event notification or to associate timeout events with appropriate object instances and methods or functions.

A commonly used metaphor is a spreadsheet. A spreadsheet has cells that observe the source cell(s). When the source cell is changed, the dependent cells are updated from the event. Channels can't do that: you can read a value from the channel at some particular place only. As you can see, it's not flexible enough, especially for the systems with lots of observers โ€” e.g. user interfaces.

Since Go does not support generics and we don't have any sort of a Meta Object Compiler from Qt, there is absolutely no way to omit the boilerplate code:

package main

import (
	"fmt"
	"github.com/tucnak/meta"
	"sync"
)

type Foo struct {
	// fields...

	Done meta.Signal
}

type Guy struct {
	Name string
}

func (mr Guy) Print(n int) {
	fmt.Printf("%s says: %d\n", mr.Name, n)
	waiter.Done()
}

var waiter sync.WaitGroup

func main() {
	var foo Foo

	johny := Guy{"Johny"}
	david := Guy{"David"}

	meta.Connect(&foo.Done, func(call *meta.Call) {
		if passed, ok := call.Data.(int); ok {
			johny.Print(passed)
		}
	})

	meta.Connect(&foo.Done, func(call *meta.Call) {
		if passed, ok := call.Data.(int); ok {
			david.Print(passed)
		}
	})

	waiter.Add(2)

	// Emit notifies all the connected slots, by running
	// them in the distinct goroutines.
	foo.Done.Emit(42)

	waiter.Wait()

	//
	// Johny says: 42
	// David says: 42
}

Here it is, feel free to contribute.

meta's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

meta's Issues

Slots in distinct goroutine

Hi!
Just a note...
You said

Once a signal gets emitted, all the slots connected are being executed in the distinct goroutines.

And this is the Emit function

func (sig *Signal) Emit(data interface{}) {
    for i := 0; i < len(sig.cons); i++ {
        sig.cons[i].slot(&Call{Data: data})
    }
}

But I think that the function could be rewritten in this way

func (sig *Signal) Emit(data interface{}) {
    for i := 0; i < len(sig.cons); i++ {
        go func(c conn) {
            c.slot(&Call{Data: data})
        }(sig.cons[i])
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.