esp-js-react 8.1.1-next.0 → 9.0.0-beta.3
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.mjs +710 -0
- package/.dist/esp-react.esm.mjs.map +1 -0
- package/.dist/esp-react.js +695 -777
- package/.dist/esp-react.js.map +1 -1
- package/.dist/esp-react.min.js +2 -3
- package/.dist/esp-react.min.js.map +1 -1
- package/.dist/typings/connectableComponent.d.ts +8 -9
- 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/useSyncStoreModelWithSelector.d.ts +47 -0
- package/README.md +62 -22
- package/package.json +63 -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/useSyncModelWithSelector.d.ts +0 -52
- 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/package.json
CHANGED
|
@@ -1,47 +1,65 @@
|
|
|
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.3",
|
|
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.mjs",
|
|
14
|
+
"types": ".dist/typings/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./.dist/typings/index.d.ts",
|
|
18
|
+
"import": "./.dist/esp-react.esm.mjs",
|
|
19
|
+
"require": "./.dist/esp-react.js",
|
|
20
|
+
"default": "./.dist/esp-react.js"
|
|
21
|
+
},
|
|
22
|
+
"./.dist/*": "./.dist/*",
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
".dist",
|
|
27
|
+
"NOTICE"
|
|
28
|
+
],
|
|
29
|
+
"author": "Dev Shop Limited",
|
|
30
|
+
"license": "Apache-2.0",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/esp/esp-js.git",
|
|
34
|
+
"directory": "packages/esp-js-react"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"clean": "nps clean",
|
|
38
|
+
"build-dev": "nps build-dev",
|
|
39
|
+
"build-prod": "nps build-prod",
|
|
40
|
+
"build-pack": "nps build-pack",
|
|
41
|
+
"test": "nps test",
|
|
42
|
+
"test-ci": "nps test-ci",
|
|
43
|
+
"dev": "nps dev"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"use-sync-external-store": "^1.6.0"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
50
|
+
"@vitejs/plugin-react": "^4.3.1",
|
|
51
|
+
"esp-js": "*",
|
|
52
|
+
"esp-js-ui": "*",
|
|
53
|
+
"react": "^19.0.0",
|
|
54
|
+
"typescript": "^5.8.0",
|
|
55
|
+
"vite": "^5.4.6",
|
|
56
|
+
"vite-plugin-dts": "^4.2.1",
|
|
57
|
+
"vitest": "^2.1.1"
|
|
58
|
+
},
|
|
59
|
+
"peerDependencies": {
|
|
60
|
+
"esp-js": "^9.0.0-beta.3",
|
|
61
|
+
"esp-js-ui": "^9.0.0-beta.3",
|
|
62
|
+
"react": "^19.0.0"
|
|
63
|
+
},
|
|
64
|
+
"gitHead": "f6791a488f3fc5f59f20e19445b848d5e2f415c4"
|
|
47
65
|
}
|
|
@@ -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,52 +0,0 @@
|
|
|
1
|
-
import { Logger } from 'esp-js';
|
|
2
|
-
export type SyncModelWithSelectorEqualityFn<T> = (last: T, next: T) => boolean;
|
|
3
|
-
export declare const logger: Logger;
|
|
4
|
-
/**
|
|
5
|
-
* Options affecting the subscription of the model to the Router.
|
|
6
|
-
*
|
|
7
|
-
* Use syncModelWithSelectorOptions() to create an instance of this type with sensible defaults.
|
|
8
|
-
*
|
|
9
|
-
* @param modelId - the modelId of the model to be observed via the esp Router.
|
|
10
|
-
* @param tryPreSelectPolimerImmutableModel - if true, and the esp model is an esp-js-polimer model, the immutable model will be used when calling the selector.
|
|
11
|
-
* @param equalityFn - Equality function which will be applied against TSelected, defaults to instance equality (a === b).
|
|
12
|
-
*/
|
|
13
|
-
export type SyncModelWithSelectorOptions<TSelected> = {
|
|
14
|
-
modelId?: string;
|
|
15
|
-
tryPreSelectPolimerImmutableModel: boolean;
|
|
16
|
-
equalityFn: SyncModelWithSelectorEqualityFn<TSelected>;
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* Convenience API to build SyncModelWithSelectorOptions<TSelected>, has sensible defaulting.
|
|
20
|
-
*/
|
|
21
|
-
export interface SyncModelWithSelectorOptionsBuilder<TSelected> extends SyncModelWithSelectorOptions<TSelected> {
|
|
22
|
-
/**
|
|
23
|
-
* Sets the model ID
|
|
24
|
-
* @param modelId - default if unset: undefined
|
|
25
|
-
*/
|
|
26
|
-
setModelId(modelId: string): this;
|
|
27
|
-
/**
|
|
28
|
-
* If the model is a PolimerModel and this set true, PolimerModel.getImmutableModel() will be selected by default.
|
|
29
|
-
* @param value - default if unset: true
|
|
30
|
-
*/
|
|
31
|
-
setTryPreSelectPolimerImmutableModel(value: boolean): this;
|
|
32
|
-
/**
|
|
33
|
-
* Equality function used when comparing the last TSelected with the next.
|
|
34
|
-
* @param fn - default if unset: obj instance equality (e.g. a===b)
|
|
35
|
-
*/
|
|
36
|
-
setEqualityFn(fn: (a: TSelected, b: TSelected) => boolean): this;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Convenience API to create SyncModelWithSelectorOptions
|
|
40
|
-
*/
|
|
41
|
-
export declare const syncModelWithSelectorOptions: <TSelected>() => SyncModelWithSelectorOptionsBuilder<TSelected>;
|
|
42
|
-
/**
|
|
43
|
-
* A hook which returns model state from the esp Router.
|
|
44
|
-
*
|
|
45
|
-
* This hooks wih observe updates and re-render the component if the model changes.
|
|
46
|
-
*
|
|
47
|
-
* To use this, please ensure your V-DOM has the EspRouterContextProvider at a higher node, and optionally EspModelContextProvider.
|
|
48
|
-
*
|
|
49
|
-
* @param selector - a function to select a selection of the model - the selector does not need to be memoized, this selector will be cached against the modelId.
|
|
50
|
-
* @param options
|
|
51
|
-
*/
|
|
52
|
-
export declare const useSyncModelWithSelector: <TModel, TSelected>(selector: (model: TModel) => TSelected, options?: SyncModelWithSelectorOptions<TSelected>) => TSelected;
|
|
@@ -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
|
-
};
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import {DefaultModelAddress, Router} from 'esp-js';
|
|
3
|
-
import {useContext, useCallback, createContext, PropsWithChildren} from 'react';
|
|
4
|
-
import {useRouter} from './espRouterContextProvider';
|
|
5
|
-
|
|
6
|
-
export const GetModelIdContext = createContext<string>(null);
|
|
7
|
-
/**
|
|
8
|
-
* Returns a function to get the ID of the model connected at a higher point in the V-DOM.
|
|
9
|
-
*
|
|
10
|
-
* To use this, please ensure your V-DOM has the EspModelContextProvider at a higher node.
|
|
11
|
-
*/
|
|
12
|
-
export const useGetModelId = () => useContext(GetModelIdContext);
|
|
13
|
-
|
|
14
|
-
export const GetModelContext = createContext<object>(null);
|
|
15
|
-
/**
|
|
16
|
-
* Returns a function which will return the model connected at a higher point in the V-DOM.
|
|
17
|
-
*
|
|
18
|
-
* To use this, please ensure your V-DOM has the EspModelContextProvider at a higher node.
|
|
19
|
-
*/
|
|
20
|
-
export const useGetModel = <TModel,>() => useContext(GetModelContext) as TModel;
|
|
21
|
-
|
|
22
|
-
export type PublishModelEventDelegate = { (eventType: string, event: any): void; (eventData: {eventType: string, event: any}): void; };
|
|
23
|
-
const createPublishModelEvent = (router: Router, modelId: string) => {
|
|
24
|
-
function publishModelEvent(eventType: string, event: any): void;
|
|
25
|
-
function publishModelEvent(eventData: {eventType: string, event: any}): void;
|
|
26
|
-
function publishModelEvent(...args: any[]) {
|
|
27
|
-
if (args.length === 1) {
|
|
28
|
-
router.publishEvent(modelId, args[0].eventType, args[0].event);
|
|
29
|
-
} else {
|
|
30
|
-
router.publishEvent(modelId, args[0], args[1]);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return publishModelEvent;
|
|
34
|
-
};
|
|
35
|
-
export const PublishModelEventContext = createContext<PublishModelEventDelegate>(null);
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Returns a function which can be used to publish an event to the model connected at a higher point in the V-DOM.
|
|
39
|
-
*
|
|
40
|
-
* Effectively, this is a version of usePublishEvent() which closes over the relevant modelId.
|
|
41
|
-
*
|
|
42
|
-
* To use this, please ensure your V-DOM has the EspModelContextProvider at a higher node.
|
|
43
|
-
*/
|
|
44
|
-
export const usePublishModelEvent = () => useContext(PublishModelEventContext);
|
|
45
|
-
|
|
46
|
-
export type PublishModelEventWithEntityKeyDelegate = {(entityKey: string, eventType: string, event: any): void; (eventData: {entityKey: string, eventType: string, event: any}): void; };
|
|
47
|
-
const createPublishModelEventWithEntityKey = (router: Router, modelId: string) => {
|
|
48
|
-
function publishModelEventWithEntityKey(entityKey: string, eventType: string, event: any): void;
|
|
49
|
-
function publishModelEventWithEntityKey(eventData: {entityKey: string, eventType: string, event: any}): void;
|
|
50
|
-
function publishModelEventWithEntityKey(...args: any[]) {
|
|
51
|
-
if (args.length === 1) {
|
|
52
|
-
router.publishEvent(new DefaultModelAddress(modelId, args[0].entityKey), args[0].eventType, args[0].event);
|
|
53
|
-
} else {
|
|
54
|
-
router.publishEvent(new DefaultModelAddress(modelId, args[0]), args[1], args[2]);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return publishModelEventWithEntityKey;
|
|
58
|
-
};
|
|
59
|
-
export const PublishModelEventWithEntityKeyContext = createContext<PublishModelEventWithEntityKeyDelegate>(null);
|
|
60
|
-
/**
|
|
61
|
-
* 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.
|
|
62
|
-
*
|
|
63
|
-
* Effectively, this is a version of usePublishEvent() which closes over the modelId.
|
|
64
|
-
*
|
|
65
|
-
* To use this, please ensure your V-DOM has the EspModelContextProvider at a higher node.
|
|
66
|
-
*/
|
|
67
|
-
export const usePublishModelEventWithEntityKey = () => useContext(PublishModelEventWithEntityKeyContext);
|
|
68
|
-
|
|
69
|
-
export interface EspModelContextProviderProps {
|
|
70
|
-
modelId: string;
|
|
71
|
-
model?: any;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Sets up various ESP context so ESP hooks are available below this point in the V-DOM
|
|
76
|
-
*
|
|
77
|
-
* To use this, please ensure your V-DOM has the EspRouterContextProvider at a higher node.
|
|
78
|
-
* @param modelId
|
|
79
|
-
* @param children
|
|
80
|
-
* @param model
|
|
81
|
-
* @constructor
|
|
82
|
-
*/
|
|
83
|
-
export const EspModelContextProvider = ({modelId, children, model}: PropsWithChildren<EspModelContextProviderProps>) => {
|
|
84
|
-
const router = useRouter();
|
|
85
|
-
// modelId may have been set by a higher level version of this component, so we try and get it from context if it's not set by props.
|
|
86
|
-
let modelIdFromContext = useGetModelId();
|
|
87
|
-
modelId = modelId || modelIdFromContext;
|
|
88
|
-
const publishModelEvent: PublishModelEventDelegate = useCallback(createPublishModelEvent(router, modelId), [router, modelId]);
|
|
89
|
-
const publishModelEventWithEntityKey: PublishModelEventWithEntityKeyDelegate = useCallback(createPublishModelEventWithEntityKey(router, modelId), [router, modelId]);
|
|
90
|
-
return (
|
|
91
|
-
<GetModelIdContext.Provider value={modelId}>
|
|
92
|
-
<PublishModelEventContext.Provider value={publishModelEvent}>
|
|
93
|
-
<PublishModelEventWithEntityKeyContext.Provider value={publishModelEventWithEntityKey}>
|
|
94
|
-
<GetModelContext.Provider value={model}>
|
|
95
|
-
{children}
|
|
96
|
-
</GetModelContext.Provider>
|
|
97
|
-
</PublishModelEventWithEntityKeyContext.Provider>
|
|
98
|
-
</PublishModelEventContext.Provider>
|
|
99
|
-
</GetModelIdContext.Provider>
|
|
100
|
-
);
|
|
101
|
-
};
|