private void SetResult(IJavascriptObject[] e, IJSCBridgeCache bridge, Task<object> resulttask) { _IWebView.RunAsync (() => { if (e.Length < 2) return; IJavascriptObject promise = e[1]; if (!resulttask.IsFaulted) { bridge.RegisterInSession(resulttask.Result, (bridgevalue) => { promise.InvokeAsync("fullfill", _IWebView, bridgevalue.GetJSSessionValue()); }); } else { string error = (resulttask.IsCanceled) ? "Cancelled" : ((resulttask.Exception == null) ? "Faulted" : resulttask.Exception.Flatten().InnerException.Message); promise.InvokeAsync("reject", _IWebView, _IWebView.Factory.CreateString(error)); } }); }
internal void RegisterCollectionMapping(IJavascriptObject jsFather, string att, int index, IJavascriptObject child) { var father = GetGlobalCached(jsFather); var jsos = (att == null) ? father : ((JsGenericObject)father).Attributes[att]; Update(((JSArray)jsos).Items[index] as IJSObservableBridge, child); }
private JsGenericObject(HTMLViewContext context, IJavascriptObject value) { JSValue = value; _MappedJSValue = value; CValue = null; _HTMLViewContext = context; }
public JsResultCommand(IWebView webView, IJavascriptToCSharpConverter converter, IResultCommand resultCommand) { _WebView = webView; _JavascriptToCSharpConverter = converter; _JSResultCommand = resultCommand; JSValue = _WebView.Factory.CreateObject(true); }
private void MappNested(object from, IJavascriptObject resobject, JsGenericObject gres) { if (from == null) return; var propertyInfos = from.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.CanRead); foreach (var propertyInfo in propertyInfos) { var propertyName = propertyInfo.Name; object childvalue; try { childvalue = propertyInfo.GetValue(from, null); } catch(Exception e) { _Logger.Info(()=> $"Unable to convert property {propertyName} from {@from} exception {e}"); continue; } var childres = UnsafelMap(childvalue); _Context.WebView.Run(() => resobject.SetValue(propertyName, childres.JSValue)); gres.UpdateCSharpProperty(propertyName, childres); } }
private JSGenericObject(IWebView context, IJavascriptObject value) { JSValue = value; _MappedJSValue = value; CValue = null; _CefV8Context = context; }
public VueJavascriptViewModelUpdater(IWebView webView, IJavascriptObject listener, Lazy<IJavascriptObject> vueHelper, IWebSessionLogger logger) { _WebView = webView; _Listener = listener; _VueHelper = vueHelper; _Logger = logger; }
public void SpliceCollection(IJavascriptObject array, int index, int number, IJavascriptObject added) { _WebView.RunAsync(() => { AddUnsafe(array, index, number, added); }); }
public void Dispose() { if (_Listener == null) return; _Listener.Dispose(); _Listener = null; }
protected override bool LocalComputeJavascriptValue(IJavascriptObjectFactory factory) { if (JSValue != null) return false; JSValue = factory.CreateObject(true); return true; }
public void ClearAllCollection(IJavascriptObject array) { _WebView.RunAsync(() => { var length = array.GetArrayLength(); array.Invoke("silentSplice", _WebView, _WebView.Factory.CreateInt(0), _WebView.Factory.CreateInt(length)); }); }
public void MoveCollectionItem(IJavascriptObject array, IJavascriptObject item, int oldIndex, int newIndex) { _WebView.RunAsync(() => { array.Invoke("silentSplice", _WebView, _WebView.Factory.CreateInt(oldIndex), _WebView.Factory.CreateInt(1)); AddUnsafe(array, newIndex, 0, item); }); }
public VueVmManager(IWebView webView, IJavascriptObject listener, IWebSessionLogger logger) { _WebView = webView; _Logger = logger; _VueHelperLazy = new Lazy<IJavascriptObject>(GetVueHelper); _VueJavascriptSessionInjector = new VueJavascriptSessionInjector(webView, listener, _VueHelperLazy, _Logger); _VueJavascriptViewModelUpdater = new VueJavascriptViewModelUpdater(webView, listener, _VueHelperLazy, _Logger); }
private async void CanExecuteCommand(IJavascriptObject[] e) { var parameter = _JavascriptToCSharpConverter.GetFirstArgumentOrNull(e); var res = await UIDispatcher.EvaluateAsync(() => _Command.CanExecute(parameter)); await WebView.RunAsync(() => { UpdateProperty("CanExecuteValue", (f) => f.CreateBool(res)); }); }
private void JavascriptColectionChanged(IJavascriptObject[] arguments) { var values = arguments[1].GetArrayElements(); var types = arguments[2].GetArrayElements(); var indexes = arguments[3].GetArrayElements(); var collectionChange = new JavascriptCollectionChanges(arguments[0], values.Zip(types, indexes, (v, t, i) => new IndividualJavascriptCollectionChange(t.GetStringValue() == "added" ? CollectionChangeType.Add : CollectionChangeType.Remove, i.GetIntValue(), v))); _Observer.OnJavaScriptCollectionChanges(collectionChange); }
internal void RegisterMapping(IJavascriptObject father, string att, IJavascriptObject child) { var global = GetGlobalCached(father); if (global is JSCommand) return; var jso = (JsGenericObject)global; Update(jso.Attributes[att] as IJSObservableBridge, child); }
protected override bool LocalComputeJavascriptValue(IJavascriptObjectFactory factory) { if (JSValue != null) return false; JSValue = factory.CreateObject(true); JSValue.SetValue("CanExecuteValue", factory.CreateBool(_InitialCanExecute)); JSValue.SetValue("CanExecuteCount", factory.CreateInt(_Count)); return true; }
public void UpdateCSharpProperty(string PropertyName, IJSCBridgeCache converter, IJavascriptObject newValue) { PropertyInfo propertyInfo = CValue.GetType().GetProperty(PropertyName, BindingFlags.Public | BindingFlags.Instance); if (!propertyInfo.CanWrite) return; var type = propertyInfo.PropertyType.GetUnderlyingNullableType() ?? propertyInfo.PropertyType; IJSCSGlue glue = converter.GetCachedOrCreateBasic(newValue, type); _Attributes[PropertyName] = glue; propertyInfo.SetValue(CValue, glue.CValue, null); }
private async Task SetError(IJavascriptObject promise, Exception exception) { if (promise == null) return; await WebView.RunAsync(async () => { var errormessage = exception?.Message ?? "Faulted"; await promise.InvokeAsync("reject", WebView, WebView.Factory.CreateString(errormessage)); }); }
private async Task SetResult(IJavascriptObject promise, object result) { if (promise == null) return; await WebView.RunAsync(async () => { var bridgevalue = await _JavascriptToCSharpConverter.RegisterInSession(result); await promise.InvokeAsync("fullfill", WebView, bridgevalue.GetJSSessionValue()); }); }
public bool GetSimpleValue(IJavascriptObject decoratedValue, out object res, Type iTargetType = null) { res = null; var value = decoratedValue.Convert(); if ((value.IsUndefined) || (value.IsNull)) { return true; } if (value.IsString) { res = value.StringValue; return true; } if (value.IsBool) { res = value.BoolValue; return true; } if (iTargetType.IsUnsigned()) { if (value.IsUint) res = value.UintValue; } else { if (value.IsInt) res = value.IntValue; } if ((res == null) && (value.IsDouble)) { res = value.DoubleValue; } if (res != null) { if (iTargetType != null) res = Convert.ChangeType(res, iTargetType); return true; } if (value.IsDate) { res = value.DateValue.ToUniversalTime(value.DateValue); return true; } return false; }
public bool GetSimpleValue(IJavascriptObject ijsvalue, out object res, Type iTargetType = null) { res = null; CefV8Value value = CefV8_JavascriptObject.Convert(ijsvalue); if ((value.IsUndefined) || (value.IsNull)) { return true; } if (value.IsString) { res = ijsvalue.GetStringValue(); return true; } if (value.IsBool) { res = value.GetBoolValue(); return true; } if (IsUnsigned(iTargetType)) { if (value.IsUInt) res = value.GetUIntValue(); } else { if (value.IsInt) res = value.GetIntValue(); } if ((res == null) && (value.IsDouble)) { res = value.GetDoubleValue(); } if (res != null) { if (iTargetType != null) res = Convert.ChangeType(res, iTargetType); return true; } if (value.IsDate) { res = value.GetDateValue(); return true; } return false; }
public HTMLViewContext(IWebView webView, IDispatcher uiDispatcher, IJavascriptFrameworkManager javascriptFrameworkManager, IJavascriptChangesObserver javascriptChangesObserver, IWebSessionLogger logger) { WebView = webView; _logger = logger; UIDispatcher = uiDispatcher; var builder = new BinderBuilder(webView, javascriptChangesObserver); _Listener = builder.BuildListener(); _VmManager = javascriptFrameworkManager.CreateManager(WebView, _Listener, _logger); ViewModelUpdater = _VmManager.ViewModelUpdater; JavascriptSessionInjector = _VmManager.Injector; }
protected void Check(IJavascriptObject coll, IList<Skill> iskill) { coll.GetArrayLength().Should().Be(iskill.Count); for (int i = 0; i < iskill.Count; i++) { var c = coll.GetValue(i); (GetSafe(() => GetStringAttribute(c, "Name"))).Should().Be(iskill[i].Name); (GetSafe(() => GetStringAttribute(c, "Type"))).Should().Be(iskill[i].Type); } }
private static object GetArgument(this IJavascriptToCSharpConverter converter, IJavascriptObject javascriptObject) { try { var found = converter.GetCachedOrCreateBasic(javascriptObject, null); return found?.CValue; } catch (Exception) { return null; } }
private IJavascriptObject GetMapper(IJavascriptMapper iMapperListener) { _IJavascriptMapper.Enqueue(iMapperListener); if (_Mapper != null) return _Mapper; _Mapper = _IWebView.Factory.CreateObject(false); _Mapper.Bind("Register", _IWebView, (c, o, e) => { if (_PullNextMapper) { _Current = _IJavascriptMapper.Dequeue(); _PullNextMapper = false; } if (_Current == null) return; int count = e.Length; IJavascriptObject registered = e[0]; switch (count) { case 1: _Current.RegisterFirst(registered); break; case 3: _Current.RegisterMapping(e[1], e[2].GetStringValue(), registered); break; case 4: _Current.RegisterCollectionMapping(e[1], e[2].GetStringValue(), e[3].GetIntValue(), registered); break; } }); _Mapper.Bind("End", _IWebView, (c, o, e) => { if (_PullNextMapper) _Current = _IJavascriptMapper.Dequeue(); if (_Current!=null) _Current.End(e[0]); _Current = null; _PullNextMapper = true; }); return _Mapper; }
private IJavascriptObject GetKo() { if (_Ko != null) return _Ko; _Ko = _WebView.GetGlobal().GetValue("ko"); if ((_Ko == null) || (!_Ko.IsObject)) throw ExceptionHelper.Get("ko object not found! You should add a link to knockout.js script to the HML document!"); _Ko.Bind("log", _WebView, (e) => _logger.Info(() => string.Join(" - ", e.Select(s => (s.GetStringValue().Replace("\n", " ")))))); return _Ko; }
public void UpdateProperty(IJavascriptObject father, string propertyName, IJavascriptObject value) { var silenter = GetSilenter(father, propertyName); if (silenter != null) { Silent(silenter, value); return; } _WebView.RunAsync(() => { silenter = GetOrCreateSilenter(father, propertyName); Silent(silenter, value); }); }
private async void Execute(IJavascriptObject[] e) { var argument = _JavascriptToCSharpConverter.GetFirstArgumentOrNull(e); var promise = (e.Length > 1) ? e[1] : null; try { var res = await _JSResultCommand.Execute(argument); await SetResult(promise, res); } catch (Exception exception) { SetError(promise, exception).DoNotWait(); } }
internal JavascriptSessionInjector(IWebView iWebView, IJavascriptListener iJavascriptListener) { _IWebView = iWebView; _IJavascriptListener = iJavascriptListener; _IWebView.Run(() => { _Listener = _IWebView.Factory.CreateObject(false); if (_IJavascriptListener != null) { _Listener.Bind("TrackChanges", _IWebView, (c, o, e) => _IJavascriptListener.OnJavaScriptObjectChanges(e[0], e[1].GetStringValue(), e[2])); _Listener.Bind("TrackCollectionChanges", _IWebView, (c, o, e) => _IJavascriptListener.OnJavaScriptCollectionChanges(e[0], e[1].GetArrayElements(), e[2].GetArrayElements(), e[3].GetArrayElements())); } }); }
public override void SetJsValue(IJavascriptObject value, IJavascriptSessionCache sessionCache) { SetJsValue(value); }
public void OnJavaScriptObjectChanges(IJavascriptObject objectchanged, string propertyName, IJavascriptObject newValue) { try { var res = _SessionCache.GetGlobalCached(objectchanged) as JsGenericObject; if (res == null) { return; } var propertyAccessor = new PropertyAccessor(res.CValue, propertyName, _Logger); if (!propertyAccessor.IsSettable) { _Logger.Info(() => $"Unable to set C# from javascript object: property: {propertyName} is readonly."); return; } var targetType = propertyAccessor.GetTargetType(); var glue = GetCachedOrCreateBasic(newValue, targetType); using (_IsListening ? _ListenerRegister.GetPropertySilenter(res.CValue) : null) { propertyAccessor.Set(glue.CValue); res.UpdateCSharpProperty(propertyName, glue); } } catch (Exception e) { _Logger.Error(() => $"Unable to update ViewModel from View, exception raised: {e.Message}"); } }
public void SetMappedJsValue(IJavascriptObject jsobject) { _MappedJsValue = jsobject; UpdateJsObject(_MappedJsValue); }
protected async Task CallAsync(IJavascriptObject value, string functionName, params IJavascriptObject[] parameter) { await _WebView.RunAsync(() => value.Invoke(functionName, _WebView, parameter)); await WaitAnotherWebContextCycle(); }
protected void Call(IJavascriptObject value, string functionName, params IJavascriptObject[] parameter) { _WebView.Run(() => value.Invoke(functionName, _WebView, parameter)); }
public void UpdateJsObject(IJavascriptObject javascriptObject) { IExecutableGlue executable = this; javascriptObject.Bind("Execute", WebView, executable.Execute); }
void IJsCsGlue.SetJsValue(IJavascriptObject value, IJavascriptSessionCache cache) => base.SetJsValue(value);
protected void AddAttribute(IJavascriptObject father, string attributeName, IJavascriptObject value) { _JavascriptFrameworkExtractor.AddAttribute(father, attributeName, value); }
public void SetValue(int index, IJavascriptObject element) { _CefV8Value.SetValue(index, Convert(element)); }
private void MoveJavascriptCollection(IJavascriptViewModelUpdater viewModelUpdater, IJavascriptObject item, int oldIndex, int newIndex) { viewModelUpdater?.MoveCollectionItem(CachableJsValue, item, oldIndex, newIndex); }
protected string GetStringAttribute(IJavascriptObject value, string attributeName) { return(_JavascriptFrameworkExtractor.GetAttribute(value, attributeName).GetStringValue()); }
public IJavascriptObject ExecuteFunction(IWebView webView, IJavascriptObject context, params IJavascriptObject[] parameters) { return(_CfrV8Value.ExecuteFunction(context?.Convert(), parameters.Convert()).Convert()); }
protected async Task SetAttributeAsync(IJavascriptObject father, string attributeName, IJavascriptObject value) { await _JavascriptFrameworkExtractor.SetAttributeAsync(father, attributeName, value); await WaitAnotherWebContextCycle(); }
protected IJavascriptObject GetCollectionAttribute(IJavascriptObject value, string attributeName) { return(_JavascriptFrameworkExtractor.GetCollectionAttribute(value, attributeName)); }
protected bool GetBoolAttribute(IJavascriptObject value, string attributeName) { return(_JavascriptFrameworkExtractor.GetBoolAttribute(value, attributeName)); }
protected async Task <double> GetDoubleAttributeAsync(IJavascriptObject value, string attributeName) { var jsObject = await GetAttributeAsync(value, attributeName); return(jsObject.GetDoubleValue()); }
protected double GetDoubleAttribute(IJavascriptObject value, string attributeName) { return(_JavascriptFrameworkExtractor.GetDoubleAttribute(value, attributeName)); }
public async void OnJavaScriptObjectChanges(IJavascriptObject objectchanged, string propertyName, IJavascriptObject newValue) { try { var currentfather = _SessionCache.GetCached(objectchanged) as JsGenericObject; if (currentfather == null) { return; } var propertyUpdater = currentfather.GetPropertyUpdater(propertyName); if (!propertyUpdater.IsSettable) { LogReadOnlyProperty(propertyName); return; } var targetType = propertyUpdater.TargetType; var glue = GetCachedOrCreateBasic(newValue, targetType); var bridgeUpdater = new BridgeUpdater(_SessionCache); await Context.RunOnUiContextAsync(() => { using (_ListenerRegister.GetPropertySilenter(currentfather.CValue, propertyName)) { var oldValue = propertyUpdater.GetCurrentChildValue(); try { propertyUpdater.Set(glue.CValue); } catch (Exception exception) { LogSetError(propertyName, targetType, glue.CValue, exception); } var actualValue = propertyUpdater.GetCurrentChildValue(); if (Equals(actualValue, glue.CValue)) { var old = currentfather.UpdateGlueProperty(propertyUpdater, glue); bridgeUpdater.Remove(old); bridgeUpdater.CleanAfterChangesOnUiThread(_ListenerRegister.Off); return; } if (!Equals(oldValue, actualValue)) { OnCSharpPropertyChanged(currentfather.CValue, new PropertyChangedEventArgs(propertyName)); } } }); if (!bridgeUpdater.HasUpdatesOnJavascriptContext) { return; } await Context.RunOnJavascriptContextAsync(() => { bridgeUpdater.UpdateOnJavascriptContext(Context.ViewModelUpdater); }); } catch (Exception exception) { LogJavascriptSetException(exception); } }
internal static CefV8Value Convert(IJavascriptObject iIJavascriptObject) { return((iIJavascriptObject as CefV8_JavascriptObject)._CefV8Value); }
public IJSCSGlue GetCachedOrCreateBasic(IJavascriptObject globalkey, Type iTargetType) { return(_Context.WebView.Evaluate(() => GetCachedOrCreateBasicUnsafe(globalkey, iTargetType))); }
public void SetValue(string AttributeName, IJavascriptObject element, CreationOption ioption = CreationOption.None) { _CefV8Value.SetValue(AttributeName, Convert(element), Convert(ioption)); }
protected IJavascriptObject CallWithRes(IJavascriptObject value, string functionName, params IJavascriptObject[] parameter) { return(_WebView.Evaluate(() => value.Invoke(functionName, _WebView, parameter))); }
public void ExecuteFunctionNoResult(IWebView webView, IJavascriptObject context, params IJavascriptObject[] parameters) { _CfrV8Value.ExecuteFunction(context?.Convert(), parameters.Convert()); }
internal RawGlueMapable(object source, IJavascriptObject javascriptObject) { Source = source; _JavascriptObject = javascriptObject; }
public JavascriptCollectionChanges(IJavascriptObject collection, IEnumerable <IndividualJavascriptCollectionChange> changes) { Collection = collection; Changes = changes.ToArray(); }
protected async Task <IJavascriptObject> CallWithResAsync(IJavascriptObject value, string functionName, params IJavascriptObject[] parameter) { await WaitAnotherWebContextCycle(); return(await _WebView.EvaluateAsync(() => value.Invoke(functionName, _WebView, parameter))); }
public void SetMappedJSValue(IJavascriptObject jsobject) { _MappedJSValue = jsobject; _MappedJSValue.Bind("Execute", WebView, Execute); }
protected async Task <IJavascriptObject> GetCollectionAttributeAsync(IJavascriptObject value, string attributeName) { await WaitAnotherWebContextCycle(); return(await _JavascriptFrameworkExtractor.GetCollectionAttributeAsync(value, attributeName)); }
public virtual void SetJsValue(IJavascriptObject value, IJavascriptSessionCache sessionCache) { SetJsValue(value); sessionCache.Cache(this); }
public void SetAttribute(IJavascriptObject father, string attibutename, IJavascriptObject value) { _WebView.Run(() => father.SetValue(attibutename, value)); }