ValentinKuzub

ValentinKuzub Valentin​Kuzub

Niner since 2011

Comments

  • Rx Workshop: Observables versus Events

    I dont  think you should be modifying original OnTextChanged, heres my code

     

     class Events
        {
            private string m_LastString = string.Empty;
    
            public event Action<string> TextChanged;
    
            private Action<string> m_ToAttachToTextChanged;
            private Action<int> m_LengthChanged;
            
            public Events()
            {
                m_ToAttachToTextChanged = text => {
                    if (m_LastString.Length != (text ?? "").Length) {
                        m_LengthChanged((text ?? "").Length);
                    }
                    m_LastString = text ?? "";
                };
            }
    
            public virtual void OnTextChanged(string text)
            {
                var t = TextChanged;
                if (t != null)
                    t(text);
            }
    
            public event Action<int> LengthChanged
            {
                add
                {
                    m_LengthChanged += value;
                    TextChanged += m_ToAttachToTextChanged;
                }
                remove
                {
                    m_LengthChanged -= value;
                    TextChanged -= m_ToAttachToTextChanged;
                }
            }
        }