diff --git a/.r3_history b/.r3_history index f21eead..fb0d44a 100644 --- a/.r3_history +++ b/.r3_history @@ -2,11 +2,5 @@ r3 create Event normal r3 create R3Object extends Event r3 create Utils normal r3 create Component extends R3Object -r3 create Project extends R3Object -r3 create A normal -r3 create B extends A -r3 create C normal ./r3-system/ -r3 create D system C -r3 create E static ./ -r3 create T extends B +r3 create Project extends R3Object r3 create System normal ./r3-system/ diff --git a/src/r3/r3-event.js b/src/r3/r3-event.js index 9e20221..bb48528 100644 --- a/src/r3/r3-event.js +++ b/src/r3/r3-event.js @@ -6,9 +6,15 @@ const Utils = require('./r3-utils'); CUSTOM_OPTIONS_END CUSTOM_METHODS_START + async(eventId, data, clientCallback, clientErrorCallback) - Simply calls 'Async()' passing it the arguments + emit(eventId, data, clientCallback, clientErrorCallback) - Simply calls 'Emit()' passing it the arguments + subscribe(eventId, callback) - Simply calls 'Subscribe()' passing it the arguments CUSTOM_METHODS_END CUSTOM_STATIC_METHODS_START + Async(eventId, data, clientCallback, clientErrorCallback) - Calls all subscription functions registered to eventId with data, clientCallback and clientErrorCallback as arguments. If an error occurs during clientCallback it additionally will execute clientErrorCallback with the error as argument. + Emit(eventId, data, clientCallback, clientErrorCallback) - Calls all subscription functions registered to eventId with data as arg. Calls clientCallback directly after the event result is obtained, passing it the result. If an exception occurs during execution, the clientErrorCallback is called with the error as argument. + Subscribe(eventId, callback) - Subscribes to 'eventName', ex. Event.BEFORE_RENDER and executes 'callback()' when eventName is raised @return {Object} - A handle to the subscription which can be removed by calling handle.remove() CUSTOM_STATIC_METHODS_END **/ @@ -38,12 +44,6 @@ class Event { } //GENERATE_CONSTRUCTOR_END - //GENERATE_METHODS_START - //GENERATE_METHODS_END - - //GENERATE_STATIC_METHODS_START - //GENERATE_STATIC_METHODS_END - //CUSTOM_IMPLEMENTATION_START /** @@ -52,83 +52,141 @@ class Event { */ static Subscriptions = {}; - static Subscribe( - eventName, - fn - ) { - /** - * Todo - maybe eventually store a boolean which indicates if the function has been executed - */ - let subscriptionId = Utils.RandomId(10); + //CUSTOM_IMPLEMENTATION_END - if (Event.Subscriptions.hasOwnProperty(eventName)) { + //GENERATE_METHODS_START - if (Event.Subscriptions[eventName][subscriptionId]) { - throw new Error('A component can only subscribe to a particular event ID once'); - } - - Event.Subscriptions[eventName][subscriptionId] = fn; - } else { - Event.Subscriptions[eventName] = {}; - Event.Subscriptions[eventName][subscriptionId] = fn; - } - - /** - * Return a handle to the caller to allow us to unsubscribe to this event - */ - return { - fn: fn, - remove: function (eventId, subscriptionId) { - - return function () { - - /** - * Stop listening for this event from this component - */ - delete Event.Subscriptions[eventId][subscriptionId]; - - /** - * If the length of listeners is 0, stop referencing this event - * @type {string[]} - */ - let listeners = Object.keys(Event.Subscriptions[eventId]); - if (listeners.length === 0) { - delete Event.Subscriptions[eventId]; - } - } - - }(eventName, subscriptionId), - subscriptionId : subscriptionId - }; - }; - - /** - * Subscribe to some events - * @param eventName - * @param callback - */ - subscribe( - eventName, - callback - ) { - return Event.Subscribe(eventName, callback.bind(this)); - }; - - /** - * Static Synchronous Event - Calls clientCallback directly after the event result is obtained - * @param eventId + /** + * async() - Simply calls 'Async()' passing it the arguments + * @param eventId * @param data - * @param clientCallback is executed ideally when the event completed + * @param clientCallback * @param clientErrorCallback - * @returns {number} of callbacks executed - * @constructor - */ - static Emit( + * + */ + async( eventId, data, clientCallback, clientErrorCallback ) { + + //CUSTOM_ASYNC_METHOD_START + return Event.Async( + eventId, + data, + clientCallback, + clientErrorCallback + ); + //CUSTOM_ASYNC_METHOD_END + + } + + /** + * emit() - Simply calls 'Emit()' passing it the arguments + * @param eventId + * @param data + * @param clientCallback + * @param clientErrorCallback + * + */ + emit( + eventId, + data, + clientCallback, + clientErrorCallback + ) { + + //CUSTOM_EMIT_METHOD_START + return Event.Emit( + eventId, + data, + clientCallback, + clientErrorCallback + ); + //CUSTOM_EMIT_METHOD_END + + } + + /** + * subscribe() - Simply calls 'Subscribe()' passing it the arguments + * @param eventId + * @param callback + * + */ + subscribe( + eventId, + callback + ) { + + //CUSTOM_SUBSCRIBE_METHOD_START + return Event.Subscribe(eventId, callback.bind(this)); + //CUSTOM_SUBSCRIBE_METHOD_END + + } + //GENERATE_METHODS_END + + //GENERATE_STATIC_METHODS_START + + /** + * Async() - Calls all subscription functions registered to eventId with data, clientCallback and + * clientErrorCallback as arguments. If an error occurs during clientCallback it additionally will + * execute clientErrorCallback with the error as argument. + * @param eventId + * @param data + * @param clientCallback + * @param clientErrorCallback + * + */ + static Async( + eventId, + data, + clientCallback, + clientErrorCallback + ) { + + //CUSTOM_STATIC_ASYNC_METHOD_START + if (Event.Subscriptions.hasOwnProperty(eventId)) { + + let subscriptionIds = Object.keys(Event.Subscriptions[eventId]); + + subscriptionIds.map( + function(subscriptionId) { + try { + Event.Subscriptions[eventId][subscriptionId](data, clientCallback, clientErrorCallback); + } catch (error) { + if (clientErrorCallback) { + clientErrorCallback(error); + } else { + console.error(error); + throw error; + } + } + } + ) + } + //CUSTOM_STATIC_ASYNC_METHOD_END + + } + + /** + * Emit() - Calls all subscription functions registered to eventId with data as arg. Calls clientCallback + * directly after the event result is obtained, passing it the result. If an exception occurs during + * execution, the clientErrorCallback is called with the error as argument. + * @param eventId + * @param data + * @param clientCallback + * @param clientErrorCallback + * + */ + static Emit( + eventId, + data, + clientCallback, + clientErrorCallback + ) { + + //CUSTOM_STATIC_EMIT_METHOD_START if (Event.Subscriptions.hasOwnProperty(eventId)) { let subscriptionIds = Object.keys(Event.Subscriptions[eventId]); @@ -152,59 +210,68 @@ class Event { } ) } - } + //CUSTOM_STATIC_EMIT_METHOD_END - emit( - eventName, - data, - clientCallback, - clientErrorCallback - ) { - return Event.Emit( - eventName, - data, - clientCallback, - clientErrorCallback - ); - } + } - /** - * Execute the functions which subscribe to this event, but don't process the client callback - the subscription function - * should execute the client callback - * @param eventId - * @param data - * @param clientCallback - * @param clientErrorCallback - * @returns {number} - * @constructor - */ - static Async( + /** + * Subscribe() - Subscribes to 'eventName', ex. Event.BEFORE_RENDER and executes 'callback()' when eventName is + * raised + * @param eventId + * @param callback + * @returns {Object} - A handle to the subscription which can be removed by calling handle.remove() + */ + static Subscribe( eventId, - data, - clientCallback, - clientErrorCallback + callback ) { + + //CUSTOM_STATIC_SUBSCRIBE_METHOD_START + let subscriptionId = Utils.RandomId(10); + if (Event.Subscriptions.hasOwnProperty(eventId)) { - let subscriptionIds = Object.keys(Event.Subscriptions[eventId]); + if (Event.Subscriptions[eventId][subscriptionId]) { + throw new Error('A component can only subscribe to a particular event ID once'); + } - subscriptionIds.map( - function(subscriptionId) { - try { - Event.Subscriptions[eventId][subscriptionId](data, clientCallback, clientErrorCallback); - } catch (error) { - if (clientErrorCallback) { - clientErrorCallback(error); - } else { - console.error(error); - throw error; - } + Event.Subscriptions[eventId][subscriptionId] = callback; + } else { + Event.Subscriptions[eventId] = {}; + Event.Subscriptions[eventId][subscriptionId] = callback; + } + + /** + * Return a handle to the caller to allow us to unsubscribe to this event + */ + return { + fn: callback, + remove: function (eventId, subscriptionId) { + + return function () { + + /** + * Stop listening for this event from this component + */ + delete Event.Subscriptions[eventId][subscriptionId]; + + /** + * If the length of listeners is 0, stop referencing this event + * @type {string[]} + */ + let listeners = Object.keys(Event.Subscriptions[eventId]); + if (listeners.length === 0) { + delete Event.Subscriptions[eventId]; } } - ) - } - }; - //CUSTOM_IMPLEMENTATION_END + + }(eventId, subscriptionId), + subscriptionId : subscriptionId + }; + //CUSTOM_STATIC_SUBSCRIBE_METHOD_END + + } + //GENERATE_STATIC_METHODS_END } @@ -257,7 +324,6 @@ Event.GetEventName = function(eventId) { }; //EVENT_GENERATED_END - //CUSTOM_OUT_OF_CLASS_IMPLEMENTATION_END module.exports = Event; \ No newline at end of file diff --git a/src/r3/r3-system/r3-system.js b/src/r3/r3-system/r3-system.js index eb5779d..98e0e47 100644 --- a/src/r3/r3-system/r3-system.js +++ b/src/r3/r3-system/r3-system.js @@ -51,7 +51,12 @@ class System { //GENERATE_CONSTRUCTOR_END //GENERATE_METHODS_START - start(options, anotherOption) { + + /** + * start + */ + start(options) { + //CUSTOM_START_METHOD_START System.Start(options); @@ -62,25 +67,56 @@ class System { console.log('something else'); + + + //CUSTOM_START_METHOD_END + } + + /** + * stop + */ stop(options) { + //CUSTOM_STOP_METHOD_START System.Stop(options); + + + //CUSTOM_STOP_METHOD_END + } //GENERATE_METHODS_END //GENERATE_STATIC_METHODS_START + + /** + * Start + */ static Start(options) { + //CUSTOM_STATIC_START_METHOD_START console.log('Starting system X'); + + + //CUSTOM_STATIC_START_METHOD_END + } + + /** + * Stop + */ static Stop(options) { + //CUSTOM_STATIC_STOP_METHOD_START console.log('Stopping system X'); + + + //CUSTOM_STATIC_STOP_METHOD_END + } //GENERATE_STATIC_METHODS_END diff --git a/src/templates/generate_methods.template b/src/templates/generate_methods.template index 3614e43..022d3cb 100644 --- a/src/templates/generate_methods.template +++ b/src/templates/generate_methods.template @@ -1,4 +1,12 @@ + + /** + * METHOD_NAME() - COMMENT + * PARAMS + * RETURNS + */ METHOD_NAME(METHOD_ARGS) { + //CUSTOM_METHOD_NAME_UPPERCASE_METHOD_START //CUSTOM_METHOD_NAME_UPPERCASE_METHOD_END + } diff --git a/src/templates/generate_static_methods.template b/src/templates/generate_static_methods.template index 69444b9..73a2a26 100644 --- a/src/templates/generate_static_methods.template +++ b/src/templates/generate_static_methods.template @@ -1,4 +1,12 @@ + + /** + * METHOD_NAME() - COMMENT + * PARAMS + * RETURNS + */ static METHOD_NAME(METHOD_ARGS) { + //CUSTOM_STATIC_METHOD_NAME_UPPERCASE_METHOD_START //CUSTOM_STATIC_METHOD_NAME_UPPERCASE_METHOD_END + } diff --git a/src/templates/normal.template b/src/templates/normal.template index 599dbd8..7f18234 100644 --- a/src/templates/normal.template +++ b/src/templates/normal.template @@ -19,15 +19,15 @@ class CLASS_NAME { //GENERATE_CONSTRUCTOR_START //GENERATE_CONSTRUCTOR_END + //CUSTOM_IMPLEMENTATION_START + //CUSTOM_IMPLEMENTATION_END + //GENERATE_METHODS_START //GENERATE_METHODS_END //GENERATE_STATIC_METHODS_START //GENERATE_STATIC_METHODS_END - //CUSTOM_IMPLEMENTATION_START - //CUSTOM_IMPLEMENTATION_END - } //CUSTOM_OUT_OF_CLASS_IMPLEMENTATION_START diff --git a/src/templates/token.db b/src/templates/token.db index 84dd333..5c4c8d1 100644 --- a/src/templates/token.db +++ b/src/templates/token.db @@ -24,10 +24,12 @@ GENERATE_UPDATE_INSTANCE_AFTER GENERATE_UPDATE_INSTANCE_BEFORE GENERATE_UPDATE_INSTANCE_OPTIONS CUSTOM_AFTER_INIT +CUSTOM_ASYNC_METHOD CUSTOM_BEFORE_INIT CUSTOM_CREATE_INSTANCE CUSTOM_DISPOSE CUSTOM_DISPOSE_INSTANCE +CUSTOM_EMIT_METHOD CUSTOM_EVENT_LISTENERS CUSTOM_EXCLUDED_FROM_INSTANCE_OPTIONS CUSTOM_IMPLEMENTATION @@ -39,12 +41,14 @@ CUSTOM_OPTIONS CUSTOM_OPTIONS_INIT CUSTOM_OUT_OF_CLASS_IMPLEMENTATION CUSTOM_START_METHOD +CUSTOM_STATIC_ASYNC_METHOD +CUSTOM_STATIC_EMIT_METHOD CUSTOM_STATIC_METHOD_NAME_UPPERCASE_METHOD CUSTOM_STATIC_METHODS CUSTOM_STATIC_START_METHOD CUSTOM_STATIC_STOP_METHOD -CUSTOM_STATIC_TEST_RENAMED_METHOD +CUSTOM_STATIC_SUBSCRIBE_METHOD CUSTOM_STOP_METHOD -CUSTOM_TEST_RENAMED_METHOD +CUSTOM_SUBSCRIBE_METHOD CUSTOM_UPDATE_FROM_INSTANCE CUSTOM_UPDATE_INSTANCE diff --git a/update_templates.php b/update_templates.php index 655cd88..949b6b8 100755 --- a/update_templates.php +++ b/update_templates.php @@ -159,14 +159,14 @@ function getWhitespace($contents, $token) { $matches = []; - if (preg_match('/^(\s*)\b' . $token . '\b/m', $contents, $matches)) { + if (preg_match('/^\n*(\s*)\b' . $token . '\b/m', $contents, $matches)) { return [ 'white-space' => $matches[1], 'inline-comment' => false ]; } else { - if (preg_match('/^(\s*)\/\/\b' . $token . '\b/m', $contents, $matches)) { + if (preg_match('/^\n*(\s*)\/\/\b' . $token . '\b/m', $contents, $matches)) { return [ 'white-space' => $matches[1], 'inline-comment' => true @@ -414,6 +414,7 @@ function doMethodUpdate($template, $tokens, $token) $updates = ''; + foreach ($store as $item) { $item = trim($item); @@ -428,20 +429,56 @@ function doMethodUpdate($template, $tokens, $token) } $args = ''; - $result = preg_match('/\((.*)\)/', $item, $matches); + $result = preg_match('/\((.*?)\)/', $item, $matches); if ($result !== false && sizeof($matches) >= 2) { $args = $matches[1]; } + $params = $args; + + $argsArray = preg_split('/,(\s*)/', $args); + + if (sizeof($argsArray) > 1) { + $args = implode(",\n\t\t", $argsArray); + $args = "\n\t\t" . $args . "\n\t"; + $params = implode("\n\t * @param ", $argsArray); + $params = "@param " . $params; + } + + $comment = preg_match('/.*?\).*?(\w.*$)/', $item, $matches); + if ($result !== false && sizeof($matches) >= 2) { + $comment = $matches[1]; + } + + $returns = preg_split('/@return[s]*\s*/', $comment); + + if (sizeof($returns) > 1) { + $comment = $returns[0]; + $returns = '@returns ' . $returns[1]; + } else { + $returns = ''; + } + $methodTokenName = strtoupper(from_camel_case($methodName)); + $spaces = ''; + $length = strlen($methodName . '() - '); + for ($i = 0; $i < $length; $i++) { + $spaces = ' ' . $spaces; + } + + + $comment = wordwrap($comment, 100, "\n * " . $spaces); + $updated = str_replace('METHOD_ARGS', $args, $template); $updated = str_replace('METHOD_NAME_UPPERCASE', $methodTokenName, $updated); $updated = str_replace('METHOD_NAME', $methodName, $updated); + $updated = str_replace('COMMENT', $comment, $updated); + $updated = str_replace('PARAMS', $params, $updated); + $updated = str_replace('RETURNS', $returns, $updated); $updates .= $updated; } - return $updates; }