<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sarmitha Krishnagobal</title>
    <description>The latest articles on DEV Community by Sarmitha Krishnagobal (@sarmitha).</description>
    <link>https://dev.to/sarmitha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1370638%2Fd300bf9a-2312-427f-bb93-b52996437872.jpg</url>
      <title>DEV Community: Sarmitha Krishnagobal</title>
      <link>https://dev.to/sarmitha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sarmitha"/>
    <language>en</language>
    <item>
      <title>Angular Defer View</title>
      <dc:creator>Sarmitha Krishnagobal</dc:creator>
      <pubDate>Wed, 01 Jan 2025 06:59:35 +0000</pubDate>
      <link>https://dev.to/sarmitha/angular-defer-view-36g9</link>
      <guid>https://dev.to/sarmitha/angular-defer-view-36g9</guid>
      <description>&lt;p&gt;Deferrable views, also known as &lt;a class="mentioned-user" href="https://dev.to/defer"&gt;@defer&lt;/a&gt; blocks, reduce the initial bundle size of your application by deferring the loading of code that is not strictly necessary for the initial rendering of a page. This often results in a faster initial load and improvement in &lt;a href="https://web.dev/articles/vitals" rel="noopener noreferrer"&gt;Core Web Vitals (CWV)&lt;/a&gt;, primarily &lt;a href="https://web.dev/articles/lcp" rel="noopener noreferrer"&gt;Largest Contentful Paint (LCP)&lt;/a&gt; and &lt;a href="https://web.dev/articles/ttfb" rel="noopener noreferrer"&gt;Time to First Byte (TTFB)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With Angular's defer view, developers can wrap a section of a template within a &lt;a class="mentioned-user" href="https://dev.to/defer"&gt;@defer&lt;/a&gt; block to defer its loading:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@defer {
  &amp;lt;large-component /&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures that the code for any components, directives, or pipes inside the &lt;a class="mentioned-user" href="https://dev.to/defer"&gt;@defer&lt;/a&gt; block is split into a separate JavaScript file and loaded only when needed, allowing the main template to render more quickly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Features and Triggers of Angular Defer View
&lt;/h2&gt;

&lt;p&gt;Angular defer view provides various triggers, prefetching options, and sub-blocks to manage placeholders, loading states, and error handling effectively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Triggers&lt;/strong&gt;&lt;br&gt;
The rendering of deferred content can be controlled using specific triggers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;idle: Renders the view when the browser is idle (default behavior).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;immediate: Renders the view immediately after the main content is loaded.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;timer(...): Delays rendering by a specified time (e.g., on timer(500ms)).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;viewport(...): Renders the view when it becomes visible in the viewport.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;interaction(...): Renders the view after user interaction, such as a click or key press.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;hover(...): Renders the view when the user hovers over an element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;when(...): Renders the view based on a custom condition.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Managing Different Stages of Deferred Loading
&lt;/h2&gt;

