piral-core 0.15.0-alpha.3555 → 0.15.0-alpha.3564
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/components/ErrorBoundary.d.ts +12 -33
- package/esm/components/ErrorBoundary.js +12 -14
- package/esm/components/ErrorBoundary.js.map +1 -1
- package/{debug-piral.d.ts → esm/debugger.d.ts} +1 -1
- package/esm/debugger.js +52 -0
- package/esm/debugger.js.map +1 -0
- package/{debug-pilet.d.ts → esm/emulator.d.ts} +1 -1
- package/esm/emulator.js +8 -0
- package/esm/emulator.js.map +1 -0
- package/esm/helpers.js +2 -2
- package/esm/helpers.js.map +1 -1
- package/esm/modules/core.js +8 -6
- package/esm/modules/core.js.map +1 -1
- package/esm/state/withApi.d.ts +1 -1
- package/esm/state/withApi.js +31 -33
- package/esm/state/withApi.js.map +1 -1
- package/esm/types/api.d.ts +12 -8
- package/esm/utils/index.d.ts +1 -0
- package/esm/utils/index.js +1 -0
- package/esm/utils/index.js.map +1 -1
- package/lib/components/ErrorBoundary.d.ts +12 -33
- package/lib/components/ErrorBoundary.js +12 -14
- package/lib/components/ErrorBoundary.js.map +1 -1
- package/lib/debugger.d.ts +4 -0
- package/lib/debugger.js +56 -0
- package/lib/debugger.js.map +1 -0
- package/lib/emulator.d.ts +3 -0
- package/{debug-pilet.js → lib/emulator.js} +4 -3
- package/lib/emulator.js.map +1 -0
- package/lib/helpers.js +2 -2
- package/lib/helpers.js.map +1 -1
- package/lib/modules/core.js +8 -6
- package/lib/modules/core.js.map +1 -1
- package/lib/state/withApi.d.ts +1 -1
- package/lib/state/withApi.js +30 -32
- package/lib/state/withApi.js.map +1 -1
- package/lib/types/api.d.ts +12 -8
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/index.js +4 -0
- package/lib/utils/index.js.map +1 -1
- package/package.json +9 -11
- package/src/components/ErrorBoundary.tsx +19 -51
- package/src/debugger.ts +82 -0
- package/src/emulator.ts +10 -0
- package/src/helpers.tsx +2 -2
- package/src/modules/core.ts +10 -8
- package/src/state/withApi.test.tsx +20 -4
- package/src/state/withApi.tsx +51 -42
- package/src/types/api.ts +23 -9
- package/src/utils/index.ts +2 -0
- package/debug-piral.js +0 -72
package/src/modules/core.ts
CHANGED
|
@@ -17,28 +17,30 @@ export function createCoreApi(context: GlobalStateContext): PiletApiExtender<Pil
|
|
|
17
17
|
const expiration = getDataExpiration(expires);
|
|
18
18
|
return context.tryWriteDataItem(name, value, pilet, target, expiration);
|
|
19
19
|
},
|
|
20
|
-
registerPage(route, arg, meta) {
|
|
20
|
+
registerPage(route, arg, meta = {}) {
|
|
21
|
+
const component = withApi(context, arg, api, 'page', undefined, { meta });
|
|
21
22
|
context.registerPage(route, {
|
|
22
23
|
pilet,
|
|
23
24
|
meta,
|
|
24
|
-
component
|
|
25
|
+
component,
|
|
25
26
|
});
|
|
26
27
|
return () => api.unregisterPage(route);
|
|
27
28
|
},
|
|
28
29
|
unregisterPage(route) {
|
|
29
30
|
context.unregisterPage(route);
|
|
30
31
|
},
|
|
31
|
-
registerExtension(name,
|
|
32
|
-
context
|
|
32
|
+
registerExtension(name, reference, defaults) {
|
|
33
|
+
const component = withApi(context, reference, api, 'extension');
|
|
34
|
+
context.registerExtension(name, {
|
|
33
35
|
pilet,
|
|
34
|
-
component
|
|
35
|
-
reference
|
|
36
|
+
component,
|
|
37
|
+
reference,
|
|
36
38
|
defaults,
|
|
37
39
|
});
|
|
38
|
-
return () => api.unregisterExtension(name,
|
|
40
|
+
return () => api.unregisterExtension(name, reference);
|
|
39
41
|
},
|
|
40
42
|
unregisterExtension(name, arg) {
|
|
41
|
-
context.unregisterExtension(name
|
|
43
|
+
context.unregisterExtension(name, arg);
|
|
42
44
|
},
|
|
43
45
|
renderHtmlExtension(element, props) {
|
|
44
46
|
const [dispose] = renderElement(context, element, props);
|
|
@@ -58,7 +58,11 @@ StubComponent.displayName = 'StubComponent';
|
|
|
58
58
|
|
|
59
59
|
describe('withApi Module', () => {
|
|
60
60
|
it('wraps a component and forwards the API as piral', () => {
|
|
61
|
-
const api: any = {
|
|
61
|
+
const api: any = {
|
|
62
|
+
meta: {
|
|
63
|
+
name: 'foo',
|
|
64
|
+
},
|
|
65
|
+
};
|
|
62
66
|
const { context } = createMockContainer();
|
|
63
67
|
const Component = withApi(context, StubComponent, api, 'feed' as any);
|
|
64
68
|
const node = mount(<Component />);
|
|
@@ -67,7 +71,11 @@ describe('withApi Module', () => {
|
|
|
67
71
|
|
|
68
72
|
it('is protected against a component crash', () => {
|
|
69
73
|
console.error = jest.fn();
|
|
70
|
-
const api: any = {
|
|
74
|
+
const api: any = {
|
|
75
|
+
meta: {
|
|
76
|
+
name: 'foo',
|
|
77
|
+
},
|
|
78
|
+
};
|
|
71
79
|
const { context } = createMockContainer();
|
|
72
80
|
const Component = withApi(context, StubComponent, api, 'feed' as any);
|
|
73
81
|
const node = mount(<Component shouldCrash />);
|
|
@@ -88,7 +96,11 @@ describe('withApi Module', () => {
|
|
|
88
96
|
});
|
|
89
97
|
|
|
90
98
|
it('Wraps component of type object', () => {
|
|
91
|
-
const api: any = {
|
|
99
|
+
const api: any = {
|
|
100
|
+
meta: {
|
|
101
|
+
name: 'foo',
|
|
102
|
+
},
|
|
103
|
+
};
|
|
92
104
|
const { context } = createMockContainer();
|
|
93
105
|
context.converters = {
|
|
94
106
|
html: (component) => {
|
|
@@ -107,7 +119,11 @@ describe('withApi Module', () => {
|
|
|
107
119
|
});
|
|
108
120
|
|
|
109
121
|
it('Wraps component which is object == null.', () => {
|
|
110
|
-
const api: any = {
|
|
122
|
+
const api: any = {
|
|
123
|
+
meta: {
|
|
124
|
+
name: 'foo',
|
|
125
|
+
},
|
|
126
|
+
};
|
|
111
127
|
const { context } = createMockContainer();
|
|
112
128
|
context.converters = {
|
|
113
129
|
html: (component) => {
|
package/src/state/withApi.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { isfunc } from 'piral-base';
|
|
3
3
|
import { __RouterContext } from 'react-router';
|
|
4
|
-
import {
|
|
4
|
+
import { ErrorBoundary, PortalRenderer } from '../components';
|
|
5
5
|
import { useGlobalStateContext } from '../hooks';
|
|
6
6
|
import { defaultRender, convertComponent, none } from '../utils';
|
|
7
7
|
import {
|
|
@@ -20,6 +20,10 @@ let portalIdBase = 123456;
|
|
|
20
20
|
|
|
21
21
|
const DefaultWrapper: React.FC = (props) => defaultRender(props.children);
|
|
22
22
|
|
|
23
|
+
interface CapturedProps {
|
|
24
|
+
piral: PiletApi;
|
|
25
|
+
}
|
|
26
|
+
|
|
23
27
|
interface ForeignComponentContainerProps<T> {
|
|
24
28
|
$portalId: string;
|
|
25
29
|
$component: ForeignComponent<T>;
|
|
@@ -87,40 +91,34 @@ class ForeignComponentContainer<T> extends React.Component<ForeignComponentConta
|
|
|
87
91
|
|
|
88
92
|
function wrapReactComponent<T>(
|
|
89
93
|
Component: React.ComponentType<T & BaseComponentProps>,
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
Wrapper: React.ComponentType<any>,
|
|
94
|
+
captured: CapturedProps,
|
|
95
|
+
Wrapper: React.FC<T>,
|
|
93
96
|
): React.ComponentType<T> {
|
|
94
97
|
return (props: T) => (
|
|
95
|
-
<Wrapper {...props}
|
|
96
|
-
<
|
|
97
|
-
<Component {...props} piral={piral} />
|
|
98
|
-
</ErrorBoundary>
|
|
98
|
+
<Wrapper {...props}>
|
|
99
|
+
<Component {...props} {...captured} />
|
|
99
100
|
</Wrapper>
|
|
100
101
|
);
|
|
101
102
|
}
|
|
102
103
|
|
|
103
104
|
function wrapForeignComponent<T>(
|
|
104
105
|
component: ForeignComponent<T & BaseComponentProps>,
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
Wrapper: React.ComponentType<any>,
|
|
106
|
+
captured: CapturedProps,
|
|
107
|
+
Wrapper: React.FC<T>,
|
|
108
108
|
) {
|
|
109
109
|
return React.memo((props: T) => {
|
|
110
110
|
const { state, readState, destroyPortal } = useGlobalStateContext();
|
|
111
111
|
const router = React.useContext(__RouterContext);
|
|
112
112
|
const id = React.useMemo(() => (portalIdBase++).toString(26), none);
|
|
113
113
|
const context = React.useMemo(() => ({ router, state, readState }), [router, state]);
|
|
114
|
-
const innerProps = React.useMemo(() => ({ ...props,
|
|
114
|
+
const innerProps = React.useMemo(() => ({ ...props, ...captured }), [props]);
|
|
115
115
|
|
|
116
116
|
React.useEffect(() => () => destroyPortal(id), none);
|
|
117
117
|
|
|
118
118
|
return (
|
|
119
|
-
<Wrapper {...
|
|
120
|
-
<
|
|
121
|
-
|
|
122
|
-
<ForeignComponentContainer innerProps={innerProps} $portalId={id} $component={component} $context={context} />
|
|
123
|
-
</ErrorBoundary>
|
|
119
|
+
<Wrapper {...props}>
|
|
120
|
+
<PortalRenderer id={id} />
|
|
121
|
+
<ForeignComponentContainer innerProps={innerProps} $portalId={id} $component={component} $context={context} />
|
|
124
122
|
</Wrapper>
|
|
125
123
|
);
|
|
126
124
|
});
|
|
@@ -133,26 +131,30 @@ function isNotExotic(component: any): component is object {
|
|
|
133
131
|
function wrapComponent<T>(
|
|
134
132
|
converters: ComponentConverters<T & BaseComponentProps>,
|
|
135
133
|
component: AnyComponent<T & BaseComponentProps>,
|
|
136
|
-
|
|
137
|
-
Wrapper: React.
|
|
138
|
-
stasisOptions: ErrorBoundaryOptions<T>,
|
|
134
|
+
captured: CapturedProps,
|
|
135
|
+
Wrapper: React.FC<T>,
|
|
139
136
|
) {
|
|
140
|
-
if (!component) {
|
|
141
|
-
console.error('The given value is not a valid component.');
|
|
142
|
-
// tslint:disable-next-line:no-null-keyword
|
|
143
|
-
component = () => null;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
137
|
if (typeof component === 'object' && isNotExotic(component)) {
|
|
147
138
|
const result = convertComponent(converters[component.type], component);
|
|
148
|
-
return wrapForeignComponent<T>(result,
|
|
139
|
+
return wrapForeignComponent<T>(result, captured, Wrapper);
|
|
149
140
|
}
|
|
150
141
|
|
|
151
|
-
return wrapReactComponent<T>(component,
|
|
142
|
+
return wrapReactComponent<T>(component, captured, Wrapper);
|
|
152
143
|
}
|
|
153
144
|
|
|
154
145
|
function getWrapper(wrappers: Record<string, React.ComponentType<any>>, wrapperType: string) {
|
|
155
|
-
|
|
146
|
+
const WrapAll = wrappers['*'];
|
|
147
|
+
const WrapType = wrappers[wrapperType];
|
|
148
|
+
|
|
149
|
+
if (WrapAll && WrapType) {
|
|
150
|
+
return (props) => (
|
|
151
|
+
<WrapAll {...props}>
|
|
152
|
+
<WrapType {...props} />
|
|
153
|
+
</WrapAll>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return WrapType || WrapAll || DefaultWrapper;
|
|
156
158
|
}
|
|
157
159
|
|
|
158
160
|
export function withApi<TProps>(
|
|
@@ -161,19 +163,26 @@ export function withApi<TProps>(
|
|
|
161
163
|
piral: PiletApi,
|
|
162
164
|
errorType: keyof Errors,
|
|
163
165
|
wrapperType: string = errorType,
|
|
166
|
+
captured = {},
|
|
164
167
|
) {
|
|
168
|
+
const outerProps = { ...captured, piral };
|
|
165
169
|
const converters = context.converters;
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
170
|
+
const OuterWrapper = context.readState((m) => getWrapper(m.registry.wrappers, wrapperType));
|
|
171
|
+
|
|
172
|
+
const Wrapper: React.FC<TProps> = (props) => (
|
|
173
|
+
<OuterWrapper {...outerProps} {...props}>
|
|
174
|
+
<ErrorBoundary {...outerProps} {...props} errorType={errorType}>
|
|
175
|
+
{props.children}
|
|
176
|
+
</ErrorBoundary>
|
|
177
|
+
</OuterWrapper>
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
if (!component) {
|
|
181
|
+
const pilet = piral.meta.name;
|
|
182
|
+
console.error(`[${pilet}] The given value is not a valid component.`);
|
|
183
|
+
// tslint:disable-next-line:no-null-keyword
|
|
184
|
+
component = () => null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return wrapComponent<TProps>(converters, component, outerProps, Wrapper);
|
|
179
188
|
}
|
package/src/types/api.ts
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
import type { ReactElement } from 'react';
|
|
2
2
|
import type { RouteComponentProps } from 'react-router';
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
PiletApi,
|
|
5
|
+
Pilet,
|
|
6
|
+
PiletEntry,
|
|
7
|
+
PiletEntries,
|
|
8
|
+
PiletMetadata,
|
|
9
|
+
EventEmitter,
|
|
10
|
+
PiletLoader,
|
|
11
|
+
PiletLoadingStrategy,
|
|
12
|
+
} from 'piral-base';
|
|
4
13
|
import type { PiletCustomApi, PiralCustomPageMeta } from './custom';
|
|
5
14
|
import type { AnyComponent } from './components';
|
|
6
15
|
import type { ExtensionSlotProps, PiralExtensionSlotMap } from './extension';
|
|
7
16
|
import type { SharedData, DataStoreOptions } from './data';
|
|
8
17
|
import type { Disposable } from './utils';
|
|
9
18
|
|
|
10
|
-
export { PiletApi, Pilet, PiletMetadata, EventEmitter, PiletEntry, PiletEntries };
|
|
19
|
+
export { PiletApi, Pilet, PiletMetadata, EventEmitter, PiletEntry, PiletEntries, PiletLoader, PiletLoadingStrategy };
|
|
11
20
|
|
|
12
21
|
/**
|
|
13
22
|
* The props that every registered component obtains.
|
|
@@ -39,6 +48,11 @@ export interface ExtensionComponentProps<T> extends BaseComponentProps {
|
|
|
39
48
|
params: T extends keyof PiralExtensionSlotMap ? PiralExtensionSlotMap[T] : T extends string ? any : T;
|
|
40
49
|
}
|
|
41
50
|
|
|
51
|
+
/**
|
|
52
|
+
* The meta data registered for a page.
|
|
53
|
+
*/
|
|
54
|
+
export interface PiralPageMeta extends PiralCustomPageMeta {}
|
|
55
|
+
|
|
42
56
|
/**
|
|
43
57
|
* The props that every registered page component obtains.
|
|
44
58
|
*/
|
|
@@ -49,12 +63,12 @@ export interface RouteBaseProps<UrlParams = any, UrlState = any>
|
|
|
49
63
|
/**
|
|
50
64
|
* The props used by a page component.
|
|
51
65
|
*/
|
|
52
|
-
export interface PageComponentProps<T = any, S = any> extends RouteBaseProps<T, S> {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
66
|
+
export interface PageComponentProps<T = any, S = any> extends RouteBaseProps<T, S> {
|
|
67
|
+
/**
|
|
68
|
+
* The meta data registered with the page.
|
|
69
|
+
*/
|
|
70
|
+
meta: PiralPageMeta;
|
|
71
|
+
}
|
|
58
72
|
|
|
59
73
|
/**
|
|
60
74
|
* Defines the Pilet API from piral-core.
|
|
@@ -126,7 +140,7 @@ export interface PiletCoreApi {
|
|
|
126
140
|
renderHtmlExtension<TName>(element: HTMLElement | ShadowRoot, props: ExtensionSlotProps<TName>): Disposable;
|
|
127
141
|
}
|
|
128
142
|
|
|
129
|
-
declare module 'piral-base/lib/types' {
|
|
143
|
+
declare module 'piral-base/lib/types/runtime' {
|
|
130
144
|
interface PiletApi extends PiletCustomApi, PiletCoreApi {}
|
|
131
145
|
}
|
|
132
146
|
|
package/src/utils/index.ts
CHANGED
package/debug-piral.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
14
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
15
|
-
if (ar || !(i in from)) {
|
|
16
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
17
|
-
ar[i] = from[i];
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
21
|
-
};
|
|
22
|
-
exports.__esModule = true;
|
|
23
|
-
exports.integrate = void 0;
|
|
24
|
-
var react_atom_1 = require("@dbeining/react-atom");
|
|
25
|
-
var piral_debug_utils_1 = require("piral-debug-utils");
|
|
26
|
-
function integrate(context, options, debug) {
|
|
27
|
-
if (debug === void 0) { debug = {}; }
|
|
28
|
-
(0, piral_debug_utils_1.installPiralDebug)(__assign(__assign({}, debug), { addPilet: context.addPilet, removePilet: context.removePilet, updatePilet: function (pilet) {
|
|
29
|
-
if (!pilet.disabled) {
|
|
30
|
-
var createApi = options.createApi;
|
|
31
|
-
var newApi = createApi(pilet);
|
|
32
|
-
try {
|
|
33
|
-
context.injectPilet(pilet);
|
|
34
|
-
pilet.setup(newApi);
|
|
35
|
-
}
|
|
36
|
-
catch (error) {
|
|
37
|
-
console.error(error);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
context.injectPilet(pilet);
|
|
42
|
-
}
|
|
43
|
-
}, fireEvent: context.emit, getDependencies: function () {
|
|
44
|
-
return Object.keys(options.dependencies);
|
|
45
|
-
}, getExtensions: function () {
|
|
46
|
-
return context.readState(function (s) { return Object.keys(s.registry.extensions); });
|
|
47
|
-
}, getRoutes: function () {
|
|
48
|
-
var registeredRoutes = context.readState(function (state) { return Object.keys(state.registry.pages); });
|
|
49
|
-
var componentRoutes = context.readState(function (state) { return Object.keys(state.routes); });
|
|
50
|
-
return __spreadArray(__spreadArray([], componentRoutes, true), registeredRoutes, true);
|
|
51
|
-
}, getGlobalState: function () {
|
|
52
|
-
return context.readState(function (s) { return s; });
|
|
53
|
-
}, getPilets: function () {
|
|
54
|
-
return context.readState(function (s) { return s.modules; });
|
|
55
|
-
}, integrate: function (dbg) {
|
|
56
|
-
context.dispatch(function (s) { return (__assign(__assign({}, s), { components: __assign(__assign({}, s.components), dbg.components), routes: __assign(__assign({}, s.routes), dbg.routes), registry: __assign(__assign({}, s.registry), { wrappers: __assign(__assign({}, s.registry.wrappers), dbg.wrappers) }) })); });
|
|
57
|
-
(0, react_atom_1.addChangeHandler)(context.state, 'debugging', function (_a) {
|
|
58
|
-
var previous = _a.previous, current = _a.current;
|
|
59
|
-
var pilets = current.modules !== previous.modules;
|
|
60
|
-
var pages = current.registry.pages !== previous.registry.pages || current.routes !== previous.routes;
|
|
61
|
-
var extensions = current.registry.extensions !== previous.registry.extensions;
|
|
62
|
-
var state = current !== previous;
|
|
63
|
-
dbg.onChange(previous, current, {
|
|
64
|
-
pilets: pilets,
|
|
65
|
-
pages: pages,
|
|
66
|
-
extensions: extensions,
|
|
67
|
-
state: state
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
} }));
|
|
71
|
-
}
|
|
72
|
-
exports.integrate = integrate;
|