Skip to content

Commit a8abaf6

Browse files
committed
feat(BlueBase): Added reboot method
1 parent ee0b796 commit a8abaf6

File tree

4 files changed

+128
-31
lines changed

4 files changed

+128
-31
lines changed

src/BlueBase.tsx

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,27 @@ import {
1717
} from './registries';
1818

1919
import { MaybeRenderPropChildren } from './utils';
20-
import React from 'react';
2120
import systemFilters from './filters';
2221

22+
export interface BlueBaseProgress {
23+
/**
24+
* Has the app booted yet
25+
*/
26+
readonly booted: boolean;
27+
28+
/**
29+
* Are we loading the app
30+
*/
31+
readonly loading: boolean;
32+
33+
/**
34+
* Any errors occured while booting the app
35+
*/
36+
readonly error: any;
37+
}
38+
39+
export type SetStateFn = (state: BlueBaseProgress) => Promise<void>;
40+
2341
export interface BootOptions {
2442
/** Collection of assets to add in BlueBase's Asset Registry. */
2543
assets: AssetCollection;
@@ -47,6 +65,21 @@ export interface BootOptions {
4765
children?: MaybeRenderPropChildren<{ BB: BlueBase }>;
4866
}
4967

68+
export interface BootOptionsInternal extends BootOptions {
69+
onProgress?: SetStateFn;
70+
reset?: boolean;
71+
}
72+
73+
const emptyBootOptions: BootOptionsInternal = {
74+
assets: {},
75+
components: {},
76+
configs: {},
77+
filters: {},
78+
fonts: {},
79+
plugins: [],
80+
themes: [],
81+
};
82+
5083
export class BlueBase {
5184
// APIs
5285
public Analytics = new Analytics(this);
@@ -67,20 +100,57 @@ export class BlueBase {
67100
// Flags
68101
public booted = false;
69102

70-
private bootOptions: BootOptions = {
71-
assets: {},
72-
components: {},
73-
configs: {},
74-
filters: {},
75-
fonts: {},
76-
plugins: [],
77-
themes: [],
78-
};
79-
80-
public async boot(options?: Partial<BootOptions> & { children?: React.ReactNode }) {
103+
private bootOptions: BootOptions = emptyBootOptions;
104+
105+
public async boot({ onProgress, ...options }: Partial<BootOptionsInternal> = {}) {
106+
// Store onProgress for later use, even after boot finishes (i.e. in reboot, etc)
107+
if (onProgress) {
108+
this.onProgress = onProgress;
109+
}
110+
111+
await this.onProgress({
112+
booted: false,
113+
error: null,
114+
loading: true,
115+
});
116+
117+
try {
118+
await this.bootInternal(options);
119+
await this.onProgress({
120+
booted: this.booted,
121+
error: null,
122+
loading: false,
123+
});
124+
} catch (error) {
125+
await this.onProgress({
126+
booted: false,
127+
error,
128+
loading: false,
129+
});
130+
}
131+
132+
return this;
133+
}
134+
135+
/**
136+
* Performs a reset and boot.
137+
*/
138+
public async reboot(options?: BootOptionsInternal) {
139+
return this.boot({ ...options, reset: true });
140+
}
141+
142+
/**
143+
* Main boot business logic
144+
* @param param
145+
*/
146+
protected async bootInternal({ reset, ...options }: Partial<BootOptionsInternal> = {}) {
81147
// Update boot options
82148
this.bootOptions = { ...this.bootOptions, ...options };
83149

150+
if (reset === true) {
151+
await this.Filters.run('bluebase.system.reset', this.bootOptions);
152+
}
153+
84154
// Register basic filters here, so they can be used in boot
85155
await this.Filters.registerNestedCollection(systemFilters);
86156

@@ -92,4 +162,8 @@ export class BlueBase {
92162

93163
return this;
94164
}
165+
166+
private onProgress: SetStateFn = async () => {
167+
return;
168+
}
95169
}

src/OfflineComponents/BlueBaseApp/BlueBaseApp.tsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BlueBase, BootOptions } from '../../BlueBase';
1+
import { BlueBase, BlueBaseProgress, BootOptions } from '../../BlueBase';
22
import {
33
BlueBaseAppError,
44
BlueBaseAppErrorProps,
@@ -80,20 +80,11 @@ export class BlueBaseApp extends React.Component<BlueBaseAppProps, BlueBaseAppSt
8080

8181
async componentDidMount() {
8282
const BB = this.state.BB;
83+
BB.boot({ ...this.props, onProgress: this.onProgress });
84+
}
8385

84-
try {
85-
await BB.boot(this.props);
86-
this.setState({
87-
booted: BB.booted,
88-
loading: false,
89-
});
90-
} catch (error) {
91-
this.setState({
92-
booted: false,
93-
error,
94-
loading: false,
95-
});
96-
}
86+
onProgress = async (params: BlueBaseProgress) => {
87+
await this.setState(params);
9788
}
9889

9990
componentDidCatch(error: Error | null) {

src/OfflineComponents/BlueBaseApp/__tests__/BlueBaseApp.test.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { waitForElement, waitForState } from 'enzyme-async-helpers';
2+
13
import { BlueBase } from '../../../BlueBase';
24
import { BlueBaseApp } from '../BlueBaseApp';
5+
import { BlueBaseAppError } from '../../BlueBaseAppError';
36
import React from 'react';
47
import { Text } from 'react-native';
58
import { mount } from 'enzyme';
6-
import { waitForState } from 'enzyme-async-helpers';
79

810
declare const global: any;
911

@@ -73,12 +75,14 @@ describe('BlueBaseApp', () => {
7375
test(`should render error state when boot throws an error`, async () => {
7476
const BB = new BlueBase();
7577
BB.Configs.setValue('development', true);
76-
BB.boot = () => {
78+
(BB as any).bootInternal = () => {
7779
throw Error('Boot Error!');
7880
};
7981

8082
const wrapper = mount(<BlueBaseApp BB={BB} />);
8183

84+
await waitForElement(wrapper, BlueBaseAppError);
85+
8286
// expect(wrapper).toMatchSnapshot();
8387
expect(
8488
wrapper
@@ -149,6 +153,8 @@ describe('BlueBaseApp', () => {
149153
await waitForState(wrapper as any, (state: any) => state.loading === false);
150154
wrapper.update();
151155

156+
await waitForElement(wrapper, BlueBaseAppError);
157+
152158
// expect(wrapper).toMatchSnapshot();
153159
expect(
154160
wrapper
@@ -184,6 +190,8 @@ describe('BlueBaseApp', () => {
184190
await waitForState(wrapper as any, (state: any) => state.loading === false);
185191
wrapper.update();
186192

193+
await waitForElement(wrapper, BlueBaseAppError);
194+
187195
// expect(wrapper).toMatchSnapshot();
188196
expect(
189197
wrapper
@@ -202,12 +210,14 @@ describe('BlueBaseApp', () => {
202210
// tslint:disable-next-line: max-line-length
203211
test(`should render error state with actual error message when development config is undefined, && isProduction is false`, async () => {
204212
const BB = new BlueBase();
205-
BB.boot = () => {
213+
(BB as any).bootInternal = () => {
206214
throw Error('Boot Error!');
207215
};
208216

209217
const wrapper = mount(<BlueBaseApp BB={BB} />);
210218

219+
await waitForElement(wrapper, BlueBaseAppError);
220+
211221
// expect(wrapper).toMatchSnapshot();
212222
expect(
213223
wrapper
@@ -226,14 +236,16 @@ describe('BlueBaseApp', () => {
226236
// tslint:disable-next-line: max-line-length
227237
test(`should render error state with custom error message when development config is undefined, && isProduction is true`, async () => {
228238
const BB = new BlueBase();
229-
BB.boot = () => {
239+
(BB as any).bootInternal = () => {
230240
throw Error('Boot Error!');
231241
};
232242

233243
global.process.env.NODE_ENV = 'production';
234244

235245
const wrapper = mount(<BlueBaseApp BB={BB} />);
236246

247+
await waitForElement(wrapper, BlueBaseAppError);
248+
237249
// expect(wrapper).toMatchSnapshot();
238250
expect(
239251
wrapper

src/filters/boot.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const boot: FilterNestedCollection = {
66
'bluebase.boot': [
77
{
88
key: 'bluebase-boot-default',
9-
priority: 5,
9+
priority: 3,
1010

1111
// tslint:disable-next-line:object-literal-sort-keys
1212
value: async (bootOptions: BootOptions, _ctx: {}, BB: BlueBase) => {
@@ -48,4 +48,24 @@ export const boot: FilterNestedCollection = {
4848
},
4949
},
5050
],
51+
52+
'bluebase.system.reset': [
53+
{
54+
key: 'bluebase-reset-default',
55+
priority: 3,
56+
57+
// tslint:disable-next-line:object-literal-sort-keys
58+
value: async (bootOptions: BootOptions, _ctx: {}, BB: BlueBase) => {
59+
BB.Assets.clear();
60+
BB.Components.clear();
61+
BB.Configs.clear();
62+
BB.Filters.clear();
63+
BB.Fonts.clear();
64+
BB.Plugins.clear();
65+
BB.Themes.clear();
66+
67+
return bootOptions;
68+
},
69+
},
70+
],
5171
};

0 commit comments

Comments
 (0)