# Class Layer<Change, P, LayerDataTransferObject>

A layer is attached to a BimChangeBase instance. A layer can contain any type of object data structure. When objects are added, deleted or modified events on the corresponding objects are raised. Since layer objects are divided into local and remote objects. Remote objects are those that are retrieved from the backend.

#### Type Parameters

- Change extends LayerCompatibleChange
- P extends MergableObject<P>
- LayerDataTransferObject

#### Implements

- LayerInterface<P>

##### Index

### Constructors

constructor

### Properties

attachedTo
attachments
layerFormat

### Accessors

changeCount
conflictCount
conflicts
definition
id
isLoaded
layer
name
objects
permission
version

### Methods

add
clear
delete
get
loadAndMerge
saveAndMerge
throwIfNoPermissionsAreSatisfied
update

## Constructors

### constructor

- new Layer<
  Change extends LayerCompatibleChange,
  P extends MergableObject<P>,
  LayerDataTransferObject,
  >(
  _api: BimApi,
  attachedTo: Change,
  layerFormat: string,
  serialize: Serialize<LayerDataTransferObject, P, LayerInterface<P>>,
  ): Layer<Change, P, LayerDataTransferObject>

  #### Type Parameters

  - Change extends LayerCompatibleChange
  - P extends MergableObject<P>
  - LayerDataTransferObject

  #### Parameters

  - _api: BimApi

    BimCoreApi instance

  - attachedTo: Change

    Change this layer is attached to

  - layerFormat: string

    Format of layer. Example: "sensors"

  - serialize: Serialize<LayerDataTransferObject, P, LayerInterface<P>>

    Methods to transform layer objects into their DTO counterparts (data transfer object). DTO's are what is actually is stored in the layer.

  #### Returns Layer<Change, P, LayerDataTransferObject>

## Properties

### `Readonly` attachedTo

attachedTo: Change

Change this layer is attached to

### attachments

attachments: LayerAttachmentsApi

Provides access to the layer attachments API (LayerAttachmentsApi) Use methods on it to get, add, update and delete attachments for this layer.

Implementation of LayerInterface.attachments

### `Readonly` layerFormat

layerFormat: string

Format of layer. Example: "sensors"

## Accessors

### changeCount

- get changeCount(): number

  Number of objects in layer not marked with state MergableObjectState.Unchanged.

  #### Returns number

  Implementation of LayerInterface.changeCount

### conflictCount

- get conflictCount(): number

  Number of objects with conflicts in the layer. A conflict occurs if there is a mismatch between the local and the remote representation of the layer. Conflicts can only occur when Layer.saveAndMerge or Layer.loadAndMerge is called.

  #### Returns number

  Implementation of LayerInterface.conflictCount

### conflicts

- get conflicts(): Readonly<MergableObjectWithState<P>>[]

  Conflicts in the layer. A conflict occurs if there is a mismatch between the local and the remote representation of an object in the layer. Conflicts can only occur when Layer.saveAndMerge or Layer.loadAndMerge is called.

  #### Returns Readonly<MergableObjectWithState<P>>[]

  Implementation of LayerInterface.conflicts

### definition

- get definition(): LayerDefinition

  The layer definition.

  #### Returns LayerDefinition

  Implementation of LayerInterface.definition

### id

- get id(): string

  Id of the layer

  #### Returns string

  Implementation of LayerInterface.id

### isLoaded

- get isLoaded(): boolean

  `true` if a layer exists and if Layer.saveAndMerge or Layer.loadAndMerge has been called successfully at least once.

  #### Returns boolean

  Implementation of LayerInterface.isLoaded

### layer

- get layer(): BimChangeLayer

  Access to the layer instance that is currently loaded. If no layer is loaded then an exception is thrown. This is the representation of the layer that is stored in the backend. Layer.saveAndMerge or Layer.loadAndMerge will change the instance of the layer so do not store a reference to it and expect it to be valid after a call to these methods.

  #### Returns BimChangeLayer

