protected override void Init()

in rd-net/RdFramework/Impl/RdProperty.cs [124:167]


    protected override void Init(Lifetime lifetime, IProtocol proto, SerializationCtx ctx)
    {
      base.Init(lifetime, proto, ctx);

      var maybe = Maybe;
      var hasInitValue = maybe.HasValue;
      if (hasInitValue && !OptimizeNested)
        maybe.Value.BindPolymorphic();

      Advise(lifetime, v =>
      {
        var shouldIdentify = !hasInitValue;
        hasInitValue = false;

        if (!IsLocalChange)
          return;

        if (!OptimizeNested && shouldIdentify)
        {
          // We need to terminate the current lifetime to unbind the existing value before assigning a new value, especially in cases where we are reassigning it.
          Memory.VolatileRead(ref myBindDefinition)?.Terminate();

          v.IdentifyPolymorphic(proto.Identities, proto.Identities.Next(RdId));

          var prevDefinition = Interlocked.Exchange(ref myBindDefinition, TryPreBindValue(lifetime, v, false));
          prevDefinition?.Terminate();
        }

        if (IsMaster) myMasterVersion++;

        proto.Wire.Send(RdId, SendContext.Of(ctx, v, this), static (sendContext, writer) =>
        {
          var sContext = sendContext.SzrCtx;
          var evt = sendContext.Event;
          var me = sendContext.This;
          writer.WriteInt32(me.myMasterVersion);
          me.WriteValueDelegate(sContext, writer, evt);
          SendTrace?.Log($"{me} :: ver = {me.myMasterVersion}, value = {me.Value.PrintToString()}");
        });

        if (!OptimizeNested && shouldIdentify)
          v.BindPolymorphic();
      });
    }