
%

%   Copyright 2011-2020 The MathWorks, Inc.

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

    properties ( Dependent )
        %RunningFcn Callback executed when the job or task starts running
        RunningFcn
    end
    
    properties( Transient, Access = private )
        % The "raw" callback function specified by the user.
        UserSpecifiedRunningFcn
        
        % Listens to Running event and calls the Callback fcn
        CallbackListener
        
        % Listens to Running event and calls the Support Callback fcn
        SupportCallbackListener
    end
    
    events( Hidden, ListenAccess = public, NotifyAccess = protected )
        Running
    end

    properties ( Constant, Access = protected )
        RunningEventName = 'Running';
    end

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