Class: Destroyable

barejs.Destroyable()

Class that adds lifetime management to JavaScript objects. Destroyable provides a few features to ensure the JavaScript garbage collector can recollect memory as soon as possible.

A Destroyable instance can own other objects. The ownership in this case implies: if this instance is destroyed, the instance I own should also be destroyed. Destroyable can own any object having either a destroy or remove method.

A Destroyable instance can also ref other objects. The reference is automatically cleared on destroy. If the ref-ed object is a Destroyable, the reference is also cleared if the target gets destroyed.

Owned objects are also destroyed if this Destroyable is destroyed. Referenced objects are automatically unlinked if this object is destroyed, and even if the referenced is destroyed (in case the referenced object is Destroyable).

Constructor

new Destroyable()

Source:

Methods

(static) isDestroyed(_target) → {boolean}

Check if _target is destroyed.

Parameters:
Name Type Description
_target module:barejs.Destroyable~Destroyable

The destroyable to check

Source:
Returns:

True if the target has been destroyed, false otherwise.

Type
boolean

addDestroyListener(_listener) → {function}

Register a destroy listener for this Destroyable object.

Parameters:
Name Type Description
_listener module:barejs.Destroyable~DestroyListener

The listener function to add.

Source:
Returns:

the listener

Type
function

destroy()

Destroy method that will notify registered listeners and clean up references.

Source:

destroyAll(_collection)

Utility method that will iterate a collection and destroy all items in it.

Parameters:
Name Type Description
_collection object

An object with a forEach method or length property (e.g. an Array).

Source:

own() → {Array}

Own a number of handles. Returns an array of the owned handles.

Source:
Returns:

The owned handles.

Type
Array

ownMember(_name, _target)

The ownMember function combines ref and own into 1 call. The target is owned and then ref-ed as _name.

Parameters:
Name Type Description
_name string

The name of the member.

_target module:barejs.Destroyable

The target to own.

Source:
Returns:

The owned _target

ref(_name, _target)

Reference a target as a member property that will be unlinked on destroy. If the referenced target is also Destroyable, the ref is also cleared if the target is destroyed.

Parameters:
Name Type Description
_name string

The name to reference.

_target object

The object to assign to the reference.

Source:
Returns:

The value of this[_name].

removeDestroyListener(_listener)

Unregister a destroy listener for this Destroyable object.

Parameters:
Name Type Description
_listener module:barejs.Destroyable~DestroyListener

The listener function to remove.

Source:

unref(_name, _valueopt)

Remove a reference (by name). If the name was given to ownMember, the member is NOT removed from the list of owned targets. Does NOT destroy the value currently referenced.

Parameters:
Name Type Attributes Description
_name string

the name to remove the reference to

_value object | function <optional>

If a value is provided (and is not null), unref will only clear the reference if _value equals whatever is currently ref-ed.

Source:

Type Definitions

DestroyListener(_destroyed)

Destroy listeners are called with one argument; the Destroyed object.

Parameters:
Name Type Description
_destroyed module:barejs.Destroyable

The Destroyable that got destroyed.

Source: