Esempio n. 1
0
 /// <summary>
 /// This handler is sending events to clients!
 /// </summary>
 /// <param name="sender">object that is sending event</param>
 /// <param name="e">data about event</param>
 public static void JSEventHandler(object sender, ObjectChangedEventArgs e)
 {
     var context = GlobalHost.ConnectionManager.GetHubContext<Event>();
     //finding user to which I need to send event
     string connectionId = MemberManager.FindConnection(e.Subscription.SubscriptionId.ToString());
     //sending event to specific user
     context.Clients[connectionId].sendEvent(e.ToJSON());
 }
        /// <summary>
        /// Invokes events for object change subscriptions
        /// </summary>
        /// <param name="workspaceId">Workspace Id which is making the changes</param>
        /// <param name="commitResult">Commit result to process</param>
        public void InvokeEvents(Guid workspaceId, CommitResult<Guid> commitResult)
        {
            lock (subscriptions)
            {
                Collection<Guid> removalList = new Collection<Guid>();

                foreach (var item in subscriptions.Values)
                {
                    // Was the object changed in the commit?
                    if (commitResult.Mapping.ContainsKey(item.InstanceId))
                    {
                        // Is the change originating from different workspace OR we wanted events from same workspace
                        bool doThrowEvent = !item.WorkspaceId.Equals(workspaceId) || item.NotifyChangesFromSameWorkspace;

                        // If property was specified
                        if (doThrowEvent && !item.MemberId.Equals(Guid.Empty))
                        {
                            var isScalarMember = typesService.IsScalarType(typesService.GetMemberTypeId(item.MemberId));

                            if (isScalarMember)
                            {
                                // Get scalar property values
                                var oldValue = objectInstancesService.GetScalarInstanceMember(item.InstanceId, item.MemberId);
                                var newValue = objectInstancesService.GetScalarInstanceMember(commitResult.Mapping[item.InstanceId], item.MemberId);

                                // Throw event if they are not the same
                                doThrowEvent = Comparer.Default.Compare(oldValue, newValue) != 0;
                            }
                            else
                            {
                                bool isPermanent;
                                // Get reference property values
                                var oldValue = objectInstancesService.GetReferenceInstanceMember(item.InstanceId, item.MemberId, out isPermanent);
                                var newValue = objectInstancesService.GetReferenceInstanceMember(commitResult.Mapping[item.InstanceId], item.MemberId, out isPermanent);

                                // Throw event if they are not pointing to the same Ids
                                doThrowEvent = Comparer.Default.Compare(oldValue, newValue) != 0;
                            }
                        }

                        if (doThrowEvent)
                        {
                            var args = new ObjectChangedEventArgs(commitResult.ResultSnapshotId, item.InstanceId, commitResult.Mapping[item.InstanceId], new Subscription(item.SubscriptionId, item.WorkspaceId));
                            // Throw event
                            item.Del(this, args);

                            if (args.RenewSubscription)
                            {
                                // Renew subscription to new version
                                item.InstanceId = commitResult.Mapping[item.InstanceId];
                            }
                            else
                            {
                                // Register for removal
                                removalList.Add(item.SubscriptionId);
                            }
                        }
                        else
                        {
                            // Automatically renew subscriptions when processing own changes
                            item.InstanceId = commitResult.Mapping[item.InstanceId];
                        }
                    }
                }

                // Remove items which are not renewed
                foreach (var item in removalList)
                {
                    subscriptions.Remove(item);
                }
            }
        }