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

NmbTrackedDataBroadcaster.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.

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

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

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

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

local check = assert( WFArguments.check )

local trackedDataBroadcasterImp_lazy

local runUntilDone = WFRxRunUntilDone

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

local function trackedDataBroadcasterImp()

	if not trackedDataBroadcasterImp_lazy then
		trackedDataBroadcasterImp_lazy = ( require 'WFWorkerThread' )( 'NmbTrackedDataBroadcasterMessageProcessor.lua' )
	end

	return trackedDataBroadcasterImp_lazy

end

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

-- @mark NmbTrackedDataBroadcaster class

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

local NmbTrackedDataBroadcaster = {}

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

--[[!
	\brief Record metrics to send to the UI process to be tracked.

	\param id: (string) The unique indentifier for the metric. Used on the client
	side to match with the appropriate Ag observer.

	\param data: a table containing the metrics to send.

	\return void
]]

function NmbTrackedDataBroadcaster.broadcastMessage( id, data )

	check( 'string', 'table' )

	data.metricId = id

	trackedDataBroadcasterImp():request( 'broadcastMessage', data )
		:call( runUntilDone )
		
end

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

--[[!
	\brief Retrieve the tracked data message stream.

	\return Observable< messsage > Fires each time a new message is created.
	
	Note: This API should only be used within NmbDaemonSideRtmfpEndpoint.lua. 
	
	The idea is that a number of threads will call NmbTrackedDataBroadcaster.broadcastMessage(),
	but only one thread ( the main thread via NmbDaemonSideRtmfpEndpoint ) will
	process those messages by listening on the broadcastStream.
]]

function NmbTrackedDataBroadcaster.broadcastStream()

	return trackedDataBroadcasterImp():request( 'broadcastStream' )

end

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

return NmbTrackedDataBroadcaster
