locked
Using multiple javascript files in a background task RRS feed

  • Question

  • So I know to specify what javascript class runs when a background task is fired is done through specifying the class name as the 'start page' in the background class declarations part of the app manifest, and through specifying the class name as the taskEntryPoint for the BackgroundTaskBuilder object, but what if the entry point class requires another javascript class be loaded in memory in order to function correctly?

     

    For example what if my entry point class's contents were:

     

    (function() {
       var dataHandler = new DataHandler();
       // code that uses dataHandler here...
    })(); 
    

     

    and DataHandler was defined in another javascript file. How could I ensure the background task loads all required javascript files, rather than just the entry point, into memory for the background task?

     



    • Edited by Joe Levy_ Tuesday, December 27, 2011 3:53 AM
    Tuesday, December 27, 2011 3:52 AM

Answers

  • Joe,

    I was bouncing this around the team.  I have not had time to test this but figured I would send this along to you.

    You can include files in Web Workers.  These should work nicely inside of a background task.  There is a sample for Web Workers here:

    http://code.msdn.microsoft.com/windowsapps/Responsive-HTML5-f2dc48d2

    and the include syntax is here:

    importScripts

    -Jeff

     

     


    Jeff Sanders (MSFT)
    Friday, December 30, 2011 8:41 PM
    Moderator
  • Josh,

    So the js containing your background task will actually be created as a web worker when win8 calls it to run in the background, which means you should use this rather than WorkerGlobalScope when calling importScripts(), so that the scripts get imported into the background task webworker.

    Ie in your backgroundtask.js file you might have:

    (function () {
        "use strict";

        this.importScripts("/winjs/js/base.js", "/winjs/js/wwaapp.js", "/js/async.js", "/js/dataHandler.js", "/js/notifier.js");

        // do background task stuff now that the scripts needed are imported

    })();

    Tuesday, March 6, 2012 9:56 PM
  • Figured it out looking at the Push and periodic notifications client-side sample... importScripts() is just available in the global namespace..

    so using your example its just

    (function () {
        "use strict";

        importScripts("/winjs/js/base.js", "/winjs/js/wwaapp.js", "/js/async.js", "/js/dataHandler.js", "/js/notifier.js");

        // do background task stuff now that the scripts needed are imported

    })();

    - Josh

    Wednesday, March 7, 2012 3:05 PM

