Skip to content

Instantly share code, notes, and snippets.

@bensheldon
Last active September 12, 2025 17:44
Show Gist options
  • Save bensheldon/c708cef377f42b2c0c06aad16f233064 to your computer and use it in GitHub Desktop.
Save bensheldon/c708cef377f42b2c0c06aad16f233064 to your computer and use it in GitHub Desktop.
const origDefine = customElements.define;
customElements.define = function(name, ctor, options) {
if (name !== 'turbo-cable-stream-source') return origDefine.call(this, name, ctor, options);
const proto = ctor.prototype;
const origDisconnectedCallback = proto.disconnectedCallback;
const origConnectedCallback = proto.connectedCallback;
proto.connectedCallback = async function() {
if (this.disconnecting) {
this.disconnecting = false;
return;
}
return await origConnectedCallback.apply(this);
};
proto.disconnectedCallback = async function() {
// defer for one tick to allow the dom node to be re-attached, if turbo-permanent
this.disconnecting = true;
await new Promise(resolve => setTimeout(resolve, 0))
if (!this.disconnecting) { return; }
return origDisconnectedCallback.apply(this);
};
return origDefine.call(this, name, ctor, options);
};
// do this before importing turbo-rails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment