Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Godot-signal-as-observable mapper #199

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

lfod1997
Copy link

@lfod1997 lfod1997 commented May 4, 2024

Summary

Extension methods and utility classes for easy conversion from a Godot signal to an observable.

Motivation

Although we can use FromEvent to create observable from any event:

var observable = Observable.FromEvent<WhoType>(
    h => myCheese.Touched += h,
    h => myCheese.Touched -= h
);

It:

  1. usually involves two capturing lambdas as there's no "FromEventWithState<T, TState>"; otherwise 2 methods must be introduced just for this
  2. requires a Func<Action<T>, TDelegate> conversion (= more boilerplate!) if the signal has more than one event arg
  3. is verbose
  4. does multiple connecting if multiple subscribers
  5. will not call OnComplete unless canceled by a CancellationToken

Prefer something easier:

var observable = myCheese.SignalAsObservable<WhoType>(Cheese.SignalName.Touched, completeOnExitTree: true);
  1. no capturing
  2. no converters when multi-arg
  3. DRY syntax
  4. connects once, to a backing Subject's OnNext
  5. gives the option (true by default) to call OnComplete automatically, if the GodotObject is a Node and has exited tree
  6. refers to Godot's SignalName explicitly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant