/// <summary> /// Handles a request for a view model from a browser client. /// </summary> /// <param name="connectionId">Identifies the client connection.</param> /// <param name="vmId">Identifies the view model.</param> /// <param name="vmArg">Optional view model's initialization argument.</param> public virtual void OnRequestVM(string connectionId, string vmId, object vmArg = null) { // Create a new view model instance whose class name is matching the given VMId. BaseVM vmInstance = !_activeVMs.ContainsKey(vmId) ? CreateVM(vmId, vmArg) : _activeVMs[vmId].Instance; RequestVMFilter.Invoke(vmId, vmInstance, vmArg, data => { var vmData = vmInstance.Serialize(); // Send the view model data back to the browser client. _vmResponse?.Invoke(connectionId, vmId, vmData); // Reset the changed property states. vmInstance.AcceptChangedProperties(); // Add the view model instance to the controller. if (!_activeVMs.ContainsKey(vmId)) { _activeVMs.TryAdd(vmId, new VMInfo { Instance = vmInstance, ConnectionId = connectionId }); vmInstance.RequestPushUpdates += VmInstance_RequestPushUpdates; } else { _activeVMs[vmId].ConnectionId = connectionId; } // If this request causes other view models to change, push those new values back to the client. PushUpdates(); }); }
/// <summary> /// Handles a request for a view model from a browser client. /// </summary> /// <param name="connectionId">Identifies the client connection.</param> /// <param name="vmId">Identifies the view model.</param> /// <param name="vmArg">Optional view model's initialization argument.</param> /// <returns>Group name, if the request is for a multicast view model associated with one.</returns> public async virtual Task <string> OnRequestVMAsync(string connectionId, string vmId, object vmArg = null) { BaseVM vmInstance = null; if (_activeVMs.ContainsKey(vmId)) { vmInstance = _activeVMs[vmId].Instance; } else { // Create a new view model instance whose class name is matching the given VMId. vmInstance = CreateVM(vmId, vmArg); await vmInstance.OnCreatedAsync(); } await RequestVMFilter.Invoke(vmId, vmInstance, vmArg, async data => { var vmData = vmInstance.Serialize(); // Send the view model data back to the browser client. await ResponseVMFilter.Invoke(vmId, vmInstance, vmData, filteredData => _vmResponse(connectionId, vmId, (string)filteredData)); // Reset the changed property states. vmInstance.AcceptChangedProperties(); // Add the view model instance to the controller. if (!_activeVMs.ContainsKey(vmId)) { var vmInfo = new VMInfo(id: vmId, instance: vmInstance, connectionId: connectionId); vmInstance.RequestPushUpdates += VmInstance_RequestPushUpdates; if (vmInstance is MulticastVM) { var multicastVM = vmInstance as MulticastVM; vmInfo.GroupName = multicastVM.GroupName; multicastVM.RequestMulticastPushUpdates += VMInstance_RequestMulticastPushUpdates; multicastVM.RequestSend += VMInstance_RequestSend; } _activeVMs.TryAdd(vmId, vmInfo); } else { _activeVMs[vmId].ConnectionId = connectionId; } // If this request causes other view models to change, push those new values back to the client. foreach (var vmInfo in _activeVMs.Values) { PushUpdates(vmInfo); } }); return(_activeVMs[vmId].GroupName); }
/// <summary> /// Handles a request for a view model from a browser client. /// </summary> /// <param name="connectionId">Identifies the client connection.</param> /// <param name="vmId">Identifies the view model.</param> /// <param name="vmArg">Optional view model's initialization argument.</param> /// <returns>Group name, if the request is for a multicast view model associated with one.</returns> public virtual string OnRequestVM(string connectionId, string vmId, object vmArg = null) { // Create a new view model instance whose class name is matching the given VMId. BaseVM vmInstance = !_activeVMs.ContainsKey(vmId) ? CreateVM(vmId, vmArg) : _activeVMs[vmId].Instance; RequestVMFilter.Invoke(vmId, vmInstance, vmArg, data => { var vmData = vmInstance.Serialize(); // Send the view model data back to the browser client. _vmResponse?.Invoke(connectionId, vmId, vmData); // Reset the changed property states. vmInstance.AcceptChangedProperties(); // Add the view model instance to the controller. if (!_activeVMs.ContainsKey(vmId)) { _activeVMs.TryAdd(vmId, new VMInfo(id: vmId, instance: vmInstance, connectionId: connectionId)); vmInstance.RequestPushUpdates += VmInstance_RequestPushUpdates; if (vmInstance is IMulticast) { (vmInstance as IMulticast).RequestMulticastPushUpdates += VMInstance_RequestMulticastPushUpdates; } } else { _activeVMs[vmId].ConnectionId = connectionId; } // If this request causes other view models to change, push those new values back to the client. foreach (var vmInfo in _activeVMs.Values) { PushUpdates(vmInfo); } }); return(vmInstance is IMulticast ? (vmInstance as IMulticast).GroupName : null); }