Options
All
  • Public
  • Public/Protected
  • All
Menu

Class EStore<E>

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

Index

Constructors

constructor

  • Store constructor (Initialization with element is optional)

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

    example

    Dynamic EStore<Todo> Creation

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

    Parameters

    Returns EStore

Properties

Private _loading

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

Protected _query

_query: string = ""

The current query state.

Private _searching

_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

active: Map<string, E> = new Map()

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

config: StoreConfig

The configuration for the store.

Private Optional entities

entities: E[]

entries

entries: Map<string, E> = new Map()

Primary index for the stores elements.

idEntries

idEntries: Map<string, E> = new Map()

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

loadingError

loadingError: Observable<any>

Observable of errors occurred during a load request.

Protected notify

notify: ReplaySubject<E[]> = new ReplaySubject<E[]>(1)

Create notifications that broacast the entire set of entries.

Private notifyActive

notifyActive: ReplaySubject<Map<string, E>> = new ReplaySubject<Map<string, E>>(1)

Notifies observers when the store is empty.

Protected notifyDelta

notifyDelta: ReplaySubject<Delta<E>> = new ReplaySubject<Delta<E>>(1)

Create notifications that broacast store or slice delta state changes.

Protected notifyEmptyState

notifyEmptyState: ReplaySubject<E[]> = new ReplaySubject<E[]>(1)

Notifies observers when the store is empty.

Protected notifyEntryCount

notifyEntryCount: ReplaySubject<E[]> = new ReplaySubject<E[]>(1)

Notifies observers of changes to the number of entries in the store.

Private notifyLoading

notifyLoading: ReplaySubject<boolean> = new ReplaySubject<boolean>(1)

Notifies observers when the store is loading.

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

Protected notifyQuery

notifyQuery: ReplaySubject<string> = new ReplaySubject<string>(1)

Notifies observers of the store query.

Private notifySearching

notifySearching: ReplaySubject<boolean> = new ReplaySubject<boolean>(1)

Notifies observers that a search is in progress.

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

observable

observable: Observable<E[]> = this.observe()

An Observable<E[]> reference so that

searchError

searchError: Observable<any>

Observable of errors occurred during a search request.

Private slices

slices: Map<string, Slice<E>> = new Map()

Store slices

Accessors

GUID_KEY

  • get GUID_KEY(): string

ID_KEY

  • get ID_KEY(): string

loading

  • get loading(): boolean
  • set loading(loading: boolean): void
  • Sets the current loading state and notifies observers.

    Returns boolean

    A snapshot of the loading state.

  • Sets the current loading state and notifies observers.

    Parameters

    • loading: boolean

    Returns void

    A snapshot of the loading state.

query

  • get query(): string
  • set query(query: string): void

searching

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

    Returns boolean

    A snapshot of the searching state.

  • Sets the current searching state and notifies observers.

    Parameters

    • searching: boolean

    Returns void

    A snapshot of the searching state.

Methods

addActive

  • addActive(e: E): void
  • 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.

    example

    Add a todo1 and todo2 as active

    addActive(todo1);
    addActive(todo2);

    Parameters

    • e: E

    Returns void

addSlice

  • addSlice(p: Predicate<E>, label: string): void
  • Adds a slice to the store and keys it by the slices label.

    example

    Setup a Todo Slice for COMPLETE Todos

    source.addSlice(todo => todo.complete, TodoSlices.COMPLETE);

    Parameters

    Returns void

allSnapshot

  • allSnapshot(): E[]
  • Snapshot of all entries.

    example

    Observe a snapshot of all the entities in the store.

    let selectedTodos:Todo[] = source.allSnapshot();

    Returns E[]

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

clearActive

  • clearActive(): void
  • 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.

    example

    Mark a todo instance as active

    deleteActive(todo1);
    deleteActive(todo2);

    Returns void

contains

  • contains(target: E | string): boolean
  • Returns true if the entries contain the identified instance.

    example
    let contains:boolean = source.contains(guid);
    

    Parameters

    • target: E | string

      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.

containsById

  • containsById(target: E | string): boolean
  • Returns true if the entries contain the identified instance.

    example
    let contains:boolean = source.contains(guid);
    

    Parameters

    • target: E | string

      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.

count

  • count(p?: Predicate<E>): Observable<number>

countSnapshot

delete

  • delete(e: E): void
  • Delete (Update) the array of elements.

    example

    Delete todo1.

    store.delete(todo1]);

    Parameters

    • e: E

    Returns void

deleteA

  • deleteA(e: E[]): void
  • Delete N elements.

    example

    Put Todo instances.

    store.delete(todo1, todo2);

    Parameters

    • e: E[]

    Returns void

deleteActive

  • deleteActive(e: E): void
  • Delete an entity as 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.

    example

    Mark a todo instance as active

    deleteActive(todo1);
    deleteActive(todo2);

    Parameters

    • e: E

    Returns void

Private deleteIDEntry

  • deleteIDEntry(e: E): void
  • 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

deleteN

  • deleteN(...e: E[]): void
  • Delete N elements.

    example

    Put Todo instances.

    store.delete(todo1, todo2);

    Parameters

    • Rest ...e: E[]

    Returns void

deleteP

  • Delete elements by Predicate.

    example

    Put Todo instances.

    store.delete(todo1, todo2);

    Parameters

    Returns void

