Skip to content

Commit

Permalink
Merge pull request #50 from AlmirKadric/feature/refactor
Browse files Browse the repository at this point in the history
added namespaces for all mage sdk files
  • Loading branch information
nullorvoid authored Oct 19, 2016
2 parents be97704 + 637bc98 commit 45aa5d1
Show file tree
Hide file tree
Showing 38 changed files with 3,243 additions and 3,167 deletions.
119 changes: 60 additions & 59 deletions Async/Async.cs
Original file line number Diff line number Diff line change
@@ -1,80 +1,81 @@
using System;
using System;
using System.Collections.Generic;


/// <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);
return;
}

int currentItemI = 0;
Action iterate = null;
iterate = () => {
if (currentItemI >= items.Count) {
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);
return;
}

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

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

// Begin the iteration
iterate ();
}
// Continue to next item
currentItemI++;
iterate();
});
};

public static void series (List<Action<Action<Exception>>> actionItems, Action<Exception> cb) {
bool isEmpty = actionItems == null || actionItems.Count == 0;
if (isEmpty) {
cb(null);
return;
// Begin the iteration
iterate();
}

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

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

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

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

// Continue to next item
currentItemI++;
iterate();
});
};

// Begin the iteration
iterate ();
// Shift an element from the list
Action<Action<Exception>> actionItem = actionItems[currentItemI];

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

// Continue to next item
currentItemI++;
iterate();
});
};

// Begin the iteration
iterate();
}
}
}
94 changes: 45 additions & 49 deletions EventEmitter/EventEmitter.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,60 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;

public class EventEmitter<T> {
//
private Dictionary<string, object> eventTags = new Dictionary<string, object>();
private EventHandlerList eventsList = new EventHandlerList();
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)) {
eventTags.Add(eventTag, new object());
//
public void on(string eventTag, Action<object, T> handler) {
if (!eventTags.ContainsKey(eventTag)) {
eventTags.Add(eventTag, new object());
}

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

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

//
public void once(string eventTag, Action<object, T> handler)
{
Action<object, T> handlerWrapper = null;
handlerWrapper = (object obj, T 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)) {
return;
}

//
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);
}

Action<object, T> execEventList = (Action<object, T>)eventsList[eventTags[eventTag]];
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)
{
eventsList.RemoveHandler(eventTags[eventTag], handler);
}
//
public void off(string eventTag, Action<object, T> handler) {
eventsList.RemoveHandler(eventTags[eventTag], handler);
}

//
public void removeAllListeners()
{
// Destroy all event handlers
eventsList.Dispose();
eventsList = new EventHandlerList();
//
public void removeAllListeners() {
// Destroy all event handlers
eventsList.Dispose();
eventsList = new EventHandlerList();

// Destroy all event tags
eventTags = new Dictionary<string, object>();
// Destroy all event tags
eventTags = new Dictionary<string, object>();
}
}
}
}
Loading

0 comments on commit 45aa5d1

Please sign in to comment.