fix: observer PR cleanup (#8484)

leftovers from #8022
pull/8485/head
Simon H 2 years ago committed by GitHub
parent cd690e025b
commit 9425f18e52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -765,20 +765,33 @@ export default class ElementWrapper extends Wrapper {
`); `);
binding_group.events.forEach(name => { binding_group.events.forEach(name => {
const resizeListenerFunctions = { if (['elementresize', 'elementresizecontentbox', 'elementresizeborderbox', 'elementresizedevicepixelcontentbox'].indexOf(name) !== -1) {
elementresize: 'add_iframe_resize_listener',
elementresizecontentbox: 'resize_observer_content_box.observe',
elementresizeborderbox: 'resize_observer_border_box.observe',
elementresizedevicepixelcontentbox: 'resize_observer_device_pixel_content_box.observe'
};
if (name in resizeListenerFunctions) {
const resize_listener = block.get_unique_name(`${this.var.name}_resize_listener`); const resize_listener = block.get_unique_name(`${this.var.name}_resize_listener`);
block.add_variable(resize_listener); block.add_variable(resize_listener);
block.chunks.mount.push( // Can't dynamically do `@fn[name]`, code-red doesn't know how to resolve it
b`${resize_listener} = @${resizeListenerFunctions[name]}(${this.var}, ${callee}.bind(${this.var}));` switch (name) {
); case 'elementresize':
block.chunks.mount.push(
b`${resize_listener} = @add_iframe_resize_listener(${this.var}, ${callee}.bind(${this.var}));`
);
break;
case 'elementresizecontentbox':
block.chunks.mount.push(
b`${resize_listener} = @resize_observer_content_box.observe(${this.var}, ${callee}.bind(${this.var}));`
);
break;
case 'elementresizeborderbox':
block.chunks.mount.push(
b`${resize_listener} = @resize_observer_border_box.observe(${this.var}, ${callee}.bind(${this.var}));`
);
break;
case 'elementresizedevicepixelcontentbox':
block.chunks.mount.push(
b`${resize_listener} = @resize_observer_device_pixel_content_box.observe(${this.var}, ${callee}.bind(${this.var}));`
);
break;
}
block.chunks.destroy.push( block.chunks.destroy.push(
b`${resize_listener}();` b`${resize_listener}();`

@ -1,3 +1,5 @@
import { globals } from './globals';
/** /**
* Resize observer singleton. * Resize observer singleton.
* One listener per element only! * One listener per element only!
@ -15,19 +17,19 @@ export class ResizeObserverSingleton {
}; };
} }
static readonly entries: WeakMap<Element, ResizeObserverEntry> = 'WeakMap' in globalThis ? new WeakMap() : undefined; private readonly _listeners: WeakMap<Element, Listener> = 'WeakMap' in globals ? new WeakMap() : undefined;
private readonly _listeners: WeakMap<Element, Listener> = 'WeakMap' in globalThis ? new WeakMap() : undefined;
private _observer?: ResizeObserver; private _observer?: ResizeObserver;
private _getObserver() { private _getObserver() {
return this._observer ?? (this._observer = new ResizeObserver((entries) => { return this._observer ?? (this._observer = new ResizeObserver((entries) => {
for (const entry of entries) { for (const entry of entries) {
ResizeObserverSingleton.entries.set(entry.target, entry); (ResizeObserverSingleton as any).entries.set(entry.target, entry);
this._listeners.get(entry.target)?.(entry); this._listeners.get(entry.target)?.(entry);
} }
})); }));
} }
} }
// Needs to be written like this to pass the tree-shake-test
(ResizeObserverSingleton as any).entries = 'WeakMap' in globals ? new WeakMap() : undefined;
type Listener = (entry: ResizeObserverEntry)=>any; type Listener = (entry: ResizeObserverEntry)=>any;

@ -747,9 +747,9 @@ export function add_iframe_resize_listener(node: HTMLElement, fn: () => void) {
}; };
} }
export const resize_observer_content_box = new ResizeObserverSingleton({ box: 'content-box' }); export const resize_observer_content_box = /* @__PURE__ */ new ResizeObserverSingleton({ box: 'content-box' });
export const resize_observer_border_box = new ResizeObserverSingleton({ box: 'border-box' }); export const resize_observer_border_box = /* @__PURE__ */ new ResizeObserverSingleton({ box: 'border-box' });
export const resize_observer_device_pixel_content_box = new ResizeObserverSingleton({ box: 'device-pixel-content-box' }); export const resize_observer_device_pixel_content_box = /* @__PURE__ */ new ResizeObserverSingleton({ box: 'device-pixel-content-box' });
export { ResizeObserverSingleton }; export { ResizeObserverSingleton };
export function toggle_class(element, name, toggle) { export function toggle_class(element, name, toggle) {

@ -1,8 +1,8 @@
/* generated by Svelte vX.Y.Z */ /* generated by Svelte vX.Y.Z */
import { import {
SvelteComponent, SvelteComponent,
add_iframe_resize_listener,
add_render_callback, add_render_callback,
add_resize_listener,
detach, detach,
element, element,
init, init,
@ -23,7 +23,7 @@ function create_fragment(ctx) {
}, },
m(target, anchor) { m(target, anchor) {
insert(target, div, anchor); insert(target, div, anchor);
div_resize_listener = add_resize_listener(div, /*div_elementresize_handler*/ ctx[2].bind(div)); div_resize_listener = add_iframe_resize_listener(div, /*div_elementresize_handler*/ ctx[2].bind(div));
}, },
p: noop, p: noop,
i: noop, i: noop,

@ -1,8 +1,8 @@
/* generated by Svelte vX.Y.Z */ /* generated by Svelte vX.Y.Z */
import { import {
SvelteComponent, SvelteComponent,
add_iframe_resize_listener,
add_render_callback, add_render_callback,
add_resize_listener,
detach, detach,
element, element,
init, init,
@ -42,7 +42,7 @@ function create_fragment(ctx) {
}, },
m(target, anchor) { m(target, anchor) {
insert(target, video, anchor); insert(target, video, anchor);
video_resize_listener = add_resize_listener(video, /*video_elementresize_handler*/ ctx[7].bind(video)); video_resize_listener = add_iframe_resize_listener(video, /*video_elementresize_handler*/ ctx[7].bind(video));
if (!mounted) { if (!mounted) {
dispose = [ dispose = [

Loading…
Cancel
Save