JobScheduler and channel-switch mockup

The below code is a complete mockup of the channel-switching messaging and interactions, without actually switching channels or showing any svg graphics. It showcases the new JobScheduler and builds on the previous example.

We set up a job scheduler which delegates to a timer adapter using the browser’s window.setTimeout(...) and window.clearTimeout(...) methods (as opposed to what’s needed on SVG browsers without a window object or in unit tests).

We next set up a model for the current playback selection and a pair of listeners for channel changes to:

  1. Display the channel-switcher window, and set or reset the timer to hide it again for one second in the future.
  2. Do nothing, but set or reset the timer to switch the playing channel for 300ms in the future.

Finally, add a job to repeatedly select a random channel every 3 seconds to trigger everything. To manually trigger channel changes, add the following code to the testbed.html file: <a href="javascript:userInputJob()">random channel!</a>

// import tv.zignal.karateka.jobscheduler.JobScheduler
// import tv.zignal.karateka.jobscheduler.BrowserTimerAdapter
// import tv.zignal.karateka.models.ObjectReferenceModel
// import dwr:interface/ScheduleClientApi

// JOB SCHEDULING

var timerAdapter = new BrowserTimerAdapter();
var jobScheduler = new JobScheduler(timerAdapter);

// PLAYBACK MODEL AND LISTENERS

// The plaback model is (for now) just a reference to the channel selected for playback
var channelPlaybackModel = new ObjectReferenceModel();

// Pretend to show a UI component with the new playing stream
// and keep it open until there hasn't been a channel change for 1 second
var channelSwitchListener = function channelSwitchListener(event) {
    var to = event.to ? event.to.title : "nothing";
    console.info("Display that we are watching " + to);
    jobScheduler.addOneTimeJob(
            function() {
                console.info("Remove display");
            }, 1000, "hide-view");
}
channelSwitchListener.subscribe(channelPlaybackModel.changed);

// Pretend to change the playing stream when there hasn't been a channel change for 300ms
var channelPlaybackListener = function channelPlaybackListener(event) {
    var to = event && event.to ? event.to.url : "nothing";
    jobScheduler.addOneTimeJob(
            function() {
                console.info("Ask PlaybackManager to start playing " + to);
            }, 300, "change-stream");
}
channelPlaybackListener.subscribe(channelPlaybackModel.changed);

// USER INTERACTION

/** Set the value of the channel selection to a random channel. */
var userInputJob = function userInputJob() {
    var channel;
    ScheduleClientApi.getRandomTvChannel({
        async:false,
        callback: function(result) {
            channel = result;
        }
    });
    channelPlaybackModel.set(channel);
};

/** Automatically change to a random channel every 3 seconds. */
var jobName = jobScheduler.addRepeatableJob(userInputJob, 3000);
console.info("Started automatic changet job: " + jobName);

Leave a Reply

You must be logged in to post a comment.