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

WFRxObservablePromiseResolver.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'

	-- WF.core.reactive
local KSPromises = require 'KSPromises'

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


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

local WFRxObservablePromiseResolver = {

	_resolvedPromise = false,

	_unsubscribeHandle = false,

	KSPromises.addPromiseResolverMethods,
	WFCloseSupport.addCloseSupportMethods,

}

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

local function dispose( self )

	if self._unsubscribeHandle then
		self._unsubscribeHandle:dispose()
		self._unsubscribeHandle = nil
	end

end

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

function WFRxObservablePromiseResolver:__init( observable )

	local initing = true

	WFCloseSupport.mustCloseAtThreadTermination( self )

	self._unsubscribeHandle = observable

		:subscribe(
			function( ... )
				if initing then
					if not self._resolvedPromise then
						self._resolvedPromise = KSPromises.resolvedPromiseP( 'data', ... )
					end
				else
					if self:canResolvePromise() then
						self:resolvePromise( 'data', ... )
					end
				end
				dispose( self )
			end,

			function( ... )
				if initing then
					if not self._resolvedPromise then
						self._resolvedPromise = KSPromises.resolvedPromiseP( 'error', ... )
					end
				else
					if self:canResolvePromise() then
						self:resolvePromise( 'error', ... )
					end
				end
				dispose( self )
			end,

			function()
				if initing then
					if not self._resolvedPromise then
						self._resolvedPromise = KSPromises.resolvedPromiseP( 'data' )
					end
				else
					if self:canResolvePromise() then
						self:resolvePromise( 'data', true )
							-- [scouten 2015-08-02]: Recent change to Observable message
							-- handling means that many APIs respond with Observable.empty().
							-- Promise-flavored APIs often signaled 'data', true; so we'll
							-- reinstate that convention here for backward compatibility.
					end
				end
				dispose( self )
			end )

	initing = false

end

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

function WFRxObservablePromiseResolver:promise()

	if self._resolvedPromise then
		return self._resolvedPromise
	else
		return KSPromises.promiseForResolverP( self, 'retained | closeTarget' )
	end

end

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

function WFRxObservablePromiseResolver:close()

	dispose( self )

end

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

function WFRxObservablePromiseResolver:stopResolution()

	self:close()

end

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

return Rx.Class 'WFRxObservablePromiseResolver'( WFRxObservablePromiseResolver )
