【发布时间】:2019-02-27 01:07:56
【问题描述】:
tl;博士:
我的类的方法名称与它扩展的类的同名冲突。我可以重命名我的班级,但我更喜欢不需要重大更改的解决方案。
背景
我发布了一个名为lit-apollo 的库,它导出了一个从LitElement 扩展而来的class。用户应该使用我的类定义自己的customElements,然后这些元素可以使用 apollo graphql 获取数据,并使用 lit-html 进行渲染。一个简单的例子见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 或类似的方法,但是我如何在不破坏我的类的 API 并因此需要专业的情况下解决这个问题版本?
我虽然检查了第一个参数以查看它是否是 ApolloCache 的实例,然后根据需要将 args 路由到 super.update,但我认为这会破坏 LitElement 合同,阻止用户实现他们自己的版本LitElement 的update
你会如何处理这种情况?
【问题讨论】:
标签: javascript apollo es6-class lit-html