--[[----------------------------------------------------------------------------

WFRxWeakEventTable.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

------------------------------------------------------------------------------]]

	-- RxLua
local Rx = require 'Rx'
local ReplaySubject = Rx.ReplaySubject
local Subject = Rx.Subject

	-- WF.core.rxlua
local WFRxWeakObservableTable = require 'WFRxWeakObservableTable'

	-- WF.core.utils
local WFArguments = require 'WFArguments'


--------------------------------------------------------------------------------

local check = assert( WFArguments.check )


--============================================================================--

-- @mark WFRxWeakEventTable class

local WFRxWeakEventTable = {

	WFRxWeakObservableTable,
		-- superclass

}

--------------------------------------------------------------------------------

function WFRxWeakEventTable:__init( replayBufferSize )

	check( 'WFRxWeakEventTable', '?number' )

	return function()

		if replayBufferSize then
			return ReplaySubject( replayBufferSize )
		else
			return Subject()
		end

	end
		-- Note that we do not pass replayBufferSize down to WFRxWeakSubjectTable
		-- because it doesn't know how to mix that with its handling of Subject.

end

--------------------------------------------------------------------------------

function WFRxWeakEventTable:postToKey( key, ... )

	check( 'WFRxWeakEventTable' )

	if key == nil or key ~= key then
		error( "WFRxWeakEventTable:postToKey: key must be valid table index", 2 )
	end

	local source = self._sources[ key ]

	if source then
		source:onNext( ... )
	end

end

--------------------------------------------------------------------------------

function WFRxWeakEventTable:postErrorToKey( key, ... )

	check( 'WFRxWeakEventTable' )

	if key == nil or key ~= key then
		error( "WFRxWeakEventTable:postErrorToKey: key must be valid table index", 2 )
	end

	local source = self._sources[ key ]

	if source then
		source:onError( ... )
	end

end

--------------------------------------------------------------------------------

function WFRxWeakEventTable:postCompletedToKey( key )

	check( 'WFRxWeakEventTable' )

	if key == nil or key ~= key then
		error( "WFRxWeakEventTable:postCompletedToKey: key must be valid table index", 2 )
	end

	local source = self._sources[ key ]

	if source then
		source:onCompleted()
	end

end

--------------------------------------------------------------------------------

return Rx.Class( 'WFRxWeakEventTable', WFRxWeakEventTable )
