Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@ngxs-contrib/emitter
Advanced tools
ER is a new pattern that provides the opportunity to feel free from actions
🚀 See it in action on Stackblitz
This package allows you to get rid of actions. You can use decorators to register actions directly in your state, you don't have to create any actions in your project (until you really need them), as they don't give any profit, only bring extra boilerplate files.
We've simplified this flow and threw out unnecessary mediators:
To install @ngxs-contrib/emitter
run the following command:
npm install @ngxs-contrib/emitter
# or if you use yarn
yarn add @ngxs-contrib/emitter
Import the module into your root application module:
import { NgModule } from '@angular/core';
import { NgxsEmitPluginModule } from '@ngxs-contrib/emitter';
@NgModule({
imports: [
...,
NgxsEmitPluginModule.forRoot()
]
})
export class AppModule {}
Receiver is a basic building block. @Receiver()
is a function that allows you to decorate static methods in your states for further passing this method to the emitter:
import { State, StateContext } from '@ngxs/store';
import { Receiver, EmitterAction } from '@ngxs-contrib/emitter';
export interface CounterStateModel {
value: number;
}
@State<CounterStateModel>({
name: 'counter',
defaults: {
value: 0
}
})
export class CounterState {
@Receiver()
public static setValue({ setState }: StateContext<CounterStateModel>, { payload }: EmitterAction<number>) {
setState({
value: payload
});
}
}
Emitter is basically a bridge between your component and receivers. @Emitter()
is a function that decorates properties defining new getter and gives you an access to the emittable interface:
import { Select } from '@ngxs/store';
import { Emitter, Emittable } from '@ngxs-contrib/emitter';
import { CounterStateModel, CounterState } from './counter.state';
@Component({
selector: 'app-counter',
template: `
<ng-container *ngIf="count$ | async as count">
<h3>Count is {{ count.value }}</h3>
<div>
<button (click)="counterValue.emit(count.value + 1)">Increment (+1)</button>
<button (click)="counterValue.emit(count.value - 1)">Decrement (-1)</button>
</div>
</ng-container>
`
})
export class CounterComponent {
@Select(CounterState)
public count$: Observable<CounterStateModel>;
// Use in components to emit asynchronously payload
@Emitter(CounterState.setValue)
public counterValue: Emittable<number>;
}
You can define custom types for debbuing purposes (works with @ngxs/logger-plugin
):
import { State, StateContext } from '@ngxs/store';
import { Receiver } from '@ngxs-contrib/emitter';
@State<number>({
name: 'counter',
defaults: 0
})
export class CounterState {
@Receiver({
type: '[Counter] Increment value'
})
public static increment({ setState, getState }: StateContext<number>) {
setState(getState() + 1);
}
@Receiver({
type: '[Counter] Decrement value'
})
public static decrement({ setState, getState }: StateContext<number>) {
setState(getState() - 1);
}
}
If you still need actions - it is possible to pass an action as an argument into @Receiver()
decorator:
import { State, StateContext } from '@ngxs/store';
import { Receiver } from '@ngxs-contrib/emitter';
export class Increment {
public static type = '[Counter] Increment value';
}
export class Decrement {
public static type = '[Counter] Decrement value';
}
@State<number>({
name: 'counter',
defaults: 0
})
export class CounterState {
@Receiver({
action: Increment
})
public static increment({ setState, getState }: StateContext<number>) {
setState(getState() + 1);
}
@Receiver({
action: Decrement
})
public static decrement({ setState, getState }: StateContext<number>) {
setState(getState() - 1);
}
}
Assume you have to make some API request and load some data from your server, it is very easy to use services with static methods, Angular provides an Injector
class for getting instances by reference:
import { Injector } from '@angular/core';
import { State, StateContext } from '@ngxs/store';
import { Receiver } from '@ngxs-contrib/emitter';
interface Todo {
userId: number;
id: number;
title: string;
completed: boolean;
}
@State<Todo[]>({
name: 'counter',
defaults: []
})
export class TodosState {
// ApiService is a class that is defined somewhere...
public static api: ApiService;
constructor(injector: Injector) {
TodosState.api = injector.get<ApiService>(ApiService);
}
@Receiver()
public static getTodos({ setState }: StateContext<Todo[]>) {
// If `ApiService.prototype.getTodos` returns an Observable - just use `tap` operator
return this.api.getTodos().pipe(
tap((todos) => setState(todos))
);
// If `ApiService.prototype.getTodos` returns a Promise - just use `then`
return this.api.getTodos().then((todos) => setState(todos));
}
}
FAQs
Unknown package
The npm package @ngxs-contrib/emitter receives a total of 1 weekly downloads. As such, @ngxs-contrib/emitter popularity was classified as not popular.
We found that @ngxs-contrib/emitter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.