Skip to content

Commit

Permalink
Fixes for #144
Browse files Browse the repository at this point in the history
  • Loading branch information
grantcopley committed Jan 27, 2024
1 parent de86eba commit 5df9f88
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 55 deletions.
80 changes: 61 additions & 19 deletions models/concerns/BaseEmitConcern.cfc
Original file line number Diff line number Diff line change
@@ -1,28 +1,70 @@
component {
/*
There is code duplication here, but I'm not sure how to avoid it.
We need to be able to parse the arguments passed to the emit() and emitTo() methods
and there are differences between how Lucee and Adobe CF handle the arguments.
/**
* Parse out emit arguments and parameters
*/
function parseEmitArguments( required eventName ){
var argumentsRef = arguments;
Creating separate functions for each method allows us to handle the differences
here.
*/
component accessors="true" {

return argumentsRef.reduce( function( agg, argument ){
if ( isNull( argumentsRef[ argument ] ) || argument == "ComponentName" ) {
return agg;
}
property name="caller";

var value = argumentsRef[ argument ];
function parseEmitParameters( required eventName ) {

if ( argument == "eventName" ) return agg;
var selfArguments = arguments;

if ( isObject( value ) ) {
return agg;
} else {
agg.append( value );
}
return selfArguments
.keyList()
.listToArray()
// Remove any arguments called "eventName" from the list
.filter( function( value ) {
return value != "eventName" && value != "componentName";
} )
// Sort the arguments by their numeric value
.sort( "numeric" )
// Convert the sorted list back to an ordered struct
.reduce( function( acc, value, index ) {
acc[ value ] = selfArguments[value];
return acc;
}, [:] )
// Filter out any null or object values
.filter( function( key, value ) {
return !isNull( value ) && !isObject( value );
} )
// Convert the struct to an array which makes up our parameters
.reduce( function( acc, key, value, thisStruct ) {
acc.append( value );
return acc;
}, [] );
}

function parseEmitToParameters( required componentName, required eventName ) {
var selfArguments = arguments;

return agg;
}, [] );
return selfArguments
.keyList()
.listToArray()
// Remove any arguments called "eventName" from the list
.filter( function( value ) {
return value != "eventName" && value != "componentName";
} )
// Sort the arguments by their numeric value
.sort( "numeric" )
// Convert the sorted list back to an ordered struct
.reduce( function( acc, value, index ) {
acc[ value ] = selfArguments[value];
return acc;
}, [:] )
// Filter out any null or object values
.filter( function( key, value ) {
return !isNull( value ) && !isObject( value );
} )
// Convert the struct to an array which makes up our parameters
.reduce( function( acc, key, value, thisStruct ) {
acc.append( value );
return acc;
}, [] );
}

}
10 changes: 5 additions & 5 deletions models/concerns/EmitConcern.cfc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
component extends="BaseEmitConcern" singleton {
component extends="BaseEmitConcern" {

function handle( comp, eventName ){
var localParameters = parseEmitArguments( argumentCollection = arguments );
function handle( required eventName ){
var emitParameters = parseEmitParameters( argumentCollection=arguments );

if ( !arguments.keyExists( "track" ) ) {
arguments.track = true;
Expand All @@ -11,10 +11,10 @@ component extends="BaseEmitConcern" singleton {
if ( arguments.track ) {
var emitter = {
"event" : arguments.eventName,
"params" : localParameters
"params" : emitParameters
};

comp.trackEmit( emitter );
getCaller().trackEmit( emitter );
}
}

Expand Down
11 changes: 6 additions & 5 deletions models/concerns/EmitSelfConcern.cfc
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
component extends="BaseEmitConcern" singleton {
component extends="BaseEmitConcern" {

function handle( comp, eventName ){
var localParameters = parseEmitArguments( argumentCollection = arguments );
function handle( required eventName ){

var emitParameters = parseEmitParameters( argumentCollection = arguments );

var emitter = {
"event" : arguments.eventName,
"params" : localParameters,
"params" : emitParameters,
"selfOnly" : true
};

// Capture the emit as we will need to notify the UI in our response
arguments.comp.trackEmit( emitter );
getCaller().trackEmit( emitter );
}

}
10 changes: 5 additions & 5 deletions models/concerns/EmitToConcern.cfc
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
component extends="BaseEmitConcern" singleton {
component extends="BaseEmitConcern" {

function handle( comp, componentName, eventName ){
var localParameters = parseEmitArguments( argumentCollection = arguments );
function handle( required componentName, required eventName ){
var emitParameters = parseEmitToParameters( argumentCollection = arguments );

var emitter = {
"event" : arguments.eventName,
"params" : localParameters,
"params" : emitParameters,
"to" : arguments.componentName
};

// Capture the emit as we will need to notify the UI in our response
arguments.comp.trackEmit( emitter );
getCaller().trackEmit( emitter );
}

}
11 changes: 5 additions & 6 deletions models/concerns/EmitUpConcern.cfc
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
component extends="BaseEmitConcern" singleton {

function handle( comp, eventName ){
var localParameters = parseEmitArguments( argumentCollection = arguments );
component extends="BaseEmitConcern" {

function handle( required eventName ){
var emitParameters = parseEmitParameters( argumentCollection = arguments );
var emitter = {
"event" : arguments.eventName,
"params" : localParameters,
"params" : emitParameters,
"ancestorsOnly" : true
};

// Capture the emit as we will need to notify the UI in our response
arguments.comp.trackEmit( emitter );
getCaller().trackEmit( emitter );
}

}
28 changes: 16 additions & 12 deletions models/renderer/SubsequentRenderer.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ component extends="BaseRenderer" {
*
* @eventName String | The name of our event to emit.
*/
function emit( required eventName, parameters ){
arguments.comp = this;
return getConcern( "Emit" ).handle( argumentCollection = arguments );
function emit( required eventName ){
return getConcern( "Emit" )
.setCaller( this )
.handle( argumentCollection = arguments );
}

/**
Expand All @@ -48,9 +49,10 @@ component extends="BaseRenderer" {
*
* @return Void
*/
function emitSelf( required eventName, parameters ){
arguments.comp = this;
return getConcern( "EmitSelf" ).handle( argumentCollection = arguments );
function emitSelf( required eventName ){
return getConcern( "EmitSelf" )
.setCaller( this )
.handle( argumentCollection = arguments );
}

/**
Expand All @@ -61,9 +63,10 @@ component extends="BaseRenderer" {
*
* @return Void
*/
function emitUp( required eventName, parameters ){
arguments.comp = this;
return getConcern( "EmitUp" ).handle( argumentCollection = arguments );
function emitUp( required eventName ){
return getConcern( "EmitUp" )
.setCaller( this )
.handle( argumentCollection = arguments );
}

/**
Expand All @@ -74,9 +77,10 @@ component extends="BaseRenderer" {
*
* @return Void
*/
function emitTo( required componentName, required eventName, parameters ){
arguments.comp = this;
return getConcern( "EmitTo" ).handle( argumentCollection = arguments );
function emitTo( required componentName, required eventName ){
return getConcern( "EmitTo" )
.setCaller( this )
.handle( argumentCollection = arguments );
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test-harness/tests/specs/CBWIRESpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -991,9 +991,9 @@ component extends="coldbox.system.testing.BaseTestCase" {
"someFile",
[
{
"name": "2022-08-21 07.52.50.gif",
"size": 424008,
"type": "image/gif"
"name": "2022-08-21 07.52.50.gif",
"size": 424008,
"type": "image/gif"
}
],
false
Expand Down

0 comments on commit 5df9f88

Please sign in to comment.