 
     return
 
-  executeSpecs: =>
+  executeSpecs: (results) =>
     promise = new spectacular.Promise
-    @nextExample promise
+    @nextExample promise, results
     promise
 
-  nextExample: (defer) =>
+  nextExample: (defer, results) =>
     spectacular.nextTick =>
       if @stack.length is 0
-        defer.resolve()
+        defer.resolve(results)
       else
         nextExample = @stack.shift()
 
         @env.currentExample = nextExample
         nextExample.run()
         .then =>
-          @handleResult nextExample
-          @nextExample defer
+          @handleResult nextExample, results
+          @nextExample defer, results
         .fail (reason) =>
-          @handleResult nextExample
-          @nextExample defer
-
-  handleResult: (example) ->
+          @handleResult nextExample, results
+          @nextExample defer, results
+
+  handleResult: (example, results) ->
+    results.results.push example.result
+    results.examples.push example
+
+    switch example.result.state
+      when 'pending' then results.pending.push example
+      when 'skipped' then results.skipped.push example
+      when 'errored' then results.errors.push example
+      when 'failure' then results.failures.push example
+
     @dispatch new spectacular.Event 'result', example
     @env.currentExample = null
 
-  notifyEnd: =>
-    @dispatch new spectacular.Event 'end', this
-
-  hasFailures: -> @examples.some (e) ->
-    e.result.state in ['skipped', 'failure', 'errored']
+  notifyEnd: (results) =>
+    @dispatch new spectacular.Event 'end', results
+    results

    @randomSort = => Math.round 1 - @random.get() * 2

  run: () =>
    promise = spectacular.Promise.unit()
    .then =>
      @specsStartedAt = new Date()
    .then(@registerSpecs)
    .then(@executeSpecs)
    .then =>
      @specsEndedAt = new Date()
    .then(@notifyEnd)
    .then =>
      if @hasFailures() then 1 else 0

  findSpecFileInStack: (stack) ->
    for p in @options.paths or []

    @seed = (@seed * 9301 + 49297) % 233280
    @seed / 233280.0

class spectacular.RunnerResults
  constructor: (@runner) ->
    @errors = []
    @failures = []
    @skipped = []
    @pending = []
    @results = []
    @examples = []
    {@loadStartedAt, @loadEndedAt} = @runner
    @specsStartedAt = null
    @specsEndedAt = null

  hasFailures: -> @examples.some (e) ->
    e.result.state in ['skipped', 'failure', 'errored']

class spectacular.Runner

  @include spectacular.EventDispatcher
