Git Product home page Git Product logo

signalhandler's Introduction

Signal Handler

Installation

Use Command Line

upm add package dev.monry.signalhandler

Note: upm command is provided by this repository.

Edit Packages/manifest.json

{
  "dependencies": {
    // (snip)
    "dev.monry.signalhandler": "[latest version]",
    // (snip)
  },
  "scopedRegistries": [
    {
      "name": "Unofficial Unity Package Manager Registry",
      "url": "https://upm-packages.dev",
      "scopes": [
        "com.unity.simpleanimation",
        "com.stevevermeulen",
        "jp.cysharp",
        "dev.monry",
        "dev.upm-packages"
      ]
    }
  ]
}

Usages

Create Signal types

Type based signal

using SignalHandler;

public class FooSignal : SignalBase<FooSignal>
{
}

Type based signal with parameters

using SignalHandler;

public struct SomeParameter
{
    private bool boolParameter;
    private int intParameter;

    public SomeParameter(bool boolParameter, int intParameter)
    {
        this.boolParameter = boolParameter;
        this.intParameter = intParameter;
    }
}

public class BarSignal : SignalBase<BarSignal, SomeParameter>
{
}

ScriptableObject signals

Use ScriptableObjectSignalBase<TSignal> or ScriptableObjectSignalBase<TSignal, TParameter>.

Declare signals in Zenject.Installer

using Zenject;
using SignalHandler.Installer; // provides extension methods

public class SomeInstaller : MonoInstaller<SomeInstaller>
{
    public override void InstallBindings()
    {
        SignalHandlerInstaller<FooSignal>.Install(Container);
        SignalHandlerInstaller<BarSignal>.Install(Container);
    }
}

Publish signals

using SignalHandler;
using Zenject;
using UnityEngine;

public class MyPublisherMonoBehaviour : MonoBehaviour
{
    [Inject] private ISignalPublisher<FooSignal> FooPublisher { get; }
    [Inject] private ISignalPublisher<BarSignal> BarPublisher { get; }

    public IEnumerator Start()
    {
        yield return new WaitForSeconds(1.0f);
        FooPublisher.Publish(FooSignal.Create());
        yield return new WaitForSeconds(1.0f);
        BarPublisher.Publish(BarSignal.Create(new SomeParameter(true, 1)));
    }
}

Receive signals

using SignalHandler;
using Zenject;
using UnityEngine;
using UniRx;

public class MyReceiverMonoBehaviour : MonoBehaviour
{
    [Inject] private ISignalReceiver<FooSignal> FooReceiver { get; }
    [Inject] private ISignalReceiver<BarSignal> BarReceiver { get; }

    public void Start()
    {
        // Log after 1 second after `MyPublisherMonoBehaviour.Start()` invoked.
        FooReceiver.Receive().Subscribe(_ => Debug.Log("Receive FooSignal"));
        // Does not log because type does not match
        FooReceiver.Receive(ExtendsFooSignal.Create()).Subscribe(_ => Debug.Log("Will not invoke"));

        // Log after 2 second after `MyPublisherMonoBehaviour.Start()` invoked.
        BarReceiver.Receive().Subscribe(_ => Debug.Log("Receive BarSignal (normal)"));
        // Does not log because type does not match
        BarReceiver.Receive(ExtendsBarSignal.Create(default)).Subscribe(_ => Debug.Log("Will not invoke"));
        // Log after 2 second after `MyPublisherMonoBehaviour.Start()` invoked because signal does match.
        BarReceiver.Receive(BarSignal.Create(new SomeParameter(true, 1))).Subscribe(_ => Debug.Log("Receive BarSignal (signal matches)"));
        // Log after 2 second after `MyPublisherMonoBehaviour.Start()` invoked because parameter does match.
        BarReceiver.Receive(new SomeParameter(true, 1)).Subscribe(_ => Debug.Log("Receive BarSignal (parameter matches)"));
        // Does not log because parameter does not match
        BarReceiver.Receive(new SomeParameter(false, 0)).Subscribe(_ => Debug.Log("Will not invoke"));
    }

    private class ExtendsFooSignal : FooSignal
    {
    }

    private class ExtendsBarSignal : BarSignal
    {
    }
}

signalhandler's People

Contributors

monry avatar

Watchers

 avatar  avatar

signalhandler's Issues

Implement an optional thing that publishes a Signal only once

What

今は Signal を何度でも送信できる仕様だが、これを「1回のみ」に制限したモノを定義したい。

Why

「完了通知」など1回しか送信し得ない Signal を Receive().Timeout() などで待つ場合に .Take(1) なり .FirstOrDefault() なりを挟む必要が出てくる。
これは、本来受信側が意識するコトではなく、送信側か宣言側で意識すべきコトだと思う。

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.