esp-js-react 8.1.0 → 9.0.0-beta.1

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 (49) hide show
  1. package/.dist/esp-react.esm.js +713 -0
  2. package/.dist/esp-react.esm.js.map +1 -0
  3. package/.dist/esp-react.js +696 -775
  4. package/.dist/esp-react.js.map +1 -1
  5. package/.dist/esp-react.min.js +20 -3
  6. package/.dist/esp-react.min.js.map +1 -1
  7. package/.dist/typings/connectableComponent.d.ts +5 -6
  8. package/.dist/typings/espApp.d.ts +25 -0
  9. package/.dist/typings/espEventBusContextProvider.d.ts +41 -0
  10. package/.dist/typings/espModulesContext.d.ts +3 -0
  11. package/.dist/typings/espStoreContextProvider.d.ts +62 -0
  12. package/.dist/typings/index.d.ts +13 -9
  13. package/.dist/typings/regionView.d.ts +19 -0
  14. package/.dist/typings/useSyncModelWithSelector.d.ts +9 -16
  15. package/README.md +62 -22
  16. package/package.json +53 -45
  17. package/.dist/esp-react.min.js.LICENSE.txt +0 -9
  18. package/.dist/typings/connect.d.ts +0 -8
  19. package/.dist/typings/espModelContextProvider.d.ts +0 -63
  20. package/.dist/typings/espRouterContextProvider.d.ts +0 -54
  21. package/.dist/typings/polimer/polimerModelBuilderExtentsions.d.ts +0 -6
  22. package/.dist/typings/viewBinder.d.ts +0 -13
  23. package/.dist/typings/viewBindingDecorator.d.ts +0 -34
  24. package/.yarnignore +0 -15
  25. package/docs/images/esp-agile-demo.gif +0 -0
  26. package/jest.config.js +0 -19
  27. package/src/connect.tsx +0 -27
  28. package/src/connectableComponent.tsx +0 -99
  29. package/src/espModelContextProvider.tsx +0 -101
  30. package/src/espRouterContextProvider.tsx +0 -97
  31. package/src/index.ts +0 -68
  32. package/src/polimer/polimerModelBuilderExtentsions.ts +0 -36
  33. package/src/useSyncModelWithSelector.ts +0 -223
  34. package/src/viewBinder.tsx +0 -51
  35. package/src/viewBindingDecorator.ts +0 -113
  36. package/tests/connectableComponentTests.tsx +0 -477
  37. package/tests/espModelContextProviderTests.tsx +0 -150
  38. package/tests/espRouterContextProviderTests.tsx +0 -56
  39. package/tests/indexTests.ts +0 -47
  40. package/tests/testApi/asserts.ts +0 -102
  41. package/tests/testApi/routerSpy.ts +0 -24
  42. package/tests/testApi/testApi.tsx +0 -100
  43. package/tests/testApi/testModel.ts +0 -43
  44. package/tests/testApi/useStoreReceivedProps.ts +0 -14
  45. package/tests/testApi/viewFactory.tsx +0 -31
  46. package/tests/useSyncModelWithSelectorTests.tsx +0 -330
  47. package/tests/viewBinderTests.tsx +0 -61
  48. package/tsconfig.json +0 -14
  49. package/webpack.config.js +0 -27
