From 83e9cc5d5d405c9cbd20176c0bc636bb782178a6 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Sun, 5 Dec 2021 11:36:42 +0700 Subject: [PATCH] Remove queue implementation for dirty components Found a way to make it work without changing the dirty components array into a queue structure, reducing bundle size. --- src/runtime/internal/scheduler.ts | 11 ++++++---- src/runtime/internal/utils.ts | 34 ------------------------------- 2 files changed, 7 insertions(+), 38 deletions(-) diff --git a/src/runtime/internal/scheduler.ts b/src/runtime/internal/scheduler.ts index d275920295..73bd5e8395 100644 --- a/src/runtime/internal/scheduler.ts +++ b/src/runtime/internal/scheduler.ts @@ -1,7 +1,7 @@ -import { run_all, Queue } from './utils'; +import { run_all } from './utils'; import { get_current_component, set_current_component } from './lifecycle'; -export const dirty_components = new Queue(); +export const dirty_components = []; export const intros = { enabled: false }; export const binding_callbacks = []; @@ -32,6 +32,7 @@ export function add_flush_callback(fn) { } const seen_callbacks = new Set(); +let flushidx = 0; // Do *not* move this inside the flush() function export function flush() { let current_component = null; @@ -44,14 +45,16 @@ export function flush() { do { // first, call beforeUpdate functions // and update components - while (dirty_components.length) { - const component = dirty_components.shift(); + while (flushidx < dirty_components.length) { + const component = dirty_components[flushidx]; + flushidx++; set_current_component(component); update(component.$$); } set_current_component(null); dirty_components.length = 0; + flushidx = 0; while (binding_callbacks.length) binding_callbacks.pop()(); diff --git a/src/runtime/internal/utils.ts b/src/runtime/internal/utils.ts index 582870ec32..8868e38ee2 100644 --- a/src/runtime/internal/utils.ts +++ b/src/runtime/internal/utils.ts @@ -58,40 +58,6 @@ export function is_empty(obj) { return Object.keys(obj).length === 0; } -export class Queue { - forward: T[]; - reverse: T[]; - - constructor() { - this.forward = []; - this.reverse = []; - } - push(value: T) { - return this.forward.push(value); - } - shift() { - if (this.reverse.length === 0) { - while (this.forward.length) { - this.reverse.push(this.forward.pop()); - } - } - return this.reverse.pop(); - } - get length() { - return this.forward.length + this.reverse.length; - } - set length(len: number) { - if (len === 0) { - this.forward.length = 0; - this.reverse.length = 0; - } else { - while (this.length > len) { - this.shift(); - } - } - } -} - export function validate_store(store, name) { if (store != null && typeof store.subscribe !== 'function') { throw new Error(`'${name}' is not a store with a 'subscribe' method`);