mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-12 21:10:23 +00:00
signals: Lint (#20648)
This commit is contained in:
Vendored
+14
-10
@@ -1,7 +1,8 @@
|
||||
// Type definitions for JS-Signals
|
||||
// Type definitions for JS-Signals 1.0
|
||||
// Project: http://millermedeiros.github.io/js-signals/
|
||||
// Definitions by: Diullei Gomes <https://github.com/diullei>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
declare var signals: signals.SignalWrapper;
|
||||
|
||||
@@ -9,9 +10,8 @@ export = signals;
|
||||
export as namespace signals;
|
||||
|
||||
declare namespace signals {
|
||||
|
||||
interface SignalWrapper<T = any> {
|
||||
Signal: Signal<T>
|
||||
Signal: Signal<T>;
|
||||
}
|
||||
|
||||
interface SignalBinding<T = any> {
|
||||
@@ -57,17 +57,21 @@ declare namespace signals {
|
||||
*
|
||||
* @param listener Signal handler function.
|
||||
* @param listenercontext Context on which listener will be executed (object that should represent the `this` variable inside listener function).
|
||||
* @param priority The priority level of the event listener. Listeners with higher priority will be executed before listeners with lower priority. Listeners with same priority level will be executed at the same order as they were added. (default = 0)
|
||||
* @param priority The priority level of the event listener.
|
||||
* Listeners with higher priority will be executed before listeners with lower priority.
|
||||
* Listeners with same priority level will be executed at the same order as they were added. (default = 0)
|
||||
*/
|
||||
add(listener: (...params: T[]) => void, listenerContext?: any, priority?: Number): SignalBinding<T>;
|
||||
|
||||
/**
|
||||
* Add listener to the signal that should be removed after first execution (will be executed only once).
|
||||
*
|
||||
* @param listener Signal handler function.
|
||||
* @param listenercontext Context on which listener will be executed (object that should represent the `this` variable inside listener function).
|
||||
* @param priority The priority level of the event listener. Listeners with higher priority will be executed before listeners with lower priority. Listeners with same priority level will be executed at the same order as they were added. (default = 0)
|
||||
*/
|
||||
* Add listener to the signal that should be removed after first execution (will be executed only once).
|
||||
*
|
||||
* @param listener Signal handler function.
|
||||
* @param listenercontext Context on which listener will be executed (object that should represent the `this` variable inside listener function).
|
||||
* @param priority The priority level of the event listener.
|
||||
* Listeners with higher priority will be executed before listeners with lower priority.
|
||||
* Listeners with same priority level will be executed at the same order as they were added. (default = 0)
|
||||
*/
|
||||
addOnce(listener: (...params: T[]) => void, listenerContext?: any, priority?: Number): SignalBinding<T>;
|
||||
|
||||
/**
|
||||
|
||||
+103
-103
@@ -1,207 +1,207 @@
|
||||
import signals = require("signals");
|
||||
// lifted from https://github.com/millermedeiros/js-signals/wiki/Examples
|
||||
interface TestObject {
|
||||
started: signals.Signal<any>;
|
||||
stopped: signals.Signal<any>;
|
||||
started: signals.Signal;
|
||||
stopped: signals.Signal;
|
||||
}
|
||||
|
||||
namespace Signals.Tests {
|
||||
//store local reference for brevity
|
||||
var Signal = signals.Signal;
|
||||
function tests() {
|
||||
// store local reference for brevity
|
||||
const Signal = signals.Signal;
|
||||
|
||||
//custom object that dispatch signals
|
||||
var myObject: TestObject = {
|
||||
started : new Signal(), //past tense is the recommended signal naming convention
|
||||
// custom object that dispatch signals
|
||||
const myObject: TestObject = {
|
||||
started : new Signal(), // past tense is the recommended signal naming convention
|
||||
stopped : new Signal()
|
||||
};
|
||||
|
||||
//single listener
|
||||
function onStarted(param1:string, param2:string){
|
||||
// single listener
|
||||
function onStarted(param1: string, param2: string) {
|
||||
alert(param1 + param2);
|
||||
}
|
||||
|
||||
myObject.started.add(onStarted); //add listener
|
||||
myObject.started.dispatch('foo', 'bar'); //dispatch signal passing custom parameters
|
||||
myObject.started.remove(onStarted); //remove a single listener
|
||||
myObject.started.add(onStarted); // add listener
|
||||
myObject.started.dispatch('foo', 'bar'); // dispatch signal passing custom parameters
|
||||
myObject.started.remove(onStarted); // remove a single listener
|
||||
|
||||
//multiple listeners
|
||||
function onStopped(){
|
||||
// multiple listeners
|
||||
function onStopped() {
|
||||
alert('stopped');
|
||||
}
|
||||
function onStopped2(){
|
||||
function onStopped2() {
|
||||
alert('stopped listener 2');
|
||||
}
|
||||
myObject.stopped.add(onStopped);
|
||||
myObject.stopped.add(onStopped2);
|
||||
myObject.stopped.dispatch();
|
||||
myObject.stopped.removeAll(); //remove all listeners of the `stopped` signal
|
||||
myObject.stopped.removeAll(); // remove all listeners of the `stopped` signal
|
||||
|
||||
//multiple dispatches
|
||||
var i = 0;
|
||||
myObject.started.add(function(){
|
||||
// multiple dispatches
|
||||
let i = 0;
|
||||
myObject.started.add(() => {
|
||||
i += 1;
|
||||
alert(i);
|
||||
});
|
||||
myObject.started.dispatch(); //will alert 1
|
||||
myObject.started.dispatch(); //will alert 2
|
||||
myObject.started.dispatch(); // will alert 1
|
||||
myObject.started.dispatch(); // will alert 2
|
||||
|
||||
//multiple dispatches + addOnce()
|
||||
var i = 0;
|
||||
myObject.started.addOnce(function(){
|
||||
// multiple dispatches + addOnce()
|
||||
i = 0;
|
||||
myObject.started.addOnce(() => {
|
||||
i += 1;
|
||||
alert(i);
|
||||
});
|
||||
myObject.started.dispatch(); //will alert 1
|
||||
myObject.started.dispatch(); //nothing happens
|
||||
myObject.started.dispatch(); // will alert 1
|
||||
myObject.started.dispatch(); // nothing happens
|
||||
|
||||
//enable/disable signal
|
||||
var i = 0;
|
||||
myObject.started.add(function(){
|
||||
// enable/disable signal
|
||||
i = 0;
|
||||
myObject.started.add(() => {
|
||||
i += 1;
|
||||
alert(i);
|
||||
});
|
||||
myObject.started.dispatch(); //will alert 1
|
||||
myObject.started.dispatch(); // will alert 1
|
||||
myObject.started.active = false;
|
||||
myObject.started.dispatch(); //nothing happens
|
||||
myObject.started.dispatch(); // nothing happens
|
||||
myObject.started.active = true;
|
||||
myObject.started.dispatch(); //will alert 2
|
||||
myObject.started.dispatch(); // will alert 2
|
||||
|
||||
//Stop/Halt Propagation (method 1)
|
||||
myObject.started.add(function(){
|
||||
myObject.started.halt(); //prevent next listeners on the queue from being executed
|
||||
// Stop/Halt Propagation (method 1)
|
||||
myObject.started.add(() => {
|
||||
myObject.started.halt(); // prevent next listeners on the queue from being executed
|
||||
});
|
||||
myObject.started.add(function(){
|
||||
alert('second listener'); //won't be called since first listener stops propagation
|
||||
myObject.started.add(() => {
|
||||
alert('second listener'); // won't be called since first listener stops propagation
|
||||
});
|
||||
myObject.started.dispatch();
|
||||
|
||||
//Stop/Halt Propagation (method 2)
|
||||
myObject.started.add(function(){
|
||||
return false; //if handler returns `false` will also stop propagation
|
||||
// Stop/Halt Propagation (method 2)
|
||||
myObject.started.add(() => {
|
||||
return false; // if handler returns `false` will also stop propagation
|
||||
});
|
||||
myObject.started.add(function(){
|
||||
alert('second listener'); //won't be called since first listener stops propagation
|
||||
myObject.started.add(() => {
|
||||
alert('second listener'); // won't be called since first listener stops propagation
|
||||
});
|
||||
myObject.started.dispatch();
|
||||
|
||||
//Set execution context of the listener handler
|
||||
var foo = 'bar';
|
||||
var obj = {
|
||||
// Set execution context of the listener handler
|
||||
const foo = 'bar';
|
||||
const obj = {
|
||||
foo : 10
|
||||
};
|
||||
|
||||
function handler1(){
|
||||
function handler1() {
|
||||
alert(this.foo);
|
||||
}
|
||||
|
||||
function handler2(){
|
||||
function handler2() {
|
||||
alert(this.foo);
|
||||
}
|
||||
|
||||
//note that you cannot add the same handler twice to the same signal without removing it first
|
||||
myObject.started.add(handler1); //default execution context
|
||||
myObject.started.add(handler2, obj); //set a different execution context
|
||||
myObject.started.dispatch(); //first handler will alert "bar", second will alert "10".
|
||||
// note that you cannot add the same handler twice to the same signal without removing it first
|
||||
myObject.started.add(handler1); // default execution context
|
||||
myObject.started.add(handler2, obj); // set a different execution context
|
||||
myObject.started.dispatch(); // first handler will alert "bar", second will alert "10".
|
||||
}
|
||||
|
||||
namespace Signals.AdvancedTests {
|
||||
//store local reference for brevity
|
||||
var Signal = signals.Signal;
|
||||
function advancedTests() {
|
||||
// store local reference for brevity
|
||||
const Signal = signals.Signal;
|
||||
|
||||
//custom object that dispatch signals
|
||||
var myObject: TestObject = {
|
||||
started : new Signal(), //past tense is the recommended signal naming convention
|
||||
// custom object that dispatch signals
|
||||
const myObject: TestObject = {
|
||||
started : new Signal(), // past tense is the recommended signal naming convention
|
||||
stopped : new Signal()
|
||||
};
|
||||
|
||||
//Set listener priority/order (v0.5.3+)
|
||||
var handler1 = function(){
|
||||
// Set listener priority/order (v0.5.3+)
|
||||
let handler1 = () => {
|
||||
alert('foo');
|
||||
};
|
||||
var handler2 = function(){
|
||||
let handler2 = () => {
|
||||
alert('bar');
|
||||
};
|
||||
myObject.started.add(handler1); //default priority is 0
|
||||
myObject.started.add(handler2, null, 2); //setting priority to 2 will make `handler2` execute before `handler1`
|
||||
myObject.started.dispatch(); //will alert "bar" than "foo"
|
||||
myObject.started.add(handler1); // default priority is 0
|
||||
myObject.started.add(handler2, null, 2); // setting priority to 2 will make `handler2` execute before `handler1`
|
||||
myObject.started.dispatch(); // will alert "bar" than "foo"
|
||||
|
||||
//Enable/Disable a single SignalBinding
|
||||
var handler1 = function(){
|
||||
// Enable/Disable a single SignalBinding
|
||||
handler1 = () => {
|
||||
alert('foo bar');
|
||||
};
|
||||
var handler2 = function(){
|
||||
handler2 = () => {
|
||||
alert('lorem ipsum');
|
||||
};
|
||||
var binding1 = myObject.started.add(handler1); //methods `add()` and `addOnce()` returns a SignalBinding object
|
||||
let binding1 = myObject.started.add(handler1); // methods `add()` and `addOnce()` returns a SignalBinding object
|
||||
myObject.started.add(handler2);
|
||||
myObject.started.dispatch(); //will alert "foo bar" than "lorem ipsum"
|
||||
binding1.active = false; //disable a single binding
|
||||
myObject.started.dispatch(); //will alert "lorem ipsum"
|
||||
myObject.started.dispatch(); // will alert "foo bar" than "lorem ipsum"
|
||||
binding1.active = false; // disable a single binding
|
||||
myObject.started.dispatch(); // will alert "lorem ipsum"
|
||||
binding1.active = true;
|
||||
myObject.started.dispatch(); //will alert "foo bar" than "lorem ipsum"
|
||||
myObject.started.dispatch(); // will alert "foo bar" than "lorem ipsum"
|
||||
|
||||
//Manually execute a signal handler
|
||||
var handler = function(){
|
||||
// Manually execute a signal handler
|
||||
const handler = () => {
|
||||
alert('foo bar');
|
||||
};
|
||||
var binding: signals.SignalBinding<() => void> = myObject.started.add(handler); //methods `add()` and `addOnce()` returns a SignalBinding object
|
||||
binding.execute(); //will alert "foo bar"
|
||||
let binding: signals.SignalBinding<() => void> = myObject.started.add(handler); // methods `add()` and `addOnce()` returns a SignalBinding object
|
||||
binding.execute(); // will alert "foo bar"
|
||||
|
||||
//Retrieve anonymous listener
|
||||
var binding:signals.SignalBinding<() => void> = myObject.started.add(function(){
|
||||
// Retrieve anonymous listener
|
||||
binding = myObject.started.add(() => {
|
||||
alert('foo bar');
|
||||
});
|
||||
|
||||
// note: in the original docs, this is handler, but that collides with handler above because these are in one giant scope
|
||||
// perhaps these are best split into functions for readability
|
||||
var anonymousHandler = binding.getListener(); //reference to the anonymous function
|
||||
const anonymousHandler = binding.getListener(); // reference to the anonymous function
|
||||
|
||||
//Remove / Detach anonymous listener
|
||||
var binding:signals.SignalBinding<() => void> = myObject.started.add(function(){
|
||||
// Remove / Detach anonymous listener
|
||||
binding = myObject.started.add(() => {
|
||||
alert('foo bar');
|
||||
});
|
||||
myObject.started.dispatch(); //will alert "foo bar"
|
||||
myObject.started.dispatch(); // will alert "foo bar"
|
||||
binding.detach();
|
||||
alert(binding.isBound()); //will alert `false`
|
||||
myObject.started.dispatch(); //nothing happens
|
||||
alert(binding.isBound()); // will alert `false`
|
||||
myObject.started.dispatch(); // nothing happens
|
||||
|
||||
//Check if binding will execute only once
|
||||
var binding1:signals.SignalBinding<any> = myObject.started.add(function(){
|
||||
// Check if binding will execute only once
|
||||
binding1 = myObject.started.add(() => {
|
||||
alert('foo bar');
|
||||
});
|
||||
var binding2:signals.SignalBinding<() => void> = myObject.started.addOnce(function(){
|
||||
const binding2: signals.SignalBinding<() => void> = myObject.started.addOnce(() => {
|
||||
alert('foo bar');
|
||||
});
|
||||
alert(binding1.isOnce()); //alert "false"
|
||||
alert(binding2.isOnce()); //alert "true"
|
||||
alert(binding1.isOnce()); // alert "false"
|
||||
alert(binding2.isOnce()); // alert "true"
|
||||
|
||||
//Change listener execution context on-the-fly
|
||||
var foo = 'bar';
|
||||
var obj = {
|
||||
// Change listener execution context on-the-fly
|
||||
const foo = 'bar';
|
||||
const obj = {
|
||||
foo : "it's over 9000!"
|
||||
};
|
||||
var binding:signals.SignalBinding<() => void> = myObject.started.add(function(){
|
||||
binding = myObject.started.add(function() {
|
||||
alert(this.foo);
|
||||
});
|
||||
myObject.started.dispatch(); //will alert "bar"
|
||||
myObject.started.dispatch(); // will alert "bar"
|
||||
binding.context = obj;
|
||||
myObject.started.dispatch(); //will alert "it's over 9000!"
|
||||
myObject.started.dispatch(); // will alert "it's over 9000!"
|
||||
|
||||
//Add default parameters to Signal dispatch (v0.6.3+)
|
||||
var binding:signals.SignalBinding<() => void> = myObject.started.add(function(a:string, b:string, c:string){
|
||||
alert(a +' '+ b +' '+ c);
|
||||
// Add default parameters to Signal dispatch (v0.6.3+)
|
||||
binding = myObject.started.add((a: string, b: string, c: string) => {
|
||||
alert(`${a} ${b} ${c}`);
|
||||
});
|
||||
binding.params = ['lorem', 'ipsum']; //set default parameters of the binding
|
||||
myObject.started.dispatch('dolor'); //will alert "lorem ipsum dolor"
|
||||
binding.params = ['lorem', 'ipsum']; // set default parameters of the binding
|
||||
myObject.started.dispatch('dolor'); // will alert "lorem ipsum dolor"
|
||||
|
||||
//Check if Signal has specific listener (v0.7.0+)
|
||||
function onStart(a:string){
|
||||
// Check if Signal has specific listener (v0.7.0+)
|
||||
function onStart(a: string) {
|
||||
console.log(a);
|
||||
}
|
||||
myObject.started.add(onStart);
|
||||
myObject.started.has(onStart); // true
|
||||
|
||||
//Memorize previously dispatched values / forget values (v0.7.0+)
|
||||
// Memorize previously dispatched values / forget values (v0.7.0+)
|
||||
myObject.started.memorize = true; // default is false
|
||||
myObject.started.dispatch('foo');
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "dtslint/dt.json",
|
||||
"rules": {
|
||||
// TODOs
|
||||
"ban-types": false,
|
||||
"no-misused-new": false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user