-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1370 from notzippy/develop
Updated revel internal event handling system so external users can raise the events manually
- Loading branch information
Showing
13 changed files
with
95 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package revel | ||
|
||
type ( | ||
// The event type | ||
Event int | ||
// The event response | ||
EventResponse int | ||
// The handler signature | ||
EventHandler func(typeOf Event, value interface{}) (responseOf EventResponse) | ||
) | ||
const ( | ||
// Event type when templates are going to be refreshed (receivers are registered template engines added to the template.engine conf option) | ||
TEMPLATE_REFRESH_REQUESTED Event = iota | ||
// Event type when templates are refreshed (receivers are registered template engines added to the template.engine conf option) | ||
TEMPLATE_REFRESH_COMPLETED | ||
// Event type before all module loads, events thrown to handlers added to AddInitEventHandler | ||
|
||
// Event type before all module loads, events thrown to handlers added to AddInitEventHandler | ||
REVEL_BEFORE_MODULES_LOADED | ||
// Event type after all module loads, events thrown to handlers added to AddInitEventHandler | ||
REVEL_AFTER_MODULES_LOADED | ||
|
||
// Event type before server engine is initialized, receivers are active server engine and handlers added to AddInitEventHandler | ||
ENGINE_BEFORE_INITIALIZED | ||
// Event type before server engine is started, receivers are active server engine and handlers added to AddInitEventHandler | ||
ENGINE_STARTED | ||
// Event type after server engine is stopped, receivers are active server engine and handlers added to AddInitEventHandler | ||
ENGINE_SHUTDOWN | ||
|
||
// Called before routes are refreshed | ||
ROUTE_REFRESH_REQUESTED | ||
// Called after routes have been refreshed | ||
ROUTE_REFRESH_COMPLETED | ||
|
||
// Fired when a panic is caught during the startup process | ||
REVEL_FAILURE | ||
) | ||
|
||
// Fires system events from revel | ||
func RaiseEvent(key Event, value interface{}) (response EventResponse) { | ||
for _, handler := range initEventList { | ||
response |= handler(key, value) | ||
} | ||
return | ||
} | ||
|
||
// Add event handler to listen for all system events | ||
func AddInitEventHandler(handler EventHandler) { | ||
initEventList = append(initEventList, handler) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package revel_test | ||
|
||
import ( | ||
"github.com/revel/revel" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
// Test that the event handler can be attached and it dispatches the event received | ||
func TestEventHandler(t *testing.T){ | ||
counter := 0 | ||
newListener := func(typeOf revel.Event, value interface{}) (responseOf revel.EventResponse) { | ||
if typeOf==revel.REVEL_FAILURE { | ||
counter ++ | ||
} | ||
return | ||
} | ||
// Attach the same handlder twice so we expect to see the response twice as well | ||
revel.AddInitEventHandler(newListener) | ||
revel.AddInitEventHandler(newListener) | ||
revel.RaiseEvent(revel.REVEL_AFTER_MODULES_LOADED, nil) | ||
revel.RaiseEvent(revel.REVEL_FAILURE, nil) | ||
assert.Equal(t, counter,2,"Expected event handler to have been called") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters