Monitoring the Currently Active Entities with Slice

Ole Ersoy
1 min readDec 29, 2018

--

Photo by Aman Bhargava on Unsplash

Updated Version

There is an updated version of this article here:

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

We first import EStore and create a EStore<Todo> instance.

import { EStore } from '@fireflysemantics/slice';
import { Observable } from 'rxjs';
export class Todo {
constructor(
public complete: boolean,
public title: string,
public gid?:string) {}
}

Next we post a Todo instance to the store and also set it as the currently active instance.

const es = new EStore<Todo>();
const todo = new Todo(false, "Finish it!");
es.post(todo);
es.addActive(todo);
//Get the active todo from the store active Map reference
const active = es.active.get(todo.gid);
console.log(active);
//Observe a clone of the active Map
//The EStore implementation clones
//the active Map in order to trigger
//Angular change detection
let monitor:Observable<Map<string, Todo>> = es.observeActive();
monitor.subscribe(m=>console.log(m.get(todo.gid)));

We see that the console logs the active entity:

Todo {complete: false, title: "Finish it!", gid: "974edf40-0b18-11e9-a633-59308f3924d7"}

Demo

--

--