From 463e25770acc5e590a45b2f1db500d75d75fbd18 Mon Sep 17 00:00:00 2001 From: Ivan Hofer Date: Fri, 20 Nov 2020 07:43:44 +0100 Subject: [PATCH] improve output size by destructuring $$ --- src/runtime/internal/Component.ts | 61 +++++++++++++++++-------------- src/runtime/internal/scheduler.ts | 5 ++- src/runtime/internal/utils.ts | 4 +- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/runtime/internal/Component.ts b/src/runtime/internal/Component.ts index 6c14c71b4c..4c4958ce93 100644 --- a/src/runtime/internal/Component.ts +++ b/src/runtime/internal/Component.ts @@ -36,11 +36,11 @@ interface T$$ { skip_bound: boolean; } -export function bind(component, name, callback) { - const index = component.$$.props[name]; +export function bind({ $$ }, name, callback) { + const index = $$.props[name]; if (index !== undefined) { - component.$$.bound[index] = callback; - callback(component.$$.ctx[index]); + $$.bound[index] = callback; + callback($$.ctx[index]); } } @@ -52,8 +52,8 @@ export function claim_component(block, parent_nodes) { block && block.l(parent_nodes); } -export function mount_component(component, target, anchor) { - const { fragment, on_mount, on_destroy, after_update } = component.$$; +export function mount_component({ $$ }, target, anchor) { + const { fragment, on_mount, on_destroy, after_update } = $$; fragment && fragment.m(target, anchor); @@ -67,18 +67,18 @@ export function mount_component(component, target, anchor) { // most likely as a result of a binding initialising run_all(new_on_destroy); } - component.$$.on_mount = []; + $$.on_mount = []; }); after_update.forEach(add_render_callback); } -export function destroy_component(component, detaching) { - const $$ = component.$$; - if ($$.fragment !== null) { +export function destroy_component({ $$ }, detaching) { + const { fragment } = $$; + if (fragment !== null) { run_all($$.on_destroy); - $$.fragment && $$.fragment.d(detaching); + fragment && fragment.d(detaching); // TODO null out other refs, including component.$$ (but need to // preserve final state?) @@ -88,12 +88,13 @@ export function destroy_component(component, detaching) { } function make_dirty(component, i) { - if (component.$$.dirty[0] === -1) { + const { $$ } = component; + if ($$.dirty[0] === -1) { dirty_components.push(component); schedule_update(); - component.$$.dirty.fill(0); + $$.dirty.fill(0); } - component.$$.dirty[(i / 31) | 0] |= (1 << (i % 31)); + $$.dirty[(i / 31) | 0] |= (1 << (i % 31)); } export function init(component, options, instance, create_fragment, not_equal, props, dirty = [-1]) { @@ -146,17 +147,19 @@ export function init(component, options, instance, create_fragment, not_equal, p $$.fragment = create_fragment ? create_fragment($$.ctx) : false; if (options.target) { + const { fragment } = $$; + if (options.hydrate) { const nodes = children(options.target); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - $$.fragment && $$.fragment!.l(nodes); + fragment && fragment!.l(nodes); nodes.forEach(detach); } else { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - $$.fragment && $$.fragment!.c(); + fragment && fragment!.c(); } - if (options.intro) transition_in(component.$$.fragment); + if (options.intro) transition_in(fragment); mount_component(component, options.target, options.anchor); flush(); } @@ -193,7 +196,8 @@ if (typeof HTMLElement === 'function') { $on(type, callback) { // TODO should this delegate to addEventListener? - const callbacks = (this.$$.callbacks[type] || (this.$$.callbacks[type] = [])); + const { $$ } = this; + const callbacks = ($$.callbacks[type] || ($$.callbacks[type] = [])); callbacks.push(callback); return () => { @@ -203,10 +207,11 @@ if (typeof HTMLElement === 'function') { } $set($$props) { - if (this.$$set && !is_empty($$props)) { - this.$$.skip_bound = true; - this.$$set($$props); - this.$$.skip_bound = false; + const { $$set, $$ } = this; + if ($$set && !is_empty($$props)) { + $$.skip_bound = true; + $$set($$props); + $$.skip_bound = false; } } }; @@ -222,7 +227,8 @@ export class SvelteComponent { } $on(type, callback) { - const callbacks = (this.$$.callbacks[type] || (this.$$.callbacks[type] = [])); + const { $$ } = this; + const callbacks = ($$.callbacks[type] || ($$.callbacks[type] = [])); callbacks.push(callback); return () => { @@ -232,10 +238,11 @@ export class SvelteComponent { } $set($$props) { - if (this.$$set && !is_empty($$props)) { - this.$$.skip_bound = true; - this.$$set($$props); - this.$$.skip_bound = false; + const { $$set, $$ } = this; + if ($$set && !is_empty($$props)) { + $$.skip_bound = true; + $$set($$props); + $$.skip_bound = false; } } } diff --git a/src/runtime/internal/scheduler.ts b/src/runtime/internal/scheduler.ts index 9d9e6ec539..0e2c3a417d 100644 --- a/src/runtime/internal/scheduler.ts +++ b/src/runtime/internal/scheduler.ts @@ -78,12 +78,13 @@ export function flush() { } function update($$) { - if ($$.fragment !== null) { + const { fragment } = $$; + if (fragment !== null) { $$.update(); run_all($$.before_update); const dirty = $$.dirty; $$.dirty = [-1]; - $$.fragment && $$.fragment.p($$.ctx, dirty); + fragment && fragment.p($$.ctx, dirty); $$.after_update.forEach(add_render_callback); } diff --git a/src/runtime/internal/utils.ts b/src/runtime/internal/utils.ts index 928882552a..dffee56cbe 100644 --- a/src/runtime/internal/utils.ts +++ b/src/runtime/internal/utils.ts @@ -64,8 +64,8 @@ export function get_store_value(store: Readable): T { return value; } -export function component_subscribe(component, store, callback) { - component.$$.on_destroy.push(subscribe(store, callback)); +export function component_subscribe({ $$ }, store, callback) { + $$.on_destroy.push(subscribe(store, callback)); } export function create_slot(definition, ctx, $$scope, fn) {