Store constructor (Initialization with element is optional)
perform initial notification to all observers, such that functions like combineLatest{} will execute at least once.
The entities to initialize the store with.
Optional
config: StoreConfigThe optional configuration instance.
// Initialize the Store
let store: EStore<Todo> = new EStore<Todo>(todosFactory());
Private
_loadingThe current loading state. Use loading when fetching new
data for the store. The default loading state is true
.
This is such that if data is fetched asynchronously in a service, components can wait on loading notification before attempting to retrieve data from the service.
Loading could be based on a composite response. For example
when the stock and mutual funds have loaded, set loading to false
.
Protected
_queryThe current query state.
Private
_searchingThe current searching
state. Use searching
for example to display a spinnner
when performing a search.
The default searching
state is false
.
Map
of active entties. The instance is public and can be used
directly to add and remove active entities, however we recommend
using the addActive and deleteActive methods.
The configuration for the store.
Primary index for the stores elements.
The element entries that are keyed by an id generated on the server.
Observable of errors occurred during a load request.
The error Observable should be created by the client.
Protected
notifyCreate notifications that broacast the entire set of entries.
Private
notifyNotifies observers when the store is empty.
Protected
notifyCreate notifications that broacast store or slice delta state changes.
Private
notifyNotifies observers when the store is loading.
This is a common pattern found when implementing
Observable
data sources.
Protected
notifyNotifies observers of the store query.
Private
notifyNotifies observers that a search is in progress.
This is a common pattern found when implementing
Observable
data sources.
An Observable<E[]> reference to the entities in the store or Slice instance.
Observable of errors occurred during a search request.
The error Observable should be created by the client.
Private
slicesStore slices
The current guid key for the EStore instance.
this.config.guidKey;
The current id key for the EStore instance.
this.config.idKey;
A snapshot of the loading state.
const loading:boolean = todoStore.loading;
Sets the current loading state and notifies observers.
A snapshot of the query state.
Sets the current query state and notifies observers.
A snapshot of the searching state.
Sets the current searching state and notifies observers.
Add multiple entity entities to active.
If the entity is not contained in the store it is added
to the store before it is added to active
.
Also we clone the map prior to broadcasting it with
notifyActive
to make sure we will trigger Angular
change detection in the event that it maintains
a reference to the active
state Map
instance.
addActive(todo1);
addActive(todo2);
Adds a slice to the store and keys it by the slices label.
source.addSlice(todo => todo.complete, TodoSlices.COMPLETE);
Clear / reset the active entity map.
Also we clone the map prior to broadcasting it with
notifyActive
to make sure we will trigger Angular
change detection in the event that it maintains
a reference to the active
state Map
instance.
store.clearActive();
Returns true if the entries contain the identified instance.
Either an instance of type E
or a guid
identifying the instance.
true if the instance identified by the guid exists, false otherwise.
<pre>
let contains:boolean = source.contains(guid);
</pre>
Returns true if the entries contain the identified instance.
Either an instance of type E
or a id
identifying the instance.
true if the instance identified by the id
exists, false otherwise.
<pre>
let contains:boolean = source.contains(guid);
</pre>
Returns the number of entries contained.
Optional
p: Predicate<E>The predicate to apply in order to filter the count
Returns a snapshot of the number of entries contained in the store.
Optional
p: Predicate<E>The predicate to apply in order to filter the count
Delete an active entity.
Also we clone the map prior to broadcasting it with
notifyActive
to make sure we will trigger Angular
change detection in the event that it maintains
a reference to the active
state Map
instance.
deleteActive(todo1);
deleteActive(todo2);
Private
deleteIDEntryProtected
notifyCall all the notifiers at once.
Observe store state changes.
Optional
sort: ((a, b) => number)Optional sorting function yielding a sorted observable.
let todos$ = source.observe();
//or with a sort by title function
let todos$ = source.observe((a, b)=>(a.title > b.title ? -1 : 1));
Observe loading.
Note that this obverable piped through `takeWhile(v->v, true), such that it will complete after each emission.
See: https://fireflysemantics.medium.com/waiting-on-estore-to-load-8dcbe161613c
For more details. Also note that v=>v is the same as v=>v!=false
@example
const observeLoadingHandler: Observer<boolean> = {
complete: () => {
console.log(`Data Loaded and Observable Marked as Complete`);
}, // completeHandler
error: () => {
console.log(`Any Errors?`);
}, // errorHandler
next: (l) => {
console.log(`Data loaded and loading is ${l}`);
},
};
const observeLoadingResubscribeHandler: Observer<boolean> = {
complete: () => {
console.log(`Data Loaded and Resubscribe Observable Marked as Complete`);
}, // completeHandler
error: () => {
console.log(`Any Resubscribe Errors?`);
}, // errorHandler
next: (l) => {
console.log(`Data loaded and resusbscribe loading value is ${l}`);
},
};
const todoStore: EStore<Todo> = new EStore();
//============================================
// Loading is true by default
//============================================
console.log(`The initial value of loading is ${todoStore.loading}`);
//============================================
// Observe Loading
//============================================
let loading$: Observable<boolean> = todoStore.observeLoading();
loading$.subscribe((l) => console.log(`The value of loading is ${l}`));
todoStore.loading = false;
loading$.subscribe(observeLoadingHandler);
//============================================
// The subscription no longer fires
//============================================
todoStore.loading = true;
todoStore.loading = false;
//============================================
// The subscription no longer fires,
// so if we want to observe loading again
// resusbscribe.
//============================================
todoStore.loading = true;
loading$ = todoStore.observeLoading();
loading$.subscribe(observeLoadingResubscribeHandler);
todoStore.loading = false;
Observe searching.
<pre>
let searching$ = source.observeSearching();
</pre>
Note that this obverable piped through
`takeWhile(v->v, true), such that it will
complete after each emission.
See:
https://medium.com/@ole.ersoy/waiting-on-estore-to-load-8dcbe161613c
For more details.
Resets the store and all contained slice instances to empty. Also perform delta notification that sends all current store entries. The ActionType.RESET code is sent with the delta notification. Slices send their own delta notification.
store.reset();
Snapshot of the entries that match the predicate.
The predicate used to query for the selection.
A snapshot array containing the entities that match the predicate.
let todos:Todo[]=store.select(todo=>todo.title.length>100);
Toggles the entity:
If the store contains the entity it will be deleted. If the store does not contains the entity, it is added.
The entity to toggle
estore.post(todo);
// Remove todo
estore.toggle(todo);
// Add it back
estore.toggle(todo);
Private
updateIDEntryGenerated using TypeDoc
This
todoFactory
code will be used to illustrate the API examples. The following utilities are used in the tests and the API Typedoc examples contained here.Example: Utilities for API Examples