Skip to main content
Version: v25

ISubscription<TFunc>

Interface
Types Module: Broker

A subscription. Used to subscribe callbacks to events.

Type parameters

NameType
TFuncextends Function

Methods

subscribe

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

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);
}
}
}

Signature

subscribe(obj: object, member: TFunc, singleshot?: boolean) => void

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


unsubscribe

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.

Signature

unsubscribe(obj: object, member: TFunc) => void

Parameters

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

Returns

void


unsubscribeAll

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

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() { ... }
}

Signature

unsubscribeAll(obj: object) => void

Parameters

NameTypeDescription
objobjectobj value passed when the callbacks were subscribed.

Returns

void