@gallink/oxygen
TypeScript icon, indicating that this package has built-in type declarations

1.2.8 • Public • Published

Oxygen brings life to other Gallink applications by providing a set of useful utilities.

Using Queues

Queues are a neat approach to processing a collection of items. Both of the implementations have different purposes, both which are particularly useful.

First is the standard Queue implementation, which is simply a box for an Array with the ability to dequeue items in a controlled, FIFO respect.

const friendsToAdd: Queue<Friend> = new Queue(api.getFriendSuggestions())

while (friendsToAdd.pending) {
    
    const friendToAdd: Friend = friendsToAdd.dequeue();
    
    await friendToAdd.sendFriendRequest() 
    
}

// Friends added!

PromiseQueue adds another layer of asynchronous debuffing to JavaScript by allowing you to queue Promises. We can rewrite the sample above to use these.

const friendsToAdd: Queue<Friend> = new Queue(api.getFriendSuggestions())
const friendsBeingAded: PromiseQueue<Friend> = new PromiseQueue<Friend>();

while (friendsToAdd.pending) {
    
    const friendToAdd: Friend = friendsToAdd.dequeue();
    
    await friendsBeingAdded.task(friendToAdd.sendFriendRequestAsync)
    
}

The difference between these two examples is that the first will handle all the friend requests at once, whereas the second example will handle one at a time.

Create Handleable Actions

Inspired by Node's EventEmitter, Actionable provides an attractive alternative to calling code in other places by firing actions that are listened to elsewhere - a professional use for callbacks.

const me: Person = new Person("Crowes");

me.on("hungry", me.eat())

Genuine Data Models

JavaScript does a poor job of supporting genuine data models, leaving you to write a lot of the boilerplate for a lot of your reflection-style work which is so commonly required in typical JavaScript applications.

const me: Person = new Person("Crowes", 21);
const differentMe: Person = new Person("Crowes", 22);

const difference: Map<string, any> = me.difference();
// { "age", 22 }

Readme

Keywords

Package Sidebar

Install

npm i @gallink/oxygen

Weekly Downloads

1

Version

1.2.8

License

MIT

Unpacked Size

31 kB

Total Files

42

Last publish

Collaborators

  • crowes