https://github.com/mkmarek/unity-sagas
Port of https://github.com/redux-saga/redux-saga to C# for Unity3d coroutines
https://github.com/mkmarek/unity-sagas
effects sagas unity3d
Last synced: about 1 year ago
JSON representation
Port of https://github.com/redux-saga/redux-saga to C# for Unity3d coroutines
- Host: GitHub
- URL: https://github.com/mkmarek/unity-sagas
- Owner: mkmarek
- License: mit
- Archived: true
- Created: 2017-05-27T22:23:28.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-29T20:52:08.000Z (about 9 years ago)
- Last Synced: 2024-08-03T05:19:31.523Z (almost 2 years ago)
- Topics: effects, sagas, unity3d
- Language: C#
- Size: 60.5 KB
- Stars: 12
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# unity-sagas
Port of https://github.com/redux-saga/redux-saga to C# for Unity3d coroutines. This project is currently in POC phase. Any contributions or ideas are welcomed!
## Examples
### Put
Allows to invoke SagaAction that can be received by multiple other sagas
```csharp
private IEnumerator RootSaga()
{
yield return Do.Put(new SagaAction("ACTION_NAME", 42));
}
void Start ()
{
var saga = new Saga();
saga.OnActionEvent += Saga_OnActionEvent;
this.StartCoroutine(saga.Run(this.RootSaga()));
}
private void Saga_OnActionEvent(SagaAction data)
{
Debug.Log("The answer is: " + data.GetPayload());
}
```
### Take
Allows to wait for SagaAction to be invoked and then continues execution
```csharp
private IEnumerator RootSaga()
{
yield return Do.Take("START", action);
}
void Start ()
{
var saga = new Saga();
this.StartCoroutine(saga.Run(this.RootSaga()));
//nothing happens until following line executes
saga.OnAction(new SagaAction("START", 11));
}
```
### Call
Invokes method with a set of parameters
```csharp
private int TestCallMethod(params object[] args)
{
return 1;
}
private IEnumerator RootSaga()
{
var returnData = new Ref();
yield return Do.Call(TestCallMethod, returnData);
}
void Start ()
{
var saga = new Saga();
this.StartCoroutine(saga.Run(this.RootSaga()));
}
```
### ThreadCall
Invokes method with a set of parameters in a separate thread and waits for the result in a non-blocking manner
```csharp
private int TestCallMethod(params object[] args)
{
Thread.Sleep(4000);
return 1;
}
private IEnumerator RootSaga()
{
var returnData = new Ref();
yield return Do.ThreadCall(TestCallMethod, returnData);
}
void Start ()
{
var saga = new Saga();
this.StartCoroutine(saga.Run(this.RootSaga()));
}
```
### Try
Yields in try/catch can be tricky. This methods tries to solve that.
```csharp
private int TestCallMethod(params object[] args)
{
throw new NotImplementedException();
}
private IEnumerator DoStuff()
{
var data = new Ref();
yield return Do.Call(TestCallMethod, data);
yield return Do.Put(new SagaAction("SUCCESS", data.Value));
}
private IEnumerator CatchRoutine(Exception exception)
{
yield return Do.Put(new SagaAction("ERROR", exception));
}
private IEnumerator FinallyRoutine()
{
yield return Do.Put(new SagaAction("FINALLY", 1));
}
private IEnumerator RootSaga()
{
yield return Do.Try(DoStuff(), CatchRoutine, FinallyRoutine());
}
void Start ()
{
var saga = new Saga();
saga.OnActionEvent += Saga_OnActionEvent;
this.StartCoroutine(saga.Run(this.RootSaga()));
}
private void Saga_OnActionEvent(SagaAction data)
{
Debug.Log("INVOKED_ACTION: " + data.Type);
// prints out:
// "INVOKED_ACTION: ERROR"
// "INVOKED_ACTION: FINALLY"
}
```
### Fork
Forks saga into a separate "subprocess" allowing to run multiple sagas simultaneously.
```csharp
private IEnumerator SagaWithTake()
{
var action = new Ref();
yield return Do.Take("TEST", action);
yield return Do.Put(new SagaAction("RESULT", action.Value.GetPayload()));
}
private IEnumerator SagaWithPut()
{
yield return Do.Put(new SagaAction("TEST", 123));
}
private IEnumerator RootSaga()
{
yield return Do.Fork(SagaWithTake());
yield return Do.Fork(SagaWithPut());
}
void Start ()
{
var saga = new Saga();
this.StartCoroutine(saga.Run(this.RootSaga()));
}
```