EventEmitter = (require 'events').EventEmitter

# This class allowes commiting and aborting. It doesn't not fully implement
# all the features of exodus transaction.
class Transaction extends EventEmitter
  # @private
  env: undefined
  # @private
  exclusive: undefined
  # @private
  stores: undefined
  # @private
  removedStores: undefined

  # Constructor.
  #
  # @param env
  # @param exclusive absolutely unused parameter
  @create$Environment$Boolean: (env, exclusive, o) ->
    if !o? then o = new Transaction()
    o.env = env
    o.exclusive = exclusive
    o.stores = new Array()
    o.removedStores = new Array()
    return o

  getUpToDateStore$String: (name) ->
    for i in [0..@stores.length - 1]
      if @stores[i].getName() == name
        return @stores[i]
    return undefined

  getCurrentList$String: (name) ->
    for i in [0..@stores.length - 1]
      if @stores[i].getName() == name
        return @stores[i].getList()
    return undefined

  addStore$Store: (store) ->
    @stores.push store

  getStores: () ->
    return @stores

  removeStore$String: (name) ->
    @removedStores.push name

  abort$emit: () ->
    @env.once 'abortTransaction', () =>
      @emit 'abort'
    @env.abortTransaction$Transaction$emit this

  commit$emit: () ->
    @env.once 'commitTransaction', () =>
      @emit 'commit'
    @env.commitTransaction$Transaction$emit this

exports.Transaction = Transaction
