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

NmbTrackedDataBroadcasterImp.lua

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

ADOBE CONFIDENTIAL
------------------
Copyright 2016 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

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

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

local check = assert( WFArguments.check )


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

-- @mark NmbTrackedDataBroadcasterImp class

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

local NmbTrackedDataBroadcasterImp = {
	
	_broadcastStream = false,
	
	WFCloseSupport.addCloseSupportMethods,

}

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

function NmbTrackedDataBroadcasterImp:__init()

	check( 'NmbTrackedDataBroadcasterImp' )

	self._broadcastStream = ReplaySubject( 1 )
	
end

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

function NmbTrackedDataBroadcasterImp:close()

	check( 'NmbTrackedDataBroadcasterImp' )

	local broadcastStream = self._broadcastStream
	self._broadcastStream = nil
	
	if broadcastStream then
		broadcastStream:dispose()
	end
	
end

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

function NmbTrackedDataBroadcasterImp:broadcastMessage( data )

	check( 'NmbTrackedDataBroadcasterImp', 'table' )
	
	local metadata = {
		isTrackedData = true
	}
	
	self._broadcastStream:onNext( data, metadata ) 

end

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

function NmbTrackedDataBroadcasterImp:broadcastStream()

	check( 'NmbTrackedDataBroadcasterImp' )

	return self._broadcastStream

end

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

return Rx.Class 'NmbTrackedDataBroadcasterImp'( NmbTrackedDataBroadcasterImp )
