esp-js-react 7.0.2 → 8.0.0-next.0
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.js +425 -510
- package/.dist/esp-react.js.map +1 -1
- package/.dist/esp-react.min.js +2 -1
- package/.dist/esp-react.min.js.LICENSE.txt +9 -0
- package/.dist/esp-react.min.js.map +1 -1
- package/.dist/typings/connect.d.ts +12 -0
- package/.dist/typings/connectableComponent.d.ts +20 -46
- package/.dist/typings/espModelContext.d.ts +7 -8
- package/.dist/typings/espRouterContext.d.ts +47 -0
- package/.dist/typings/index.d.ts +7 -6
- package/.dist/typings/{getEspReactRenderModel.d.ts → polimer/getEspReactRenderModel.d.ts} +7 -0
- package/.dist/typings/useModelSelector.d.ts +13 -0
- package/.dist/typings/viewBinder.d.ts +1 -8
- package/.dist/typings/viewBinderConnectable.d.ts +15 -0
- package/.dist/typings/viewBindingDecorator.d.ts +1 -2
- package/package.json +7 -6
- package/src/connect.tsx +31 -0
- package/src/connectableComponent.tsx +93 -190
- package/src/espModelContext.tsx +49 -23
- package/src/espRouterContext.tsx +88 -0
- package/src/index.ts +32 -16
- package/src/polimer/getEspReactRenderModel.ts +50 -0
- package/src/useModelSelector.ts +94 -0
- package/src/viewBinder.tsx +14 -22
- package/src/viewBinderConnectable.tsx +53 -0
- package/src/viewBindingDecorator.ts +2 -3
- package/tests/connectWithSelectorTests.tsx +67 -0
- package/tests/connectableComponentLegacyTests.tsx +405 -0
- package/tests/indexTests.ts +0 -12
- package/.dist/typings/routerProvider.d.ts +0 -18
- package/.dist/typings/shallowEqual.d.ts +0 -21
- package/.dist/typings/shouldUpdateMixin.d.ts +0 -10
- package/.dist/typings/smartComponent.d.ts +0 -33
- package/.dist/typings/utils.d.ts +0 -5
- package/src/getEspReactRenderModel.ts +0 -27
- package/src/routerProvider.tsx +0 -47
- package/src/shallowEqual.ts +0 -70
- package/src/shouldUpdateMixin.ts +0 -73
- package/src/smartComponent.tsx +0 -93
- package/src/utils.ts +0 -61
- package/tests/connectableComponentTests.tsx +0 -390
package/src/espModelContext.tsx
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import {DefaultModelAddress,
|
|
2
|
+
import {DefaultModelAddress, Router, Status, Logger, utils} from 'esp-js';
|
|
3
3
|
import {useContext, useCallback, createContext, PropsWithChildren} from 'react';
|
|
4
|
-
import {
|
|
4
|
+
import {useRouter} from './espRouterContext';
|
|
5
|
+
|
|
6
|
+
const _log = Logger.create('EspModelContext');
|
|
5
7
|
|
|
6
8
|
export type GetModelIdDelegate = () => string;
|
|
7
9
|
export const GetModelIdContext = createContext<GetModelIdDelegate>(null);
|
|
8
10
|
/**
|
|
9
11
|
* Returns a function which will return the ID of the model currently being rendered.
|
|
10
12
|
*/
|
|
11
|
-
export const useGetModelId = () => useContext(GetModelIdContext);
|
|
13
|
+
export const useGetModelId = () => useContext(GetModelIdContext)();
|
|
12
14
|
|
|
13
|
-
export type
|
|
14
|
-
export const
|
|
15
|
+
export type GetModelDelegate = <TModel = any, >() => TModel;
|
|
16
|
+
export const GetModelContext = createContext<GetModelDelegate>(null);
|
|
15
17
|
/**
|
|
16
|
-
* Returns a function which
|
|
18
|
+
* Returns a function which will return the model set at last dispatch.
|
|
17
19
|
*/
|
|
18
|
-
export const
|
|
20
|
+
export const useGetModel = () => useContext(GetModelContext)();
|
|
19
21
|
|
|
20
22
|
export type PublishModelEventDelegate = (eventType: string, event: any) => void;
|
|
21
23
|
export const PublishModelEventContext = createContext<PublishModelEventDelegate>(null);
|
|
@@ -37,16 +39,18 @@ export const usePublishModelEventWithEntityKey = () => useContext(PublishModelEv
|
|
|
37
39
|
|
|
38
40
|
export interface EspModelContextProps {
|
|
39
41
|
modelId: string;
|
|
40
|
-
|
|
42
|
+
model?: unknown;
|
|
41
43
|
}
|
|
42
44
|
|
|
43
|
-
export const EspModelContext = ({modelId, children,
|
|
45
|
+
export const EspModelContext = ({modelId, children, model}: PropsWithChildren<EspModelContextProps>) => {
|
|
46
|
+
const router = useRouter();
|
|
44
47
|
const getModelId: () => string = useCallback(() => {
|
|
45
48
|
return modelId;
|
|
46
49
|
}, [router, modelId]);
|
|
47
|
-
const
|
|
48
|
-
router
|
|
49
|
-
|
|
50
|
+
const getModel: () => any = useCallback(() => {
|
|
51
|
+
warnIfModelAccessedOutSideDispatchLoop(router, modelId);
|
|
52
|
+
return model;
|
|
53
|
+
}, [model, router, modelId]);
|
|
50
54
|
const publishModelEvent: PublishModelEventDelegate = useCallback((eventType: string, event: any) => {
|
|
51
55
|
router.publishEvent(modelId, eventType, event);
|
|
52
56
|
}, [router, modelId]);
|
|
@@ -54,16 +58,38 @@ export const EspModelContext = ({modelId, children, router}: PropsWithChildren<E
|
|
|
54
58
|
router.publishEvent(new DefaultModelAddress(modelId, entityKey), eventType, event);
|
|
55
59
|
}, [router, modelId]);
|
|
56
60
|
return (
|
|
57
|
-
<
|
|
58
|
-
<
|
|
59
|
-
<
|
|
60
|
-
<
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
</GetModelIdContext.Provider>
|
|
67
|
-
</RouterContext.Provider>
|
|
61
|
+
<GetModelIdContext.Provider value={getModelId}>
|
|
62
|
+
<PublishModelEventContext.Provider value={publishModelEvent}>
|
|
63
|
+
<PublishModelEventWithEntityKeyContext.Provider value={publishModelEventWithEntityKey}>
|
|
64
|
+
<GetModelContext.Provider value={getModel}>
|
|
65
|
+
{children}
|
|
66
|
+
</GetModelContext.Provider>
|
|
67
|
+
</PublishModelEventWithEntityKeyContext.Provider>
|
|
68
|
+
</PublishModelEventContext.Provider>
|
|
69
|
+
</GetModelIdContext.Provider>
|
|
68
70
|
);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const warnIfModelAccessedOutSideDispatchLoop = (router: Router, modelId: string) => {
|
|
74
|
+
if (process.env.NODE_ENV === 'production') {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (utils.stringIsEmpty(modelId)) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
let isDispatchModelUpdateStatus = router.isModelDispatchStatus(modelId, Status.DispatchModelUpdates);
|
|
81
|
+
if (!isDispatchModelUpdateStatus) {
|
|
82
|
+
let stack: string | undefined = undefined;
|
|
83
|
+
try {
|
|
84
|
+
// noinspection ExceptionCaughtLocallyJS
|
|
85
|
+
throw new Error();
|
|
86
|
+
} catch (e) {
|
|
87
|
+
stack = (e as Error).stack;
|
|
88
|
+
}
|
|
89
|
+
_log.warn(
|
|
90
|
+
`EspModelContext useGetModelId has been invoked outside of the models (${modelId}) dispatch loop.` +
|
|
91
|
+
`This may have unknown state state issues.` +
|
|
92
|
+
stack
|
|
93
|
+
);
|
|
94
|
+
}
|
|
69
95
|
};
|
|
@@ -0,0 +1,88 @@
|
|
|
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
|
+
import * as React from 'react';
|
|
20
|
+
import {ModelAddress, Router} from 'esp-js';
|
|
21
|
+
import {PropsWithChildren, createContext, useContext, useCallback} from 'react';
|
|
22
|
+
|
|
23
|
+
export const RouterContext = createContext<Router>(null);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Get the esp Router from the RouterContext
|
|
27
|
+
*
|
|
28
|
+
* To use this, please ensure your V-DOM has the EspRouterContext at a higher node:
|
|
29
|
+
*
|
|
30
|
+
* <EspRouterContext router={router}>
|
|
31
|
+
* <YourComponents />
|
|
32
|
+
* </EspRouterContext>
|
|
33
|
+
*/
|
|
34
|
+
export const useRouter = () => {
|
|
35
|
+
const context: Router = useContext(RouterContext);
|
|
36
|
+
if (context === undefined) {
|
|
37
|
+
throw new Error('useRouter must be used within a RouterContext.Provider');
|
|
38
|
+
}
|
|
39
|
+
return context;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type PublishEventDelegate = (modelIdOrModelAddress: string | ModelAddress, eventType: string, event: any) => void;
|
|
43
|
+
export const PublishEventContext = createContext<PublishEventDelegate>(null);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Returns a function which can be used to publish an event.
|
|
47
|
+
*
|
|
48
|
+
* To use this, please ensure your V-DOM has the EspRouterContext at a higher node:
|
|
49
|
+
*
|
|
50
|
+
* <EspRouterContext router={router}>
|
|
51
|
+
* <YourComponents />
|
|
52
|
+
* </EspRouterContext>
|
|
53
|
+
*/
|
|
54
|
+
export const usePublishEvent = () => useContext(PublishEventContext);
|
|
55
|
+
|
|
56
|
+
export type EspRouterContextProps = PropsWithChildren<{ router: Router; }>;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Used to set the router on the RouterContext making it available to components down the tree via the useRouter() hook.
|
|
60
|
+
*
|
|
61
|
+
* Also sets up the usePublishEvent() hook so nested components can publish to the router.
|
|
62
|
+
*/
|
|
63
|
+
export const EspRouterContext = ({children, router}: EspRouterContextProps) => {
|
|
64
|
+
const publishEvent: PublishEventDelegate = useCallback((modelIdOrModelAddress: string, eventType: string, event: any) => {
|
|
65
|
+
router.publishEvent(modelIdOrModelAddress, eventType, event);
|
|
66
|
+
}, [router]);
|
|
67
|
+
return (
|
|
68
|
+
<RouterContext.Provider value={router}>
|
|
69
|
+
<PublishEventContext.Provider value={publishEvent}>
|
|
70
|
+
{children}
|
|
71
|
+
</PublishEventContext.Provider>
|
|
72
|
+
</RouterContext.Provider>
|
|
73
|
+
);
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Used to set the router on the RouterContext making it available to components down the tree (via useRouter()).
|
|
78
|
+
*
|
|
79
|
+
* @deprecated use EspRouterContext
|
|
80
|
+
*/
|
|
81
|
+
export const RouterProvider = EspRouterContext;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Props for RouterProvider
|
|
85
|
+
*
|
|
86
|
+
* @deprecated use EspRouterContextProps
|
|
87
|
+
*/
|
|
88
|
+
export type RouterProviderProps = EspRouterContextProps;
|
package/src/index.ts
CHANGED
|
@@ -20,39 +20,55 @@
|
|
|
20
20
|
// import for side effects
|
|
21
21
|
import './polimer/polimerExtentsions';
|
|
22
22
|
|
|
23
|
-
export {RouterProvider, RouterContext, useRouter} from './routerProvider';
|
|
24
|
-
export {SmartComponent} from './smartComponent';
|
|
25
23
|
export {ViewBinder} from './viewBinder';
|
|
24
|
+
export {
|
|
25
|
+
ViewBinderConnectableProps,
|
|
26
|
+
ViewBinderConnectable
|
|
27
|
+
} from './viewBinderConnectable';
|
|
26
28
|
export {viewBinding, DEFAULT_VIEW_KEY} from './viewBindingDecorator';
|
|
27
|
-
export
|
|
28
|
-
|
|
29
|
+
export {
|
|
30
|
+
GetEspReactRenderModelMetadata,
|
|
31
|
+
GetEspReactRenderModelConsts,
|
|
32
|
+
getEspReactRenderModel
|
|
33
|
+
} from './polimer/getEspReactRenderModel';
|
|
29
34
|
export {createViewForModel} from './viewBindingDecorator';
|
|
30
35
|
export {
|
|
31
|
-
ConnectableComponent,
|
|
32
36
|
connect,
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
ConnectableComponentFactory
|
|
38
|
+
} from './connect';
|
|
39
|
+
export {
|
|
35
40
|
CreatePublishEventProps,
|
|
36
|
-
|
|
41
|
+
MapModelToProps,
|
|
42
|
+
ConnectableComponentProps,
|
|
37
43
|
ConnectableComponentChildProps,
|
|
44
|
+
ConnectableComponent,
|
|
38
45
|
} from './connectableComponent';
|
|
39
46
|
export {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
47
|
+
useModelSelector,
|
|
48
|
+
ConnectEqualityFn,
|
|
49
|
+
defaultConnectEqualityFn
|
|
50
|
+
} from './useModelSelector';
|
|
51
|
+
export {
|
|
52
|
+
RouterProvider,
|
|
53
|
+
RouterProviderProps,
|
|
54
|
+
EspRouterContext,
|
|
55
|
+
EspRouterContextProps,
|
|
56
|
+
RouterContext,
|
|
57
|
+
useRouter,
|
|
44
58
|
PublishEventDelegate,
|
|
45
59
|
PublishEventContext,
|
|
46
60
|
usePublishEvent,
|
|
47
|
-
|
|
61
|
+
} from './espRouterContext';
|
|
62
|
+
export {
|
|
63
|
+
useGetModelId,
|
|
64
|
+
GetModelIdDelegate,
|
|
65
|
+
GetModelIdContext,
|
|
48
66
|
PublishModelEventDelegate,
|
|
49
67
|
PublishModelEventContext,
|
|
50
68
|
usePublishModelEvent,
|
|
51
|
-
|
|
52
69
|
PublishModelEventWithEntityKeyDelegate,
|
|
53
70
|
PublishModelEventWithEntityKeyContext,
|
|
54
71
|
usePublishModelEventWithEntityKey,
|
|
55
|
-
|
|
56
72
|
EspModelContext,
|
|
57
|
-
EspModelContextProps
|
|
73
|
+
EspModelContextProps,
|
|
58
74
|
} from './espModelContext';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {EspMetadata, EspDecoratorUtil, utils} from 'esp-js';
|
|
2
|
+
|
|
3
|
+
export interface GetEspReactRenderModelMetadata {
|
|
4
|
+
functionName: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export namespace GetEspReactRenderModelConsts {
|
|
8
|
+
export const CustomDataKey = 'GetEspReactRenderModelCustomDataKey';
|
|
9
|
+
/**
|
|
10
|
+
* If there is no @getEspReactRenderModel decorator and a function by this name exists on a model, then it will be invoked to identify the sub model which will replaced the `model` prop passed to the child view of a ConnectableComponent
|
|
11
|
+
*/
|
|
12
|
+
export const HandlerFunctionName = 'getEspReactRenderModel';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A decorator which can be used to identify the render model which will replaced the `model` prop passed to the child view of a ConnectableComponent
|
|
17
|
+
*/
|
|
18
|
+
export function getEspReactRenderModel() {
|
|
19
|
+
return function (target, name, descriptor) {
|
|
20
|
+
let metadata: EspMetadata = EspDecoratorUtil.getOrCreateMetaData(target.constructor);
|
|
21
|
+
metadata.addCustomData(
|
|
22
|
+
GetEspReactRenderModelConsts.CustomDataKey,
|
|
23
|
+
<GetEspReactRenderModelMetadata>{ functionName: name }
|
|
24
|
+
);
|
|
25
|
+
return descriptor;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Returns the model which will be used to pluck state/props for the current view.
|
|
31
|
+
*
|
|
32
|
+
* This is basically a hook (not a React 'hook') to call a function on `model` and get a new sub graph to use as state.
|
|
33
|
+
*
|
|
34
|
+
*/
|
|
35
|
+
export const tryGetRenderModel = <TRenderModel>(model: any): TRenderModel => {
|
|
36
|
+
// does the given model have a decorated function we can invoke to get a different model to render?
|
|
37
|
+
if (EspDecoratorUtil.hasMetadata(model)) {
|
|
38
|
+
let metadata: GetEspReactRenderModelMetadata = EspDecoratorUtil.getCustomData(model, GetEspReactRenderModelConsts.CustomDataKey);
|
|
39
|
+
if (metadata) {
|
|
40
|
+
return model[metadata.functionName]();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// else see if there is a function with name GetEspReactRenderModelConsts.HandlerFunctionName we can invoke to get a different model to render?
|
|
44
|
+
let renderModelGetter = model[GetEspReactRenderModelConsts.HandlerFunctionName];
|
|
45
|
+
if (renderModelGetter && utils.isFunction(renderModelGetter)) {
|
|
46
|
+
return renderModelGetter.call(model);
|
|
47
|
+
}
|
|
48
|
+
// else return the initial model passed in
|
|
49
|
+
return model;
|
|
50
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import {useMemo} from 'react';
|
|
2
|
+
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector';
|
|
3
|
+
import {Guard} from 'esp-js';
|
|
4
|
+
import {useRouter} from './espRouterContext';
|
|
5
|
+
import {useGetModelId} from './espModelContext';
|
|
6
|
+
import {tryGetRenderModel} from './polimer/getEspReactRenderModel';
|
|
7
|
+
|
|
8
|
+
export const defaultConnectEqualityFn = <TSelected>(a: TSelected, b: TSelected) => a === b; // reference equality
|
|
9
|
+
|
|
10
|
+
export type ConnectEqualityFn<T> = (last: T, next: T) => boolean;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A hook which returns model state from the esp Router.
|
|
14
|
+
*
|
|
15
|
+
* This hooks wih observe updates and re-render the component if the model changes.
|
|
16
|
+
*
|
|
17
|
+
* @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.
|
|
18
|
+
* @param modelId - the modelId of the model to be observed via the esp Router.
|
|
19
|
+
* @param tryPreSelectPolimerImmutableModel - if true, and the esp model is an esp-js-polimer model, the immutable model will be used when calling the selector.
|
|
20
|
+
* @param equalityFn - Equality function which will be applied against TSelected, defaults to instance equality (a === b).
|
|
21
|
+
*/
|
|
22
|
+
export const useModelSelector = <TModel = unknown, TSelected = unknown>(
|
|
23
|
+
selector: (model: TModel) => TSelected,
|
|
24
|
+
modelId?: string,
|
|
25
|
+
tryPreSelectPolimerImmutableModel: boolean = true,
|
|
26
|
+
equalityFn: ConnectEqualityFn<TSelected> = defaultConnectEqualityFn
|
|
27
|
+
) => {
|
|
28
|
+
Guard.isFunction(selector, 'You must pass a selector function to connectSelector');
|
|
29
|
+
const router = useRouter();
|
|
30
|
+
modelId = modelId || useGetModelId();
|
|
31
|
+
const dependencies = useMemo(
|
|
32
|
+
() => {
|
|
33
|
+
// Because of how useSyncExternalStore works, there is an implicit dependency between the subscribe and getSnapshot functions.
|
|
34
|
+
// When the subscription receives the new state from the Router, it can't pass this directly onto getSnapshot, React calls that when it deems it needs to.
|
|
35
|
+
// Given that, we need to cache the state which getSnapshot will return.
|
|
36
|
+
//
|
|
37
|
+
// It's not ideal but shouldn't be a problem as here we've wrapped all these dependent functions inside a single useMemo()
|
|
38
|
+
let model: TModel = null;
|
|
39
|
+
return {
|
|
40
|
+
subscribe: (stateChanged: () => void) => {
|
|
41
|
+
const modelSubscriptionDisposable = router
|
|
42
|
+
.getModelObservable(modelId)
|
|
43
|
+
.subscribe(
|
|
44
|
+
(m: any) => {
|
|
45
|
+
// try and get a submodel to render
|
|
46
|
+
const nextModel = tryPreSelectPolimerImmutableModel
|
|
47
|
+
? tryGetRenderModel(m)
|
|
48
|
+
: m;
|
|
49
|
+
// if the above didn't manage to get a submodel,
|
|
50
|
+
// we need to mutate to force useSyncExternalStoreWithSelector to pick up the change
|
|
51
|
+
model = nextModel === m
|
|
52
|
+
? Object.create(nextModel)
|
|
53
|
+
: nextModel;
|
|
54
|
+
stateChanged();
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
return () => {
|
|
58
|
+
model = null;
|
|
59
|
+
modelSubscriptionDisposable.dispose();
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
// React expects getSnapshot to be immutable, it'll only re-render if the instance changes
|
|
63
|
+
getSnapshot: () => {
|
|
64
|
+
return model;
|
|
65
|
+
},
|
|
66
|
+
// While getSnapshot needs to model to be immutable,
|
|
67
|
+
|
|
68
|
+
// TODO confirm this comment with a test:
|
|
69
|
+
|
|
70
|
+
// useSyncExternalStoreWithSelector builds on top of this so the TSelected can defer to an equality check to determine if the change is propagated.
|
|
71
|
+
// The selector here just maps the TSelected, useSyncExternalStoreWithSelector internally does the equality check.
|
|
72
|
+
wrappedSelector: (snapshot: TModel) => {
|
|
73
|
+
// The first time this renders, react calls getSnapshot and the selector before the router has pushed a change.
|
|
74
|
+
// In that case, there is no state for any component to process, therefore, we return null;
|
|
75
|
+
return snapshot
|
|
76
|
+
? selector(snapshot)
|
|
77
|
+
: null;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
},
|
|
81
|
+
// Don't cache check against 'selector' argument as that will likely change unless the caller memoizes it.
|
|
82
|
+
[router, modelId]
|
|
83
|
+
);
|
|
84
|
+
// Docs on useSyncExternalStore https://github.com/reactwg/react-18/discussions/86
|
|
85
|
+
// Redux's usage of this: https://github.com/reduxjs/react-redux/blob/master/src/hooks/useSelector.ts
|
|
86
|
+
// Code: https://github.com/facebook/react/blob/main/packages/use-sync-external-store/src/useSyncExternalStoreWithSelector.js
|
|
87
|
+
return useSyncExternalStoreWithSelector(
|
|
88
|
+
dependencies.subscribe,
|
|
89
|
+
dependencies.getSnapshot,
|
|
90
|
+
undefined,
|
|
91
|
+
dependencies.wrappedSelector,
|
|
92
|
+
equalityFn,
|
|
93
|
+
);
|
|
94
|
+
};
|
package/src/viewBinder.tsx
CHANGED
|
@@ -16,33 +16,25 @@
|
|
|
16
16
|
*/
|
|
17
17
|
// notice_end
|
|
18
18
|
|
|
19
|
-
import * as esp from 'esp-js';
|
|
20
19
|
import * as React from 'react';
|
|
21
|
-
import
|
|
22
|
-
import {
|
|
20
|
+
import {createViewForModel} from './viewBindingDecorator';
|
|
21
|
+
import {useRouter} from './espRouterContext';
|
|
23
22
|
|
|
24
23
|
export interface ViewBinderProps {
|
|
25
24
|
model: any;
|
|
26
25
|
viewContext: string;
|
|
27
26
|
}
|
|
28
27
|
|
|
29
|
-
export
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return createViewForModel(
|
|
40
|
-
this.props.model,
|
|
41
|
-
newProps,
|
|
42
|
-
this.props.viewContext,
|
|
43
|
-
null
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
return null;
|
|
28
|
+
export const ViewBinder: React.FC<ViewBinderProps> = ({model, viewContext, ...rest}) => {
|
|
29
|
+
if (model) {
|
|
30
|
+
const router = useRouter();
|
|
31
|
+
const newProps = Object.assign({}, {model, router}, rest);
|
|
32
|
+
return createViewForModel(
|
|
33
|
+
model,
|
|
34
|
+
newProps,
|
|
35
|
+
viewContext,
|
|
36
|
+
null
|
|
37
|
+
);
|
|
47
38
|
}
|
|
48
|
-
|
|
39
|
+
return null;
|
|
40
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {useRouter} from './espRouterContext';
|
|
2
|
+
import {EspModelContext} from './espModelContext';
|
|
3
|
+
import {Logger, Router} from 'esp-js';
|
|
4
|
+
import * as React from 'react';
|
|
5
|
+
import {useModelSelector} from './useModelSelector';
|
|
6
|
+
import {createViewForModel} from './viewBindingDecorator';
|
|
7
|
+
import {tryGetRenderModel} from './polimer/getEspReactRenderModel';
|
|
8
|
+
|
|
9
|
+
const _log = Logger.create('ViewBinderConnectable');
|
|
10
|
+
|
|
11
|
+
export interface ViewBinderConnectableProps {
|
|
12
|
+
modelId?: string;
|
|
13
|
+
viewContext?: string;
|
|
14
|
+
view?: React.ComponentType;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface ViewBinderConnectableChildProps {
|
|
18
|
+
modelId: string;
|
|
19
|
+
model: object;
|
|
20
|
+
router: Router;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Dynamically binds a view found via @viewBinder(SomeJsXView) to a model managed by the Router.
|
|
25
|
+
* Optionally provide a view override and/or a viewContext
|
|
26
|
+
* @param modelId
|
|
27
|
+
* @param viewContext
|
|
28
|
+
* @param view
|
|
29
|
+
* @constructor
|
|
30
|
+
*/
|
|
31
|
+
export const ViewBinderConnectable = ({modelId, viewContext, view}: ViewBinderConnectableProps) => {
|
|
32
|
+
const router = useRouter();
|
|
33
|
+
const model = useModelSelector<object, object>(
|
|
34
|
+
m => m,
|
|
35
|
+
modelId,
|
|
36
|
+
false
|
|
37
|
+
);
|
|
38
|
+
if (!model) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
let renderModel = tryGetRenderModel<object>(model);
|
|
42
|
+
const childProps: ViewBinderConnectableChildProps = {
|
|
43
|
+
modelId,
|
|
44
|
+
router: router,
|
|
45
|
+
model: renderModel
|
|
46
|
+
};
|
|
47
|
+
let viewElement = createViewForModel(model, childProps, viewContext, view);
|
|
48
|
+
return (
|
|
49
|
+
<EspModelContext modelId={modelId} router={router} model={renderModel} {...childProps}>
|
|
50
|
+
{viewElement}
|
|
51
|
+
</EspModelContext>
|
|
52
|
+
);
|
|
53
|
+
};
|
|
@@ -18,7 +18,6 @@
|
|
|
18
18
|
|
|
19
19
|
import * as React from 'react';
|
|
20
20
|
import {Guard} from 'esp-js';
|
|
21
|
-
import {ViewComponentTypes} from './connectableComponent';
|
|
22
21
|
|
|
23
22
|
export const DEFAULT_VIEW_KEY = 'default-view-key';
|
|
24
23
|
|
|
@@ -41,8 +40,8 @@ function getMetadata(target) {
|
|
|
41
40
|
* @param displayContext
|
|
42
41
|
* @param view
|
|
43
42
|
*/
|
|
44
|
-
export function createViewForModel(model: any, props, displayContext: string, view:
|
|
45
|
-
let finalView:
|
|
43
|
+
export function createViewForModel(model: any, props, displayContext: string, view: React.ComponentType) {
|
|
44
|
+
let finalView: React.ComponentType = view; // default to that passed in, if any.
|
|
46
45
|
|
|
47
46
|
// displayContext = displayContext || DEFAULT_VIEW_KEY;
|
|
48
47
|
// the view decorator isn't on the instance, it's on the constructor function that created that instance
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import 'jest';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import {act} from 'react';
|
|
4
|
+
import {render, RenderResult} from '@testing-library/react';
|
|
5
|
+
import {useModelSelector, EspModelContext, EspRouterContext} from '../src';
|
|
6
|
+
import {observeEvent, Router} from 'esp-js';
|
|
7
|
+
// import for extra asserts
|
|
8
|
+
import '@testing-library/jest-dom';
|
|
9
|
+
|
|
10
|
+
class TestModel {
|
|
11
|
+
public value: string;
|
|
12
|
+
|
|
13
|
+
constructor() {
|
|
14
|
+
this.value = 'initial-value';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@observeEvent('test-event')
|
|
18
|
+
_onTestEvent(ev: string) {
|
|
19
|
+
this.value = ev;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const MyComponent = () => {
|
|
24
|
+
const modelValue = useModelSelector<TestModel, string>(
|
|
25
|
+
model => model.value
|
|
26
|
+
);
|
|
27
|
+
return (
|
|
28
|
+
<div>
|
|
29
|
+
<span data-testid='valueDisplay'>{modelValue}</span>
|
|
30
|
+
</div>
|
|
31
|
+
);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
describe('connectWithSelector', () => {
|
|
35
|
+
let router: Router,
|
|
36
|
+
testModel: TestModel,
|
|
37
|
+
renderResult: RenderResult,
|
|
38
|
+
testModelId = 'this-model-id';
|
|
39
|
+
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
router = new Router();
|
|
42
|
+
testModel = new TestModel();
|
|
43
|
+
router.addModel(testModelId, testModel);
|
|
44
|
+
router.observeEventsOn(testModelId, testModel);
|
|
45
|
+
renderResult = render((
|
|
46
|
+
<EspRouterContext router={router}>
|
|
47
|
+
<EspModelContext modelId={testModelId}>
|
|
48
|
+
<MyComponent />,
|
|
49
|
+
</EspModelContext>
|
|
50
|
+
</EspRouterContext>
|
|
51
|
+
));
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('component is rendered', () => {
|
|
55
|
+
let testElement = renderResult.getByTestId('valueDisplay');
|
|
56
|
+
expect(testElement).toHaveTextContent('initial-value');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('model updates are rendered', () => {
|
|
60
|
+
let testElement = renderResult.getByTestId('valueDisplay');
|
|
61
|
+
expect(testElement).toHaveTextContent('initial-value');
|
|
62
|
+
act(() => {
|
|
63
|
+
router.publishEvent(testModelId, 'test-event', 'new-value');
|
|
64
|
+
});
|
|
65
|
+
expect(testElement).toHaveTextContent('new-value');
|
|
66
|
+
});
|
|
67
|
+
});
|