Browse Source

Updated sendEvent to accept a send event data object to act upon.

Shaddock Heath 8 years ago
parent
commit
d7113f15a4

+ 2 - 2
Script/Packages/Atomic/Core.json

@@ -27,11 +27,11 @@
 
 		"Object" : [
 			"sendEvent(eventType:string, data?:Object);",
-			"sendEvent<T extends Atomic.EventMetaData>(eventType:Atomic.EventType, data?:T);",
+			"sendEvent<T extends Atomic.EventCallbackMetaData>(eventCallbackMetaData:T);",
 			"subscribeToEvent(eventType:string, callback:(data:any) => void);",
 			"subscribeToEvent(sender:AObject, eventType:string, callback:(data: any) => void);",
 			"subscribeToEvent(eventMetaData:Atomic.EventMetaData);",
-			"subscribeToEvent(sender: AObject, eventMetaData:Atomic.EventMetaData);"
+			"subscribeToEvent(sender:AObject, eventMetaData:Atomic.EventMetaData);"
 		]
 	},
 	"haxe_decl" : {

+ 24 - 1
Script/TypeScript/AtomicWork.d.ts

@@ -29,7 +29,10 @@ declare module Atomic {
     /** Base for all event types */
     type EventType = string;
 
-    // Base interface for events, contains eventType string and callback
+    /** Base for all event callback data */
+    type EventData = Object;
+
+    /** Base interface for events, contains eventType string and callback */
     interface EventMetaData {
         /**@internal*/
         _eventType?: string;
@@ -37,6 +40,14 @@ declare module Atomic {
         _callback?: (...params) => any;
     }
 
+    /** Base interface for event data sent to event handlers */
+    interface EventCallbackMetaData {
+        /**@internal*/
+        _eventType?: string;
+        /**@internal*/
+        _callbackData?: any;
+    }
+
     interface NativeEvent extends EventMetaData { }
 
     interface ScriptEvent extends EventMetaData { }
@@ -44,8 +55,20 @@ declare module Atomic {
     // typed callback generic
     type EventCallback<T extends EventMetaData> = (data: T) => void;
 
+    /**
+     * Utility function to wrap up an event callback to pass to subscribeToEvent
+     * @param eventType The type of event to wrap
+     * @param callback A callback to call when the event is fired
+     */
     export function ScriptEvent<T extends Atomic.EventMetaData>(eventType: string, callback: Atomic.EventCallback<T>): Atomic.EventMetaData;
 
+    /**
+     * Utility function to wrap up event data to pass to sendEvent
+     * @param eventType The type of event to wrap
+     * @param callbackData The data to pass to the event subscriber
+     */
+    export function ScriptEventData<T extends Atomic.EventData>(eventType: string, callbackData?: T): Atomic.EventCallbackMetaData;
+
     export interface PathInfo {
 
         pathName: string;

+ 30 - 0
Source/AtomicJS/Javascript/JSAPI.cpp

@@ -214,18 +214,48 @@ namespace Atomic
         return 1;
     }
 
+    static int js_push_native_event_callbackdata(duk_context* ctx) {
+
+        duk_push_current_function(ctx);
+
+        duk_push_object(ctx);
+        duk_get_prop_string(ctx, -2, "_eventType");
+        duk_put_prop_string(ctx, -2, "_eventType");
+        duk_dup(ctx, 0);
+        duk_put_prop_string(ctx, -2, "_callbackData");
+
+        return 1;
+    }
+
     void js_define_native_event(duk_context* ctx, const String& eventType, const String &eventName)
     {
+        // Set up the pieces for subscribe to eventj
+
         // push c function which takes 1 argument, the callback
         duk_push_c_function(ctx, js_push_native_event_metadata, 1);
 
         // store the event type in the function object
+        // function {event}( eventName, callback ) : { _eventType , _callback }
         duk_push_string(ctx, eventType.CString());
         duk_put_prop_string(ctx, -2, "_eventType");
 
         // store to module object
         duk_put_prop_string(ctx, -2, eventName.CString());
 
+        // Set up the pieces for sendEvent
+
+        // push c function which takes 1 argument, the callback
+        duk_push_c_function(ctx, js_push_native_event_callbackdata, 1);
+
+        // store the event type in the function object
+        // function {event}Data( eventName, callbackData ) : { _eventType , _callbackData }
+        duk_push_string(ctx, eventType.CString());
+        duk_put_prop_string(ctx, -2, "_eventType");
+
+        // store to module object
+        duk_put_prop_string(ctx, -2, ToString("%sData", eventName.CString()).CString());
+
+
     }
 
     void js_object_to_variantmap(duk_context* ctx, int objIdx, VariantMap &v)

+ 25 - 0
Source/AtomicJS/Javascript/JSAtomic.cpp

@@ -237,6 +237,29 @@ static int js_atomic_ScriptEvent(duk_context* ctx)
     return 1;
 }
 
+static int js_atomic_ScriptEventData(duk_context* ctx)
+{
+    if (duk_get_top(ctx) != 2 || !duk_is_string(ctx, 0))
+    {
+        duk_push_string(ctx, "Atomic.ScriptEventData(eventType:string, data?:Object); - passed invalid parameters");
+        duk_throw(ctx);
+        return 0;
+    }
+
+    String eventType = duk_get_string(ctx, 0);
+    duk_push_object(ctx);
+    duk_push_string(ctx, eventType.CString());
+    duk_put_prop_string(ctx, -2, "_eventType");
+
+    if (duk_is_object(ctx, 1))
+    {
+        duk_dup(ctx, 1);
+        duk_put_prop_string(ctx, -2, "_callbackData");
+    }
+
+    return 1;
+}
+
 static void js_atomic_destroy_node(Node* node, duk_context* ctx, bool root = false)
 {
 
@@ -458,6 +481,8 @@ void jsapi_init_atomic(JSVM* vm)
     duk_push_c_function(ctx, js_atomic_ScriptEvent, 2);
     duk_put_prop_string(ctx, -2, "ScriptEvent");
 
+    duk_push_c_function(ctx, js_atomic_ScriptEventData, 2);
+    duk_put_prop_string(ctx, -2, "ScriptEventData");
 
     duk_pop(ctx);
 

+ 27 - 1
Source/AtomicJS/Javascript/JSCore.cpp

@@ -165,7 +165,33 @@ static int Object_SendEvent(duk_context* ctx)
 
     if (top == 1)
     {
-        sender->SendEvent(duk_to_string(ctx, 0));
+        if (duk_is_string(ctx, 0))
+        {
+            sender->SendEvent(duk_to_string(ctx, 0));
+        } else if (duk_is_object(ctx, 0)) {
+            duk_get_prop_string(ctx, 0, "_callbackData");
+            duk_get_prop_string(ctx, 0, "_eventType");
+
+            if (!duk_is_string(ctx, -1))
+            {
+                duk_push_string(ctx, "Object.sendEvent() - Bad callback meta data");
+                duk_throw(ctx);
+            }
+
+            if (duk_is_object(ctx, -2))
+            {
+                VariantMap sendEventVMap;
+                js_object_to_variantmap(ctx, -2, sendEventVMap);
+
+                sender->SendEvent(duk_to_string(ctx, -1), sendEventVMap);
+            } else {
+                sender->SendEvent(duk_to_string(ctx, -1));
+            }
+        } else {
+            duk_push_string(ctx, "Object.sendEvent() - Bad callback meta data");
+            duk_throw(ctx);
+        }
+
     }
     else if (top == 2)
     {

+ 11 - 1
Source/ToolCore/JSBind/JSBTypeScript.cpp

@@ -393,7 +393,7 @@ void JSBTypeScript::ExportModuleEvents(JSBModule* module)
 
         // Write the event interface
         source += ToString("    /** object returned in the callback for the %s event.**/\n", event->GetEventName().CString());
-        source += ToString("    export interface %s extends Atomic.NativeEvent {\n", scriptEventName.CString());
+        source += ToString("    export interface %s extends Atomic.EventData {\n", scriptEventName.CString());
 
         // parameters
 
@@ -472,6 +472,16 @@ void JSBTypeScript::ExportModuleEvents(JSBModule* module)
             source += ToString("    /** Wrapper function to generate a properly formatted event handler to pass to 'subscribeToEvent' for the %s event. **/\n", event->GetEventName().CString());
         }
         source += ToString("    export function %s (callback : Atomic.EventCallback<%s>) : Atomic.EventMetaData;\n", scriptEventName.CString(), scriptEventName.CString());
+        source += "\n";
+
+        if (params.Size() > 0)
+        {
+            source += ToString("    /** Wrapper function to construct callback data to pass to 'sendEvent' for the %s event. **/ \n", event->GetEventName().CString());
+            source += ToString("    export function %sData (callbackData : %s) : Atomic.EventCallbackMetaData; \n", scriptEventName.CString(), scriptEventName.CString());
+        } else {
+            source += ToString("    /** Wrapper function to construct object to pass to 'sendEvent' for the %s event. **/ \n", event->GetEventName().CString());
+            source += ToString("    export function %sData () : Atomic.EventCallbackMetaData; \n", scriptEventName.CString());
+        }
 
         source += "\n\n";