Code Monkey home page Code Monkey logo

ti.worker's Introduction

Titanium Worker Module

This is a Titanium module that provides a Web Worker like interface to applications built with Titanium.

This module is designed to be used when applications need to process asynchronous application logic. Due to the limitations of Javascript engines; tasks will not be executed on a seperate thread, but instead executed asynchronously.

Example

The following is a trivial echo background service. In your app.js, use the following:

var worker = require('ti.worker');

// create a worker thread instance
var task = worker.createWorker('echo.js');

// subscribe to any worker thread instance messages
task.addEventListener('message',function(event){
	
	// data that is sent will be in the data property
	alert(event.data);
	
	// stop terminating this thread instance
	task.terminate();
});

// send data to the worker thread which will be posted on the threads event queue
// you can send any data here
task.postMessage({
	msg:'Hello'
});

Now, in a separate file named echo.js, use the following:

// subscribe to events send with postMessage
worker.addEventListener('message',function(event){
	
	// send data back to any subscribers
	// pull data from the event from the data property
	worker.postMessage(event.data.msg);
});

Note: the worker global variable is always defined inside the worker execution context.

API

  • postMessage - send a message to or from the worker thread. Send any data as the first argument.
  • terminate - terminate the worker thread and stop as soon as possible processing.
  • nextTick - this method is only available inside the worker instance and provides an ability to process the function passed on the next available thread event loop cycle.

Events

  • message - receive an event. The data property of the event will contain as-is any data specified as the first argument.
  • terminated - the worker thread was terminated.

Properties

The worker instance has only one property:

  • url - the url of the worker thread JS file passed in during creation.

File Location

In order for your source (worker) files to be picked up, place them in the following directory:

Alloy: /app/lib

Classic: /Resources

Warning

This module is experimental and has not been finalized.

Legal

Titanium is a registered trademark of TiDev Inc. All Titanium trademark and patent rights were transferred and assigned to TiDev Inc. on 4/7/2022. Please see the LEGAL information about using our trademarks, privacy policy, terms of usage and other legal information at https://tidev.io/legal.

ti.worker's People

Contributors

caspahouzer avatar cb1kenobi avatar designbymind avatar garymathews avatar hansemannn avatar ingo avatar janvennemann avatar jonalter avatar rborn avatar sgtcoolguy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ti.worker's Issues

Stuck during JS processing

I found a problem after repeating my background process several times during a normal use. (The user click on a ListView, the bg process starts/ends, a new window is open)

The module gets stuck right after

[_bridge evalJSWithoutResult:@"TiWorkerStart__();"]; in the booted method:

- (void)booted:(id)bridge
{
  // this callback is called when the thread is up and running
  dispatch_async(_serialQueue, ^{
    _booted = YES;
    NSLog(@"[INFO] Worker %@ (0x%X) is running", [_selfProxy url], self);
    [_selfProxy setExecutionContext:_bridge];
    [[NSFileManager defaultManager] removeItemAtPath:_tempFile error:nil];
  });

  // start our JS processing
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      [_bridge evalJSWithoutResult:@"TiWorkerStart__();"]; // STUCK HERE
  });
}

usually the postMessage method is what happens after, but not in some cases.

Debug Info Incorrect/Obfuscated

When there is a syntax error in the worker file, the debug message in the console provides an incorrect/obfuscated source file.

Eg:
sourceURL = "file://localhost/var/folders/jb/b8vsmqzx00ng22t1383j_qfr0000gn/T/5694";

Android equivalent

Just wondering if there was an android equivalent to this module.

Many thanks.

Crash

I am getting a crash on the following code when posting data back from the worker.

-(void)postMessage:(id)msg
{
    ENSURE_SINGLE_ARG(msg,NSObject); 
    // this is from the worker, posting back to the creator
    NSDictionary *dict = [NSDictionary dictionaryWithObject:msg forKey:@"data"];
    TiWorkerProxy *proxy = (TiWorkerProxy*)_parent;
    [proxy fireMessageEvent:dict];
}

Is there a limit to the size of data that can be sent back?

Ti.API.info Not Exposed

I am unable to log to the console within the worker file. Ti.API.info throws a parse error.

Needed to run on background IOS

I have a problem with large database sincronization beacouse it's blocking user interface on iOs phones.
So it is posibble to add background task support to iOS so the database operations dont block the UI.

ios 6 crush

works fine on ios<6

on 6 - SDK 2.1.3
KrollBridge.mm line 598

[self evalFile:[url path] callback:self selector:@selector(booted)];

url is nil: (supposed ti be the wrapped javascript) ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.