Skip to content

Instantly share code, notes, and snippets.

@mstaicu
Last active September 12, 2020 11:36
Show Gist options
  • Save mstaicu/e9c99f62f03b771cc6b25d11050e741a to your computer and use it in GitHub Desktop.
Save mstaicu/e9c99f62f03b771cc6b25d11050e741a to your computer and use it in GitHub Desktop.

Revisions

  1. mstaicu revised this gist Nov 13, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion semaphore.js
    Original file line number Diff line number Diff line change
    @@ -116,7 +116,7 @@ var doStuff = data =>
    console.log(data);
    res();
    },
    500,
    1000,
    data
    )
    );
  2. mstaicu revised this gist Nov 13, 2019. 1 changed file with 29 additions and 0 deletions.
    29 changes: 29 additions & 0 deletions semaphore.js
    Original file line number Diff line number Diff line change
    @@ -98,3 +98,32 @@ let run = (async () => {

    return result;
    })();

    /**
    * Limit
    */

    var limit = (max, fn) => {
    var semaphore = Semaphore(2);

    return (...args) => semaphore(() => fn(...args));
    };

    var doStuff = data =>
    new Promise(res =>
    setTimeout(
    () => {
    console.log(data);
    res();
    },
    500,
    data
    )
    );

    var limitStuffToDo = limit(2, doStuff);

    limitStuffToDo("one");
    limitStuffToDo("two");
    limitStuffToDo("3");
    limitStuffToDo("four");
  3. mstaicu revised this gist Nov 13, 2019. 1 changed file with 59 additions and 1 deletion.
    60 changes: 59 additions & 1 deletion semaphore.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    /**
    * OOP paradigm
    */
    class Semaphore {
    constructor(max) {
    this.tasks = [];
    @@ -39,4 +42,59 @@ var run = (async() => {
    await semaphore.acquire();

    console.log('third runner');
    })();
    })();


    /**
    * FP paradigm
    */
    let Semaphore = max => {
    let tasks = [];
    let counter = max;

    let dispatch = () => {
    if (counter > 0 && tasks.length > 0) {
    counter--;
    tasks.shift()();
    }
    };

    let release = () => {
    counter++;
    dispatch();
    };

    let acquire = () =>
    new Promise(res => {
    tasks.push(res);
    /**
    * Adjust for desired platform
    */
    setTimeout(dispatch);
    });

    return async fn => {
    await acquire();
    let result;

    try {
    result = await fn();
    } catch (e) {
    throw e;
    } finally {
    release();
    }

    return result;
    };
    };

    let semaphore = Semaphore(2);

    let run = (async () => {
    let result = await semaphore(async () => {
    return await Promise.resolve(1);
    });

    return result;
    })();
  4. mstaicu created this gist Nov 13, 2019.
    42 changes: 42 additions & 0 deletions semaphore.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    class Semaphore {
    constructor(max) {
    this.tasks = [];
    this.counter = max;
    this.dispatch = this.dispatch.bind(this);
    }

    dispatch() {
    if (this.counter > 0 && this.tasks.length > 0) {
    this.counter--;
    this.tasks.shift()();
    }
    }

    release() {
    this.counter++;
    this.dispatch();
    }

    acquire() {
    return new Promise(res => {
    this.tasks.push(res);
    setTimeout(this.dispatch, 100);
    });
    }
    }

    var semaphore = new Semaphore(2);

    var run = (async() => {
    await semaphore.acquire();

    console.log('first runner');

    await semaphore.acquire();

    console.log('second runner');

    await semaphore.acquire();

    console.log('third runner');
    })();