package/README.md CHANGED
@@ -1,29 +1,69 @@
1
- [![Build Status](https://travis-ci.org/esp/esp-js.svg?branch=master)](https://travis-ci.org/esp/esp-js)
2
- ![](https://img.shields.io/npm/types/esp-js)
3
- [![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lernajs.io/)
1
+ [![npm](https://img.shields.io/npm/v/esp-js-react.svg)](https://www.npmjs.com/package/esp-js-react)
2
+ ![npm type definitions](https://img.shields.io/npm/types/esp-js-react)
4
3
 
5
- # Evented State Processor (ESP) - Package esp-js-react
4
+ # Evented State Processor (ESP) — esp-js-react
6
5
 
7
- ESP gives you the ability to manage changes to a model in a deterministic event driven manner.
8
- It does this by adding specific processing workflow around changes to a model's state.
9
- It was born out of the need to manage complex UI and/or server state.
6
+ `esp-js-react` provides the React bindings for ESP.
7
+ It connects React views to ESP stores and re-renders them as new immutable model snapshots are pushed from the `EventBus`.
10
8
 
11
- At its core is a `Router` which sits between event publishers and the model.
12
- Those wanting to change the model publish events to the `Router`.
13
- The model observes the events and applies the changes.
14
- The model is then dispatched to model observers so new state can be applied.
15
- It's lightweight, easy to apply and puts the model at the forefront of your design.
9
+ Key building blocks:
16
10
 
17
- ESP 2.0 adds a host of other additional libraries to help you build composite single page application with React.
18
- It allows you to use either OO, and/or immutable pattens (Redux like) for modeling independent and decoupled screens within your composite application.
19
- Features include:
11
+ * `EspApp` — wraps the app root with the EventBus and modules contexts
12
+ * `RegionView` — renders the views that loaded modules registered for a named region
13
+ * `ConnectableComponent` — subscribes a view to a store by `storeId`
14
+ * hooks — `useSyncModelWithSelector`, `usePublishStoreEvent`, `useGetStore`, `useEventBus`, and more
20
15
 
21
- * The core event router - esp-js [![npm](https://img.shields.io/npm/v/esp-js.svg)](https://www.npmjs.com/package/esp-js)
22
- * Dependency injection container - esp-js-di [![npm](https://img.shields.io/npm/v/esp-js-di.svg)](https://www.npmjs.com/package/esp-js-di)
23
- * Module loading system and composite application toolbox - esp-js-ui [![npm](https://img.shields.io/npm/v/esp-js-ui.svg)](https://www.npmjs.com/package/esp-js-ui)
24
- * React support - esp-js-react [![npm](https://img.shields.io/npm/v/esp-js-react.svg)](https://www.npmjs.com/package/esp-js-react)
25
- * Immutable state models - esp-js-polimer [![npm](https://img.shields.io/npm/v/esp-js-polimer.svg)](https://www.npmjs.com/package/esp-js-polimer)
16
+ > Peer dependencies: `esp-js`, `esp-js-ui` and `react` (19).
26
17
 
27
- It's built on typescript and type definitions are included in the npm packages.
18
+ ## Install
28
19
 
29
- For full documentation please see [https://esp.github.io/](https://esp.github.io/).
20
+ ```bash
21
+ npm install esp-js-react esp-js esp-js-ui react
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ With `esp-js-ui`, wrap the app after `app.start()` resolves and let regions render module views:
27
+
28
+ ```tsx
29
+ import { EspApp, RegionView, usePublishStoreEvent } from 'esp-js-react';
30
+
31
+ // after app.start():
32
+ ReactDOM.createRoot(document.getElementById('root')!).render(
33
+ <EspApp app={app}>
34
+ <RegionView regionName="main" />
35
+ </EspApp>
36
+ );
37
+
38
+ // A view receives the current model snapshot as a prop and publishes events back to its store:
39
+ function OrdersView({ model }: { model: OrdersStore }) {
40
+ const publish = usePublishStoreEvent(); // (eventType, event) => void — storeId is inferred from context
41
+ return <button onClick={() => publish('AddOrder', {})}>{model.title}</button>;
42
+ }
43
+ ```
44
+
45
+ You can also connect a single view directly, without the module/region machinery:
46
+
47
+ ```tsx
48
+ import { EspEventBusContextProvider, ConnectableComponent } from 'esp-js-react';
49
+
50
+ <EspEventBusContextProvider bus={bus}>
51
+ <ConnectableComponent storeId="counter" view={CounterView} />
52
+ </EspEventBusContextProvider>
53
+ ```
54
+
55
+ The model handed to views and hooks is always a frozen immutable snapshot — never mutate it directly.
56
+ For fine-grained re-rendering, prefer `useSyncModelWithSelector` with a selector and equality function.
57
+
58
+ > The published bundle is named `esp-react.js` (matching the `main` field), not `esp-js-react.js`.
59
+
60
+ ## The ESP package family
61
+
62
+ * **esp-js** — the core `EventBus` and `StoreBuilder` [![npm](https://img.shields.io/npm/v/esp-js.svg)](https://www.npmjs.com/package/esp-js)
63
+ * **esp-js-di** — a standalone IoC / dependency-injection container [![npm](https://img.shields.io/npm/v/esp-js-di.svg)](https://www.npmjs.com/package/esp-js-di)
64
+ * **esp-js-ui** — application bootstrapping and module loading [![npm](https://img.shields.io/npm/v/esp-js-ui.svg)](https://www.npmjs.com/package/esp-js-ui)
65
+ * **esp-js-react** — React bindings (this package)
66
+
67
+ Written in TypeScript; type definitions are included in the package.
68
+
69
+ For full documentation see [https://esp.github.io/](https://esp.github.io/), or browse the source at [github.com/esp/esp-js](https://github.com/esp/esp-js).
package/package.json CHANGED
@@ -1,47 +1,55 @@
1
1
  {
2
- "name": "esp-js-react",
3
- "version": "8.1.0",
4
- "description": "Evented State Processor (ESP) React Components",
5
- "keywords": [
6
- "esp",
7
- "state processor",
8
- "evented state processor",
9
- "reactive",
10
- "react"
11
- ],
12
- "main": ".dist/esp-react.js",
13
- "types": ".dist/typings/index.d.ts",
14
- "author": "Dev Shop Limited",
15
- "license": "Apache-2.0",
16
- "repository": "https://github.com/esp/esp-js",
17
- "scripts": {
18
- "clean": "nps clean",
19
- "build-dev": "nps build-dev",
20
- "build-prod": "nps build-prod",
21
- "build-pack": "nps build-pack",
22
- "test": "nps test",
23
- "test-ci": "nps test-ci",
24
- "dev": "nps dev"
25
- },
26
- "dependencies": {
27
- "esp-js": "^8.1.0",
28
- "esp-js-polimer": "^8.1.0",
29
- "prop-types": "^15.7.2",
30
- "react": "^18.3.1",
31
- "use-sync-external-store": "^1.2.2"
32
- },
33
- "devDependencies": {
34
- "jest": "^29.7.0",
35
- "typescript": "^5.5.2",
36
- "webpack": "^5.92.1",
37
- "webpack-cli": "^5.1.4",
38
- "webpack-dev-server": "^5.0.4"
39
- },
40
- "peerDependencies": {
41
- "esp-js": "^7.0.0",
42
- "esp-js-polimer": "^7.0.0",
43
- "prop-types": "^15.0.0",
44
- "react": "^18.0.0 || ^19.0.0"
45
- },
46
- "gitHead": "19df19acda2815952dbdbbd7d5a97bf4980c1d6f"
2
+ "name": "esp-js-react",
3
+ "version": "9.0.0-beta.1",
4
+ "description": "Evented State Processor (ESP) React Components",
5
+ "keywords": [
6
+ "esp",
7
+ "state processor",
8
+ "evented state processor",
9
+ "reactive",
10
+ "react"
11
+ ],
12
+ "main": ".dist/esp-react.js",
13
+ "module": ".dist/esp-react.esm.js",
14
+ "types": ".dist/typings/index.d.ts",
15
+ "files": [
16
+ ".dist",
17
+ "NOTICE"
18
+ ],
19
+ "author": "Dev Shop Limited",
20
+ "license": "Apache-2.0",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/esp/esp-js.git",
24
+ "directory": "packages/esp-js-react"
25
+ },
26
+ "scripts": {
27
+ "clean": "nps clean",
28
+ "build-dev": "nps build-dev",
29
+ "build-prod": "nps build-prod",
30
+ "build-pack": "nps build-pack",
31
+ "test": "nps test",
32
+ "test-ci": "nps test-ci",
33
+ "dev": "nps dev"
34
+ },
35
+ "dependencies": {
36
+ "esp-js": "^9.0.0-beta.1",
37
+ "react": "^19.0.0",
38
+ "use-sync-external-store": "^1.6.0"
39
+ },
40
+ "devDependencies": {
41
+ "@rollup/plugin-terser": "^0.4.4",
42
+ "@vitejs/plugin-react": "^4.3.1",
43
+ "esp-js-ui": "*",
44
+ "vite": "^5.4.6",
45
+ "vite-plugin-dts": "^4.2.1",
46
+ "vitest": "^2.1.1",
47
+ "typescript": "^5.8.0"
48
+ },
49
+ "peerDependencies": {
50
+ "esp-js": "^9.0.0-beta.1",
51
+ "esp-js-ui": "^9.0.0-beta.1",
52
+ "react": "^19.0.0"
53
+ },
54
+ "gitHead": "f6791a488f3fc5f59f20e19445b848d5e2f415c4"
47
55
  }
@@ -1,9 +0,0 @@
1
- /**
2
- * @license React
3
- * use-sync-external-store-with-selector.production.min.js
4
- *
5
- * Copyright (c) Facebook, Inc. and its affiliates.
6
- *
7
- * This source code is licensed under the MIT license found in the
8
- * LICENSE file in the root directory of this source tree.
9
- */
@@ -1,8 +0,0 @@
1
- import * as React from 'react';
2
- import { ConnectableComponentProps, CreatePublishEventProps, MapModelToProps } from './connectableComponent';
3
- export type ConnectableComponentFactory<TModel, TPublishEventProps, TModelMappedToProps = {}> = (view: React.ComponentType) => (props: ConnectableComponentProps<TModel, TPublishEventProps, TModelMappedToProps>) => React.JSX.Element;
4
- /**
5
- * @param mapModelToProps - maps the model to a different structure, that structure is sent as props to the view.
6
- * @param createPublishEventProps - creates functions to publish events, the structure is sent as props to the view.
7
- */
8
- export declare const connect: <TModel, TPublishEventProps, TModelMappedToProps = {}>(mapModelToProps?: MapModelToProps<TModel, TModelMappedToProps, TPublishEventProps>, createPublishEventProps?: CreatePublishEventProps<TPublishEventProps>) => ConnectableComponentFactory<TModel, TPublishEventProps, TModelMappedToProps>;
@@ -1,63 +0,0 @@
1
- import * as React from 'react';
2
- import { PropsWithChildren } from 'react';
3
- export declare const GetModelIdContext: React.Context<string>;
4
- /**
5
- * Returns a function to get the ID of the model connected at a higher point in the V-DOM.
6
- *
7
- * To use this, please ensure your V-DOM has the EspModelContextProvider at a higher node.
8
- */
9
- export declare const useGetModelId: () => string;
10
- export declare const GetModelContext: React.Context<object>;
11
- /**
12
- * Returns a function which will return the model connected at a higher point in the V-DOM.
13
- *
14
- * To use this, please ensure your V-DOM has the EspModelContextProvider at a higher node.
15
- */
16
- export declare const useGetModel: <TModel>() => TModel;
17
- export type PublishModelEventDelegate = {
18
- (eventType: string, event: any): void;
19
- (eventData: {
20
- eventType: string;
21
- event: any;
22
- }): void;
23
- };
24
- export declare const PublishModelEventContext: React.Context<PublishModelEventDelegate>;
25
- /**
26
- * Returns a function which can be used to publish an event to the model connected at a higher point in the V-DOM.
27
- *
28
- * Effectively, this is a version of usePublishEvent() which closes over the relevant modelId.
29
- *
30
- * To use this, please ensure your V-DOM has the EspModelContextProvider at a higher node.
31
- */
32
- export declare const usePublishModelEvent: () => PublishModelEventDelegate;
33
- export type PublishModelEventWithEntityKeyDelegate = {
34
- (entityKey: string, eventType: string, event: any): void;
35
- (eventData: {
36
- entityKey: string;
37
- eventType: string;
38
- event: any;
39
- }): void;
40
- };
41
- export declare const PublishModelEventWithEntityKeyContext: React.Context<PublishModelEventWithEntityKeyDelegate>;
42
- /**
43
- * Returns a function which can be used to publish an event to the model connected at a higher point in the V-DOM, allows an entityKey to be specified.
44
- *
45
- * Effectively, this is a version of usePublishEvent() which closes over the modelId.
46
- *
47
- * To use this, please ensure your V-DOM has the EspModelContextProvider at a higher node.
48
- */
49
- export declare const usePublishModelEventWithEntityKey: () => PublishModelEventWithEntityKeyDelegate;
50
- export interface EspModelContextProviderProps {
51
- modelId: string;
52
- model?: any;
53
- }
54
- /**
55
- * Sets up various ESP context so ESP hooks are available below this point in the V-DOM
56
- *
57
- * To use this, please ensure your V-DOM has the EspRouterContextProvider at a higher node.
58
- * @param modelId
59
- * @param children
60
- * @param model
61
- * @constructor
62
- */
63
- export declare const EspModelContextProvider: ({ modelId, children, model }: PropsWithChildren<EspModelContextProviderProps>) => React.JSX.Element;
@@ -1,54 +0,0 @@
1
- import * as React from 'react';
2
- import { ModelAddress, Router } from 'esp-js';
3
- import { PropsWithChildren } from 'react';
4
- export declare const RouterContext: React.Context<Router>;
5
- /**
6
- * Get the esp Router from context.
7
- *
8
- * To use this, please ensure your V-DOM has the EspRouterContextProvider at a higher node:
9
- *
10
- * <EspRouterContextProvider router={router}>
11
- * <YourComponents />
12
- * </EspRouterContextProvider>
13
- */
14
- export declare const useRouter: () => Router;
15
- export type PublishEventDelegate = {
16
- (modelIdOrModelAddress: string | ModelAddress, eventType: string, event: any): void;
17
- (eventData: {
18
- address: string | ModelAddress;
19
- eventType: string;
20
- event: any;
21
- }): void;
22
- };
23
- export declare const PublishEventContext: React.Context<PublishEventDelegate>;
24
- /**
25
- * Returns a function which can be used to publish an event.
26
- *
27
- * To use this, please ensure your V-DOM has the EspRouterContextProvider at a higher node:
28
- *
29
- * <EspRouterContextProvider router={router}>
30
- * <YourComponents />
31
- * </EspRouterContextProvider>
32
- */
33
- export declare const usePublishEvent: () => PublishEventDelegate;
34
- export type EspRouterContextProviderProps = PropsWithChildren<{
35
- router: Router;
36
- }>;
37
- /**
38
- * Used to set the router on the RouterContext making it available to components down the tree via the useRouter() hook.
39
- *
40
- * Also sets up the usePublishEvent() hook so nested components can publish to the router.
41
- */
42
- export declare const EspRouterContextProvider: ({ children, router }: EspRouterContextProviderProps) => React.JSX.Element;
43
- /**
44
- * Used to set the router on the RouterContext making it available to components down the tree (via useRouter()).
45
- *
46
- * @deprecated use EspRouterContextProvider
47
- */
48
- export declare const RouterProvider: ({ children, router }: EspRouterContextProviderProps) => React.JSX.Element;
49
- /**
50
- * Props for RouterProvider
51
- *
52
- * @deprecated use EspRouterContextProviderProps
53
- */
54
- export type RouterProviderProps = EspRouterContextProviderProps;
@@ -1,6 +0,0 @@
1
- declare module 'esp-js-polimer/.dist/typings/modelBuilderUpdater' {
2
- interface PolimerModelBuilder<TModel> {
3
- withViewBindings?(view: any, displayContext?: string): PolimerModelBuilder<TModel>;
4
- }
5
- }
6
- export {};
@@ -1,13 +0,0 @@
1
- import * as React from 'react';
2
- export interface ViewBinderProps {
3
- model: any;
4
- viewContext?: string;
5
- /**
6
- * ...rest props, these will be passed to the child
7
- */
8
- [key: string]: any;
9
- }
10
- /**
11
- * Takes a model with a @viewBinding decorator and dynamically crates the view specified by the decorator
12
- */
13
- export declare const ViewBinder: React.FC<ViewBinderProps>;
@@ -1,34 +0,0 @@
1
- import * as React from 'react';
2
- export declare const DEFAULT_VIEW_KEY = "default-view-key";
3
- /**
4
- * Tries to get a view for the given model.
5
- *
6
- * It does this using the following order:
7
- * 1) If a @viewBinding decorator was used and a display context was given, try get a view for that
8
- * 2) Else if a view was explicitly provided use that
9
- * 3) Else if a @viewBinding decorator was used and there is a view associated with the default display context, use that view
10
- * @param model
11
- * @param props
12
- * @param displayContext
13
- * @param view
14
- */
15
- export declare function createViewForModel(model: any, props: object, displayContext: string, view: React.ComponentType): React.ReactElement<{}, string | React.JSXElementConstructor<any>>;
16
- /**
17
- * An ES7 style decorator that associates a model with a view
18
- * @param view the react component that will be used to display this model
19
- * @param displayContext an optional context allowing for different views to display the same model
20
- * @returns {Function}
21
- */
22
- export declare function viewBinding(view: any, displayContext?: string): (target: any) => void;
23
- export declare class ViewMetadata {
24
- private _viewRegistrations;
25
- get viewRegistrations(): {};
26
- hasRegisteredViewContext(displayContext: any): boolean;
27
- }
28
- export declare class ViewMetadataRegistration {
29
- private _view;
30
- private _displayContext;
31
- constructor(_view: any, _displayContext: any);
32
- get view(): any;
33
- get displayContext(): any;
34
- }
package/.yarnignore DELETED
@@ -1,15 +0,0 @@
1
- examples/
2
- .git
3
- .gitignore
4
- .npmignore
5
- .idea
6
- node_modules/
7
- *.log
8
- esp-js-*.tgz
9
- tests
10
- typings
11
- !.dist/typings
12
- jest.config.js
13
- webpack.config.js
14
- docs
15
- tsconfig.json
Binary file
package/jest.config.js DELETED
@@ -1,19 +0,0 @@
1
- // notice_start
2
- /*
3
- * Copyright 2015 Dev Shop Limited
4
- *
5
- * Licensed under the Apache License, Version 2.0 (the 'License');
6
- * you may not use this file except in compliance with the License.
7
- * You may obtain a copy of the License at
8
- *
9
- * http://www.apache.org/licenses/LICENSE-2.0
10
- *
11
- * Unless required by applicable law or agreed to in writing, software
12
- * distributed under the License is distributed on an 'AS IS' BASIS,
13
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- * See the License for the specific language governing permissions and
15
- * limitations under the License.
16
- */
17
- // notice_end
18
-
19
- module.exports = require('../../__jest__/jest.config');
package/src/connect.tsx DELETED
@@ -1,27 +0,0 @@
1
- import * as React from 'react';
2
- import {ConnectableComponent, ConnectableComponentProps, CreatePublishEventProps, MapModelToProps} from './connectableComponent';
3
-
4
- export type ConnectableComponentFactory<TModel, TPublishEventProps, TModelMappedToProps = {}> = (view: React.ComponentType) => (props: ConnectableComponentProps<TModel, TPublishEventProps, TModelMappedToProps>) => React.JSX.Element;
5
-
6
- /**
7
- * @param mapModelToProps - maps the model to a different structure, that structure is sent as props to the view.
8
- * @param createPublishEventProps - creates functions to publish events, the structure is sent as props to the view.
9
- */
10
- export const connect = function <TModel, TPublishEventProps, TModelMappedToProps = {}>(
11
- mapModelToProps?: MapModelToProps<TModel, TModelMappedToProps, TPublishEventProps>,
12
- createPublishEventProps?: CreatePublishEventProps<TPublishEventProps>
13
- ): ConnectableComponentFactory<TModel, TPublishEventProps, TModelMappedToProps> {
14
- return function (view: React.ComponentType) {
15
- return function (props: ConnectableComponentProps<TModel, TPublishEventProps, TModelMappedToProps>) {
16
- const {modelId, viewContext, ...rest} = props;
17
- return <ConnectableComponent
18
- modelId={modelId}
19
- view={view}
20
- viewContext={viewContext}
21
- createPublishEventProps={createPublishEventProps}
22
- mapModelToProps={mapModelToProps}
23
- {...rest}
24
- />;
25
- };
26
- };
27
- };
@@ -1,99 +0,0 @@
1
- import * as React from 'react';
2
- import {useRouter} from './espRouterContextProvider';
3
- import {useMemo} from 'react';
4
- import {Router} from 'esp-js';
5
- import {EspModelContextProvider, useGetModelId} from './espModelContextProvider';
6
- import {createViewForModel} from './viewBindingDecorator';
7
- import {syncModelWithSelectorOptions, useSyncModelWithSelector} from './useSyncModelWithSelector';
8
- import {PolimerModel} from 'esp-js-polimer';
9
-
10
- export type CreatePublishEventProps<TPublishEventProps> = (publishModelEvent: (eventType: string, event: any) => void) => TPublishEventProps;
11
-
12
- export type MapModelToProps<TModel, TModelMappedToProps, TPublishEventProps = {}> = (model: TModel, publishEventProps: TPublishEventProps) => TModelMappedToProps;
13
-
14
- export interface ConnectableComponentProps<TModel = {}, TPublishEventProps = {}, TModelMappedToProps = {}> {
15
- modelId?: string;
16
- viewContext?: string;
17
- view?: React.ComponentType;
18
- createPublishEventProps?: CreatePublishEventProps<TPublishEventProps>;
19
- mapModelToProps?: MapModelToProps<TModel, TModelMappedToProps, TPublishEventProps>;
20
- [key: string]: any; // ...rest props, including the result of mapPublish and mapPublish if 'connect' was used
21
- }
22
-
23
- export interface ConnectableComponentChildProps<TModel = object> {
24
- modelId: string;
25
- model: TModel;
26
- router: Router;
27
- [key: string]: any; // ...rest props
28
- }
29
-
30
- const getChildProps = <TModel, TModelMappedToProps, TPublishEventProps>(
31
- router: Router,
32
- modelId: string,
33
- restProps: object,
34
- initialModel: TModel,
35
- mapModelToProps: MapModelToProps<TModel, TModelMappedToProps, TPublishEventProps>,
36
- publishEventProps: TPublishEventProps
37
- ): ConnectableComponentChildProps<TModel> => {
38
- const model = PolimerModel.isPolimerModel(initialModel)
39
- ? initialModel.getEspPolimerImmutableModel()
40
- : initialModel;
41
- let childProps = {
42
- modelId,
43
- router: router,
44
- ...restProps,
45
- ...publishEventProps,
46
- model
47
- };
48
- if (mapModelToProps) {
49
- childProps = {
50
- ...childProps,
51
- ...(mapModelToProps(model, publishEventProps) as TModelMappedToProps)
52
- };
53
- }
54
- return childProps;
55
- };
56
-
57
- export const ConnectableComponent = <TModel = {}, TPublishEventProps = {}, TModelMappedToProps = {}>(
58
- {
59
- modelId,
60
- mapModelToProps,
61
- createPublishEventProps,
62
- view,
63
- viewContext,
64
- ...rest
65
- }: ConnectableComponentProps<TModel, TPublishEventProps, TModelMappedToProps>
66
- ) => {
67
- const router = useRouter();
68
- // Note: we always need to render the hooks else react will complain about a different count.
69
- let modelIdFromContext = useGetModelId();
70
- modelId = modelId || modelIdFromContext;
71
- const publishEventProps: TPublishEventProps = useMemo(
72
- () => {
73
- if (createPublishEventProps) {
74
- const publishModelEvent = (eventType: string, event: any) => {
75
- router.publishEvent(modelId, eventType, event);
76
- };
77
- return createPublishEventProps(publishModelEvent);
78
- }
79
- return {} as TPublishEventProps;
80
- },
81
- [router, modelId]
82
- );
83
- const model = useSyncModelWithSelector<TModel, TModel>(
84
- m => m,
85
- syncModelWithSelectorOptions()
86
- .setModelId(modelId)
87
- .setTryPreSelectPolimerImmutableModel(false)
88
- );
89
- if (model == null) {
90
- return null;
91
- }
92
- let childProps = getChildProps(router, modelId, rest, model, mapModelToProps, publishEventProps);
93
- let viewElement = createViewForModel(model, childProps, viewContext, view);
94
- return (
95
- <EspModelContextProvider modelId={modelId} model={model} router={router} {...childProps}>
96
- {viewElement}
97
- </EspModelContextProvider>
98
- );
99
- };