Typed, mutable, async events for deno and nodejs

mutevents

Events allows multiple listeners (A, B, C) to be executed when objects (O, S) trigger them; without ever knowing their existence at compile time.

Syntax

The syntax is (almost) the same as nodejs EventEmitter.

emitter.emit(event: string, ...args: any[]): Promise<"cancelled" | any[]>
emitter.on([event: string, priority: string], listener: (...args: any[]) => EventResult): void
emitter.off(...) // same as on()

There is no once() function since events can be cancelled. You have to stick with on() and off() when you want to remove the listener.

Types

Types are useful with TypeScript autocompletion and compiler warnings. Plus, they allow you to inherit events from a superclass.

Class way

We define a generic type Animal with a “death” event type

class Animal<T> extends EventEmitter<T | "death"> {
	// ...	
}

Then we define a type Dog that extends Animal with a “woof” event type.

class Dog extends Animal<"woof"> {
	// ...
}

Dog can now emit two event types: “woof” and “death”

Attribute way

We define an interface Animal with an events attribute. OpenEventEmitter is like a regular emitter but we can override its event types. Animal can emit “death” event type.

interface Animal {
	events: OpenEventEmitter<"death">
}

Then we define a type Duck that overrides Animal’s events attribute type to inject a new “quack” event type.

interface Duck extends Animal {
	events: OpenEventEmitter<"quack" | "death">
}

Duck can now emit both “quack” and “death”.

Note we could completely remove the “death” event from Duck

interface GodDuck extends Animal {
	events: OpenEventEmitter<"quack">
}

Implementation

class Quacky implements Duck {
	events = emitter<"quack" | "death">() // OpenEventEmitter<"quack" | "death">
}

Priorities

An event listener can have a priority:

  • “high” means it will be handled first
  • “normal” means normal
  • “low” means it will be handled last

The priority must be defined in the array after the event name. If no priority is defined, “normal” will be used.

Example

dog.on(["woof"], () => console.log("Normal"));
dog.on(["woof", "low"], () => console.log("Last"));
dog.on(["woof", "high"], () => console.log("First"));

The “low” listener will be executed after the “normal” one, which will be executed after the “high” one.

When multiple listeners are on the same priority, the first defined will be the first executed.

dog.on(["woof", "high"], () => console.log("First"));
dog.on(["woof", "high"], () => console.log("Last"));

Cancellation

Any event can be cancelled by any listener. The listener needs to explicitly return a “cancelled” string. The next listener will not be executed, and the emit() result will be “cancelled”.

dog.on(["woof", "high"], () => "cancelled");
dog.on(["woof"], () => console.log("This won't be displayed"));

Block form

dog.on(["woof"], () => {
	if(dog.name !== "Rex") return "cancelled";
	console.log("Rex: woof");
});

You can check for cancellation on the emitter side

const result = dog.emit("woof");
if(result === "cancelled") return;

Mutability

Any event is mutable by any listener. If a listener returns an array, the next listener will be passed this array.

player.on(["command"], (cmd: string) => {
	if(cmd === "man") return ["tldr"];
})

player.on(["command"], (cmd: string) => {
	// cmd is now "tldr"
})

With multiple arguments

player.on(["move"], (x, y, z) => {
	return [x, 0, z];
})

player.on(["move"], (x, y, z) => {
	// y is now 0
})

You can retrieve modification on the emitter side

let x = 1;
let y = 2;
let z = 3;

const result = player.emit("move", x, y, z);
if(result === "cancelled") return; // optional
[x, y, z] = result;

Download Details:

Author: hazae41

GitHub: https://github.com/hazae41/mutevents

#deno #nodejs #node #javascript

What is GEEK

Buddha Community

Typed, mutable, async events for deno and nodejs

Typed, mutable, async events for deno and nodejs

mutevents

Events allows multiple listeners (A, B, C) to be executed when objects (O, S) trigger them; without ever knowing their existence at compile time.

Syntax

The syntax is (almost) the same as nodejs EventEmitter.

emitter.emit(event: string, ...args: any[]): Promise<"cancelled" | any[]>
emitter.on([event: string, priority: string], listener: (...args: any[]) => EventResult): void
emitter.off(...) // same as on()

There is no once() function since events can be cancelled. You have to stick with on() and off() when you want to remove the listener.

Types

Types are useful with TypeScript autocompletion and compiler warnings. Plus, they allow you to inherit events from a superclass.

Class way

We define a generic type Animal with a “death” event type

class Animal<T> extends EventEmitter<T | "death"> {
	// ...	
}

Then we define a type Dog that extends Animal with a “woof” event type.

class Dog extends Animal<"woof"> {
	// ...
}

Dog can now emit two event types: “woof” and “death”

Attribute way

We define an interface Animal with an events attribute. OpenEventEmitter is like a regular emitter but we can override its event types. Animal can emit “death” event type.

interface Animal {
	events: OpenEventEmitter<"death">
}

Then we define a type Duck that overrides Animal’s events attribute type to inject a new “quack” event type.

interface Duck extends Animal {
	events: OpenEventEmitter<"quack" | "death">
}

Duck can now emit both “quack” and “death”.

Note we could completely remove the “death” event from Duck

interface GodDuck extends Animal {
	events: OpenEventEmitter<"quack">
}

Implementation

class Quacky implements Duck {
	events = emitter<"quack" | "death">() // OpenEventEmitter<"quack" | "death">
}

Priorities

An event listener can have a priority:

  • “high” means it will be handled first
  • “normal” means normal
  • “low” means it will be handled last