All replies

  • Hi Joe,

    The JavaScript Language does not have the concept of including multiple files from another js file.  Is that what you are asking about?

    -Jeff


    Jeff Sanders (MSFT)
    Tuesday, December 27, 2011 3:07 PM
    Moderator
  • I know you can't "import" one javascript file from another, but how can one have a background task that uses more than just a single javascript class then? I have refactored my program functionality into a variety of javascript files, and my background task uses some of that functionality, so I need to be able to access multiple javascript classes in my background task. But as far as I know myBackgroundTaskBuilder.taskEntryPoint and "Start Page" in the app manifest declarations for a background task only let you define a single javascript class to be run when the background task condition/trigger is fired.

    Is there any way to have multiple javascript files run in the background task thread when a background task starts? Like how in an html file I can include multiple <script> tags to have access to multiple javascript files on that page and in each of the javascript files?

    - Joe

    Tuesday, December 27, 2011 5:40 PM
  • Hi Joe,

    No sorry you cannot do that.  You would have to put all the logic in one .js file.

    -Jeff


    Jeff Sanders (MSFT)
    Tuesday, December 27, 2011 6:03 PM
    Moderator
  • This seems like a large flaw in the background task system then. Are you expecting that all apps written will have background tasks that are only as simplistic as to use one file, and not require any of the rest of the program's logic? It also means one can't use any external libraries like jQuery in a background task unless they copy and paste the source code into the background task js file.

    In my opinion this comes in direct competition with writing maintainable code. If I have to copy and paste all my functionality into one file in order to make a background task work, but then need to change some of this functionality, I need to make the same change twice - once in the original js file holding the logic and once in the background task js file holding the copy/pasted logic.

    Tuesday, December 27, 2011 6:50 PM
  • Not to mention the WinJS library isn't automatically loaded by the background task runner (and the only way to include the functionality of that library is to copy and paste the entire library contents into your background task javascript file), so its not really even possible to use any of Microsoft's own WinJS functionality in one's background tasks.

    I think there needs to be a better solution than simply only allowing one js file to be loaded for a background task.

    Friday, December 30, 2011 12:17 AM
  • Joe,

    I was bouncing this around the team.  I have not had time to test this but figured I would send this along to you.

    You can include files in Web Workers.  These should work nicely inside of a background task.  There is a sample for Web Workers here:

    http://code.msdn.microsoft.com/windowsapps/Responsive-HTML5-f2dc48d2

    and the include syntax is here:

    importScripts

    -Jeff

     

     


    Jeff Sanders (MSFT)
    Friday, December 30, 2011 8:41 PM
    Moderator
  • Jeff,

    After testing it out, importScript works perfectly as a solution to my problem. Thanks.

    - Joe

    Saturday, December 31, 2011 9:15 AM
  • Was there a resolution here.. I need to schedule a background task to go poll an update.. I would like to reuse my communication and storage libraries rather than have to copy paste that code into the background task.

    -Josh

    Tuesday, March 6, 2012 8:21 PM
  • Hi Josh,

    Yes there was.  See the Answer for this post.

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, March 6, 2012 8:28 PM
    Moderator
  • The link is broken... So can I just call WorkerGlobalScope.importScripts(urls);  from my background.js?

    Thanks

    Josh

    Tuesday, March 6, 2012 8:30 PM
  • FYI: I can't call  WorkerGlobalScope.importScripts(urls) from my background task.. And my usecase is a background tasks running with lock screen permissions..

    Is there another way to use a Worker in a background task?

    Josh

    Tuesday, March 6, 2012 9:37 PM
  • Josh,

    So the js containing your background task will actually be created as a web worker when win8 calls it to run in the background, which means you should use this rather than WorkerGlobalScope when calling importScripts(), so that the scripts get imported into the background task webworker.

    Ie in your backgroundtask.js file you might have:

    (function () {
        "use strict";

        this.importScripts("/winjs/js/base.js", "/winjs/js/wwaapp.js", "/js/async.js", "/js/dataHandler.js", "/js/notifier.js");

        // do background task stuff now that the scripts needed are imported

    })();

    Tuesday, March 6, 2012 9:56 PM
  • Looks like that has changed in Consumer Preview... this is now undefined inside the context of a background task..

    And Windows.UI.WebUI.WebUIBackgroundTaskInstance.current is useless as well :-(

    Josh

    Wednesday, March 7, 2012 2:48 PM
  • Figured it out looking at the Push and periodic notifications client-side sample... importScripts() is just available in the global namespace..

    so using your example its just

    (function () {
        "use strict";

        importScripts("/winjs/js/base.js", "/winjs/js/wwaapp.js", "/js/async.js", "/js/dataHandler.js", "/js/notifier.js");

        // do background task stuff now that the scripts needed are imported

    })();

    - Josh

    Wednesday, March 7, 2012 3:05 PM
  • Sweet, good in fo Josh!

    Jeff Sanders (MSFT)

    Wednesday, March 7, 2012 3:06 PM
    Moderator
  • Minor update for RTM, use //Microsoft.WinJS.1.0 for base.js etc:

    importScripts("//Microsoft.WinJS.1.0/js/base.js", ...

    Thursday, September 20, 2012 4:46 AM
  • Hi! I tried the following and I'm getting errors:

    - importScripts(urls):  'importScripts' is undefined

    - this.importScripts:  Object doesn't support property or method 'importScripts'

    - WorkerGlobalScope.importScripts: 'WorkerGlobalScope' is undefined

    What am I missing? Thanks!


    Thursday, December 19, 2013 2:15 PM