Skip to content

Commit

Permalink
fix(core): plug fireChangeOnCreate leak
Browse files Browse the repository at this point in the history
  • Loading branch information
clshortfuse committed Aug 9, 2023
1 parent ad60541 commit 1063a85
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
4 changes: 1 addition & 3 deletions core/CustomElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ export default class CustomElement extends ICustomElement {
if (customCallbacks.length) {
this.onPropChanged(customCallbacks);
if (fireChangeOnCreate) {
this._onConstructedCallbacks.push(function onCreateChangeEmitter() {
this._addCallback('_onConstructedCallbacks', function onCreateChangeEmitter() {
const value = this[name];
if (customSimpleCallback) {
customSimpleCallback.call(this, undefined, value, null);
Expand Down Expand Up @@ -906,14 +906,12 @@ export default class CustomElement extends ICustomElement {
for (const callbacks of this.static._onConnectedCallbacks) {
callbacks.call(this, this.callbackArguments);
}
this.dispatchEvent(new Event('mdw:connected'));
}

disconnectedCallback() {
for (const callbacks of this.static._onDisconnectedCallbacks) {
callbacks.call(this, this.callbackArguments);
}
this.dispatchEvent(new Event('mdw:disconnected'));
}
}

Expand Down
15 changes: 14 additions & 1 deletion core/customTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ function elementStylerMicrotaskCallback(name) {
}
}

const pendingResizeCallbacks = new WeakMap();
const pendingConnections = new ResizeObserver((entries) => {
for(const {target} of entries) {
if (pendingResizeCallbacks.has(target)) {
const callback = pendingResizeCallbacks.get(target);
pendingResizeCallbacks.delete(target);
pendingConnections.unobserve(target);
callback();
}
}
});

/** @type {import('./typings.js').ObserverOptions<'object',ElementStylerOptions, CustomElement>} */
export const ELEMENT_STYLER_TYPE = {
type: 'object',
Expand All @@ -138,7 +150,8 @@ export const ELEMENT_STYLER_TYPE = {
if (this.isConnected) {
queueMicrotask(callback);
} else {
this.addEventListener('mdw:connected', callback, { once: true });
pendingResizeCallbacks.set(this, callback);
pendingConnections.observe(this);
}
},
fireChangeOnCreate: true,
Expand Down

0 comments on commit 1063a85

Please sign in to comment.