【问题标题】:What is the "ownerID" in Immutable.js?Immutable.js 中的“ownerID”是什么?
【发布时间】:2016-03-14 21:51:55
【问题描述】:

我正在浏览 Immutable.js 的源代码,其中有一个我不明白的 ownerID 字段。

这是Map.asMutable()Map.asImmutable() 的来源:https://github.com/facebook/immutable-js/blob/master/src/Map.js#L171

似乎可变对象和不可变对象之间的唯一区别是它们的ownerIDs。什么是ownerID,它的用途是什么?

【问题讨论】:

  • 没有一个答案给出明确的解释,这是我目前的理解:ownerID 基本上是isMutable。但是,无论出于何种原因,它们都需要一种比较可变对象的方法,因此它们需要每个对象的唯一标识符。 isMutable 表示它是一个布尔值,因此他们将其命名为 ownerID。它可以重命名为mutableObjectID
  • 如果您查看下面我的答案的最后一部分,您会看到他们的代码正在做什么(我认为)是通过检查 ownerID 是否已更改来检查对象是否已发生变异.如果它发生了变异,它们会返回一个新对象。如果它没有发生变异或ownerId 不存在,它们只会返回当前对象,因为没有理由创建一个新对象。
  • 这不是__altered 的用途吗?似乎他们也必须将对象与其他对象进行比较。我不知道为什么。

标签: javascript immutable.js


【解决方案1】:

如果您追溯该属性:

L#14:

import { DELETE, SHIFT, SIZE, MASK, NOT_SET, CHANGE_LENGTH, DID_ALTER, OwnerID,
          MakeRef, SetRef, arrCopy } from './TrieUtils'

src/TrieUtils.js 中:

L#36:

// A function which returns a value representing an "owner" for transient writes
// to tries. The return value will only ever equal itself, and will not equal
// the return of any subsequent call of this function.
export function OwnerID() {}

这是他们创建的一些属性,例如哈希来代表虚拟所有者。

【讨论】:

  • 我知道new OwnerID 基本上是Symbol()。虚拟所有者是什么意思,这与不变性有何关系?
  • @Linksku 添加 ID 作为属性就像创建一个唯一的哈希,但他们称之为所有者,你可能有 2 个相同的对象具有不同的哈希,这意味着它们不是同一个对象,这与不变性无关,您的问题也与不变性无关,您只是想知道 OwnerID 是什么,对吗?
  • 它与不可变性有关,因为在 ImmutableJS 中可变对象和不可变对象之间的唯一区别是 ownerID 属性。我想了解他们为什么添加此属性以及它的用途。
  • 这个答案实际上没有帮助
【解决方案2】:

它用于确保asMutable 返回的实例中的可变性。当asMutable 被调用时,它会确保__ownerId 并返回当前实例 -

asMutable() {
    return this.__ownerID ? this : this.__ensureOwner(new OwnerID());
}

然后任何supported 变异操作都返回当前实例,而不是创建具有更改的新实例(这是不变性的关键)。

例如,这里是“清除”方法如何根据__ownerId 的存在来操作 -

clear() {
    if (this.size === 0) {
      return this;
    }
    if (this.__ownerID) {
      this.size = 0;
      this._root = null;
      this.__hash = undefined;
      this.__altered = true;
      return this;
    }
    return emptyMap();
}

请注意,当 this.__ownerID 存在时,该方法返回当前实例(因此 mutating 本身)。但是当它不存在时,它会返回一个新的映射以确保不变性。

【讨论】:

    【解决方案3】:

    来自source code

    // A function which returns a value representing an "owner" for transient writes
    // to tries. The return value will only ever equal itself, and will not equal
    // the return of any subsequent call of this function.
    function OwnerID() {}
    

    我对上面的理解是this.__ownerID这个字段是用来比较对象的。 Map 与其自身进行比较将具有相同的ownerID,而将Map 与另一个Map 进行比较将看到两个不同的ownerIDs。

    您可以在little farther down in the file in question 中查看此用法的示例:

    __ensureOwner(ownerID) {
      if (ownerID === this.__ownerID) {
        return this;
      }
      if (!ownerID) {
        this.__ownerID = ownerID;
        this.__altered = false;
        return this;
      }
      return makeMap(this.size, this._root, ownerID, this.__hash);
    }
    

    事实上,searching the entire repo,你会看到这个函数在数据类型中是通用的,每种类型都有一个稍微修改过的版本,以返回该类型的正确新版本。

    【讨论】:

      猜你喜欢
      • 2017-02-17
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 2017-01-17
      • 2021-06-16
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      相关资源
      最近更新 更多