@jaenster/synchronized
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

Synchronize

  • npm; npm install @jaenster/synchronized
  • yarn; yarn add @jaenster/synchronized
  • deno; import {Synchronized} from 'https://github.com/jaenster/synchronized/raw/master/src/index.ts'

Problem

Ever had an async method that should not run twice at the same time?

Solution

synchronize with a decorator!

Example

import {Synchronized} from "@jaenster/synchronized";

// generate a promise which delays
const delay = ms => new Promise<void>(resolve => setTimeout(resolve, ms)); 

class Worker {
    
    @Synchronized()
    async runTask(id: number) {
        // Do something
        
        // simulate some work
        console.log(`task #${id} started`);
        await delay(500);
        console.log(`task #${id} done`);
    }
    
}

(async () => {
    const worker = new Worker();

    await Promise.all([
        worker.runTask(0),
        worker.runTask(1),
        worker.runTask(2),
        worker.runTask(3),
    ]);
})();

What happens

If Worker.runTask has the decorator Synchronized,it takes 2 seconds to run and logs:

  • task #0 started
  • task #0 done
  • task #1 started
  • task #1 done
  • task #2 started
  • task #2 done
  • task #3 started
  • task #3 done

if Synchronized is omitted from the example code, it takes a half second to run and logs:

  • task #0 started
  • task #1 started
  • task #2 started
  • task #3 started
  • task #0 done
  • task #1 done
  • task #2 done
  • task #3 done

Package Sidebar

Install

npm i @jaenster/synchronized

Weekly Downloads

4

Version

0.1.1

License

MIT

Unpacked Size

4.21 kB

Total Files

4

Last publish

Collaborators

  • jaenster