github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/hook/source.go (about)

     1  // Copyright 2012-2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package hook
     5  
     6  // Source defines a generator of Info events.
     7  //
     8  // A Source's client must be careful to:
     9  //  * use it from a single goroutine.
    10  //  * collect asynchronous events from Changes(), and synchronously Apply()
    11  //    them whenever possible.
    12  //  * only use fresh values returned from Empty() and Next(); i.e. only those
    13  //    which have been generated since the last call to Pop() or Update().
    14  //  * Stop() it when finished.
    15  type Source interface {
    16  
    17  	// Changes returns a channel sending events which must be processed
    18  	// synchronously, and in order.
    19  	Changes() <-chan SourceChange
    20  
    21  	// Stop causes the Source to clean up its resources and stop sending
    22  	// changes.
    23  	Stop() error
    24  
    25  	// Empty returns false if any hooks are scheduled.
    26  	Empty() bool
    27  
    28  	// Next returns the first scheduled hook. It will panic if no hooks are
    29  	// scheduled.
    30  	Next() Info
    31  
    32  	// Pop removes the first scheduled hook, and invalidates the results of
    33  	// previous calls to Next() and Empty(). It will panic if no hooks are
    34  	// scheduled.
    35  	Pop()
    36  }
    37  
    38  // SourceChange is the type of functions returned via Source.Changes().
    39  type SourceChange func() error
    40  
    41  // Apply applies the change to its Source's schedule, and invalidates the
    42  // results of previous calls to Next() and Empty().
    43  func (s SourceChange) Apply() error {
    44  	return s()
    45  }
    46  
    47  // NoUpdates implements a subset of HookSource that delivers no changes, errors
    48  // on update, and ignores stop requests (because there's nothing running in the
    49  // first place). It's useful for implementing static HookSources.
    50  type NoUpdates struct{}
    51  
    52  func (_ *NoUpdates) Stop() error                  { return nil }
    53  func (_ *NoUpdates) Changes() <-chan SourceChange { return nil }