Services.Subscriber

An example demonstrating how to subscribe to signal changes for a particular named service.

  static void subscribing () {

    final StringBuilder out =
      new StringBuilder ();

    final Context context =
      Services.context ();

    final Services.Name n1 =
      Services.name (
        "service.one"
      );

    final Services.Name n2 =
      Services.name (
        "service.two"
      );

    final Subscription subscription =
      context
        .subscribe (
          ( name, registrar ) -> {
            if ( name == n1 )
              registrar.accept (
                ( orientation, signal ) ->
                  out.append (
                    format (
                      "%s %s %s%n",
                      name,
                      orientation,
                      signal
                    )
                  )
              );
          },
          Signal.class
        );

    context
      .service ( n1 )
      .start ()
      .succeed ()
      .stop ();

    assert
      format (
        "%s %s %s%n" +
          "%s %s %s%n" +
          "%s %s %s%n",
        n1, EMIT, START,
        n1, EMIT, SUCCEED,
        n1, EMIT, STOP
      ).equals (
        out.toString ()
      );

    out.setLength (
      CLEAR
    );

    context
      .service ( n2 )
      .start ()
      .succeed ()
      .stop ();

    assert
      out.length ()
        == CLEAR;

    subscription
      .cancel ();

    context
      .service ( n1 )
      .start ()
      .succeed ()
      .stop ();

    assert
      out.length ()
        == CLEAR;

  }