### name

- get name(): undefined | string

  Get the name of the layer. Returns undefined if no layer has been loaded.

  #### Returns undefined | string

  Implementation of LayerInterface.name

### objects

- get objects(): IterableIterator<Readonly<MergableObjectWithState<P>>>

  Get the objects currently in the layer

  #### Returns IterableIterator<Readonly<MergableObjectWithState<P>>>

  Implementation of LayerInterface.objects

### permission

- get permission(): Permissions

  Permissions user has on layer.

  #### Returns Permissions

  Implementation of LayerInterface.permission

### version

- get version(): number

  Get the version of the currently loaded layer. [-1] if no layer has yet been loaded.

  #### Returns number

  Implementation of LayerInterface.version

## Methods

### add

- add(o: P): boolean

  Adds an object. If an object with the same MergableObjectProps.id already exists, no further object will be added. Requires user to have Access.add on layer. permission should therefore be validated before method call to ensure that no exception is generated.

  #### Parameters

  - o: P

    Object to add

  #### Returns boolean

  `true` if object was added, otherwise `false`.

  Implementation of LayerInterface.add

### clear

- clear(): void

  Clear the layer.

  #### Returns void

  Implementation of LayerInterface.clear

### delete

- delete(p: string | P): boolean

  Deletes an object. Requires user to have Access.delete on layer. permission should therefore be validated before method call to ensure that no exception is generated.

  #### Parameters

  - p: string | P

    Id of object to delete

  #### Returns boolean

  `true` if object was deleted. Otherwise `false`

  Implementation of LayerInterface.delete

### get

- get(id: string): undefined | MergableObjectWithState<P>

  Get a specific object in the layer. Both the local and the remote representation of the object are returned

  #### Parameters

  - id: string

    id of the object to get. Returns undefined if no such object exists.

  #### Returns undefined | MergableObjectWithState<P>

  Implementation of LayerInterface.get

### loadAndMerge

- loadAndMerge(): Promise<number | LayerFailure>

  Loads all existing (remote) objects from the backend and attempts to merge them into objects. This may result in conflicts between the local and the remote objects. A typical example is if the same object has been modified locally but also remotely (in the backend.)

  #### Returns Promise<number | LayerFailure>

  Implementation of LayerInterface.loadAndMerge

### saveAndMerge

- saveAndMerge(overwriteLatestVersion?: boolean): Promise<number | LayerFailure>

  Saves all objects in objects and retrieves any added, modified layer objects made in backend (Remote) at the same time. An attempt is then made to merge the object from the backend (Remote) into the local (browser) objects. Requires that user has Access.view (to add new layer) or Access.edit (to edit existing layer). permission should therefore be validated before method call to ensure that no exception is generated.

  #### Parameters

  - overwriteLatestVersion: boolean = false

    If `true` then latest version is overwritten (if save was successful).

  #### Returns Promise<number | LayerFailure>

  Number of conflicts. If > 0 then the operation was not successful. The conflicts must be resolved before a new attempt is made. If a LayerFailure is returned then it signifies that some unexpected error occured. Inspect the contents to determine the cause.

  Implementation of LayerInterface.saveAndMerge

### throwIfNoPermissionsAreSatisfied

- throwIfNoPermissionsAreSatisfied(...requiredPermissions: Permission[]): void

  #### Parameters

  - ...requiredPermissions: Permission[]

  #### Returns void

### update

- update(id: string, updateAction: (existingItem: P) => void): boolean

  Updates an existing object. Requires user to have Access.edit on layer. permission should therefore be validated before method call to ensure that no exception is generated.

  #### Parameters

  - id: string

    Id of object to update

  - updateAction: (existingItem: P) => void

    function called with MergableObject instance of specified id. Caller should make any required modfifications to the object in this function.

  #### Returns boolean

  `true` if object did exist, otherwise `false`.

  Implementation of LayerInterface.update