23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

var internetCondition = new<br />

background.SystemCondition(background.SystemConditionType.internetAvailable);<br />

taskBuilder.addCondition(internetCondition);<br />

taskBuilder.register();<br />

Because the expiration period for channel URIs is 30 days, the sample creates a trigger on a recurring<br />

10-day interval (10 days * 24 hours/day * 60 minutes/hour). It also wisely adds the internetAvailable<br />

condition because it’s again pointless to attempt to renew channel URIs when there’s no connectivity.<br />

The task itself can be found in the js/backgroundTask.js file of the sample, as indicated in the<br />

taskEntryPoint property:<br />

(function () {<br />

// Import the Notifier helper object<br />

importScripts("//Microsoft.WinJS.1.0/js/base.js");<br />

importScripts("notifications.js");<br />

var closeFunction = function () {<br />

close();<br />

};<br />

var notifier = new SampleNotifications.Notifier();<br />

notifier.renewAllAsync().done(closeFunction, closeFunction);<br />

})();<br />

This task code pulls in a couple of other script files using importScripts, the second of which,<br />

notifications.js, is the sample’s set of helper functions for notifications where renewAllAsync refreshes<br />

the app’s list of previously saved channel URIs.<br />

Important You’ll also notice that the completed and error handlers given to the promise from<br />

renewAllAsync both go to closeFunction, which makes this mysterious call to close. What close is<br />

this? Well, it’s not window.close (as you might guess) but rather WorkerGlobalScope.-close.<br />

Background tasks in an app written in JavaScript run within the scope of a web worker, so the global<br />

scope within the code is WorkerGlobalScope rather than window. Calling this makes sure the<br />

independently running background task is shut down and guarantees that the resources that were<br />

allocated for the task are properly released.<br />

Sidebar: The Task Instance and Background Task Deferrals<br />

Within a JavaScript background task, the<br />

Windows.UI.WebUI.WebUIBackgroundTask-Instance.current contains a<br />

WebUIBackgroundTaskInstanceRuntimeClass object that provides additional details about the<br />

running task: its instanceId, its associated Background-TaskRegistration object in the task<br />

property, a progress property in which the task can store a percentage value, a succeeded flag to<br />

indicate that the task has completed, a suspended count (when the task is suspended due to the<br />

resource quota being exceeded), and a canceled event that informs the task that the app as a<br />

whole has been terminated.<br />

621

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!