Package Callback Examples

The examples in this section (based on the Packages white paper on the Jade website at https://www.jadeplatform.tech/developer-centre/learn/whitepapers) have a ScheduledEvent class in the exported package. This class has targetMethod, targetContext, and targetObject properties that are supplied to a scheduleEvent method. The targetObject property is an instance of any class in the schema using the package and the targetMethod property is a method on that class or a superclass of that class. The methods to be called in these examples are in the DiaryTester class (a subclass of Application), so the app system variable is used as the targetObject value.

The method in the following example schedules an event.

scheduleEvent_click(btn: Button input) updating;
begin
    scheduler.scheduleEvent(whenToInvoke, app,
                            selectEvent.listObject.Method, appContext);
end;

The method to schedule an event can include a check that the target method supplied by the user of the package has the same signature as the exampleCallback method (for example, it may expect to call it with the scheduled time) and have the form: shown in the following example.

scheduleEvent(when: Time; targetObject: Object; targetMethod: Method;
              targetContext: ApplicationContext) updating;
vars
    se : ScheduledEvent;
begin
    if not targetMethod.isCallCompatibleWith(targetObject,
                       Scheduler::exampleCallback) then
        write 'callback not valid';
        return;
    endif;
    create se transient;
    se.targetObject  := targetObject;
    se.targetContext := targetContext;
    se.targetMethod  := targetMethod;
    se.whenToStart   := when;
    se.myScheduler   := self;
    updateTimer;
end;

The method in the following example is called when the timer fires and inspects all events at the start of the queue and calls all those whose time has passed.

causeDueEvents();
vars
    se : ScheduledEvent;
begin
    foreach se in allScheduledEvents do
        if se.whenToStart > app.actualTime.time then
            return;
        endif;
        // Call users method, supplying expected start time as a parameter
        if se.targetObject <> null and se.targetMethod <> null then
            targetObject.invokeMethod(se.targetContext, se.targetMethod,
                                      se.whenToStart);
            se.myScheduler := null;
            delete se;
        endif;
    endforeach;
end;