Skip to content

Commit ebfaa7b

Browse files
committed
Add hookableComponent to named export in Reactium core. Clean up state usage in Router. Add IDs to number of hooks.
1 parent 7d77506 commit ebfaa7b

File tree

8 files changed

+56
-75
lines changed

8 files changed

+56
-75
lines changed

.core/app/index.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const App = async () => {
2121
console.log('Loading Core SDK');
2222
const {
2323
default: Reactium,
24-
useHookComponent,
24+
hookableComponent,
2525
isBrowserWindow,
2626
Zone,
2727
} = await import('reactium-core/sdk');
@@ -33,11 +33,6 @@ export const App = async () => {
3333
Reactium.Hook.runSync('sdk-init', Reactium);
3434
await Reactium.Hook.run('sdk-init', Reactium);
3535

36-
const hookableComponent = name => props => {
37-
const Component = useHookComponent(name);
38-
return <Component {...props} />;
39-
};
40-
4136
const context = {};
4237

4338
/**

.core/app/reactium-hooks.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import('reactium-core/sdk').then(async ({ default: Reactium }) => {
7474
return Promise.resolve();
7575
},
7676
Reactium.Enums.priority.highest,
77+
'REACTIUM_COMPONENT_BINDINGS',
7778
);
7879

7980
Reactium.Hook.register(
@@ -86,6 +87,7 @@ import('reactium-core/sdk').then(async ({ default: Reactium }) => {
8687
return Promise.resolve();
8788
},
8889
Reactium.Enums.priority.highest,
90+
'REACTIUM_PLUGIN_DEPENDENCIES',
8991
);
9092

9193
Reactium.Hook.register(
@@ -95,6 +97,7 @@ import('reactium-core/sdk').then(async ({ default: Reactium }) => {
9597
return Promise.resolve();
9698
},
9799
Reactium.Enums.priority.highest,
100+
'REACTIUM_APP_BINDPOINT',
98101
);
99102

100103
const getSaneZoneComponents = () => {
@@ -121,11 +124,16 @@ import('reactium-core/sdk').then(async ({ default: Reactium }) => {
121124
);
122125
};
123126

124-
Reactium.Hook.register('zone-defaults', async context => {
125-
op.set(context, 'controls', deps().plugableConfig);
126-
op.set(context, 'components', getSaneZoneComponents());
127-
console.log('Initializing Content Zones');
128-
});
127+
Reactium.Hook.register(
128+
'zone-defaults',
129+
async context => {
130+
op.set(context, 'controls', deps().plugableConfig);
131+
op.set(context, 'components', getSaneZoneComponents());
132+
console.log('Initializing Content Zones');
133+
},
134+
Reactium.Enums.priority.highest,
135+
'REACTIUM_ZONE_DEFAULTS',
136+
);
129137

130138
const NoopProvider = ({ children }) => children;
131139
Reactium.Hook.register(
@@ -147,6 +155,7 @@ import('reactium-core/sdk').then(async ({ default: Reactium }) => {
147155
console.log('Defining Router.');
148156
},
149157
Reactium.Enums.priority.highest,
158+
'REACTIUM_APP_ROUTER',
150159
);
151160

152161
Reactium.Hook.register(
@@ -156,6 +165,7 @@ import('reactium-core/sdk').then(async ({ default: Reactium }) => {
156165
return Promise.resolve();
157166
},
158167
Reactium.Enums.priority.highest,
168+
'REACTIUM_APP_SSR_MODE',
159169
);
160170

161171
Reactium.Hook.register(
@@ -174,6 +184,7 @@ import('reactium-core/sdk').then(async ({ default: Reactium }) => {
174184
return Promise.resolve();
175185
},
176186
Reactium.Enums.priority.highest,
187+
'REACTIUM_APP_BOOT_MESSAGE',
177188
);
178189
});
179190

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
1-
import React, { useRef, useState, useEffect, Fragment } from 'react';
2-
import Reactium, { useRouting } from 'reactium-core/sdk';
3-
import op from 'object-path';
4-
import { Route, useRouteMatch } from 'react-router';
1+
import React, { useEffect } from 'react';
2+
import Reactium, { useRouting, useSyncState } from 'reactium-core/sdk';
3+
import { Route } from 'react-router';
54

65
const useRoutes = () => {
7-
const routesRef = useRef(Reactium.Routing.get());
8-
const [value, setValue] = useState(routesRef.current);
9-
6+
const routeState = useSyncState(Reactium.Routing.get());
107
const setState = () => {
11-
routesRef.current = Reactium.Routing.get();
12-
setValue(routesRef.current);
8+
routeState.set(Reactium.Routing.get());
139
};
1410

1511
useEffect(() => {
1612
setState();
1713
return Reactium.Routing.subscribe(setState);
18-
}, [Reactium.Routing.updated]);
14+
}, []);
1915

20-
return routesRef.current;
16+
return routeState;
2117
};
2218

2319
const RoutedContent = () => {
2420
// cause rerender if routes are added/removed at runtime
2521
useRoutes();
2622
const routing = useRouting();
27-
const { active } = routing;
28-
const route = op.get(active, 'match.route');
29-
const Component = op.get(route, 'component');
23+
const route = routing.get('active.match.route');
24+
const Component = routing.get('active.match.route.component');
3025

3126
// if we have a route with no component, let react-router handle it however it will
32-
return Component ? <Component {...routing} /> : <Route {...route} />;
27+
return Component ? <Component {...routing.get()} /> : <Route {...route} />;
3328
};
3429

3530
export default RoutedContent;

.core/components/Router/reactium-hooks.js

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
1-
import Reactium, { isBrowserWindow } from 'reactium-core/sdk';
2-
import React, { forwardRef } from 'react';
3-
import op from 'object-path';
1+
import Reactium, {
2+
hookableComponent,
3+
isBrowserWindow,
4+
} from 'reactium-core/sdk';
45
import _ from 'underscore';
5-
import getComponents from 'dependencies/getComponents';
66
import RoutedContent from './RoutedContent';
77
import deps from 'dependencies';
88

9-
const lookupRouteComponent = async route => {
10-
let Found;
11-
if (typeof route.component === 'string') {
12-
route.component = Reactium.Component.get(
13-
route.component,
14-
op.get(
15-
getComponents([{ type: route.component }]),
16-
route.component,
17-
forwardRef(() => null),
18-
),
19-
);
20-
}
21-
22-
return route;
23-
};
24-
259
Reactium.Hook.register(
2610
'routes-init',
2711
async Routing => {
@@ -65,16 +49,20 @@ Reactium.Hook.register(
6549
}
6650
},
6751
Reactium.Enums.priority.highest,
52+
'REACTIUM_ROUTES_INIT',
6853
);
6954

70-
Reactium.Hook.register('register-route', lookupRouteComponent);
55+
Reactium.Hook.register(
56+
'register-route',
57+
async route => {
58+
if (typeof route.component === 'string') {
59+
route.component = hookableComponent(route.component);
60+
}
7161

72-
let { NotFound = null } = getComponents([{ type: 'NotFound' }]);
73-
if (NotFound !== null)
74-
Reactium.Component.register(
75-
'NotFound',
76-
NotFound,
77-
Reactium.Enums.priority.highest,
78-
);
62+
return route;
63+
},
64+
Reactium.Enums.priority.highest,
65+
'REACTIUM_REGISTER_ROUTE',
66+
);
7967

8068
Reactium.Component.register('RoutedContent', RoutedContent);

.core/dependencies/getComponents.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
import {
2-
isBrowserWindow,
3-
useHookComponent,
4-
} from '@atomic-reactor/reactium-sdk-core';
5-
import React, { Suspense, lazy } from 'react';
6-
import manifestLoader from 'manifest';
7-
8-
const hookableComponent = name => props => {
9-
const Component = useHookComponent(name);
10-
return <Component {...props} />;
11-
};
1+
import { hookableComponent } from '../sdk/named-exports';
122

133
export default (elms = []) =>
144
elms.reduce((cmps, { type, path }) => {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { useHookComponent } from '@atomic-reactor/reactium-sdk-core';
2+
3+
export const hookableComponent = name => props => {
4+
const Component = useHookComponent(name);
5+
return <Component {...props} />;
6+
};

.core/sdk/named-exports/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './setting';
44
export * from './window';
55
export * from './i18n';
66
export * from './routing';
7+
export * from './hookable-component';

.core/sdk/named-exports/routing.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import uuid from 'uuid/v4';
2-
import { useState, useRef, useEffect } from 'react';
2+
import { useEffect } from 'react';
33
import Routing from '../routing';
4+
import { useSyncState } from '@atomic-reactor/reactium-sdk-core';
45

56
export const useRouting = () => {
6-
const [, update] = useState(new Date());
7-
const routing = useRef({
7+
const routing = useSyncState({
88
current: Routing.currentRoute,
99
previous: Routing.previousRoute,
1010
active: Routing.currentRoute,
@@ -14,12 +14,7 @@ export const useRouting = () => {
1414
});
1515

1616
const handler = (updates, forceRefresh = true) => {
17-
routing.current = {
18-
...routing.current,
19-
...updates,
20-
};
21-
22-
if (forceRefresh === true) update(new Date());
17+
routing.set(updates, undefined, forceRefresh);
2318
};
2419

2520
const refreshFromRouting = () => {
@@ -58,5 +53,5 @@ export const useRouting = () => {
5853
};
5954
}, []);
6055

61-
return routing.current;
56+
return routing;
6257
};

0 commit comments

Comments
 (0)