% timed_action - return action function that checks the time
%
% timed_action :: 
%    (A1, A2, ... => B1, B2, ...) ~'some action',
%    options {
%       print   :: bool/false     ~'if true, then print timing error on action';
%       errorfn :: (time => real) ~'action to compute timing error from scheduled time'
%    }
% -> timed_action({A1,A2,...},{B1,B2,...}).
function a0=timed_action(x,varargin)
	opts=prefs('print',0,'errorfn',@(t)nows-t,varargin{:});
	if opts.print, a0=@aa_print; else a0=@aa; end
	errfn=opts.errorfn;

	function varargout=aa_print(t0,varargin),
		varargout{1}=errfn(t0); 
		[varargout{2:nargout}]=x(varargin{:});
		fprintf('timed_action: error=%g\n',varargout{1});
	end
	function varargout=aa(t0,varargin),
		varargout{1}=errfn(t0);
		[varargout{2:nargout}]=x(varargin{:});
	end
end