&lt;p&gt;Angular's &lt;a class="mentioned-user" href="https://dev.to/defer"&gt;@defer&lt;/a&gt; blocks allow developers to define sub-blocks for different stages of content loading:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a class="mentioned-user" href="https://dev.to/placeholder"&gt;@placeholder&lt;/a&gt;: Displays content before the deferred section is triggered.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@defer {
  &amp;lt;large-component /&amp;gt;
} @placeholder {
  &amp;lt;p&amp;gt;Loading content...&amp;lt;/p&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;a class="mentioned-user" href="https://dev.to/loading"&gt;@loading&lt;/a&gt;: Shows content while deferred dependencies are loading
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@defer {
  &amp;lt;large-component /&amp;gt;
} @loading {
  &amp;lt;img alt="loading..." src="loading.gif" /&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;a class="mentioned-user" href="https://dev.to/error"&gt;@error&lt;/a&gt;: Provides fallback content if deferred loading fails
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@defer {
  &amp;lt;large-component /&amp;gt;
} @error {
  &amp;lt;p&amp;gt;Failed to load component.&amp;lt;/p&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Prefetching
&lt;/h2&gt;

&lt;p&gt;Angular allows prefetching to load deferred JavaScript files before the content is rendered. This can be specified using the prefetch option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@defer (on interaction; prefetch on idle) {
  &amp;lt;large-component /&amp;gt;
} @placeholder {
  &amp;lt;div&amp;gt;Placeholder content&amp;lt;/div&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best Practices for &lt;a class="mentioned-user" href="https://dev.to/defer"&gt;@defer&lt;/a&gt; Blocks
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Avoid Nested Loads:&lt;/strong&gt; Use different triggers for nested &lt;a class="mentioned-user" href="https://dev.to/defer"&gt;@defer&lt;/a&gt; blocks to prevent simultaneous loading spikes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Minimize Layout Shifts:&lt;/strong&gt; Ensure deferred components visible in the initial viewport do not disrupt layout stability.&lt;/p&gt;

&lt;p&gt;To dive deeper into Angular defer view, check out the &lt;a href="https://angular.dev/guide/templates/defer" rel="noopener noreferrer"&gt;official Angular guide&lt;/a&gt; and start streamlining your web applications today!&lt;/p&gt;

</description>
      <category>angular</category>
      <category>defer</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Implement Idle Timeout in Angular</title>
      <dc:creator>Sarmitha Krishnagobal</dc:creator>
      <pubDate>Wed, 21 Aug 2024 09:08:48 +0000</pubDate>
      <link>https://dev.to/sarmitha/implement-idle-timeout-in-angular-c94</link>
      <guid>https://dev.to/sarmitha/implement-idle-timeout-in-angular-c94</guid>
      <description>&lt;p&gt;In today's web applications, staying logged in on a website for an extended period, whether an hour or two, can pose security risks. During this time, background API calls might still be running, and if you're not actively using the site, someone else could potentially access it with your credentials. To mitigate these risks and reduce unnecessary server requests, it's crucial to monitor user activity, such as cursor movement or key presses. If the system detects inactivity for a specified duration, it can automatically log the user out. This feature, known as Idle Timeout, is essential for modern web applications.&lt;/p&gt;

&lt;p&gt;Let’s dive into implementing idle timeout in Angular.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;idleService.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable } from '@angular/core';
import { interval, Observable, Subject, Subscription, throttle } from 'rxjs';

export class IdleService {
  private idleSubject = new Subject&amp;lt;boolean&amp;gt;();
  private timeout = 1200; //seconds
  private lastActivity?: Date;
  private idleCheckInterval = 600; //seconds
  private idleSubscription?: Subscription

  constructor() { 
    this.resetTimer();
    this.startWatching();
  }

  get idleState(): Observable&amp;lt;boolean&amp;gt; {
    return this.idleSubject.asObservable();
  }

  private startWatching(): void {
    this.idleSubscription = interval(this.idleCheckInterval * 1000)
    .pipe(throttle(() =&amp;gt; interval(1000)))
    .subscribe(() =&amp;gt; {
      const now = new Date();

      if (now.getTime() - this.lastActivity?.getTime()! &amp;gt; this.timeout * 1000) {
        this.idleSubject.next(true);
      }
    });
  }

  resetTimer(): void {
    this.lastActivity = new Date();
    this.idleSubject.next(false);
  }

  stopWatching(): void {
    if (this.idleSubscription) {
      this.idleSubscription.unsubscribe();
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This service monitors user activity and checks if the user has been idle for a specified duration. If so, it triggers an event through &lt;strong&gt;idleSubject&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app.component.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { inject, OnDestroy, OnInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { IdleService } from 'path-to-your-idleService';

export class AppComponent implements OnInit, OnDestroy {
private idleSubscription?: Subscription;
private idleService = inject(IdleService); 

ngOnInit(): void {
   this.idleSubscription = this.idleService.idleState.subscribe((isIdle) =&amp;gt; {     
      if (isIdle) {
        console.log("user is idle");
        // Add logic for handling idle state, e.g., logging out
      } else {
       console.log("user is active");
     }
    });   
}

onUserAction(): void {
    this.idleService.resetTimer();
}

ngOnDestroy(): void {
    if (this.idleSubscription) {
      this.idleSubscription.unsubscribe();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Subscribes to the idle state and handles the user being idle or active. It also resets the idle timer on user interactions such as mouse movements, clicks, or key presses.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app.component.html&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div  
    (mousemove)="onUserAction()"
    (click)="onUserAction()"
    (keypress)="onUserAction()"
&amp;gt;
    &amp;lt;router-outlet /&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Captures user interactions to reset the idle timer whenever activity is detected.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>idletimeout</category>
    </item>
    <item>
      <title>Delaying Actions with setTimeout()⏳</title>
      <dc:creator>Sarmitha Krishnagobal</dc:creator>
      <pubDate>Sat, 23 Mar 2024 11:19:05 +0000</pubDate>
      <link>https://dev.to/sarmitha/delaying-actions-with-settimeout-48nb</link>
      <guid>https://dev.to/sarmitha/delaying-actions-with-settimeout-48nb</guid>
      <description>&lt;p&gt;&lt;em&gt;This is a submission for DEV Challenge v24.03.20, One Byte Explainer: Browser API or Feature.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Explainer
&lt;/h2&gt;

&lt;p&gt;The setTimeout() feature enables delaying JavaScript function execution for a specified interval. This asynchronous feature ensures smooth program operation while waiting, making it ideal for timed messages, animations, or data refreshing at intervals.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Context
&lt;/h2&gt;

&lt;p&gt;setTimeout() returns a timer ID that can be used to clear the timer if needed (using clearTimeout()).&lt;/p&gt;

</description>
      <category>frontendchallenge</category>
      <category>devchallenge</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
