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

export const enum TodoSliceEnum {
COMPLETE = "Complete",
INCOMPLETE = "Incomplete"
}
export class Todo {
constructor(
public complete: boolean,
public title: string,
public gid?:string,
public id?:string) {}
}

export let todos = [new Todo(false, "You complete me!"), new Todo(true, "You completed me!")];

export function todosFactory():Todo[] {
return [new Todo(false, "You complete me!"), new Todo(true, "You completed me!")];
}

Type Parameters

  • E

Hierarchy

Constructors

  • Store constructor (Initialization with element is optional)

    perform initial notification to all observers, such that functions like combineLatest{} will execute at least once.

    Type Parameters

    • E

    Parameters

    • entities: E[] = []

      The entities to initialize the store with.

    • Optional config: StoreConfig

      The optional configuration instance.

    Returns EStore<E>

    Example: EStore<Todo> Creation

    // Initialize the Store
    let store: EStore<Todo> = new EStore<Todo>(todosFactory());

Properties

_loading: boolean = true

The 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.

_query: string = ''

The current query state.

_searching: boolean = false

The current searching state. Use searching for example to display a spinnner when performing a search. The default searching state is false.

active: Map<string, E> = ...

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.

config: StoreConfig

The configuration for the store.

entries: Map<string, E> = ...

Primary index for the stores elements.

idEntries: Map<string, E> = ...

The element entries that are keyed by an id generated on the server.

loadingError: Observable<any>

Observable of errors occurred during a load request.

The error Observable should be created by the client.

notify: ReplaySubject<E[]> = ...

Create notifications that broacast the entire set of entries.

notifyActive: ReplaySubject<Map<string, E>> = ...

Notifies observers when the store is empty.

notifyDelta: ReplaySubject<Delta<E>> = ...

Create notifications that broacast store or slice delta state changes.

notifyLoading: ReplaySubject<boolean> = ...

Notifies observers when the store is loading.

This is a common pattern found when implementing Observable data sources.

notifyQuery: ReplaySubject<string> = ...

Notifies observers of the store query.

notifySearching: ReplaySubject<boolean> = ...

Notifies observers that a search is in progress.

This is a common pattern found when implementing Observable data sources.

obs: Observable<E[]> = ...

An Observable<E[]> reference to the entities in the store or Slice instance.

searchError: Observable<any>

Observable of errors occurred during a search request.

The error Observable should be created by the client.

slices: Map<string, Slice<E>> = ...

Store slices

Accessors

  • get GUID_KEY(): string
  • The current guid key for the EStore instance.

    Returns string

    this.config.guidKey;

  • get ID_KEY(): string
  • The current id key for the EStore instance.

    Returns string

    this.config.idKey;

  • get loading(): boolean
  • Returns boolean

    A snapshot of the loading state.

    Example: Create a reference to the loading state

    const loading:boolean = todoStore.loading;
    
  • set loading(loading): void
  • Sets the current loading state and notifies observers.

    Parameters

    • loading: boolean

    Returns void

  • get query(): string
  • Returns string

    A snapshot of the query state.

  • set query(query): void
  • Sets the current query state and notifies observers.

    Parameters

    • query: string

    Returns void

  • get searching(): boolean
  • Returns boolean

    A snapshot of the searching state.

  • set searching(searching): void
  • Sets the current searching state and notifies observers.

    Parameters

    • searching: boolean

    Returns void

