Skip to main content

Interface: ISubscription<TFunc>

Charting Library.ISubscription

A subscription. Used to subscribe callbacks to events.

Type parameters

NameType
TFuncextends Function

Hierarchy

Methods

subscribe

subscribe(obj, member, singleshot?): void

Subscribe a callback function to this event. Subscribed callbacks are called when the event fires.

Parameters

NameTypeDescription
objobjectObject used as the this value bound to the callback function.
memberTFuncFunction called when the event is fired.
singleshot?booleantrue if the subscription should be automatically removed after the first time it is fired.

Returns

void

Example

// Log 'Series data loaded!' to the console the next time the data loaded event fires and then unsubscribe automatically.
seriesApi.onDataLoaded().subscribe(null, () => { console.log('Series data loaded!'); }, true);

Subscribe to an event within a class. Manually unsubscribe when some condition is true.

class Example {
constructor(seriesApi) {
this._seriesApi = seriesApi;
this._seriesApi.onDataLoaded().subscribe(this, this._onDataLoaded);
}

_onDataLoaded() {
// Do something in response to the event.

if (someUnsubscribeCondition) {
this._seriesApi.onDataLoaded().unsubscribe(this, this._onDataLoaded);
}
}
}

unsubscribe

unsubscribe(obj, member): void

Unsubscribe a previously subscribed callback function. It is important that the obj and member arguments are the same as the ones passed when the callback was subscribed.

Parameters

NameTypeDescription
objobjectObject passed as the this value when the callback was subscribed.
memberTFuncFunction subscribed to the event.

Returns

void


unsubscribeAll

unsubscribeAll(obj): void

Unsubscribe all callbacks that were subscribed with the same obj value.

Parameters

NameTypeDescription
objobjectobj value passed when the callbacks were subscribed.

Returns

void

Example

// Unsubscribe all three callback functions at once in the `destroy` method.
class Example {
constructor(seriesApi) {
this._seriesApi = seriesApi;
this._seriesApi.onDataLoaded().subscribe(this, this._callback1);
this._seriesApi.onDataLoaded().subscribe(this, this._callback2);
this._seriesApi.onDataLoaded().subscribe(this, this._callback3);
}

destroy() {
this._seriesapi.onDataLoaded().unsubscribeAll(this);
}

_callback1() { ... }
_callback2() { ... }
_callback3() { ... }
}