The priority must be defined in the array after the event name. If no priority is defined, “normal” will be used.

Example

dog.on(["woof"], () => console.log("Normal"));
dog.on(["woof", "low"], () => console.log("Last"));
dog.on(["woof", "high"], () => console.log("First"));

The “low” listener will be executed after the “normal” one, which will be executed after the “high” one.

When multiple listeners are on the same priority, the first defined will be the first executed.

dog.on(["woof", "high"], () => console.log("First"));
dog.on(["woof", "high"], () => console.log("Last"));

Cancellation

Any event can be cancelled by any listener. The listener needs to explicitly return a “cancelled” string. The next listener will not be executed, and the emit() result will be “cancelled”.

dog.on(["woof", "high"], () => "cancelled");
dog.on(["woof"], () => console.log("This won't be displayed"));

Block form

dog.on(["woof"], () => {
	if(dog.name !== "Rex") return "cancelled";
	console.log("Rex: woof");
});

You can check for cancellation on the emitter side

const result = dog.emit("woof");
if(result === "cancelled") return;

Mutability

Any event is mutable by any listener. If a listener returns an array, the next listener will be passed this array.

player.on(["command"], (cmd: string) => {
	if(cmd === "man") return ["tldr"];
})

player.on(["command"], (cmd: string) => {
	// cmd is now "tldr"
})

With multiple arguments

player.on(["move"], (x, y, z) => {
	return [x, 0, z];
})

player.on(["move"], (x, y, z) => {
	// y is now 0
})

You can retrieve modification on the emitter side

let x = 1;
let y = 2;
let z = 3;

const result = player.emit("move", x, y, z);
if(result === "cancelled") return; // optional
[x, y, z] = result;

Download Details:

Author: hazae41

GitHub: https://github.com/hazae41/mutevents

#deno #nodejs #node #javascript

Hire NodeJs Developer

Looking to build dynamic, extensively featured, and full-fledged web applications?

Hire NodeJs Developer to create a real-time, faster, and scalable application to accelerate your business. At HourlyDeveloper.io, we have a team of expert Node.JS developers, who have experience in working with Bootstrap, HTML5, & CSS, and also hold the knowledge of the most advanced frameworks and platforms.

Contact our experts: https://bit.ly/3hUdppS

#hire nodejs developer #nodejs developer #nodejs development company #nodejs development services #nodejs development #nodejs

studio52 dubai

studio52 dubai

1621850716

7 Tips for a Successful Live Event Coverage - Studio 52

Live events have been a growing trend in the events industry this past year, offering many businesses a much-needed lifeline. Read on for our simple tips to planning your virtual event

#event coverage services #event photography #event video production #event videography #event coverage services #event photography

Arvel  Miller

Arvel Miller

1603068240

Decoding Nodejs

The main goal of this blog is to explain the “Architecture of Nodejs” and to know how the Nodejs works behind the scenes,

Generally, most of the server-side languages, like PHP, ASP.NET, Ruby, and including Nodejs follows multi-threaded architecture. That means for each client-side request initiates a new thread or even a new process.

In Nodejs, all those requests from the clients are handled in a single-thread using shared resources concurrently as It follows the “Single-Threaded Event Loop Model”.

ARCHITECTURE OF NODEJS

What Is EVENT-LOOP?

Event-Loop programming is a flow control in an application-defined by events. The basic principle of Nodejs’s event-driven loop is implementing a central mechanism that hears for events and calls the callback function once an event is turning up.

Nodejs is an event-loop that implements a run-time environment model to achieve non-blocking asynchronous behavior runs on Google Chrome’s V8 engine.

#nodejs #nodejs-developer #nodejs-architecture #nodejs-tutorial #backend #javascript #beginners #event-loop

How much does it cost to develop an app for the event planning business?

Event planning & management app is the generic term for a wide range of mobile app products that are used in the management of professional and academic conferences, trade exhibitions, conventions, and events such as Continuing Professional Development meetings.

Development Cost to build an Event Planning & Management App:

Based on the number of hours invested, features, and technologies involved, you can determine a rough estimate of Event Planning or Management app development cost. Cost depends on various factors such as follows.

• Basic & Advance Features

• Technology used

• Chosen Platform (iOS & Android)

• The Location of the app development center

• Mobile App complexity

• Numbers of hours invested in Project

The cost to create such an app can be as high as the number of integrated technologies. However, an app with basic features is certain to cost somewhere around $10,000 to $35,000 for single platforms (Android or iOS). If you want to make an app for various platforms then the cost will be vary based on features, location, development team, etc factors.

Best Event Planning & Management App Development Company:

Event Management apps streamline the entry process for events. Save your precious time for a smooth experience and devote time for more significant activities such as increasing revenue with a mobile app from AppClues Infotech, a top Events & Exhibitions App Development Company based in the USA and offer the best services across the world.

Save your time and money with their cost-effective mobile apps to easily collaborate with the participants of the events. Real-time chats help to carry out video conferencing with both the participants and the employees as well. Their mobile apps increase the efficiency of multiple events by sending important messages to the participants with a single click.

Offering services that make events successful:

  • Corporate events
  • Conferences
  • Trade shows
  • Sports and entertainment
  • Custom event schedule app development

Incorporate features that elevate experiences

  • Event creation
  • Event search
  • Event calendar
  • Ticket booking
  • Photo sharing
  • GPS
  • Push notification
  • Chatbot
  • Messaging
  • In-app payment
  • News & social feeds
  • In-app analytics

#how to develop an event app #event management app development company #custom event management app development #how to build an event planning app #develop a mobile app for events #cost to build a event management app