Skip to content

Instantly share code, notes, and snippets.

@ThomasBurleson
Created May 25, 2011 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasBurleson/990951 to your computer and use it in GitHub Desktop.
Save ThomasBurleson/990951 to your computer and use it in GitHub Desktop.
Swiz EventChains with conditionals
[EventHandler(event="DeviceEvent.LOAD_SUMMARY",properties="device,filters,forceLoad")]
/**
* Here is an example of Parallel chaining. Developers should not that the workflow() method returns
* IAsynchronousOperation to allow workflow() to be itself chained with other eventHandlers.
*
* - DeviceEvent extends AsynchronousEvent.
* - FunctionChainStep is also available
*
* Here we get Device Snapshot information (includes upTimes and durationInState)
*/
public function workflow_loadDeviceSummaries(device:DeviceVO=null, filterIDs:Array=null, force:Boolean=false):IAsynchronousOperation
{
logger.debug( "workflow_loadDeviceSummaries( )" );
device ||= model.selected;
if (device == null) return null;
var evTests : Event = new DeviceEvent(DeviceEvent.LOAD_TESTS, device, filterIDs);
var evSnaps : Event = new DeviceEvent(DeviceEvent.LOAD_SNAPSHOTS, device );
var chain : IChain = new EventChain(dispatcher, ChainType.PARALLEL);
if ( force || !device.hasTests ) chain.addStep( new EventChainStep(evTests) );
if ( force || !device.hasSeverities || !device.hasSnapshots ) chain.addStep( new EventChainStep(evSnaps) );
chain.start();
return new AsynchronousChainOperation(chain);
}
/**
* Here is the same method; but it now uses the static
* EventChain::createEventChain() to do the dirty work...
*
*/
[EventHandler(event="DeviceEvent.LOAD_SUMMARY",properties="device,filters,forceLoad")]
public function workflow_loadDeviceSummaries(device:DeviceVO=null, filterIDs:Array=null, force:Boolean=false):IAsynchronousOperation
{
logger.debug( "workflow_loadDeviceSummaries( )" );
device ||= model.selected;
if (device == null) return null;
var evTests : Event = new DeviceEvent(DeviceEvent.LOAD_TESTS, device, filterIDs);
var evSnaps : Event = new DeviceEvent(DeviceEvent.LOAD_SNAPSHOTS, device );
var events : Array = [ ];
if ( force || !device.hasTests ) events.push( evTests );
if ( force || !device.hasSeverities || !device.hasSnapshots ) events.push( evSnaps );
// Use the static EventChain::createEventChain() to do the dirty work...
var chain : IChain = EventChain.createEventChain(events, dispatcher, ChainType.PARALLEL);
return new AsynchronousChainOperation( chain.start() );
}
@ThomasBurleson
Copy link
Author

The 2nd version is possible with the latest changes to EventChain; see my Swiz fork @ http://bit.ly/jzf4gh

@orubel
Copy link

orubel commented Jul 30, 2014

How is this api chaining when the client cannot chain the api calls? The 'api chaining spec' allows for client to chain api calls within one request and on response by chaining the output from one api call to another.

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