Skip to content

Instantly share code, notes, and snippets.

@loganj
Last active December 23, 2015 11:39
Show Gist options
  • Save loganj/6630117 to your computer and use it in GitHub Desktop.
Save loganj/6630117 to your computer and use it in GitHub Desktop.
This is probably a bad idea.
package com.squareup.otto;
/**
* A bus which supports detaching from its parent and attaching new children.
*
* THIS IS A THOUGHT EXPERIMENT. Don't freak out.
*/
public interface DetachableBus extends Bus {
/**
* Detach bus from its parent, making it a {@link Root}.
*
* Idempotent on roots.
*/
Root detach();
/**
* Make this bus the parent of the given bus.
*
* @throws IllegalArgumentException if child already has a parent (is not a root).
*/
void attach(Bus child);
/** @see com.squareup.otto.Bus#spawn() */
@Override DetachableBus spawn();
interface Root extends DetachableBus {
/**
* Turn on delivery of events to subscribers on the entire tree.
*/
void enableDelivery();
/**
* Turn off delivery of events to subscribers on the entire tree. Any attempted posts while
* delivery is disabled will result in a call to the dead event handler.
*/
void disableDelivery();
}
}
@loganj
Copy link
Author

loganj commented Sep 23, 2013

Thought this through a bit more. Has major problems with calls made during handler execution. I'm shelving the whole idea for now, more trouble than it's worth. Here are my notes:

  • what happens if there's an attach within a handler?
    • how are dispatch queues merged? what does the ordering guarantee mean here?
  • what happens if there's a detach within a handler?
    • what about enqueued events?
    • seems like we just proceed as normal, maybe. would that violate ordering guarantee?
  • what happens if the bus is disabled within a handler?
    • do we clear the queue, or just pause delivery?
  • can we just forbid detach/attach/enable/disable while handling an event?
    • would fit our only actual use case: onResume()/onPause()
    • does that mean we'd need to expose isDispatching() or something?
  • detach, reattach is going to thrash on queues.
    • could create queue lazily, only if needed.
      • good idea anyway?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment