Skip to content

Commit

Permalink
feat(Bus): added on change event to constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
DerKekser committed Oct 26, 2024
1 parent 216b62c commit b3307d7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
11 changes: 11 additions & 0 deletions Assets/Kekser/PowerBus/Bus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@ namespace Kekser.PowerBus
public class Bus<T> : IBus<T> where T : class
{
private BusManager _manager;
private Action<T> _onChange;

public Bus(T initialValue = null, BusManager manager = null)
{
_manager = manager ?? BusManager.GlobalInstance;
_manager.RegisterBus(this, initialValue);
}

public Bus(Action<T> onChange, T initialValue = null, BusManager manager = null)
{
_manager = manager ?? BusManager.GlobalInstance;
_manager.RegisterBus(this, initialValue);
_onChange = onChange;
OnChange += _onChange;
}

~Bus()
{
if (_onChange != null)
OnChange -= _onChange;
_manager.UnregisterBus(this);
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/Kekser/PowerBus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "com.kekser.powerbus",
"displayName": "Power Bus",
"author": { "name": "Kekser", "url": "https://github.com/DerKekser" },
"version": "1.0.0",
"version": "1.1.0",
"unity": "2020.3",
"description": "A simple event bus for Unity",
"keywords": [ "bus", "event", "eventbus", "powerbus", "simple", "unity" ],
Expand Down
8 changes: 8 additions & 0 deletions Assets/Kekser/Tests/PowerBusTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public IEnumerator TestBusOnChange()
Assert.AreEqual("World", receivedValue.StringValue);
}

[UnityTest]
public IEnumerator TestBusOnChangeInline()
{
yield return null;
var bus = new Bus<Event1>((v) => Assert.AreEqual("Hello", v.StringValue));
bus.Value = new Event1 { StringValue = "Hello" };
}

[UnityTest]
public IEnumerator TestBusImplicitConversion()
{
Expand Down
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
- [Simple Example](#simple-example)
- [Event Handling](#event-handling)
- [Bus Creation](#bus-creation)
- [Initial Value](#initial-value)
- [Bus Manager](#bus-manager)
- [Inline Event Handling](#inline-event-handling)
- [Initial Value](#initial-value)
- [Bus Manager](#bus-manager)
- [Install](#install)
- [Install via Unity Package](#install-via-unity-package)
- [Install via git URL](#install-via-git-url)
Expand Down Expand Up @@ -52,7 +53,26 @@ var bus = new Bus<StringEvent>();
bus.Value = new StringEvent { Value = "Hello World" };
```

### Initial Value
#### Inline Event Handling

You can handle events inline by passing a lambda function or a method to the constructor.

```csharp
var bus = new Bus<StringEvent>(e => Debug.Log(e.Value));
bus.Value = new StringEvent { Value = "Hello World" };

// or
void HandleEvent(StringEvent e)
{
Debug.Log(e.Value);
}

var bus = new Bus<StringEvent>(HandleEvent);
bus.Value = new StringEvent { Value = "Hello World" };
```

#### Initial Value

You can set the initial value of the bus by passing it to the constructor.

Expand All @@ -62,7 +82,7 @@ You can set the initial value of the bus by passing it to the constructor.
var bus = new Bus<StringEvent>(new StringEvent { Value = "Hello World" });
```

### Bus Manager
#### Bus Manager

You can manage multiple buses using the `BusManager` class.

Expand Down

0 comments on commit b3307d7

Please sign in to comment.