private void SocketClient_ReceivedMessage(object sender, EventArgs e)
        {
            using (var ms = new MemoryStream(SocketClient.ReceiveMessage()))
            {
                Dispatcher.Invoke(() =>
                {
                    var caretIndex = txtDocument.CaretIndex;

                    AppliedOperation appliedOperation = ProtoBuf.Serializer.Deserialize <AppliedOperation>(ms);
                    try
                    {
                        DocumentState.ApplyTransform(appliedOperation, ref caretIndex);
                    }
                    catch (Exception)
                    {
                        System.Diagnostics.Debugger.Break();
                        throw;
                    }

                    txtDocument.TextChanged -= Document_TextChanged;
                    txtDocument.Text         = DocumentState.CurrentState;
                    txtDocument.TextChanged += Document_TextChanged;
                    txtDocument.CaretIndex   = caretIndex;
                });
            }
        }
Esempio n. 2
0
        public void DocumentState_ApplyTransform_Convergence_MultipleInsertsSameIndex()
        {
            var localDocumentState  = new DocumentState(1, "");
            var remoteDocumentState = new DocumentState(2, "");
            var localTransform      = new AppliedOperation(new InsertOperation(localDocumentState, 0, 'x'), localDocumentState);

            localDocumentState.ApplyTransform(localTransform);
            var localTransform2 = new AppliedOperation(new InsertOperation(localDocumentState, 1, 'x'), localDocumentState);

            localDocumentState.ApplyTransform(localTransform2);

            var remoteTransform = new AppliedOperation(new InsertOperation(remoteDocumentState, 0, 'Y'), remoteDocumentState);

            remoteDocumentState.ApplyTransform(remoteTransform);
            var remoteTransform2 = new AppliedOperation(new InsertOperation(remoteDocumentState, 1, 'Y'), remoteDocumentState);

            remoteDocumentState.ApplyTransform(remoteTransform2);

            localDocumentState.ApplyTransform(remoteTransform);
            localDocumentState.ApplyTransform(remoteTransform2);

            remoteDocumentState.ApplyTransform(localTransform);
            remoteDocumentState.ApplyTransform(localTransform2);

            Assert.AreEqual(localDocumentState.CurrentState, remoteDocumentState.CurrentState);
        }
Esempio n. 3
0
        public void DocumentState_ApplyTransform_Convergence()
        {
            var localDocumentState  = new DocumentState(1, "12345");
            var remoteDocumentState = new DocumentState(2, "12345");
            var localTransform      = new AppliedOperation(new InsertOperation(localDocumentState, 0, 'x'), localDocumentState);
            var remoteTransform     = new AppliedOperation(new InsertOperation(remoteDocumentState, 2, 'Y'), remoteDocumentState);

            localDocumentState.ApplyTransform(localTransform);
            localDocumentState.ApplyTransform(remoteTransform);

            remoteDocumentState.ApplyTransform(remoteTransform);
            remoteDocumentState.ApplyTransform(localTransform);

            Assert.AreEqual(localDocumentState.CurrentState, remoteDocumentState.CurrentState);
        }
Esempio n. 4
0
        public void DocumentState_ApplyTransform_IntentionPreservation()
        {
            var localDocumentState  = new DocumentState(1, "ABCDE");
            var remoteDocumentState = new DocumentState(2, "ABCDE");
            var localTransform      = new AppliedOperation(new InsertOperation(localDocumentState, 1, '1'), localDocumentState);
            var localTransform2     = new AppliedOperation(new InsertOperation(localDocumentState, 2, '2'), new[] { localTransform.Operation.Id });
            var remoteTransform     = new AppliedOperation(new DeleteOperation(remoteDocumentState, 2), remoteDocumentState);
            var remoteTransform2    = new AppliedOperation(new DeleteOperation(remoteDocumentState, 2), new[] { remoteTransform.Operation.Id });

            localDocumentState.ApplyTransform(localTransform);
            localDocumentState.ApplyTransform(localTransform2);
            localDocumentState.ApplyTransform(remoteTransform);
            localDocumentState.ApplyTransform(remoteTransform2);

            Assert.AreEqual("A12BE", localDocumentState.CurrentState);
        }
Esempio n. 5
0
        public void DocumentState_ApplyTransform_TransformGetsApplied()
        {
            var DocumentState = new DocumentState(1, "1234");
            var transform     = new InsertOperation(DocumentState, 0, 'a');

            DocumentState.ApplyTransform(new AppliedOperation(transform));
            Assert.AreEqual(DocumentState.AppliedOperations.SingleOrDefault().Value, transform);
            Assert.AreEqual("a1234", DocumentState.CurrentState);
        }
        public void DocumentState_ApplyTransform_CaretSameForDeleteSame()
        {
            var DocumentState = new DocumentState(1, "1234");
            var transform     = new DeleteOperation(DocumentState, 3);
            var caretIndex    = 3;

            DocumentState.ApplyTransform(new AppliedOperation(transform), ref caretIndex);
            Assert.AreEqual(3, caretIndex);
        }
        public void DocumentState_ApplyTransform_CaretSameForInsertAfter()
        {
            var DocumentState = new DocumentState(1, "1234");
            var transform     = new InsertOperation(DocumentState, 3, 'a');
            var caretIndex    = 1;

            DocumentState.ApplyTransform(new AppliedOperation(transform), ref caretIndex);
            Assert.AreEqual(1, caretIndex);
        }
        public void DocumentState_ApplyTransform_CaretClampedForDeleteBefore()
        {
            var DocumentState = new DocumentState(1, "1234");
            var transform     = new DeleteOperation(DocumentState, 0);
            var caretIndex    = 0;

            DocumentState.ApplyTransform(new AppliedOperation(transform), ref caretIndex);
            Assert.IsTrue(caretIndex >= 0);
        }
        public void DocumentState_ApplyTransform_CaretClampedForInsertBefore()
        {
            var DocumentState = new DocumentState(1, "1234");
            var transform     = new InsertOperation(DocumentState, 0, 'a');
            var caretIndex    = 4;

            DocumentState.ApplyTransform(new AppliedOperation(transform), ref caretIndex);
            Assert.IsTrue(caretIndex <= DocumentState.CurrentState.Length);
        }
        public void DocumentState_ApplyTransform_CaretAdjustedForDeleteBefore()
        {
            var DocumentState = new DocumentState(1, "1234");
            var transform     = new DeleteOperation(DocumentState, 0);
            var caretIndex    = 2;

            DocumentState.ApplyTransform(new AppliedOperation(transform), ref caretIndex);
            Assert.AreEqual(1, caretIndex);
        }