PackageDescription: BeforeYouLeave


Before You Leave

Last published: December 15, 2006 by 'michael'

Defines 2 Classes
Extends 5 Classes


This package provides an alternative ensure-like mechanism that inserts a stack frame in to the executing stack so that on return from your current method, a block will run, eg:

myMethod
self doSomething.
thisContext onExitDo: [self doSomethingWhenMyMethodLeaves].
self doSomethingElse.
^self returnSomeValue

The return value of #myMethod is preserved, exceptions are caught by the same onExitDo: block too. You can even resume the exceptions.

This is equivalent to:

^[self myMethod] ensure: [self doSomethingWhenMyMethodLeaves]

except that instead of the caller deciding something needs to be done on exit, the method can decide to inject something on exit without messying up its own return clauses.

For the curious - there is no protocol such as '[] doOnExit' because the call #doOnExit within a debugger will add simulated calls in to the execution stack such as sendFast:*, etc. Therefore, the onExitDo: block would be registered in the wrong place.

You can also catch the end of a process as well by calling:

aProcess onExitDo: [ ... ]

Note that this can have some curious timing issues when you're just starting a block the context may not have progressed enough yet, if the process hasn't started at all it won't work and if the process has finished it'll also fail. See the #testProcessLeaving to see how I protect the test with a bunch of semaphores. (Thanks to Travis Griggs for the process ending idea)