Class ObservableList<I, P>Abstract

Description

This class inherits from List and implements the mobx observer interface nested over all object properties

Features:

  • observed over all objects and properties and all nested objects in collection

Example

import * as collection from '@biorate/collection';
const { embed, observable } = collection;

class Item extends collection.ObservableItem {
@observable() @embed(Item.Int) public id: number = null;
@observable() @embed(Item.String) public text: string = null;
}

class List extends collection.ObservableList<Item> {
protected get _keys() {
return [['id']];
}

protected get _Item() {
return Item;
}
}

const list = new List();
list.subscribe((data) => {
console.log(data);
// Called 3 times with data values:
// {
// type: 'add',
// object: ObservableSet {...},
// newValue: Item { id: [Getter/Setter], text: [Getter/Setter] }
// }
// {
// type: 'update',
// object: Item { id: [Getter/Setter], text: [Getter/Setter] },
// oldValue: 'hello!',
// name: 'text',
// newValue: 'hello world!'
// }
// {
// type: 'delete',
// object: ObservableSet {...},
// oldValue: Item { id: [Getter/Setter], text: [Getter/Setter] }
// }
});
list.set({ id: 1, text: 'hello!' });
list.find(1).text = 'hello world!';
list.delete(1);

Type Parameters

  • I = any

  • P = {
        parent?: any;
    }

Hierarchy

  • List<I, P>
    • ObservableList

Constructors

  • Type Parameters

    • I = any

    • P = {
          parent?: any;
      }

    Parameters

    • items: any[] = []

      initialization items

    • parent: P = null

      parent item

    Returns ObservableList<I, P>

    Example

    import * as collection from '@biorate/collection';

    class List extends collection.ObservableList<{ id: number }> {
    protected get _keys() {
    return [['id']];
    }
    }

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);

Properties

[_callbacks]: Set<((...args) => void)> = ...

Type declaration

    • (...args): void
    • Parameters

      • Rest ...args: any[]
        Rest

      Returns void

Description

Callbacks storage

[_map]: Map<string, I> = ...

Description

Key combinations storage

[_processed]: Record<string, any> = null

Description

Current processing item alias, useful for dynamic class substitution in _Item getter

[_set]: Set<I> = ...

Description

Unique items storage

Accessors

  • get _Item(): Ctor<I>
  • Returns Ctor<I>

    Description

    Object instantiation. The constructor from which the instance will be created must be passed here. If null is passed, the object that was passed will be added to the collection.

    Default

    **null**
    

    Example

    class List extends collection.List<{ id: number }> {
    protected get _keys() {
    return [['id']];
    }

    protected get _Item() {
    return null;
    }
    }

    const list = new List([{ id: 1 }, { id: 2 }]);
    console.log(list.find(1)); // { id: 1 }
    console.log(list.find(1)); // { id: 2 }

    Example

    import * as collection from '@biorate/collection';
    const { embed } = collection;

    class Item extends collection.Item {
    @embed(Item.Int) public id: number = null;
    @embed(Item.String) public title: string = null;
    }

    class List extends collection.List<Item> {
    protected get _keys() {
    return [['id']];
    }

    protected get _Item() {
    return Item;
    }
    }

    const list = new List([{ id: 1, title: 'Cat' }, { id: 2, title: 'Dog' }]);
    console.log(list.find(1)); // Item { id: 1, title: 'Cat' }
    console.log(list.find(2)); // Item { id: 2, title: 'Dog' }
  • get _keys(): Keys
  • Returns Keys

    Description

    Indexes map. Designed to declare fields by which indexes will be built.

    Example

    class List extends collection.List<{ a: number; b: number; c: number }> {
    protected get _keys() {
    return [
    ['a', 'b'],
    ['b', 'c'],
    ];
    }
    }

    const list = new List([
    { a: 1, b: 1, c: 3 },
    { a: 2, b: 2, c: 2 },
    { a: 3, b: 1, c: 1 },
    ]);

    console.log(list.get(1, 1)); // [ { a: 1, b: 1, c: 3 }, { a: 3, b: 1, c: 1 } ]
    console.log(list.get(2, 2)); // [ { a: 2, b: 2, c: 2 } ]
  • get _processed(): Record<string, any>
  • Returns Record<string, any>

    Description

    Alias to current processed item, to private #processed property. Useful for dynamic class substitution in _Item getter

    Example

    class Base extends collection.Item {
    id: number = null;
    type: string = null;
    }
    class One extends Base {}
    class Two extends Base {}
    class Three extends Base {}

    const Types = { One, Two, Three }

    class List extends collection.List<Base> {
    protected get _keys() {
    return [['id']];
    }

    protected get _Item() {
    return Types[this.processed.type];
    }
    }

    const list = new List([
    { id: 1, type: 'One' },
    { id: 2, type: 'Two' },
    { id: 3, type: 'Three' }
    ]);

    console.log(list.find(1)); // One { id: 1, type: 'One' }
    console.log(list.find(2)); // Two { id: 2, type: 'Two' }
    console.log(list.find(3)); // Three { id: 3, type: 'Three' }
  • get size(): number
  • Returns number

    Description

    Unique collection items count

    Example

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);
    console.log(list.size); // 3
  • get index(): symbol
  • Returns symbol

    Description

    Alias for index-symbol, if used in _keys getter - enumerate and index items with sequence number.

    Example

    import { List as Base } from '@biorate/collection';

    class List extends Base<{ id: number }> {
    protected get _keys() {
    return [[Base.index]];
    }
    }

    const list = new List([{ id: 3 }, { id: 2 }, { id: 1 }]);

    console.log(list.find(0)); // { id: 3, [Symbol(Props.Index)]: 0 }
    console.log(list.find(1)); // { id: 2, [Symbol(Props.Index)]: 1 }
    console.log(list.find(2)); // { id: 1, [Symbol(Props.Index)]: 2 }

