Skip to content

Commit

Permalink
Merge pull request #51 from AlmirKadric/feature/refactor
Browse files Browse the repository at this point in the history
refactored code to follow new styles, also imported fixes from COJ branch
  • Loading branch information
nullorvoid authored Oct 25, 2016
2 parents 45aa5d1 + cf3f124 commit 54edf07
Show file tree
Hide file tree
Showing 47 changed files with 2,389 additions and 1,548 deletions.
6 changes: 6 additions & 0 deletions AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.Runtime.InteropServices;

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
56 changes: 33 additions & 23 deletions Async/Async.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
using System;
using System.Collections.Generic;

namespace Wizcorp.MageSDK.Utils {
namespace Wizcorp.MageSDK.Utils
{
/// <summary>
/// Async is a utility class which provides straight-forward, powerful functions for working with asynchronous C#.
///
/// Async provides around 20 functions that include the usual 'functional' suspects (map, reduce, filter, each…) as well as
/// some common patterns for asynchronous control flow (parallel, series, waterfall…). All these functions assume you follow
/// the convention of providing a single callback as the last argument of your async function.
/// </summary>
public class Async {
public static void each<T>(List<T> items, Action<T, Action<Exception>> fn, Action<Exception> cb) {
if (items == null || items.Count == 0) {
cb(null);
public static class Async
{
public static void Each<T>(List<T> items, Action<T, Action<Exception>> action, Action<Exception> callback)
{
if (items == null || items.Count == 0)
{
callback(null);
return;
}

int currentItemI = 0;
var currentItemI = 0;
Action iterate = null;
iterate = () => {
if (currentItemI >= items.Count) {
cb(null);
if (currentItemI >= items.Count)
{
callback(null);
return;
}

// Execute the given function on this item
fn(items[currentItemI], (Exception error) => {
action(items[currentItemI], error => {
// Stop iteration if there was an error
if (error != null) {
cb(error);
if (error != null)
{
callback(error);
return;
}

Expand All @@ -42,34 +48,38 @@ public static void each<T>(List<T> items, Action<T, Action<Exception>> fn, Actio
iterate();
}

public static void series(List<Action<Action<Exception>>> actionItems, Action<Exception> cb) {
bool isEmpty = actionItems == null || actionItems.Count == 0;
if (isEmpty) {
cb(null);
public static void Series(List<Action<Action<Exception>>> actions, Action<Exception> callback)
{
bool isEmpty = actions == null || actions.Count == 0;
if (isEmpty)
{
callback(null);
return;
}

int currentItemI = 0;
var currentActionI = 0;
Action iterate = null;
iterate = () => {
if (currentItemI >= actionItems.Count) {
cb(null);
if (currentActionI >= actions.Count)
{
callback(null);
return;
}

// Shift an element from the list
Action<Action<Exception>> actionItem = actionItems[currentItemI];
Action<Action<Exception>> action = actions[currentActionI];

// Execute the given function on this item
actionItem((Exception error) => {
action(error => {
// Stop iteration if there was an error
if (error != null) {
cb(error);
if (error != null)
{
callback(error);
return;
}

// Continue to next item
currentItemI++;
currentActionI++;
iterate();
});
};
Expand Down
48 changes: 33 additions & 15 deletions EventEmitter/EventEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,71 @@
using System.Collections.Generic;
using System.ComponentModel;

namespace Wizcorp.MageSDK.Event {
public class EventEmitter<T> {
namespace Wizcorp.MageSDK.Event
{
public class EventEmitter<T>
{
//
private Dictionary<string, object> eventTags = new Dictionary<string, object>();
private EventHandlerList eventsList = new EventHandlerList();

//
public void on(string eventTag, Action<object, T> handler) {
if (!eventTags.ContainsKey(eventTag)) {
public void On(string eventTag, Action<object, T> handler)
{
if (!eventTags.ContainsKey(eventTag))
{
eventTags.Add(eventTag, new object());
}

eventsList.AddHandler(eventTags[eventTag], handler);
}

//
public void once(string eventTag, Action<object, T> handler) {
public void Once(string eventTag, Action<object, T> handler)
{
Action<object, T> handlerWrapper = null;
handlerWrapper = (object obj, T arguments) => {
handlerWrapper = (obj, arguments) => {
eventsList.RemoveHandler(eventTags[eventTag], handlerWrapper);
handler(obj, arguments);
};

on(eventTag, handlerWrapper);
On(eventTag, handlerWrapper);
}

//
public void emit(string eventTag, object sender, T arguments) {
if (!eventTags.ContainsKey(eventTag)) {
public void Emit(string eventTag, object sender, T arguments)
{
if (!eventTags.ContainsKey(eventTag))
{
return;
}

Action<object, T> execEventList = (Action<object, T>)eventsList[eventTags[eventTag]];
execEventList(sender, arguments);
var execEventList = (Action<object, T>)eventsList[eventTags[eventTag]];
if (execEventList != null)
{
execEventList(sender, arguments);
}
}

public void emit(string eventTag, T arguments) {
emit(eventTag, null, arguments);
public void Emit(string eventTag, T arguments)
{
Emit(eventTag, null, arguments);
}

//
public void off(string eventTag, Action<object, T> handler) {
public void Off(string eventTag, Action<object, T> handler)
{
if (!eventTags.ContainsKey(eventTag))
{
return;
}

eventsList.RemoveHandler(eventTags[eventTag], handler);
}

//
public void removeAllListeners() {
public void RemoveAllListeners()
{
// Destroy all event handlers
eventsList.Dispose();
eventsList = new EventHandlerList();
Expand Down
82 changes: 55 additions & 27 deletions HTTPRequest/HTTPRequest.DotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@
using System.Text;
using System.Threading;

namespace Wizcorp.MageSDK.Network.Http {
public class HTTPRequest {
namespace Wizcorp.MageSDK.Network.Http
{
public class HttpRequest
{
private HttpWebRequest request;
private CookieContainer cookies;
private Action<Exception, string> cb;
private Timer timeoutTimer;

// Timeout setting for request
private long _timeout = 100 * 1000;
public long timeout {
get {
public long timeout
{
get
{
return _timeout;
}
set {
set
{
_timeout = value;

timeoutTimer.Dispose();
Expand All @@ -36,7 +41,8 @@ public long timeout {


// Constructor
public HTTPRequest(string url, string contentType, byte[] postData, Dictionary<string, string> headers, CookieContainer cookies, Action<Exception, string> cb) {
public HttpRequest(string url, string contentType, byte[] postData, Dictionary<string, string> headers, CookieContainer cookies, Action<Exception, string> cb)
{
// Start timeout timer
timeoutTimer = new Timer((object state) => {
this.Abort();
Expand All @@ -48,19 +54,23 @@ public HTTPRequest(string url, string contentType, byte[] postData, Dictionary<s
httpRequest.Method = (postData != null) ? WebRequestMethods.Http.Post : WebRequestMethods.Http.Get;

// Set request headers
if (headers != null) {
foreach (KeyValuePair<string, string> entry in headers) {
if (headers != null)
{
foreach (KeyValuePair<string, string> entry in headers)
{
httpRequest.Headers.Add(entry.Key, entry.Value);
}
}

// Set content type if provided
if (contentType != null) {
if (contentType != null)
{
httpRequest.ContentType = contentType;
}

// Set cookies if provided
if (cookies != null) {
if (cookies != null)
{
httpRequest.CookieContainer = cookies;
}

Expand All @@ -70,12 +80,16 @@ public HTTPRequest(string url, string contentType, byte[] postData, Dictionary<s
this.request = httpRequest;

// Initiate response
if (postData == null) {
if (postData == null)
{
ReadResponseData(cb);
return;
} else {
}
else
{
WritePostData(postData, (Exception requestError) => {
if (requestError != null) {
if (requestError != null)
{
cb(requestError, null);
return;
}
Expand All @@ -89,13 +103,17 @@ public HTTPRequest(string url, string contentType, byte[] postData, Dictionary<s


// Write post data to request buffer
private void WritePostData(byte[] postData, Action<Exception> cb) {
private void WritePostData(byte[] postData, Action<Exception> cb)
{
request.BeginGetRequestStream((IAsyncResult callbackResult) => {
try {
try
{
Stream postStream = request.EndGetRequestStream(callbackResult);
postStream.Write(postData, 0, postData.Length);
postStream.Close();
} catch (Exception error) {
}
catch (Exception error)
{
cb(error);
return;
}
Expand All @@ -104,7 +122,8 @@ private void WritePostData(byte[] postData, Action<Exception> cb) {
}

// Read response data from request buffer
private void ReadResponseData(Action<Exception, string> cb) {
private void ReadResponseData(Action<Exception, string> cb)
{
// Begin waiting for a response
request.BeginGetResponse(new AsyncCallback((IAsyncResult callbackResult) => {
// Cleanup timeout
Expand All @@ -113,15 +132,19 @@ private void ReadResponseData(Action<Exception, string> cb) {

// Process response
string responseString = null;
try {
try
{
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);

using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream())) {
using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
{
responseString = httpWebStreamReader.ReadToEnd();
}

response.Close();
} catch (Exception error) {
}
catch (Exception error)
{
cb(error, null);
return;
}
Expand All @@ -132,8 +155,10 @@ private void ReadResponseData(Action<Exception, string> cb) {


// Abort request
public void Abort() {
if (request == null) {
public void Abort()
{
if (request == null)
{
return;
}

Expand All @@ -147,21 +172,24 @@ public void Abort() {


// Create GET request and return it
public static HTTPRequest Get(string url, Dictionary<string, string> headers, CookieContainer cookies, Action<Exception, string> cb) {
public static HttpRequest Get(string url, Dictionary<string, string> headers, CookieContainer cookies, Action<Exception, string> cb)
{
// Create request and return it
// The callback will be called when the request is complete
return new HTTPRequest(url, null, null, headers, cookies, cb);
return new HttpRequest(url, null, null, headers, cookies, cb);
}

// Create POST request and return it
public static HTTPRequest Post(string url, string contentType, string postData, Dictionary<string, string> headers, CookieContainer cookies, Action<Exception, string> cb) {
public static HttpRequest Post(string url, string contentType, string postData, Dictionary<string, string> headers, CookieContainer cookies, Action<Exception, string> cb)
{
byte[] binaryPostData = Encoding.UTF8.GetBytes(postData);
return Post(url, contentType, binaryPostData, headers, cookies, cb);
}

// Create POST request and return it
public static HTTPRequest Post(string url, string contentType, byte[] postData, Dictionary<string, string> headers, CookieContainer cookies, Action<Exception, string> cb) {
return new HTTPRequest(url, contentType, postData, headers, cookies, cb);
public static HttpRequest Post(string url, string contentType, byte[] postData, Dictionary<string, string> headers, CookieContainer cookies, Action<Exception, string> cb)
{
return new HttpRequest(url, contentType, postData, headers, cookies, cb);
}
}
}
Expand Down
Loading

0 comments on commit 54edf07

Please sign in to comment.