Skip to content

Commit

Permalink
Bootstrap dynamic imports changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Oksydan committed Oct 16, 2023
1 parent 6a2f048 commit 41edf41
Showing 1 changed file with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ const useBootstrapComponentDynamicImport = (importFiles, {
let filesLoaded = false;
const callStack = [];
const jQueryCallStack = [];
const instancesMap = new Map();

const setInstanceInMap = (element, instance) => {
if (!instancesMap.has(element)) {
instancesMap.set(element, instance);
}
}

Check failure on line 19 in _dev/js/theme/utils/dynamicImports/useBootstrapComponentDynamicImport.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Missing semicolon

Check failure on line 19 in _dev/js/theme/utils/dynamicImports/useBootstrapComponentDynamicImport.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Missing semicolon

const getInstanceFromMap = (element) => {
if (!instancesMap.has(element)) {
return null;
}

return instancesMap.get(element);
}

Check failure on line 27 in _dev/js/theme/utils/dynamicImports/useBootstrapComponentDynamicImport.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Missing semicolon

Check failure on line 27 in _dev/js/theme/utils/dynamicImports/useBootstrapComponentDynamicImport.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

Missing semicolon

if (!componentName) {
throw new Error('Component name is required');
Expand Down Expand Up @@ -40,18 +55,18 @@ const useBootstrapComponentDynamicImport = (importFiles, {
const pluginInstance = getComponentInstance(element);

Check failure on line 55 in _dev/js/theme/utils/dynamicImports/useBootstrapComponentDynamicImport.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

'getComponentInstance' was used before it was defined

Check failure on line 55 in _dev/js/theme/utils/dynamicImports/useBootstrapComponentDynamicImport.js

View workflow job for this annotation

GitHub Actions / Code quality - ESLint

'getComponentInstance' was used before it was defined

if (pluginInstance) {
return pluginInstance.instanceProxy;
return pluginInstance.proxyInstance;
}

return new ComponentObjectConstructorFunction(element);
}
const proxyInstance = new ComponentObjectConstructorFunction(element);

const getComponentInstance = (element) => {
const pluginInstance = callStack.find(({ element: instanceElement }) => instanceElement === element);
setInstanceInMap(element, proxyInstance);

return pluginInstance ? pluginInstance.componentInstance : null;
return proxyInstance;
}

const getComponentInstance = (element) => getInstanceFromMap(element);

const proxyFactory = (pluginInstance) => {
const pluginObject = {};
const proxyHandler = {
Expand Down Expand Up @@ -83,14 +98,13 @@ const useBootstrapComponentDynamicImport = (importFiles, {
args,
instanceMethodCall: [],
componentInstance: null,
element: null,
}

pluginInstance.instanceProxy = proxyFactory(pluginInstance);
pluginInstance.proxyInstance = proxyFactory(pluginInstance);

callStack.push(pluginInstance);

return pluginInstance.instanceProxy;
return pluginInstance.proxyInstance;
};

ComponentObjectConstructorFunction.getOrCreateInstance = getOrCreateInstance;
Expand All @@ -110,7 +124,8 @@ const useBootstrapComponentDynamicImport = (importFiles, {
componentInstance = new window.bootstrap[componentName](args);

callStack[i].componentInstance = componentInstance;
callStack[i].element = componentInstance._element;

setInstanceInMap(componentInstance._element, componentInstance);

instanceMethodCall.forEach(({ prop, args }) => {
componentInstance[prop](...args);
Expand Down

0 comments on commit 41edf41

Please sign in to comment.