New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[reactive-element] Controller that references a #private field in hostConnected crashes when transpiled using tslib #2446
Comments
|
I don't see any problems in the code there. I think this might be a TypeScript bug. |
|
This is just an issue with subclass hooks. The subclass's This is equivalent to the following code: class Host {
addController(controller) {
controller.hostConnected();
}
}
class BaseController {
constructor(host) {
host.addController(this);
}
}
class SubController extends BaseController {
constructor(host) {
super(host);
/* The field is initialized _after_ super() returns */
this._hasDisconnected = false;
}
hostConnected() {
if (this._hasDisconnected === undefined) {
throw new Error('Cannot read private member from an object whose class did not declare it');
}
if (this._hasDisconnected) {
this._hasDisconnected = false;
}
}
}
new SubController(new Host());
// Error! |
|
Argh, another problem that Dart's two-pass constructor feature fixes. Thanks for pointing this out @jridgewell The only robust way I can think of avoiding this is to move the responsibility of adding a controller from the controller itself to the host. That way it can be done after all subclass construction. export class SimpleGreeting extends LitElement {
// we'd make addController return the controller
private cont = this.addController(new Cont(this));
}or, in connectedCallback: export class SimpleGreeting extends LitElement {
private cont?: Cont;
connectedCallback() {
super.connectedCallback();
// we'd make addController return the controller
this.cont = this.addController(new Cont(this));
}
} |
|
I've encountered this as well, with my project using latest The relevant line is here, which transpiles to
This seems to be what I'm experiencing. For reasons of client connection encapsulation I've been instantiating |
GraphQL context now fully resolves async init logic before rendering app elements adminWebSocket URI removed from GraphQL init to avoid unnecessary Lair auth which Tauri windows are privileged to use without includes some workarounds to bypass context for GraphQL client, avoiding lit/lit#2446 (comment)
Description
#privatefields.host.addController(this)in its constructor. 3. The controller accessed a private field in itshostConnectedcallbackWhen the controller is instantiated on a class field (per typical usage), everything works fine:
lit playground
However, when the controller is instantiated after the
constructor, e.g. inconnectedCallback, it fails, as tslib's private-field-specific WeakMap is yet uninitialized by the time the controller tries to access it.Steps to Reproduce
Live Reproduction Link
playground link
Expected Results
The controller instantiates without error
Actual Results
Above error thrown
Browsers Affected
The text was updated successfully, but these errors were encountered: