IEnumerable "<"invention">" invention = from i in DataContext.invention where i.Sharable == true select i
Wednesday, 24 November 2021
Automatically Unsubscribe in Angular Component
Sunday, 21 November 2021
Are observables synchronous or asynchronous?
1.Synchronous observable:
Observables are lazy Push collections of multiple values.
In our async operator we've used setTimeout
, this setTimeout
will assure that the main thread will not get blocked because setTimeout
is a browser API and our code will be moved to the event loop.
Sunday, 14 November 2021
Cut-Paste or Copy-Paste not working on Ubuntu 20.04 on using right-click context menu
set ubuntu new drive permissions
Cut-Paste or Copy-Paste not working on Ubuntu 20.04 on using right-click context menu ?
sudo chown -v $USER:$USER /media/hp/e7582b74-0571-4a0d-b36f-e1ec8cb67c6f
Sunday, 11 July 2021
rxjs takeUntil and takeWhile Example
https://stackblitz.com/edit/rxjs-pranay-example?file=index.ts
takeUntil
My favorite way of unsubscribing from streams is takeUntil
. This operator will allow you to unsubscribe a stream when the stream passed as input to the operator emits an event. Oh, that seems too complicated, but it’s actually not.
Let’s see an example:
- we have two observables that emit with an interval of respectively 1000 ms (1 second) and 100 ms
- the stream that emits every 100ms will unsubscribe when the other stream emits, which will happen every 1 second
takeWhile
This operator is very useful to unsubscribe streams based on their own value. One of the ways I needed to use this operator is to stop certain timers once they reach a certain number of iterations. For instance, a countdown timer.
In the following example, I want to stop a timer once it iterates for 5 times. —
- the
takeWhile
operator accepts a predicate function whose parameter is the current value of the stream - if the predicate is truthy, it will keep emitting values; if it’s falsy, then it will unsubscribe the stream
What are Subjects?
Subject is both an observable and observer.
- Observer — it has the next, error, and complete methods.
- Observable — it has all the observable operators, and you can subscribe to him.
A subject can act as a bridge/proxy between the source observable and many observers, making it possible for multiple observers to share the same observable execution.
remember that a subject is also an observer, and what observers can do? They can listen to observables with the next()
, error()
and complete()
methods.
Monday, 5 July 2021
create margin and padding helpers class using scss
Friday, 18 June 2021
What are the different types of binding available in Angular ?
<img [src]="ImageUrl">
<button (click)="onUpdate($event)">Save</button>
<input [(ngModel)]="name">
<button [attr.aria-label]="help">help</button>
<span [class.specialClass]="isSpecialClass">Special class</span>
<button [style.color]="isSpecialClass ? 'blue' : 'black'">Click Me</button>
What is Angular DSL?
A domain-specific language (DSL) is a computer language specialized to a particular application domain. Angular has its own Domain Specific Language (DSL) which allows us to write Angular specific html-like syntax on top of normal html. It has its own compiler that compiles this syntax to html that the browser can understand. This DSL is defined in NgModules such as animations, forms, and routing and navigation.
Basically you will see 3 main syntax in Angular DSL.
(): Used for Output and DOM events.
[]: Used for Input and specific DOM element attributes.
*: Structural directives(*ngFor or *ngIf) will affect/change the DOM structure.
Wednesday, 16 June 2021
Apply vs. Call vs. Bind javascript example
Apply vs Call vs Bind Examples
Call
Apply
Bind
for await of vs promise.all in javascript
Sunday, 16 May 2021
useReducer in React
const [state, dispatch] = useReducer(reducer, initialArg, init);
An alternative to useState
. Accepts a reducer of type (state, action) => newState
, and returns the current state paired with a dispatch
method. (If you’re familiar with Redux, you already know how this works.)
useReducer
is usually preferable to useState
when you have complex state logic that involves multiple sub-values or when the next state depends on the previous one. useReducer
also lets you optimize performance for components that trigger deep updates because you can pass dispatch
down instead of callbacks.
Here’s the counter example from the useState
section, rewritten to use a reducer:
Specifying the initial state
There are two different ways to initialize useReducer
state. You may choose either one depending on the use case. The simplest way is to pass the initial state as a second argument:
const [state, dispatch] = useReducer(
reducer,
{count: initialCount} );
Lazy initialization
You can also create the initial state lazily. To do this, you can pass an init
function as the third argument. The initial state will be set to init(initialArg)
.
It lets you extract the logic for calculating the initial state outside the reducer. This is also handy for resetting the state later in response to an action:
function init(initialCount) { return {count: initialCount};}
function reducer(state, action) {
switch (action.type) {
case 'increment':
return {count: state.count + 1};
case 'decrement':
return {count: state.count - 1};
case 'reset': return init(action.payload); default:
throw new Error();
}
}
function Counter({initialCount}) {
const [state, dispatch] = useReducer(reducer, initialCount, init); return (
<>
Count: {state.count}
<button
onClick={() => dispatch({type: 'reset', payload: initialCount})}> Reset
</button>
<button onClick={() => dispatch({type: 'decrement'})}>-</button>
<button onClick={() => dispatch({type: 'increment'})}>+</button>
</>
);
}
- Using useMemo( ) -
It is a React hook that is used for caching CPU-Expensive functions.
Sometimes in a React app, a CPU-Expensive function gets called repeatedly due to re-renders of a component, which can lead to slow rendering.
useMemo( ) hook can be used to cache such functions. By using useMemo( ), the CPU-Expensive function gets called only when it is needed.
- Using React.PureComponent -
It is a base component class that checks state and props of a component to know whether the component should be updated.
Instead of using the simple React.Component, we can use React.PureComponent to reduce the re-renders of a component unnecessarily.
- Maintaining State Colocation -
This is a process of moving the state as close to where you need it as possible.
Sometimes in React app, we have a lot of unnecessary states inside the parent component which makes the code less readable and harder to maintain. Not to forget, having many states inside a single component leads to unnecessary re-renders for the component.
It is better to shift states which are less valuable to the parent component, to a separate component.
- Lazy Loading -
It is a technique used to reduce the load time of a React app. Lazy loading helps reduce the risk of web app performances to minimal.
It is a React hook that is used for caching CPU-Expensive functions.
Sometimes in a React app, a CPU-Expensive function gets called repeatedly due to re-renders of a component, which can lead to slow rendering.
useMemo( ) hook can be used to cache such functions. By using useMemo( ), the CPU-Expensive function gets called only when it is needed.
It is a base component class that checks state and props of a component to know whether the component should be updated.
Instead of using the simple React.Component, we can use React.PureComponent to reduce the re-renders of a component unnecessarily.
This is a process of moving the state as close to where you need it as possible.
Sometimes in React app, we have a lot of unnecessary states inside the parent component which makes the code less readable and harder to maintain. Not to forget, having many states inside a single component leads to unnecessary re-renders for the component.
It is better to shift states which are less valuable to the parent component, to a separate component.
It is a technique used to reduce the load time of a React app. Lazy loading helps reduce the risk of web app performances to minimal.