piral-core 0.15.0-alpha.3540 → 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.
Files changed (77) hide show
  1. package/esm/actions/app.d.ts +3 -1
  2. package/esm/actions/app.js +18 -0
  3. package/esm/actions/app.js.map +1 -1
  4. package/esm/components/ErrorBoundary.d.ts +12 -33
  5. package/esm/components/ErrorBoundary.js +12 -14
  6. package/esm/components/ErrorBoundary.js.map +1 -1
  7. package/esm/createInstance.js +5 -1
  8. package/esm/createInstance.js.map +1 -1
  9. package/{debug-piral.d.ts → esm/debugger.d.ts} +1 -1
  10. package/esm/debugger.js +52 -0
  11. package/esm/debugger.js.map +1 -0
  12. package/{debug-pilet.d.ts → esm/emulator.d.ts} +1 -1
  13. package/esm/emulator.js +8 -0
  14. package/esm/emulator.js.map +1 -0
  15. package/esm/helpers.d.ts +3 -2
  16. package/esm/helpers.js +4 -3
  17. package/esm/helpers.js.map +1 -1
  18. package/esm/modules/core.js +8 -6
  19. package/esm/modules/core.js.map +1 -1
  20. package/esm/modules/dependencies.d.ts +2 -2
  21. package/esm/modules/dependencies.js.map +1 -1
  22. package/esm/state/withApi.d.ts +1 -1
  23. package/esm/state/withApi.js +31 -33
  24. package/esm/state/withApi.js.map +1 -1
  25. package/esm/types/api.d.ts +12 -8
  26. package/esm/types/state.d.ts +13 -3
  27. package/esm/types/utils.d.ts +1 -1
  28. package/esm/utils/index.d.ts +1 -0
  29. package/esm/utils/index.js +1 -0
  30. package/esm/utils/index.js.map +1 -1
  31. package/lib/actions/app.d.ts +3 -1
  32. package/lib/actions/app.js +21 -1
  33. package/lib/actions/app.js.map +1 -1
  34. package/lib/components/ErrorBoundary.d.ts +12 -33
  35. package/lib/components/ErrorBoundary.js +12 -14
  36. package/lib/components/ErrorBoundary.js.map +1 -1
  37. package/lib/createInstance.js +5 -1
  38. package/lib/createInstance.js.map +1 -1
  39. package/lib/debugger.d.ts +4 -0
  40. package/lib/debugger.js +56 -0
  41. package/lib/debugger.js.map +1 -0
  42. package/lib/emulator.d.ts +3 -0
  43. package/lib/emulator.js +12 -0
  44. package/lib/emulator.js.map +1 -0
  45. package/lib/helpers.d.ts +3 -2
  46. package/lib/helpers.js +4 -3
  47. package/lib/helpers.js.map +1 -1
  48. package/lib/modules/core.js +8 -6
  49. package/lib/modules/core.js.map +1 -1
  50. package/lib/modules/dependencies.d.ts +2 -2
  51. package/lib/modules/dependencies.js.map +1 -1
  52. package/lib/state/withApi.d.ts +1 -1
  53. package/lib/state/withApi.js +30 -32
  54. package/lib/state/withApi.js.map +1 -1
  55. package/lib/types/api.d.ts +12 -8
  56. package/lib/types/state.d.ts +13 -3
  57. package/lib/types/utils.d.ts +1 -1
  58. package/lib/utils/index.d.ts +1 -0
  59. package/lib/utils/index.js +4 -0
  60. package/lib/utils/index.js.map +1 -1
  61. package/package.json +9 -11
  62. package/src/actions/app.ts +25 -0
  63. package/src/components/ErrorBoundary.tsx +19 -51
  64. package/src/createInstance.tsx +5 -1
  65. package/src/debugger.ts +82 -0
  66. package/src/emulator.ts +10 -0
  67. package/src/helpers.tsx +6 -2
  68. package/src/modules/core.ts +10 -8
  69. package/src/modules/dependencies.ts +3 -2
  70. package/src/state/withApi.test.tsx +20 -4
  71. package/src/state/withApi.tsx +51 -42
  72. package/src/types/api.ts +23 -9
  73. package/src/types/state.ts +13 -3
  74. package/src/types/utils.ts +1 -1
  75. package/src/utils/index.ts +2 -0
  76. package/debug-pilet.js +0 -12
  77. package/debug-piral.js +0 -59
@@ -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 { PiralError, PiralLoadingIndicator, ErrorBoundary, ErrorBoundaryOptions, PortalRenderer } from '../components';
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
- stasisOptions: ErrorBoundaryOptions<T>,
91
- piral: PiletApi,
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} piral={piral}>
96
- <ErrorBoundary {...stasisOptions} renderProps={props}>
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
- stasisOptions: ErrorBoundaryOptions<T>,
106
- piral: PiletApi,
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, piral }), [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 {...innerProps}>
120
- <ErrorBoundary {...stasisOptions} renderProps={props}>
121
- <PortalRenderer id={id} />
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
- piral: PiletApi,
137
- Wrapper: React.ComponentType<any>,
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, stasisOptions, piral, Wrapper);
139
+ return wrapForeignComponent<T>(result, captured, Wrapper);
149
140
  }
150
141
 
151
- return wrapReactComponent<T>(component, stasisOptions, piral, Wrapper);
142
+ return wrapReactComponent<T>(component, captured, Wrapper);
152
143
  }
153
144
 
