ngx-wcore v1.3.1
Wcore
This library allows for easy data store and event management.
install
npm i ngx-wcore
Configuration
First import the CoreModule into your app.module.ts
file
imports: [
CoreModule
],
Then create your custom WorkerService to your specifications. Here a minimal example :
import {Injectable} from '@angular/core';
import {WorkerDataClass, WorkerServicePrototype} from 'ngx-wcore';
import {RootDataClass} from '../Models/root-data-class';
@Injectable({
providedIn: 'root'
})
@WorkerDataClass(new RootDataClass()) // VERY important
export class WorkerService extends WorkerServicePrototype { // VERY important
constructor() {
super();
}
data(): RootDataClass { // VERY important
return super.data() as RootDataClass;
}
}
The Root DataClass can be whatever Object you want it to be :).
Once your Service is up and running you can configure the CoreService and set up your events.
Here a small example:
import {Component} from '@angular/core';
import {WorkerService} from './services/worker.service';
import {TestService} from './services/test.service';
import {SystemActions, WCoreService} from 'ngx-wcore';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(
public w: WorkerService,
private core: WCoreService
) {
this.core.initCore([
{
name: 'TestService', // your service ID -> only used for debugging purposes. you can put any kind of string there
service: TestService, // the service class. Must extend AbstractService
initOn: SystemActions.init
// and here the Event that should start the Service.
// By default it is the init event right at the startup of the app
}
],
this.w);
LOG.debug.log('DATASET', this.w.data()); // Here you can access all your data from the store
LOG.debug.log('available services', this.w.availableServices); // and here you can checkout all your available services
}
}
You are now ready to use the service however you like :)
Create custom Events
Here an example of how to create a custom Event class
import {ActionPrototype} from 'ngx-wcore';
export class A<T, K> extends ActionPrototype<T, K> {
// Here you can add as many events as you like with any values but they must be static and readonly
static readonly localEvent = new A<number, Error>('custom Event');
static readonly eventA = new A<number, Error>('custom Event');
static readonly eventB = new A<number, Error>('custom Event');
constructor(
description: string,
protected readonly payload: T = null,
public readonly error: K = null
) {
super(description, payload, error);
this.init(this, A);
}
}
An action takes 3 kinds of parameters
- the payload : What type of data is associated to the event
- the error : What error does the event trigger
- the description : What does the event do -> For debugging purposes
Use the WorkerService
Anf here multiple examples of how to use the workerservice
With actions
Trigger actions with payload or without
this.w.trigger(A.eventA, 8);
this.w.trigger(A.eventB);
Subscribe to actions and use their payload. Also access the datasStore
this.w.on(A.eventA).pipe(
tap((payload) => {
this.w.data().test.counterA = payload;
})).subscribe();
10 months ago
10 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago