garyprinting.com

Exploring Deno: OS Signals, File System Events, and Module Info

Written on

Chapter 1: Introduction to Deno

Deno is an innovative server-side runtime for executing JavaScript and TypeScript applications. In this article, we will explore how to begin developing applications using Deno.

Handling OS Signals

Signals can be managed using the Deno.signal method. For instance, consider the following code snippet:

console.log("Press Ctrl-C");

for await (const _ of Deno.signal(Deno.Signal.SIGINT)) {

console.log("interrupted!");

Deno.exit();

}

When we press Ctrl+C, it triggers the SIGINT signal, leading to an interruption of the program. The for-await-of loop allows us to monitor this signal, while Deno.Signal.SIGINT represents the SIGINT signal. The program concludes with Deno.exit().

To execute the program, use the command:

deno run --unstable index.ts

Alternatively, the signal handling can be implemented using promises:

console.log("Press Ctrl-C to end the program");

await Deno.signal(Deno.Signal.SIGINT);

console.log("interrupted!");

Deno.exit();

Stopping Signal Monitoring

If you wish to cease monitoring signals, you can invoke the sig.dispose method. Here’s an example:

const sig = Deno.signal(Deno.Signal.SIGINT);

setTimeout(() => {

sig.dispose();

console.log("No longer watching SIGINT signal");

}, 5000);

console.log("Watching SIGINT signals");

for await (const _ of sig) {

console.log("interrupted");

}

In the setTimeout callback, we call sig.dispose to halt the monitoring of the SIGINT signal. The for-await-of loop will stop after five seconds when sig.dispose is executed.

File System Events

Monitoring file system events can be achieved through the Deno.watchFs method. Below is an example:

const watcher = Deno.watchFs(".");

for await (const event of watcher) {

console.log(event);

}

This code observes the current directory, and we can retrieve events from the event object. The output will display events such as creation, modification, and access of files.

You can run this with the following command:

deno run --allow-read index.ts

Module Metadata

To access module metadata, we can utilize the import.meta property. For example:

console.log(import.meta.url);

console.log(Deno.mainModule);

console.log(import.meta.main);

When executed, this may yield output similar to:

file:///home/runner/IntelligentWorthwhileMice/index.ts

file:///home/runner/IntelligentWorthwhileMice/index.ts

true

In this output, both import.meta.url and Deno.mainModule provide the path of the module, while import.meta.main returns true if it is the entry point.

Conclusion

In summary, Deno allows for effective handling of OS signals, retrieval of module metadata, and monitoring of file system events. If you enjoyed this article, be sure to subscribe to our YouTube channel for more content!

Chapter 2: Additional Resources

This video titled "How to Sign an Executable File using Signtool In Windows" provides further insights into executable file management and security practices.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Reflecting on the Weekend and Welcoming the Week Ahead

Join us as we reflect on the weekend and set intentions for the week ahead, embracing growth, gratitude, and safety.

The Role of AI in Transforming Education for the Future

Exploring how AI is revolutionizing education with personalized learning and improved teaching methods.

The Zeigarnik Effect: Understanding Interruption and Mindfulness

Explore the Zeigarnik effect's implications on memory and mindfulness amidst modern distractions.

Understanding Polymorphism in Python: A Comprehensive Overview

An insightful exploration of polymorphism in Python, covering its principles, method overriding, and abstract base classes.

The Incredible Story of Apollo 13: A Journey Against All Odds

Explore the extraordinary events of Apollo 13, highlighting the challenges faced by the crew and mission control during this historic space flight.

# Enhancing Empathy: A Pathway to Personal Growth and Team Success

Explore Tim's journey of developing empathy at work, transforming his leadership style and improving team dynamics.

Title: The Resurgence of the Lab Leak Theory in COVID-19 Origins

This week, the lab leak theory gained attention with a New York magazine cover story, sparking debates about COVID-19's origins.

Steampunk Clockwork Dreams: Ludwig Boltzmann's Thermodynamic Legacy

This piece explores Ludwig Boltzmann's revolutionary ideas in thermodynamics and their philosophical implications.