0

When I simply use 'connect', there is no way I can specify the order in the callbacks will be called. Having connect_before and connect_after to connect before/after the default doesn't serve my purpose. I want something like connect_first and connect_last or anything else that will help me specify the order in which callbacks connect to a signal.

Something like this?

    something.connect_first('my-signal', callback1)
    somethingelse.connect_last('my-signal', callback2)
Suhas
  • 131
  • 1
  • 12
  • Asynchronous tasks are asynchronous. If you want to order them then wait to fire off some until after others have completed or failed. – Dave S Jun 25 '17 at 00:54
  • 1
    How would you handle the case where you call `connect_last`, and then later in another piece of code you call it again? Only one handler can be last. One way to solve your problem is to make only one connection to an event, and in that one handler you do several things in a specific order. – Paul Cornelius Jun 25 '17 at 00:58
  • 1
    GObject signals are not asynchronous: they are fully synchronous. The control flow will block until `g_signal_emit()` returns. – ebassi Jun 25 '17 at 16:33

1 Answers1

2

There is only one explicit ordering guarantee for GObject signals:

  • the class closure added when creating a new signal with G_SIGNAL_RUN_FIRST will be called before all callbacks added using g_signal_connect()
  • the class closure added when creating a new signal eith G_SIGNAL_RUN_LAST will be called after all callbacks added using g_signal_connect() and before all callbacks added using g_signal_connect_after()

This means that you can only control whether a callback is invoked before or after all other callbacks connected manually when you're creating a new signal - for obvious reasons, since you may want to provide an initial state at the start of an emission chain, or you want to ensure a stable state at the end of an emission chain.

As for the order of the callbacks added using g_signal_connect(), there is no explicit guarantee of any ordering. There's an implicit order, the order of connection, though, that won't likely ever be changed. Remember that signal handlers can install new handlers, or disconnect them, or block the signal emission, so relying on a specific order is usually an indication of a design problem in the code.

ebassi
  • 8,648
  • 27
  • 29