destroy

  • destroy(): void
  • Calls complete on all {@link BehaviorSubject} instances.

    Call destroy when disposing of the store.

    Returns void

equalsByGUID

  • equalsByGUID(e1: any, e2: any): boolean
  • Compare entities by GUID

    example

    Compare todo1 with todo2 by gid.

    if (equalsByGUID(todo1, todo2)){...};

    Parameters

    • e1: any

      The first entity

    • e2: any

      The second entity

    Returns boolean

    true if the two entities have equal GUID ids

equalsByID

  • equalsByID(e1: any, e2: any): boolean
  • Compare entities by ID

    example

    Compare todo1 with todo2 by id.

    if (equalsByID(todo1, todo2)){...};

    Parameters

    • e1: any

      The first entity

    • e2: any

      The second entity

    Returns boolean

    true if the two entities have equal ID ids

findOne

  • findOne(guid: string): E
  • Find and return the entity identified by the GUID parameter if it exists and return it.

    Parameters

    • guid: string

    Returns E

    The entity instance if it exists, null otherwise

findOneByID

  • findOneByID(id: string): E
  • Find and return the entity identified by the ID parameter if it exists and return it.

    Parameters

    • id: string

    Returns E

    The entity instance if it exists, null otherwise

getSlice

  • getSlice(label: string): Slice<E>
  • Get a slice

    example

    Get the TodoSlices.COMPLETE slice

    source.getSlice(TodoSlices.COMPLETE);

    Parameters

    • label: string

      The label identifying the slice

    Returns Slice<E>

isEmpty

  • isEmpty(): Observable<boolean>
  • Check whether the store is empty.

    example
    source.isEmpty();
    

    Returns Observable<boolean>

    A hot {@link Observable} that indicates whether the store is empty.

isEmptySnapshot

  • isEmptySnapshot(): boolean

Protected notifyAll

  • notifyAll(v: E[], delta: Delta<E>): void

observe

  • observe(sort?: (a: any, b: any) => number): Observable<E[]>
  • Observe store state changes.

    example
    let todos$ = source.observe();
    or with a sort function
    let todos$ = source.observe((a, b)=>(a.title > b.title ? -1 : 1));

    Parameters

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

      Optional sorting function yielding a sorted observable.

        • (a: any, b: any): number
        • Parameters

          • a: any
          • b: any

          Returns number

    Returns Observable<E[]>

observeActive

  • observeActive(): Observable<Map<string, E>>
  • Observe the active entity.

    example
    let active$ = source.observeActive();
    

    Returns Observable<Map<string, E>>

observeDelta

  • observeDelta(): Observable<Delta<E>>

observeLoading

  • observeLoading(): Observable<boolean>

observeLoadingComplete

  • observeLoadingComplete(): Observable<boolean>
  • Notfiies when loading has completed.

    Returns Observable<boolean>

observeQuery

  • observeQuery(): Observable<string>

observeSearching

  • observeSearching(): Observable<boolean>

observeSearchingComplete

  • observeSearchingComplete(): Observable<boolean>
  • Notfiies when searching has completed.

    Returns Observable<boolean>

post

  • post(e: E): void
  • Post (Add a new) element to the store.

    example

    Post a todo.

    store.post(todo);

    Parameters

    • e: E

    Returns void

postA

  • postA(e: E[]): void
  • Post (Add) an array of elements to the store.

    example

    Post a Todo array.

    store.post([todo1, todo2]);

    Parameters

    • e: E[]

    Returns void

postN

  • postN(...e: E[]): void
  • Post elements to the store.

    example

    Post two Todo instances.

    store.post(todo1, todo2);

    Parameters

    • Rest ...e: E[]

    Returns void

put

  • put(e: E): void
  • Put (Update) an element.

    example

    Put a Todo instance.

    store.put(todo1);

    Parameters

    • e: E

    Returns void

putA

  • putA(e: E[]): void
  • Put (Update) the array of elements.

    example

    Put Todo instances.

    store.put([todo1, todo2]);

    Parameters

    • e: E[]

    Returns void

putN

  • putN(...e: E[]): void
  • Put (Update) an element or add an element that was read from a persistence source and thus already has an assigned global id`.

    example

    Put Todo instances.

    store.put(todo1, todo2);

    Parameters

    • Rest ...e: E[]

    Returns void

removeSlice

  • removeSlice(label: string): void
  • Remove a slice

    example

    Remove the TodoSlices.COMPLETE Slice

    source.removeSlice(TodoSlices.COMPLETE);

    Parameters

    • label: string

      The label identifying the slice

    Returns void

reset

  • reset(): void
  • 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.

    example

    Reset the store.

    store.reset();

    Returns void

select

  • Snapshot of the entries that match the predicate.

    example

    Select all the Todo instance where the title length is greater than 100.

    let todos:Todo[]=store.select(todo=>todo.title.length>100);

    Parameters

    • p: Predicate<E>

      The predicate used to query for the selection.

    Returns E[]

    A snapshot array containing the entities that match the predicate.

toggle

  • toggle(e: E): void
  • Toggles the entity:

    If the store contains the entity it will be deleted. If the store does not contains the entity, it is added.

    example

    Toggle the Todo instance

    estore.post(todo);
    // Remove todo
    estore.toggle(todo);
    // Add it back
    estore.toggle(todo);
    

    Parameters

    • e: E

    Returns void

Private updateIDEntry

  • updateIDEntry(e: E): void
  • 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