Signal

API ReferenceSignal

Signal is a function with listeners which can be emitted.

Access it with:

const { signal } = Neft;

Source


signal.STOP_PROPAGATION

Static property of
signal
Type
Integer

Special constant used to stop calling further listeners.

Must be returned by the listener function.

var obj = {};
signal.create(obj, 'onPress');

obj.onPress(function(){
  console.log('listener 1');
  return signal.STOP_PROPAGATION;
});

// this listener won't be called, because first listener will capture this signal
obj.onPress(function(){
  console.log('listener 2');
});

obj.onPress.emit();
// listener 1

Source


signal.create()

Static property of
signal
Parameters
  • object — NotPrimitiveoptional
  • name — Stringoptional
Type
Signal

Creates a new signal in the given object under the given name property.

Returns created signal.

var obj = {};

signal.create(obj, 'onRename');

obj.onRename.connect(function(){
  console.log(arguments);
});

obj.onRename.emit('Max', 'George');
// {0: "Max", 1: "George"}

Source


signal.isEmpty()

Static method of
signal
Parameters
  • signal — Signal
Returns
Boolean

Returns true if the given signal has no listeners.

Source


emit()

Parameters
  • argument1 — Anyoptional
  • argument2 — Anyoptional

Call all of the signal listeners with the given arguments (2 maximally).

Source


connect()

Parameters
  • listener — Function
  • context — Anyoptional

Adds the given listener function into the signal listeners.

By default, the signal function works like this method.

var obj = {};
signal.create(obj, 'onPress');

obj.onPress(function(){
  console.log('listener 1');
});

obj.onPress.connect(function(){
  console.log('listener 2');
});

obj.onPress.emit()
// listener 1
// listener 2

The given context will be used as a context in listener calling. By default, the listener is called with the object on which the signal is created.

var obj = {standard: true};
signal.create(obj, 'onPress');

var fakeContext = {fake: true};
obj.onPress(function(){
  console.log(this);
}, fakeContext);

obj.onPress(function(){
  console.log(this);
});

obj.onPress.emit();
// {fake: true}
// {standard: true}

Source


disconnect()

Parameters
  • listener — Function
  • context — Anyoptional

Returns the given listener function from the signal listeners.

var obj = {};

signal.create(obj, 'onPress');

var listener = function(){
  console.log('listener called!');
};

obj.onPress.connect(listener);
obj.onPress.disconnect(listener);

obj.onPress.emit()
// no loggs...

Source


disconnectAll()

Removes all the signal listeners.

Source

results matching ""

    No results matching ""