Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/quasilyte/gsignal
A lightweight Godot/Qt inspired signals and slots for Go
https://github.com/quasilyte/gsignal
ebiten ebitengine event event-listener gamedev ge go godot golang qt signal-slot signals
Last synced: 4 months ago
JSON representation
A lightweight Godot/Qt inspired signals and slots for Go
- Host: GitHub
- URL: https://github.com/quasilyte/gsignal
- Owner: quasilyte
- License: mit
- Created: 2023-01-30T10:39:07.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-08-02T12:06:07.000Z (6 months ago)
- Last Synced: 2024-09-29T08:24:29.732Z (4 months ago)
- Topics: ebiten, ebitengine, event, event-listener, gamedev, ge, go, godot, golang, qt, signal-slot, signals
- Language: Go
- Homepage:
- Size: 15.6 KB
- Stars: 8
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Godot-like signals library
![Build Status](https://github.com/quasilyte/gsignal/workflows/Go/badge.svg)
[![PkgGoDev](https://pkg.go.dev/badge/mod/github.com/quasilyte/gsignal)](https://pkg.go.dev/mod/github.com/quasilyte/gsignal)### Overview
A [Godot](https://docs.godotengine.org/en/stable/getting_started/step_by_step/signals.html)-inspired signals library for Go.
**Key features:**
* Amortized zero allocations in most use cases
* Efficient `Connect`, `Emit`, `Disconnect`
* Generic-based API gives us type safety and convenienceSome games that were built with this library (this list is incomplete):
* [Roboden](https://quasilyte.itch.io/roboden)
* [Cavebots](https://quasilyte.itch.io/cavebots)
* [Assemblox](https://quasilyte.itch.io/assemblox)
* [Decipherism](https://quasilyte.itch.io/decipherism)
* [Retrowave City](https://quasilyte.itch.io/retrowave-city)
* [Autotanks](https://quasilyte.itch.io/autotanks)
* [Sinecord](https://quasilyte.itch.io/sinecord)
* [Learn Georgian](https://quasilyte.itch.io/georgian-trainer)Why bother and use something like this?
* It reduces the objects coupling
* It's an elegant event listener solution for Go
* Signals are a familiar concept (Godot, Phaser, Qt, ...)### Installation
```bash
go get github.com/quasilyte/gsignal
```### Quick Start
```go
package mainimport (
"fmt""github.com/quasilyte/gsignal"
)type button struct {
Name string
EventClicked gsignal.Event[*button]
}func (b *button) Click() { b.EventClicked.Emit(b) }
type listener struct {
disposed bool
}func (l *listener) IsDisposed() bool { return l.disposed }
func (l *listener) onClick(b *button) {
fmt.Println("listener on click")
}func main() {
b := &button{Name: "example"}
b.Click() // nothing happens, 0 connectionsi := 1
b.EventClicked.Connect(nil, func(b *button) {
fmt.Printf("%s clicked (%d)\n", b.Name, i)
i++
})
b.Click() // prints "example clicked (1)" once
b.Click() // prints "example clicked (2)" once; againl := &listener{}
b.EventClicked.Connect(l, l.onClick)
b.Click() // prints "example clicked (3)", then "listener on click"l.disposed = true // this will cause a disconnect
b.Click() // prints "example clicked (4)" once
}
```### Introduction
This concept is borrowed from [Godot signals](https://docs.godotengine.org/en/stable/getting_started/step_by_step/signals.html), but it also resembles [Signals and Slots from Qt](https://doc.qt.io/qt-6/signalsandslots.html).
In `gsignal` terms:
* `Signal` = a field inside a struct
* `Slot` = a function (or method value) bound to a signal
* `Disconnect` = remove bound function
* `Emit` = call all bound functionsThis library disconnects **disposed** objects automatically. This is convenient when you connect a scene object to some `Event`. When object goes away from a scene (becomes disposed), there is no need to call its event handler anymore.
### Thread Safety Notice
This library never does any synchronization on its own. It's implied that event emitters and their subscribers are executed inside the same goroutine.
This is possible in the game context, but it may not be as easy to enforce in some other applications.
Let's imagine that you want to do a background task in a game and provide a signal-style event for its completion. You spawn a goroutine for the task, but you keep the code outside look like it's still single-threaded. All concurrent communication should be incapsulated in the object owning the `Event` object. When this object knows that this concurrent job is completed, it should emit the event and notify all the subscribers.