npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nativescript-use/nativescript-task

v0.0.11

Published

Multiprocessing task in background

Downloads

36

Readme

@nativescript-use/nativescript-task

npm install @nativescript-use/nativescript-task

A NativeScript module for simply handling background tasks via web workers.

This module will initialize a worker that will be available during the lifetime of your application and will be able to execute any task on it.

This lib provides online workers with promises and updates.

Examples

Without using dependencies

import { Task } from "@nativescript-use/nativescript-task";

// TS <number, string, boolean> = <InitialData, ReturnData, OnProgressData>
Task.start<number, string, boolean>((ctx) => {
  // ⚡ This function runs in the background
  ctx.onProgressUpdate(false);
  return ctx.state === 1000 ? "YES" : "NO";
}, {
    state: 1000,
    onProgressUpdate(update) {
      console.log(update.data);
    }
})
  .then((result) => {
    console.log('Result: ' + result.data);  // "YES"
  })
  .catch((result) => {
    console.log('ERROR: ' + result.error);
    // result.state = 1000
  });

Using inline functions

We can add local functions to run inside the worker using attachToContextFunctions.

import { Task, TaskContext } from "@nativescript-use/nativescript-task";

const myFunction = (myNumber: number) => 5 + myNumber;
const myOtherFunction = (myNumber: number) => 20 + myNumber;

Task.start((ctx) => {
    return myFunction(ctx.state) + myOtherFunction(ctx.state);
}, { state: 10, attachToContextFunctions: { myFunction, myOtherFunction }})

Using dependencies

We need to define a worker with the imports that we want to have defined in our tasks. We define the file globalWorker.ts|js in the src|app folder of our project, import the modules that we want to have available and pass them to the defineWorker function that this library provides.

// /app/globalWorker.ts
import '@nativescript/core/globals';
import { defineWorker } from "@nativescript-use/nativescript-task";

import { myUtils } from '@utils';
import { otherLib } from 'other-lib';

defineWorker({ imports: { otherLib } });

Now access the modules defined in the globalWorker file from the context.

import { Task } from "@nativescript-use/nativescript-task";
import { myUtils } from '@utils';
import { otherLib } from 'other-lib';

Task.start((ctx) => {
  // access imported modules
  return myUtils.someFunction(1000) + otherLib.otherFunction(ctx.state);
}, { state: 1000 })
  .then((result) => {
    console.log('Result: ' + result.data); 
  })

Global configuration

// app.ts | main.ts (entry file app)

import { Task } from "@nativescript-use/nativescript-task";

Task.globalWorkerConfig({
    stickyWorker: true,
    newWorkerIfGlobalIsUsed: true,
    startGlobalWorker: true
});

// run app
  • stickyWorker - default true: When set to true the plugin always keeps a worker running to launch your tasks to this worker. This saves time when launching the task since initializing a worker takes time, by default it is true to launch each task as quickly as possible. If set to false the plugin will initialize a worker and terminate each task.
  • newWorkerIfGlobalIsUsed - default true: When stickyWorker is true and we have a worker always running, if we launch a task and the main worker is running with another task, a new worker will be created to launch this task and not wait for the previous task to finish. If you disable this flag and launch another task while one is running, it will have to wait until it reaches the beginning of the queue.
  • startGlobalWorker - default true: Initialize the global worker when the configuration is set, it will be available when you launch your first task. If disabled, when the first task is launched it will have the worker creation delay.

Note: when the worker execution time is mentioned we are talking about about 200ms (it is almost nothing). It's not much, but this plugin prioritizes speed, which is why we keep a stickyWorker always available.

Limitations

  • Only submit and return serializable objects and values.
  • All task functions are 'closures', what means that you CANNOT access variables outside such functions. All functions are serialized as strings and submitted to the worker script where "external stuff" is NOT available! The only way to share data with the functions is to submit an optional and serializable "state value".

Read the official documentation to get more information.

For more information you can enter the NativeScript discord server!

This plugin is largely based on https://github.com/mkloubert/nativescript-tasks

License

Apache License Version 2.0