Giter Club home page Giter Club logo

deno-reservator's Introduction

reservator

jsr denoland deno doc Test

The reservator is a TypeScript module that provides the Reservator class. The Reservator class is used to reserve resources or events with unique keys, and wait for them to be resolved or rejected.

Installation

To use the reservator module, you can import it directly from the JSR.

import { Reservator } from "@lambdalisue/reservator";

Usage

The Reservator class provides three methods: reserve, resolve, and reject.

  • The reserve method is used to reserve a resource or event with a unique key. It returns a promise that is resolved or rejected when the resource is resolved or rejected. If a reservation with the same key already exists, an AlreadyReservedError is thrown.

  • The resolve method is used to resolve a reservation with a key and a value. It deletes the reservation from the Reservator object and resolves the corresponding promise. If no reservation with the given key exists, a NoReservationError is thrown.

  • The reject method is used to reject a reservation with a key and an optional reason. It deletes the reservation from the Reservator object and rejects the corresponding promise. If no reservation with the given key exists, a NoReservationError is thrown.

Here is an example usage of the Reservator class:

import { delay } from "@std/async";
import { AlreadyReservedError, NoReservationError, Reservator } from "@lambdalisue/reservator";

async function main(): Promise<void> {
  const key = "reservationKey:1";

  const reservator = new Reservator();

  const consumer = (reservator () => {
    // Reserve `key` and wait
    const received = await reservator.reserve(key);
    console.log(`Received: ${received}`);
  })();

  const producer = (reservator () => {
    // Wait 3 seconds
    await delay(3000);
    // Resolve with the key
    reservator.resolve(key, "Hello world");
  })();

  await Promise.all([consumer, producer]);
}

main();

License

The code follows the MIT license written in LICENSE. By contributing to this repository, you agree that any modifications you make also follow the MIT license.

deno-reservator's People

Contributors

lambdalisue avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

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.