% MJSQueuedCallbackMixin

%   Copyright 2011-2020 The MathWorks, Inc.

classdef ( Hidden ) MJSQueuedCallbackMixin < parallel.internal.cluster.MJSCallbackMixin

    properties ( Dependent )
        %QueuedFcn Callback executed when the job is queued
        QueuedFcn
    end
        
    properties( Transient, Access = private )
        % The "raw" callback function specified by the user.
        UserSpecifiedQueuedFcn
        
        % Listens to Queued event and calls the Callback fcn
        CallbackListener
        
        % Listens to Queued event and calls the Support Callback fcn
        SupportCallbackListener
    end
    
    events( Hidden, ListenAccess = public, NotifyAccess = protected )
        Queued
    end
    
    properties ( Constant, Access = protected )
        QueuedEventName = 'Queued';
    end

    methods
        function val = get.QueuedFcn( obj )
            val = obj.UserSpecifiedQueuedFcn;
        end
        
        function set.QueuedFcn( obj, val )
            try
                if isempty(obj.CallbackListener)
                    obj.CallbackListener = event.listener( obj, obj.QueuedEventName, obj.NoOpCallback );
                    obj.CallbackListener.Enabled = false;
                end
                val = obj.setCallbackFcn( 'QueuedFcn', obj.CallbackListener, val );
                obj.UserSpecifiedQueuedFcn = val;
            catch err
                % Get the internal details off the stack
                throw(err);
            end
        end
    end
    methods ( Hidden, Access = ?parallel.internal.cluster.MJSSupport )
        function setSupportQueuedCallbackFcn( obj, val )
            % Allow MJSSupport to set additional queued event listener
            try
                if isempty(obj.SupportCallbackListener)
                    obj.SupportCallbackListener = event.listener( obj, obj.QueuedEventName, obj.NoOpCallback );
                    obj.SupportCallbackListener.Enabled = false;
                end
                obj.setCallbackFcn( 'QueuedFcn', obj.SupportCallbackListener, val );
            catch err
                % Get the internal details off the stack
                throw(err);
            end
        end
    end
    
end
