meta

package module
v0.0.0-...-0dcccbe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2015 License: MIT Imports: 0 Imported by: 0

README

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.

Documentation

Overview

Package meta provides signals and slots for the Observer pattern.

Note: This is a proof of concept package, therefore you are not supposed to use it in production or any near. Public API of this package is likely to change (a lot).

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.

Once a signal gets emitted, all the slots connected are being executed in the distinct goroutines. Value passed to the signal is available from the call context object.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Disconnect

func Disconnect(sig *Signal, cid Connection)

Disconnect does the opposite to connect.

Types

type Call

type Call struct {
	Data interface{}
}

Call stands for the call context, handled in the slot.

type Connection

type Connection int

Connection is a signal-slot connection descriptor, unique within the lifetime of the signal.

You may use it to terminate existing connections.

func Connect

func Connect(sig *Signal, slot Slot) Connection

Connect attaches a new slot to the signal. It also does panic if any of the params given is equal to nil.

type Signal

type Signal struct {
	// contains filtered or unexported fields
}

Signal is an adorable piece of a structure!

func (*Signal) Emit

func (sig *Signal) Emit(data interface{})

Emit executes all the connected slots with data given.

type Slot

type Slot func(*Call)

Slot is a reciver function, usually a wrapper.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL