处理 class 中的方法名称冲突 JavaScript

Dealing with class method name conflicts in JavaScript

tl;博士:

我的 class' 方法名称与其扩展的 class 上的相同名称冲突。我可以重命名我的 class,但我更喜欢不需要重大更改的解决方案。

背景

为了一个简单的例子,我发布了一个名为 lit-apollo, which exports a class that extends from LitElement. Users are meant to define their own customElements with my class, those elements then can use apollo graphql for data, and render using lit-html. See README 的库。

问题

LitElement 的最新版本公开了一个名为 update 的实例方法,实现可以选择重写以控制元素呈现的方式和时间。 我的库还有一个update属性,对应update option of Apollo mutation constructors.

import gql from 'graphql-tag'
import { ApolloMutation, html } from 'lit-apollo/apollo-mutation'

const mutation = gql`
    mutation($id: ID!) {
      MyMutation(id: $id) {
        myResponse
      }
    }
`

const updateFunc = (cache, response) => 
  cache.writeData(doSomethingWith(cache, response))

class MutatingElement extends ApolloMutation {
  constructor() {
      this.mutation = mutation;
      this.variables = {id: "foo"};
      // here's where we break the LitElement contract
      this.update = updateFunc;
  }

  render() {
    return html`<div>${this.data.myResponse}</div>`
  }
}

customElements.define('mutating-element', MutatingElement)

这两种方法,显然是相互冲突的。

问题

我知道我可以对 lit-apollo 进行重大更改,将其自己的 update 方法重命名为 onUpdate 或类似的方法,但是我如何在不破坏我的方法的情况下解决这个问题classes 的 API,因此需要主要版本?

虽然我检查了第一个参数以查看它是否是 ApolloCache 的实例,然后根据需要将参数路由到 super.update,但我认为这会破坏 LitElement 合同,从而阻止用户实现他们自己的 LitElement 版本 update

你会如何处理这种情况?

I know I could just issue a breaking change to lit-apollo, but how might I address this problem without breaking my classes' APIs?

你不能,真的。但这不是你的错:

LitElement's latest version exposes an instance method called update

是突破性的变化。因此,如果您将 lit-element 依赖项更新为最新版本,则还必须创建一个主要版本。为此重命名您的 update 方法是很自然的。或者,保留您的 API 并继续使用旧的 lit-element 版本。