Skip to content

Commit 5a0c0b3

Browse files
committed
retry component loading
1 parent 9e7a477 commit 5a0c0b3

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.1.5",
2+
"version": "0.1.6",
33
"license": "MIT",
44
"repository": {
55
"type": "git",

src/abode.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,31 @@ export let componentSelector = 'data-component';
2222
export let components: RegisteredComponents = {};
2323
export let unPopulatedElements: Element[] = [];
2424

25-
export const register = (name: string, fn: () => Promise<any>) => {
26-
components[name] = fn();
25+
export const register = async (name: string, fn: () => Promise<any>) => {
26+
components[name] = await retry(fn, 10, 20);
27+
};
28+
29+
export const unRegisterAllComponents = () => {
30+
components = {};
31+
};
32+
33+
export const delay = (ms: number) => new Promise(res => setTimeout(res, ms));
34+
35+
const retry = async (
36+
fn: () => any,
37+
times: number,
38+
delayTime: number
39+
): Promise<any> => {
40+
try {
41+
return await fn();
42+
} catch (err) {
43+
if (times > 1) {
44+
await delay(delayTime);
45+
return retry(fn, times - 1, delayTime * 2);
46+
} else {
47+
throw new Error(err);
48+
}
49+
}
2750
};
2851

2952
export const setComponentSelector = (selector: string) => {
@@ -86,7 +109,7 @@ export const renderAbode = async (el: Element) => {
86109
at => at.name === componentSelector
87110
)?.value;
88111

89-
if (!componentName) {
112+
if (!componentName || componentName === '') {
90113
throw new Error(
91114
`not all react-abode elements have a value for ${componentSelector}`
92115
);

test/TestComponent.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
3+
const TestComponent = (): JSX.Element => {
4+
return <div>testing 1 2 3 </div>;
5+
};
6+
7+
export default TestComponent;

test/abode.test.tsx

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import {
55
setUnpopulatedElements,
66
getElementProps,
77
setAttributes,
8+
renderAbode,
9+
register,
10+
unRegisterAllComponents,
11+
components,
812
} from '../src/abode';
913

1014
describe('getCleanPropName', () => {});
@@ -54,14 +58,53 @@ describe('helper functions', () => {
5458
expect(abodeElement.getAttribute('classname')).toBe('test-class-name');
5559
});
5660

57-
it.skip('getSriptProps', () => {});
58-
it.skip('renderAbode', () => {});
61+
it('renderAbode without component name set', async () => {
62+
const abodeElement = document.createElement('div');
63+
abodeElement.setAttribute('data-component', '');
64+
65+
let err;
66+
try {
67+
await renderAbode(abodeElement);
68+
} catch (error) {
69+
err = error;
70+
}
71+
72+
expect(err.message).toEqual(
73+
'not all react-abode elements have a value for data-component'
74+
);
75+
});
76+
77+
it('renderAbode without component registered', async () => {
78+
const abodeElement = document.createElement('div');
79+
abodeElement.setAttribute('data-component', 'TestComponent');
80+
81+
let err;
82+
try {
83+
await renderAbode(abodeElement);
84+
} catch (error) {
85+
err = error;
86+
}
87+
88+
expect(err.message).toEqual('no component registered for TestComponent');
89+
});
5990
});
6091

6192
describe('exported functions', () => {
62-
it.skip('register', () => {});
93+
beforeEach(() => {
94+
document.getElementsByTagName('html')[0].innerHTML = '';
95+
unRegisterAllComponents();
96+
});
97+
98+
it('register', async () => {
99+
await register('TestComponent', () => import('./TestComponent'));
100+
expect(Object.keys(components)).toEqual(['TestComponent']);
101+
expect(Object.values(components).length).toEqual(1);
102+
expect(Object.keys(Object.values(components)[0])).toEqual(['default']);
103+
});
104+
63105
it.skip('getActiveComponents', () => {});
64106
it.skip('setComponentSelector', () => {});
65107
it.skip('register', () => {});
66108
it.skip('populate', () => {});
109+
it.skip('getSriptProps', () => {});
67110
});

0 commit comments

Comments
 (0)