From 8cfed175d44a5248b9d38be260e811668b3e838c Mon Sep 17 00:00:00 2001 From: Michael Wheeler Date: Mon, 18 Mar 2024 12:29:41 -0500 Subject: [PATCH] feat: Remove useless queue --- src/initWidgets.js | 31 ++++++++++++++-------------- src/queue.js | 37 ---------------------------------- test/queue.test.js | 50 ---------------------------------------------- 3 files changed, 16 insertions(+), 102 deletions(-) delete mode 100644 src/queue.js delete mode 100644 test/queue.test.js diff --git a/src/initWidgets.js b/src/initWidgets.js index 4907a03..f04ece9 100644 --- a/src/initWidgets.js +++ b/src/initWidgets.js @@ -1,4 +1,3 @@ -import queue from './queue'; import { extractOptions } from '@g2crowd/extract-options'; import camelize from './camelize'; import { strategies } from './strategies'; @@ -62,30 +61,32 @@ const wrapPlugin = function wrapPlugin(name, pluginFn, element) { }; }; -const loadWidget = function (element, name, widgetQueue, registered, initiatedWidgets) { - const pluginFn = registered[name]; +async function startWidget(name, pluginFn, element) { + const wrapped = wrapPlugin(name, pluginFn, element); + const strategy = strategies.get(pluginFn.init); + + strategy(wrapped, element); +} + +const loadWidget = async function (element, name, availableWidgets, initiatedWidgets) { + const pluginFn = availableWidgets[name]; if (!pluginFn) { return; } - const existingPlugin = initiatedWidgets.get(name, element); - const wrapped = wrapPlugin(name, pluginFn, element); + if (initiatedWidgets.get(name, element)) { + return; + } - if (!existingPlugin) { - widgetQueue.add(() => { - strategies.get(pluginFn.init)(wrapped, element); - }); - widgetQueue.flush(); + startWidget(name, pluginFn, element); - initiatedWidgets.set(name, element, true); - element.dataset[`vvidget_${camelize(name)}`] = true; - } + initiatedWidgets.set(name, element, true); + element.dataset[`vvidget_${camelize(name)}`] = true; }; const initiatedWidgets = widgetTracker(); export const widgetInitiator = function ({ attr, data, registered }, fn = loadWidget) { - const widgetQueue = queue(); registered = registered || {}; return function initWidgets(elements) { @@ -95,7 +96,7 @@ export const widgetInitiator = function ({ attr, data, registered }, fn = loadWi names .split(' ') .filter((i) => i) - .forEach((name) => fn(element, name, widgetQueue, registered, initiatedWidgets)); + .forEach((name) => fn(element, name, registered, initiatedWidgets)); }); }; }; diff --git a/src/queue.js b/src/queue.js deleted file mode 100644 index 4b17614..0000000 --- a/src/queue.js +++ /dev/null @@ -1,37 +0,0 @@ -// @format - -function queue() { - let list = []; - let completed = null; - - function flush() { - list.forEach((i) => i()); - list = []; - } - - return { - add(item) { - list.push(item); - }, - - flush() { - if (completed === null) { - completed = new Promise(function (resolve) { - window.setTimeout(function () { - flush(); - completed = null; - resolve(); - }, 0); - }); - } - - return completed; - }, - - size() { - return list.length; - } - }; -} - -export default queue; diff --git a/test/queue.test.js b/test/queue.test.js deleted file mode 100644 index 3531b52..0000000 --- a/test/queue.test.js +++ /dev/null @@ -1,50 +0,0 @@ -// @format - -import queue from '../src/queue'; - -test('can add a function', () => { - let q = queue(); - - q.add(jest.fn()); - - expect(q.size()).toEqual(1); -}); - -test('flush calls asynchronously', () => { - let q = queue(); - let fn = jest.fn(); - q.add(fn); - q.flush(); - - return q.flush().then(() => { - expect(fn.mock.calls.length).toEqual(1); - }); -}); - -test('flush will run fns added after initial call', () => { - let q = queue(); - let fn = jest.fn(); - let fn2 = jest.fn(); - q.add(fn); - q.flush(); - q.add(fn2); - - return q.flush().then(() => { - expect(fn2.mock.calls.length).toEqual(1); - }); -}); - -test('flush will run again after delayed completion', (done) => { - let q = queue(); - let fn = jest.fn(); - let fn2 = jest.fn(); - q.add(fn); - - q.flush().then(() => { - q.add(fn2); - q.flush().then(() => { - expect(fn2.mock.calls.length).toEqual(1); - done(); - }); - }); -});