# Convenience functions for Ignition development, typically loaded
# as "shared.later"
#
# Copyright 2008-2012 Automation Professionals, LLC <sales@automation-pros.com>
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
#   1. Redistributions of source code must retain the above copyright notice,
#      this list of conditions and the following disclaimer.
#   2. Redistributions in binary form must reproduce the above copyright notice,
#      this list of conditions and the following disclaimer in the documentation
#      and/or other materials provided with the distribution.
#   3. The name of the author may not be used to endorse or promote products
#      derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
# OF SUCH DAMAGE.

#------------
# Asynchronous method execution
#
# This routine accepts a target function, and a variable argument list, and
# schedules the function call for asynchronous execution.  The function can
# be a bare (no parens) object method, but must not call any gui or
# gui component methods.
def callAsync(func, *args, **kwargs):
	def callAsyncInvoked(f=func, a=args, kw=kwargs):
		f(*a, **kw)
	system.util.invokeAsynchronous(callAsyncInvoked)

#------------
# Deferred property assignment
#
# Procedures executing in the 'invokeAsynchronous' environment are not allowed
# to assign to properties of gui objects.  This routine accepts a target
# object, a property name, and a new value, and schedules the assignment
# in the gui thread.
#
# Cannot be used in gateway scripts, as invokeLater is not available.
def assignLater(comp, prop, val, ms = 0):
	def assignLaterInvoked(c=comp, p=prop, v=val):
		try:
			setattr(c, p, v)
		except:
			c.setPropertyValue(p, v)
	system.util.invokeLater(assignLaterInvoked, ms)

#------------
# Deferred method execution
#
# Procedures executing in the 'invokeAsynchronous' environment are not allowed
# to execute methods of gui objects.  This routine accepts a target
# function, and a variable argument list, and schedules the function call
# in the gui thread.  The function can be a bare (no parens) object method.
#
# Cannot be used in gateway scripts, as invokeLater is not available.
def callLater(func, *args, **kwargs):
	def callLaterInvoked(f=func, a=args, kw=kwargs):
		f(*a, **kw)
	system.util.invokeLater(callLaterInvoked)

#------------
# Deferred Asynchronous method execution
#
# This routine accepts a target function, and a variable argument list, and
# schedules the function call for asynchronous execution, but only after also
# waiting for gui events to complete.  The function can be a bare (no parens)
# object method, but must not call any gui or gui component methods.
#
# Cannot be used in gateway scripts, as invokeLater is not available.
def callAsyncLater(func, *args, **kwargs):
	def callLaterInvoked(f=func, a=args, kw=kwargs):
		import system
		def callAsyncInvoked(func=f, args=a, kwargs=kw):
			func(*args, **kwargs)
		system.util.invokeAsynchronous(callAsyncInvoked)
	system.util.invokeLater(callLaterInvoked)

#------------
# Deferred Asynchronous method execution
#
# This routine accepts a target function, a time, and a variable argument
# list, and schedules the function call for asynchronous execution, but
# only after also waiting the specified milliseconds after all gui events
# complete.  The function can be a bare (no parens) object method, but must
# not call any gui or gui component methods.
#
# Cannot be used in gateway scripts, as invokeLater is not available.
def callAsyncDelayed(func, delay=1000, *args, **kwargs):
	def callLaterInvoked(f=func, a=args, kw=kwargs):
		import system
		def callAsyncInvoked(func=f, args=a, kwargs=kw):
			func(*args, **kwargs)
		system.util.invokeAsynchronous(callAsyncInvoked)
	system.util.invokeLater(callLaterInvoked, delay)

