Observing the Currently Active Entities with Slice | Task

Ole Ersoy
Feb - 11  -  2 min

Scenario

We have created an entity store ( EStore<Todo> ) for Todo entities, and we want to monitor which Todo entities are currently active using RxJS Observable<Map<Todo>>.

Approach

Model

Our Todo class:

export class Todo {
    constructor(
        public complete: boolean, 
        public title: string,
        public  gid?:string) {}
}

Import EStore and create an EStore instance.

import { EStore } from '@fireflysemantics/slice';
import { Observable } from 'rxjs';

const es = new EStore<Todo>();

Post two Todo instances to the store and also set them as active.

const todo1 = new Todo(false, "Finish it!");
const todo2 = new Todo(false, "Just do it!");

es.post(todo1);
es.post(todo2);
es.addActive(todo1);
es.addActive(todo2);

const active1 = es.active.get(todo1.gid);
//console.log(active1);

let active$: Observable<Map<string, Todo>> = es.observeActive();
active$.subscribe(m => m.forEach(a => console.log("ACTIVE TODO: ", a)));

We see that the console logs the active entities:

After deleting an entity:

console.log("DELETING ACTIVE ENTITY TODO1");
es.deleteActive(todo1);

The console logs the remaining active entity ( todo2 ).

Demo