Methods

  • Returns Generator<I, any, unknown>

    Description

    Instance can be iterated by unique items storage

    Example

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);
    for (const item of list)
    console.log(item);
  • Returns void

    Description

    Clear collection

    Example

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);
    console.log(list.size); // 3
    list.clear();
    console.log(list.size); // 0
  • Parameters

    • Rest ...args: any[]

      key values

      Rest

    Returns boolean

    Description

    Delete item from collection by keys

    Example

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);
    list.delete(1); // true
    console.log(list); // [{ id: 2 }, { id: 3 }];
  • Parameters

    • Rest ...args: any[]

      key values

      Rest

    Returns I

    Description

    Find first item in collection by keys

    Example

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);
    list.find(1); // { id: 1 }
  • Parameters

    • Rest ...args: any[]

      key values

      Rest

    Returns I[]

    Description

    Find all items in collection by keys

    Example

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);
    list.get(1); // [{ id: 1 }]
  • Parameters

    • criteria: Record<string | symbol, any>

      query params

    • one: boolean = false

      return only first item?

    Returns I[]

    Description

    Get all items by criteria

    Example

    const list = new List([{ id: 1, a: 1 }, { id: 2, a: 1 }, { id: 3, a: 2 }]);
    list.getBy({ a: 1 }); // [{ id: 1, a: 1 }, { id: 2, a: 1 }]
    list.getBy({ id: 1, a: 1 }); // [{ id: 1, a: 1 }]
  • Parameters

    • Rest ...args: any[]

      key values

      Rest

    Returns boolean

    Description

    Check item exists in collection by keys

    Example

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);
    list.has(1); // true
    list.has(4); // false
  • Parameters

    • items: any[] = []

    Returns void

    Description

    Initialize list

    Example

    const list = new List();
    list.initialize([{ a: 1 }, { a: 2 }])
    console.log([...list]); // [{ a: 1 }, { a: 2 }]
    list.initialize([{ a: 3 }, { a: 4 }])
    console.log([...list]); // [{ a: 3 }, { a: 4 }]
  • Parameters

    • Rest ...args: any[]
      Rest

    Returns I[]

    Description

    Add item into collection

    Example

    const list = new List();
    list.set({ id: 1 }, { id: 2 }, { id: 3 });
  • Parameters

    • callback: ((...args) => void)
        • (...args): void
        • Parameters

          • Rest ...args: any[]
            Rest

          Returns void

    Returns ObservableList<I, P>

    Description

    Subscribe to changes

    Example

    const list = new List([{ id: 1 }, { id: 2 }, { id: 3 }]);
    const callback = (data) => console.log(data);
    list.subscribe(callback);

Generated using TypeDoc