Skip to content

Commit

Permalink
이벤트 핸들러 기능 개선중
Browse files Browse the repository at this point in the history
에러 있음 빌드 안됨
  • Loading branch information
s2quake committed May 3, 2018
1 parent ced6808 commit e870796
Show file tree
Hide file tree
Showing 57 changed files with 2,218 additions and 128 deletions.
2 changes: 1 addition & 1 deletion client/Ntreev.Crema.Services/Authentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Authentication BeginCommission()
throw new InvalidOperationException(Resources.Exception_Expired);
if (this.child != null)
throw new InvalidOperationException(Resources.Exception_Commissioned);
var authentication = new Authentication(new UserAuthenticationProvider(this.provider.ID, this.provider.Name, this.provider.AuthenticationTypes), this.token)
var authentication = new Authentication(new UserAuthenticationProvider(this.provider.ID, this.provider.Name, this.provider.Authority, this.provider.AuthenticationTypes), this.token)
{
signatureDate = this.signatureDate,
parent = this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ public UserAuthenticationProvider(User user, bool dummy)
user.UserInfoChanged += User_UserInfoChanged;
}

public UserAuthenticationProvider(string userID, string userName, AuthenticationType authenticationTypes)
public UserAuthenticationProvider(string userID, string userName, Authority authority, AuthenticationType authenticationTypes)
{
this.userID = userID;
this.userName = userName;
this.authority = authority;
this.authenticationTypes = authenticationTypes;
this.dummy = true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//Released under the MIT License.
//
//Copyright (c) 2018 Ntreev Soft co., Ltd.
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
//documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
//rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
//persons to whom the Software is furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
//Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
//COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
//OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using Ntreev.Crema.Services;
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Jint.Native;

namespace Ntreev.Crema.Javascript.Methods
{
[Export(typeof(IScriptMethod))]
[PartCreationPolicy(CreationPolicy.NonShared)]
class AddDataBaseEventListenerMethod : ScriptMethodBase
{
private readonly ICremaHost cremaHost;
private readonly CremaDataBaseEventListenerBase[] eventListeners;
private CremaDataBaseEventListenerContext eventListenerContext;

[ImportingConstructor]
public AddDataBaseEventListenerMethod(ICremaHost cremaHost, [ImportMany]IEnumerable<CremaDataBaseEventListenerBase> eventListeners)
{
this.cremaHost = cremaHost;
this.eventListeners = eventListeners.ToArray();
}

protected override Delegate CreateDelegate()
{
return new Action<CremaDataBaseEvents, CremaDataBaseEventListener>(this.AddDataBaseEventListener);
}

protected override void OnInitialized()
{
base.OnInitialized();
}

protected override void OnDisposed()
{
base.OnDisposed();
this.eventListenerContext?.Dispose();
}

private void AddDataBaseEventListener(CremaDataBaseEvents eventName, CremaDataBaseEventListener listener)
{
if (this.Context.Properties.ContainsKey(typeof(CremaDataBaseEventListenerContext)) == false)
{
this.eventListenerContext = new CremaDataBaseEventListenerContext(this.cremaHost, this.eventListeners);
this.Context.Properties[typeof(CremaDataBaseEventListenerContext)] = this.eventListenerContext;
}

if (this.eventListenerContext != null)
{
this.eventListenerContext.AddEventListener(eventName, listener);
}
else
{
throw new NotImplementedException();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ namespace Ntreev.Crema.Javascript.Methods
class AddEventListenerMethod : ScriptMethodBase
{
private readonly ICremaHost cremaHost;
private readonly IDictionary<CremaEvents, EventHandlerBase> eventHandlers;
private readonly CremaEventListenerBase[] eventListeners;
private CremaEventListenerContext eventListenerContext;

[ImportingConstructor]
public AddEventListenerMethod(ICremaHost cremaHost, [ImportMany]IEnumerable<EventHandlerBase> eventHandlers)
public AddEventListenerMethod(ICremaHost cremaHost, [ImportMany]IEnumerable<CremaEventListenerBase> eventListeners)
{
this.cremaHost = cremaHost;
this.eventHandlers = eventHandlers.ToDictionary(item => item.EventName);
this.eventListeners = eventListeners.ToArray();
}

protected override Delegate CreateDelegate()
{
return new Func<CremaEvents, Action<IDictionary<string, object>>, int>(this.AddEventListener);
return new Action<CremaEvents, CremaEventListener>(this.AddEventListener);
}

protected override void OnInitialized()
Expand All @@ -54,31 +55,20 @@ protected override void OnInitialized()
protected override void OnDisposed()
{
base.OnDisposed();
foreach (var item in this.eventHandlers)
{
item.Value.Dispose();
}
this.eventListenerContext?.Dispose();
}

public int AddEventListener(CremaEvents eventName, Action<IDictionary<string, object>> action)
private void AddEventListener(CremaEvents eventName, CremaEventListener listener)
{
if (this.eventHandlers.ContainsKey(eventName) == true)
{
var eventHandler = this.eventHandlers[eventName];
return eventHandler.Subscribe(action);
}
else
if (this.Context.Properties.ContainsKey(typeof(CremaEventListenerContext)) == false)
{
throw new NotImplementedException();
this.eventListenerContext = new CremaEventListenerContext(this.eventListeners);
this.Context.Properties[typeof(CremaEventListenerContext)] = this.eventListenerContext;
}
}

public void RemoveEventListener(CremaEvents eventName, int hashCode)
{
if (this.eventHandlers.ContainsKey(eventName) == true)
if (this.eventListenerContext != null)
{
var eventHandler = this.eventHandlers[eventName];
eventHandler.Unsubscribe(hashCode);
this.eventListenerContext.AddEventListener(eventName, listener);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Ntreev.Crema.Javascript.Methods
{
delegate void CremaDataBaseEventListener(string dataBaseName, IDictionary<string, object> e);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using Ntreev.Crema.Services;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Ntreev.Crema.ServiceModel;

namespace Ntreev.Crema.Javascript.Methods
{
abstract class CremaDataBaseEventListenerBase
{
private readonly CremaDataBaseEvents eventName;
private readonly Dictionary<IDataBase, List<CremaDataBaseEventListener>> dataBases = new Dictionary<IDataBase, List<CremaDataBaseEventListener>>();
//private readonly Dictionary<int, CremaDataBaseEventListener> hashes = new Dictionary<int, CremaDataBaseEventListener>();

protected CremaDataBaseEventListenerBase(CremaDataBaseEvents eventName)
{
this.eventName = eventName;
}

public void Dispose()
{
foreach (var item in this.dataBases)
{
this.OnUnsubscribe(item.Key);
}
this.dataBases.Clear();
}

public int Subscribe(IDataBase dataBase, CremaDataBaseEventListener listener)
{
if (this.dataBases.ContainsKey(dataBase) == false)
{
this.dataBases[dataBase] = new List<CremaDataBaseEventListener>();
}
var listenerList = this.dataBases[dataBase];
if (listenerList.Any() == false)
{
this.OnSubscribe(dataBase);
}
listenerList.Add(listener);
return listener.GetHashCode();
}

public void Unsubscribe(IDataBase dataBase, CremaDataBaseEventListener listener)
{
var listenerList = this.dataBases[dataBase];

for (var i = 0; i < listenerList.Count; i++)
{
if (this.GetHashCode(listenerList[i]) == this.GetHashCode(listener))
{
listenerList.RemoveAt(i);
break;
}
}
if (listenerList.Any() == false)
{
this.OnUnsubscribe(dataBase);
this.dataBases.Remove(dataBase);
}
}

public CremaDispatcher Dispatcher
{
get; set;
}

public CremaDataBaseEvents EventName => this.eventName;

protected void Invoke(IDictionary<string, object> properties)
{
this.Dispatcher.InvokeAsync(() =>
{
foreach (var item in this.dataBases)
{
//item.Invoke(properties);
}
});
}

protected abstract void OnSubscribe(IDataBase dataBase);

protected abstract void OnUnsubscribe(IDataBase dataBase);

//private void DataBase_Unloaded(object sender, EventArgs e)
//{
// if (sender is IDataBase dataBase)
// {
// var listenerList = this.dataBases[dataBase];
// foreach (var item in listenerList)
// {
// this.hashes.Remove(item.GetHashCode());
// }
// this.dataBases.Remove(dataBase);
// }
//}

private int GetHashCode(CremaDataBaseEventListener listener)
{
if (listener.Target is System.Runtime.CompilerServices.Closure c)
{
return c.Constants[0].GetHashCode();
}
throw new NotImplementedException();
}
}
}
Loading

0 comments on commit e870796

Please sign in to comment.