Methods

  • Observe the active entity.

    Returns E[]

    Example

    <pre>
    let active$ = source.activeSnapshot();
    </pre>
  • 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.

    Parameters

    • e: E

    Returns void

    Example: Add todo1 and todo2 as active

    addActive(todo1);
    addActive(todo2);
  • Adds a slice to the store and keys it by the slices label.

    Parameters

    Returns void

    Example: Setup a Todo Slice for COMPLETE Todos

    source.addSlice(todo => todo.complete, TodoSlices.COMPLETE);
    
  • Snapshot of all entries.

    Returns E[]

    Snapshot array of all the elements the entities the store contains.

    Example: Observe a snapshot of all the entities in the store.

    let selectedTodos:Todo[] = source.allSnapshot();
    
  • 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.

    Returns void

    Example: Clear active todo instances

    store.clearActive();
    
  • Returns true if the entries contain the identified instance.

    Parameters

    • target: string | E

      Either an instance of type E or a guid identifying the instance.

    Returns boolean

    true if the instance identified by the guid exists, false otherwise.

    Example

    <pre>
    let contains:boolean = source.contains(guid);
    </pre>
  • Returns true if the entries contain the identified instance.

    Parameters

    • target: string | E

      Either an instance of type E or a id identifying the instance.

    Returns boolean

    true if the instance identified by the id exists, false otherwise.

    Example

    <pre>
    let contains:boolean = source.contains(guid);
    </pre>
  • Returns the number of entries contained.

    Parameters

    • Optional p: Predicate<E>

      The predicate to apply in order to filter the count

    Returns Observable<number>

  • Delete (Update) the array of elements.

    Parameters

    • e: E | E[]

    Returns void

    Example: Delete todo1.

    store.delete(todo1]);
    
  • Delete an array of elements.

    Parameters

    • e: E[]

      The array of instances to be deleted

    Returns void

    Example: Delete the array of Todo instances.

    store.deleteA([todo1, todo2]);
    
  • 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.

    Parameters

    • e: E

    Returns void

    Example: Remove todo1 and todo2 as active entities

    deleteActive(todo1);
    deleteActive(todo2);
  • If the entity has the id key initialized with a value, then also delete the entity to the idEntries.

    Parameters

    • e: E

      The element to be added to the idEntries.

    Returns void

  • Delete N elements.

    Parameters

    • Rest ...e: E[]

    Returns void

    Example: Delete N Todo instance argument.

    store.deleteN(todo1, todo2);
    
  • Delete elements by Predicate.

    Parameters

    Returns void

    Example: Delete the Todo instances.

    store.delete(todo1, todo2);
    
  • Calls complete on all EStore ReplaySubject instances.

    Call destroy when disposing of the store.

    Returns void

  • Compare entities by GUID

    Parameters

    • e1: any

      The first entity

    • e2: any

      The second entity

    Returns boolean

    true if the two entities have equal GUID ids

    Example: Compare todo1 with todo2 by gid.

    if (equalsByGUID(todo1, todo2)){...};
    
  • Compare entities by ID

    Parameters

    • e1: any

      The first entity

    • e2: any

      The second entity

    Returns boolean

    true if the two entities have equal ID ids

    Example: Compare todo1 with todo2 by id.

    if (equalsByID(todo1, todo2)){...};
    
  • Find and return the entity identified by the GUID parameter if it exists and return it.

    Parameters

    • guid: string

    Returns undefined | E

    The entity instance if it exists, null otherwise

  • Find and return the entity identified by the ID parameter if it exists and return it.

    Parameters

    • id: string

    Returns undefined | E

    The entity instance if it exists, null otherwise

  • Get a slice

    Parameters

    • label: string

      The label identifying the slice

    Returns undefined | Slice<E>

    The Slice instance or undefined

    Example: Get the TodoSlices.COMPLETE slice

    source.getSlice(TodoSlices.COMPLETE);
    
  • Check whether the store is empty.

    Returns Observable<boolean>

    A hot Observable that indicates whether the store is empty.

    Example

    const empty$:Observable<boolean> = source.isEmpty();
    
  • Check whether the store is empty.

    Returns boolean

    A snapshot that indicates whether the store is empty.

    Example

    const empty:boolean = source.isEmptySnapshot();
    
  • Observe store state changes.

    Parameters

    • Optional sort: ((a, b) => number)

      Optional sorting function yielding a sorted observable.

        • (a, b): number
        • Parameters

          • a: any
          • b: any

          Returns number

    Returns Observable<E[]>

    Example

    let todos$ = source.observe();
    //or with a sort by title function
    let todos$ = source.observe((a, b)=>(a.title > b.title ? -1 : 1));
  • Observe the active entities.

    Returns Observable<Map<string, E>>

    Example

    let active$ = store.observeActive();
    
  • 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;

    Returns Observable<boolean>

  • Notfiies when loading has completed.

    Returns Observable<boolean>

  • Observe searching.

    Returns Observable<boolean>

    Example

    <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.
  • Notfiies when searching has completed.

    Returns Observable<boolean>

  • Post (Add a new) element(s) to the store.

    Parameters

    • e: E | E[]

      An indiidual entity or an array of entities

    Returns void

    Example: Post a Todo instance.

    store.post(todo);
    
  • Post (Add) an array of elements to the store.

    Parameters

    • e: E[]

    Returns void

    Example: Post a Todo array.

    store.post([todo1, todo2]);
    
  • Post N entities to the store.

    Parameters

    • Rest ...e: E[]

    Returns void

    Example: Post two Todo instances.

    store.post(todo1, todo2);
    
  • Put (Update) an entity.

    Parameters

    • e: E | E[]

    Returns void

    Example: Put a Todo instance.

    store.put(todo1);
    
  • Put (Update) the array of enntities.

    Parameters

    • e: E[]

      The array of enntities to update

    Returns void

    Example: Put an array of Todo instances.

    store.put([todo1, todo2]);
    
  • Put (Update) an element or add an element that was read from a persistence source and thus already has an assigned global id`.

    Parameters

    • Rest ...e: E[]

      The enetity instances to update.

    Returns void

    Example: Put N Todo instances.

    store.put(todo1, todo2);
    
  • Remove a slice

    Parameters

    • label: string

      The label identifying the slice

    Returns void

    Example: Remove the TodoSlices.COMPLETE Slice

    source.removeSlice(TodoSlices.COMPLETE);
    
  • 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.

    Returns void

    Example: Reset the store.

    store.reset();
    
  • Snapshot of the entries that match the predicate.

    Parameters

    • p: Predicate<E>

      The predicate used to query for the selection.

    Returns E[]

    A snapshot array containing the entities that match the predicate.

    Example: Select all the Todo instances where the title length is greater than 100.

    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.

    Parameters

    • e: E

      The entity to toggle

    Returns void

    Example: Toggle the Todo instance

    estore.post(todo);
    // Remove todo
    estore.toggle(todo);
    // Add it back
    estore.toggle(todo);
  • If the entity has the id key initialized with a value, then also add the entity to the idEntries.

    Parameters

    • e: E

      The element to be added to the idEntries.

    Returns void

Generated using TypeDoc