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.
- package/.dist/esp-react.esm.js +713 -0
- package/.dist/esp-react.esm.js.map +1 -0
- package/.dist/esp-react.js +696 -775
- package/.dist/esp-react.js.map +1 -1
- package/.dist/esp-react.min.js +20 -3
- package/.dist/esp-react.min.js.map +1 -1
- package/.dist/typings/connectableComponent.d.ts +5 -6
- package/.dist/typings/espApp.d.ts +25 -0
- package/.dist/typings/espEventBusContextProvider.d.ts +41 -0
- package/.dist/typings/espModulesContext.d.ts +3 -0
- package/.dist/typings/espStoreContextProvider.d.ts +62 -0
- package/.dist/typings/index.d.ts +13 -9
- package/.dist/typings/regionView.d.ts +19 -0
- package/.dist/typings/useSyncModelWithSelector.d.ts +9 -16
- package/README.md +62 -22
- package/package.json +53 -45
- package/.dist/esp-react.min.js.LICENSE.txt +0 -9
- package/.dist/typings/connect.d.ts +0 -8
- package/.dist/typings/espModelContextProvider.d.ts +0 -63
- package/.dist/typings/espRouterContextProvider.d.ts +0 -54
- package/.dist/typings/polimer/polimerModelBuilderExtentsions.d.ts +0 -6
- package/.dist/typings/viewBinder.d.ts +0 -13
- package/.dist/typings/viewBindingDecorator.d.ts +0 -34
- package/.yarnignore +0 -15
- package/docs/images/esp-agile-demo.gif +0 -0
- package/jest.config.js +0 -19
- package/src/connect.tsx +0 -27
- package/src/connectableComponent.tsx +0 -99
- package/src/espModelContextProvider.tsx +0 -101
- package/src/espRouterContextProvider.tsx +0 -97
- package/src/index.ts +0 -68
- package/src/polimer/polimerModelBuilderExtentsions.ts +0 -36
- package/src/useSyncModelWithSelector.ts +0 -223
- package/src/viewBinder.tsx +0 -51
- package/src/viewBindingDecorator.ts +0 -113
- package/tests/connectableComponentTests.tsx +0 -477
- package/tests/espModelContextProviderTests.tsx +0 -150
- package/tests/espRouterContextProviderTests.tsx +0 -56
- package/tests/indexTests.ts +0 -47
- package/tests/testApi/asserts.ts +0 -102
- package/tests/testApi/routerSpy.ts +0 -24
- package/tests/testApi/testApi.tsx +0 -100
- package/tests/testApi/testModel.ts +0 -43
- package/tests/testApi/useStoreReceivedProps.ts +0 -14
- package/tests/testApi/viewFactory.tsx +0 -31
- package/tests/useSyncModelWithSelectorTests.tsx +0 -330
- package/tests/viewBinderTests.tsx +0 -61
- package/tsconfig.json +0 -14
- package/webpack.config.js +0 -27
package/README.md
CHANGED
|
@@ -1,29 +1,69 @@
|
|
|
1
|
-
[
|
|
3
|
-
[](https://lernajs.io/)
|
|
1
|
+
[](https://www.npmjs.com/package/esp-js-react)
|
|
2
|
+

|
|
4
3
|
|
|
5
|
-
# Evented State Processor (ESP)
|
|
4
|
+
# Evented State Processor (ESP) — esp-js-react
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
It
|
|
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
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
22
|
-
* Dependency injection container - esp-js-di [](https://www.npmjs.com/package/esp-js-di)
|
|
23
|
-
* Module loading system and composite application toolbox - esp-js-ui [](https://www.npmjs.com/package/esp-js-ui)
|
|
24
|
-
* React support - esp-js-react [](https://www.npmjs.com/package/esp-js-react)
|
|
25
|
-
* Immutable state models - esp-js-polimer [](https://www.npmjs.com/package/esp-js-polimer)
|
|
16
|
+
> Peer dependencies: `esp-js`, `esp-js-ui` and `react` (19).
|
|
26
17
|
|
|
27
|
-
|
|
18
|
+
## Install
|
|
28
19
|
|
|
29
|
-
|
|
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` [](https://www.npmjs.com/package/esp-js)
|
|
63
|
+
* **esp-js-di** — a standalone IoC / dependency-injection container [](https://www.npmjs.com/package/esp-js-di)
|
|
64
|
+
* **esp-js-ui** — application bootstrapping and module loading [](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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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,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
|
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
|
-
};
|