154
145
  function getWrapper(wrappers: Record<string, React.ComponentType<any>>, wrapperType: string) {
155
- return wrappers[wrapperType] || wrappers['*'] || DefaultWrapper;
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 Wrapper = context.readState((m) => getWrapper(m.registry.wrappers, wrapperType));
167
-
168
- return wrapComponent<TProps>(converters, component, piral, Wrapper, {
169
- onError(error) {
170
- console.error(piral, error);
171
- },
172
- renderChild(child) {
173
- return <React.Suspense fallback={<PiralLoadingIndicator />}>{child}</React.Suspense>;
174
- },
175
- renderError(error, props: any) {
176
- return <PiralError type={errorType} error={error} {...props} />;
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 { PiletApi, Pilet, PiletMetadata, EventEmitter, SinglePilet, MultiPilet } from 'piral-base';
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, SinglePilet, MultiPilet };
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
- * The meta data registered for a page.
56
- */
57
- export interface PiralPageMeta extends PiralCustomPageMeta {}
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
 
@@ -12,7 +12,6 @@ import type {
12
12
  PiralCustomComponentsState,
13
13
  } from './custom';
14
14
  import type {
15
- PiletMetadata,
16
15
  EventEmitter,
17
16
  Pilet,
18
17
  BaseComponentProps,
@@ -20,6 +19,7 @@ import type {
20
19
  ExtensionComponentProps,
21
20
  PiletsBag,
22
21
  PiralPageMeta,
22
+ PiletEntry,
23
23
  } from './api';
24
24
  import type {
25
25
  ComponentConverters,
@@ -162,7 +162,7 @@ export interface GlobalState extends PiralCustomState {
162
162
  /**
163
163
  * Gets the loaded modules.
164
164
  */
165
- modules: Array<PiletMetadata>;
165
+ modules: Array<Pilet>;
166
166
  /**
167
167
  * The foreign component portals to render.
168
168
  */
@@ -205,10 +205,20 @@ export interface PiralActions extends PiralCustomActions {
205
205
  */
206
206
  initialize(loading: boolean, error: Error | undefined, modules: Array<Pilet>): void;
207
207
  /**
208
- * Injects a pilet at runtime - removes the pilet from registry first if available.
208
+ * Injects an evaluated pilet at runtime - removes the pilet from registry first if available.
209
209
  * @param pilet The pilet to be injected.
210
210
  */
211
211
  injectPilet(pilet: Pilet): void;
212
+ /**
213
+ * Adds a pilet at runtime by loading it, evaluating it, and then injecting it.
214
+ * @param pilet The pilet to be added.
215
+ */
216
+ addPilet(pilet: PiletEntry): void;
217
+ /**
218
+ * Removes a pilet by unloading it and deleting all component registrations.
219
+ * @param name The name of the pilet to remove.
220
+ */
221
+ removePilet(name: string): void;
212
222
  /**
213
223
  * Defines a single action for Piral.
214
224
  * @param actionName The name of the action to define.
@@ -60,7 +60,7 @@ export interface PiralStoreDataEvent<TValue = any> {
60
60
  expires: number;
61
61
  }
62
62
 
63
- declare module 'piral-base/lib/types' {
63
+ declare module 'piral-base/lib/types/api' {
64
64
  interface PiralEventMap extends PiralCustomEventMap {
65
65
  'store-data': PiralStoreDataEvent;
66
66
  }
@@ -1,3 +1,5 @@
1
+ export { isfunc, requireModule } from 'piral-base';
2
+
1
3
  export * from './compare';
2
4
  export * from './data';
3
5
  export * from './extension';
package/debug-pilet.js DELETED
@@ -1,12 +0,0 @@
1
- "use strict";
2
- exports.__esModule = true;
3
- exports.integrate = void 0;
4
- var piral_debug_utils_1 = require("piral-debug-utils");
5
- function integrate(context, options) {
6
- options.fetchPilets = (0, piral_debug_utils_1.withEmulatorPilets)(options.fetchPilets, {
7
- injectPilet: context.injectPilet,
8
- createApi: options.createApi,
9
- loadPilet: options.loadPilet
10
- });
11
- }
12
- exports.integrate = integrate;
package/debug-piral.js DELETED
@@ -1,59 +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), { createApi: options.createApi, loadPilet: options.loadPilet, injectPilet: context.injectPilet, fireEvent: context.emit, getDependencies: function () {
29
- return Object.keys(options.dependencies);
30
- }, getExtensions: function () {
31
- return context.readState(function (s) { return Object.keys(s.registry.extensions); });
32
- }, getRoutes: function () {
33
- var registeredRoutes = context.readState(function (state) { return Object.keys(state.registry.pages); });
34
- var componentRoutes = context.readState(function (state) { return Object.keys(state.routes); });
35
- return __spreadArray(__spreadArray([], componentRoutes, true), registeredRoutes, true);
36
- }, getGlobalState: function () {
37
- return context.readState(function (s) { return s; });
38
- }, getPilets: function () {
39
- return context.readState(function (s) { return s.modules; });
40
- }, setPilets: function (modules) {
41
- context.dispatch(function (state) { return (__assign(__assign({}, state), { modules: modules })); });
42
- }, integrate: function (dbg) {
43
- 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) }) })); });
44
- (0, react_atom_1.addChangeHandler)(context.state, 'debugging', function (_a) {
45
- var previous = _a.previous, current = _a.current;
46
- var pilets = current.modules !== previous.modules;
47
- var pages = current.registry.pages !== previous.registry.pages || current.routes !== previous.routes;
48
- var extensions = current.registry.extensions !== previous.registry.extensions;
49
- var state = current !== previous;
50
- dbg.onChange(previous, current, {
51
- pilets: pilets,
52
- pages: pages,
53
- extensions: extensions,
54
- state: state
55
- });
56
- });
57
- } }));
58
- }
59
- exports.integrate = integrate;