示例#1
0
文件: RdMap.cs 项目: yvvan/rd
        protected override void Init(Lifetime lifetime)
        {
            base.Init(lifetime);

            var serializationContext = SerializationContext;

            using (UsingLocalChange())
            {
                Advise(lifetime, it =>
                {
                    if (!IsLocalChange)
                    {
                        return;
                    }

                    AssertNullability(it.Key);

                    if (it.Kind != AddUpdateRemove.Remove)
                    {
                        AssertNullability(it.NewValue);
                    }

                    if (!OptimizeNested && it.Kind != AddUpdateRemove.Remove)
                    {
                        it.NewValue.IdentifyPolymorphic(Proto.Identities, Proto.Identities.Next(RdId));
                    }

                    Wire.Send(RdId, SendContext.Of(serializationContext, it, this), (sendContext, stream) =>
                    {
                        var sContext      = sendContext.SzrCtx;
                        var evt           = sendContext.Event;
                        var me            = sendContext.This;
                        var versionedFlag = me.IsMaster ? 1 << versionedFlagShift : 0;
                        stream.Write(versionedFlag | (int)evt.Kind);

                        var version = ++me.myNextVersion;
                        if (me.IsMaster)
                        {
                            me.myPendingForAck[evt.Key] = version;
                            stream.Write(version);
                        }

                        me.WriteKeyDelegate(sContext, stream, evt.Key);
                        if (evt.Kind != AddUpdateRemove.Remove)
                        {
                            me.WriteValueDelegate(sContext, stream, evt.NewValue);
                        }


                        SendTrace?.Log($"{me} :: {evt.Kind} :: key = {evt.Key.PrintToString()}"
                                       + (me.IsMaster     ? " :: version = " + version : "")
                                       + (evt.Kind != AddUpdateRemove.Remove  ? " :: value = " + evt.NewValue.PrintToString() : ""));
                    });
                });
            }

            Wire.Advise(lifetime, this);


            if (!OptimizeNested) //means values must be bindable
            {
                this.View(lifetime, (lf, k, v) =>
                {
                    v.BindPolymorphic(lf, this, "[" + k + "]");
                });
            }
        }