From 1021233f9c5ffb0c842b7d167a65672ca6d722fd Mon Sep 17 00:00:00 2001 From: zepumph Date: Fri, 4 Jan 2019 17:08:10 -0900 Subject: [PATCH] Emitter to use argumentTypes with Validator validation, see https://github.com/phetsims/axon/issues/182 --- js/input/Input.js | 63 +++++++++++++++++++++++++++++++++++ js/input/SimpleDragHandler.js | 16 +++++++-- js/listeners/DragListener.js | 3 ++ js/listeners/PressListener.js | 22 ++++++++---- 4 files changed, 95 insertions(+), 9 deletions(-) diff --git a/js/input/Input.js b/js/input/Input.js index d46ebb903..f7812580e 100644 --- a/js/input/Input.js +++ b/js/input/Input.js @@ -240,6 +240,9 @@ define( require => { this.mouseUpEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'mouseUpEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: Vector2 }, { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'point', type: Vector2IO }, { name: 'event', type: DOMEventIO } @@ -257,6 +260,9 @@ define( require => { this.mouseDownEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'mouseDownEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: Vector2 }, { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'point', type: Vector2IO }, { name: 'event', type: DOMEventIO } @@ -274,6 +280,9 @@ define( require => { this.mouseMovedEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'mouseMovedEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: Vector2 }, { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'point', type: Vector2IO }, { name: 'event', type: DOMEventIO } @@ -292,6 +301,9 @@ define( require => { this.mouseOverEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'mouseOverEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: Vector2 }, { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'point', type: Vector2IO }, { name: 'event', type: DOMEventIO } @@ -309,6 +321,9 @@ define( require => { this.mouseOutEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'mouseOutEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: Vector2 }, { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'point', type: Vector2IO }, { name: 'event', type: DOMEventIO } @@ -326,6 +341,9 @@ define( require => { this.wheelScrolledEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'wheelScrolledEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'event', type: DOMEventIO } ] ), @@ -349,6 +367,9 @@ define( require => { this.touchStartedEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'touchStartedEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: 'number' }, { valueType: Vector2 }, { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'id', type: NumberIO }, { name: 'point', type: Vector2IO }, @@ -367,6 +388,9 @@ define( require => { this.touchEndedEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'touchEndedEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: 'number' }, { valueType: Vector2 }, { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'id', type: NumberIO }, { name: 'point', type: Vector2IO }, @@ -388,6 +412,9 @@ define( require => { this.touchMovedEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'touchMovedEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: 'number' }, { valueType: Vector2 }, { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'id', type: NumberIO }, { name: 'point', type: Vector2IO }, @@ -409,6 +436,9 @@ define( require => { this.touchCanceledEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'touchCanceledEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: 'number' }, { valueType: Vector2 }, { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'id', type: NumberIO }, { name: 'point', type: Vector2IO }, @@ -430,6 +460,9 @@ define( require => { this.penStartedEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'penStartedEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: 'number' }, { valueType: Vector2 }, { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'id', type: NumberIO }, { name: 'point', type: Vector2IO }, @@ -448,6 +481,9 @@ define( require => { this.penEndedEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'penEndedEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: 'number' }, { valueType: Vector2 }, { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'id', type: NumberIO }, { name: 'point', type: Vector2IO }, @@ -469,6 +505,9 @@ define( require => { this.penMovedEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'penMovedEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: 'number' }, { valueType: Vector2 }, { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'id', type: NumberIO }, { name: 'point', type: Vector2IO }, @@ -490,6 +529,9 @@ define( require => { this.penCanceledEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'penCanceledEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: 'number' }, { valueType: Vector2 }, { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'id', type: NumberIO }, { name: 'point', type: Vector2IO }, @@ -533,6 +575,9 @@ define( require => { this.focusinEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'focusinEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'event', type: DOMEventIO } ] ), @@ -554,6 +599,9 @@ define( require => { this.focusoutEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'focusoutEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'event', type: DOMEventIO } ] ), @@ -582,6 +630,9 @@ define( require => { this.clickEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'clickEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'event', type: DOMEventIO } ] ), @@ -603,6 +654,9 @@ define( require => { this.inputEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'inputEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'event', type: DOMEventIO } ] ), @@ -624,6 +678,9 @@ define( require => { this.changeEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'changeEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'event', type: DOMEventIO } ] ), @@ -645,6 +702,9 @@ define( require => { this.keydownEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'keydownEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'event', type: DOMEventIO } ] ), @@ -666,6 +726,9 @@ define( require => { this.keyupEmitter = new Emitter( { phetioPlayback: true, tandem: options.tandem.createTandem( 'keyupEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: window.Event } ], phetioType: EmitterIO( [ { name: 'event', type: DOMEventIO } ] ), diff --git a/js/input/SimpleDragHandler.js b/js/input/SimpleDragHandler.js index ab4547801..dbcd80a69 100644 --- a/js/input/SimpleDragHandler.js +++ b/js/input/SimpleDragHandler.js @@ -10,11 +10,10 @@ define( function( require ) { 'use strict'; // modules + var BooleanProperty = require( 'AXON/BooleanProperty' ); var Emitter = require( 'AXON/Emitter' ); var EmitterIO = require( 'AXON/EmitterIO' ); - var Vector2IO = require( 'DOT/Vector2IO' ); - var VoidIO = require( 'TANDEM/types/VoidIO' ); - var BooleanProperty = require( 'AXON/BooleanProperty' ); + var Event = require( 'SCENERY/input/Event' ); var inherit = require( 'PHET_CORE/inherit' ); var Mouse = require( 'SCENERY/input/Mouse' ); var PhetioObject = require( 'TANDEM/PhetioObject' ); @@ -22,6 +21,8 @@ define( function( require ) { var Tandem = require( 'TANDEM/Tandem' ); var Touch = require( 'SCENERY/input/Touch' ); var Vector2 = require( 'DOT/Vector2' ); + var Vector2IO = require( 'DOT/Vector2IO' ); + var VoidIO = require( 'TANDEM/types/VoidIO' ); /** * @param {Object} [options] @@ -96,6 +97,9 @@ define( function( require ) { // @private this.dragStartedEmitter = new Emitter( { tandem: options.tandem.createTandem( 'dragStartedEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: Vector2 }, { isValidValue: function( value ) { return value === null || value instanceof Event; } } ], phetioType: EmitterIO( [ { name: 'point', type: Vector2IO, documentation: 'the position of the drag start in view coordinates' }, { name: 'event', type: VoidIO, documentation: 'the scenery pointer Event' } ] ), @@ -110,6 +114,9 @@ define( function( require ) { this.draggedEmitter = new Emitter( { phetioHighFrequency: true, tandem: options.tandem.createTandem( 'draggedEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: Vector2 }, { valueType: Vector2 }, { isValidValue: function( value ) { return value === null || value instanceof Event; } } ], phetioType: EmitterIO( [ { name: 'point', type: Vector2IO, documentation: 'the position of the drag in view coordinates' }, { @@ -146,6 +153,9 @@ define( function( require ) { // @private this.dragEndedEmitter = new Emitter( { tandem: options.tandem.createTandem( 'dragEndedEmitter' ), + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ { valueType: Vector2 }, { isValidValue: function( value ) { return value === null || value instanceof Event; } } ], phetioType: EmitterIO( [ { name: 'point', type: Vector2IO, documentation: 'the position of the drag end in view coordinates' }, { name: 'event', type: VoidIO, documentation: 'the scenery pointer Event' } ] ), diff --git a/js/listeners/DragListener.js b/js/listeners/DragListener.js index aeda3afa3..e69e8558f 100644 --- a/js/listeners/DragListener.js +++ b/js/listeners/DragListener.js @@ -43,6 +43,7 @@ define( function( require ) { var Bounds2 = require( 'DOT/Bounds2' ); var Emitter = require( 'AXON/Emitter' ); var EmitterIO = require( 'AXON/EmitterIO' ); + var Event = require( 'SCENERY/input/Event' ); var EventIO = require( 'SCENERY/input/EventIO' ); var inherit = require( 'PHET_CORE/inherit' ); var PhetioObject = require( 'TANDEM/PhetioObject' ); @@ -191,6 +192,8 @@ define( function( require ) { phetioDocumentation: 'Emits whenever a drag occurs with an EventIO argument.', phetioReadOnly: options.phetioReadOnly, phetioEventType: 'user', + + argumentTypes: [ { valueType: Event } ], phetioType: DraggedEmitterIO, listener: function( event ) { diff --git a/js/listeners/PressListener.js b/js/listeners/PressListener.js index 0e1bea5dc..b31058cbb 100644 --- a/js/listeners/PressListener.js +++ b/js/listeners/PressListener.js @@ -19,6 +19,7 @@ define( function( require ) { var DerivedProperty = require( 'AXON/DerivedProperty' ); var Emitter = require( 'AXON/Emitter' ); var EmitterIO = require( 'AXON/EmitterIO' ); + var Event = require( 'SCENERY/input/Event' ); var EventIO = require( 'SCENERY/input/EventIO' ); var inherit = require( 'PHET_CORE/inherit' ); var Mouse = require( 'SCENERY/input/Mouse' ); @@ -28,22 +29,19 @@ define( function( require ) { var scenery = require( 'SCENERY/scenery' ); var Tandem = require( 'TANDEM/Tandem' ); var timer = require( 'PHET_CORE/timer' ); - var TypeDef = require( 'AXON/TypeDef' ); var VoidIO = require( 'TANDEM/types/VoidIO' ); // global var globalID = 0; - var nullOrFunctionPredicate = TypeDef.getNullOrTypeofPredicate( 'function' ); - // constants - factored out to reduce memory usage, see https://github.com/phetsims/unit-rates/issues/207 var PressedEmitterIO = EmitterIO( [ { name: 'event', type: EventIO }, - { name: 'targetNode', type: VoidIO, predicate: TypeDef.getNullOrInstanceOfPredicate( Node ) }, - { name: 'callback', type: VoidIO, predicate: nullOrFunctionPredicate } + { name: 'targetNode', type: VoidIO }, + { name: 'callback', type: VoidIO } ] ); - var ReleasedEmitterIO = EmitterIO( [ { name: 'callback', type: VoidIO, predicate: nullOrFunctionPredicate } ] ); + var ReleasedEmitterIO = EmitterIO( [ { name: 'callback', type: VoidIO } ] ); // Factor out to reduce memory footprint, see https://github.com/phetsims/tandem/issues/71 const truePredicate = _.constant( true ); @@ -225,6 +223,13 @@ define( function( require ) { phetioReadOnly: options.phetioReadOnly, phetioFeatured: options.phetioFeatured, phetioEventType: 'user', + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ + { valueType: Event }, + { isValidValue: function( v ) { return v === null || v instanceof Node; } }, + { isValidValue: function( v ) { return v === null || typeof v === 'function'; } } + ], phetioType: PressedEmitterIO, // The main implementation of "press" handling is implemented as a callback to the emitter, so things are nested @@ -239,6 +244,11 @@ define( function( require ) { phetioReadOnly: options.phetioReadOnly, phetioFeatured: options.phetioFeatured, phetioEventType: 'user', + + // TODO: use of both of these is redundant, and should get fixed with https://github.com/phetsims/axon/issues/194 + argumentTypes: [ + { isValidValue: function( v ) { return v === null || typeof v === 'function'; } } + ], phetioType: ReleasedEmitterIO, // The main implementation of "release" handling is implemented as a callback to the emitter, so things are nested