@merkur/plugin-select-preact 0.38.2
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/LICENSE +7 -0
- package/README.md +47 -0
- package/lib/index.cjs +98 -0
- package/lib/index.es9.cjs +95 -0
- package/lib/index.es9.mjs +91 -0
- package/lib/index.js +98 -0
- package/lib/index.mjs +94 -0
- package/lib/index.umd.js +1 -0
- package/package.json +66 -0
- package/rollup.config.mjs +11 -0
- package/tsconfig.json +19 -0
- package/types.d.ts +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2020 Miroslav Jancarik jancarikmiroslav@gmail.com
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://merkur.js.org/docs/getting-started" title="Getting started">
|
|
3
|
+
<img src="https://raw.githubusercontent.com/mjancarik/merkur/master/images/merkur-illustration.png" width="100px" height="100px" alt="Merkur illustration"/>
|
|
4
|
+
</a>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
# Merkur
|
|
8
|
+
|
|
9
|
+
[](https://github.com/mjancarik/merkur/actions/workflows/ci.yml)
|
|
10
|
+
[](https://www.npmjs.com/package/@merkur/core)
|
|
11
|
+

|
|
12
|
+
[](https://github.com/prettier/prettier)
|
|
13
|
+
|
|
14
|
+
The [Merkur](https://merkur.js.org/) is tiny extensible javascript library for front-end microservices(micro frontends). It allows by default server side rendering for loading performance boost. You can connect it with other frameworks or languages because merkur defines easy API. You can use one of six predefined template's library [Preact](https://preactjs.com/), [µhtml](https://github.com/WebReflection/uhtml#readme), [Svelte](https://svelte.dev/) and [vanilla](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) but you can easily extend for others.
|
|
15
|
+
|
|
16
|
+
## Features
|
|
17
|
+
- Flexible templating engine
|
|
18
|
+
- Usable with all tech stacks
|
|
19
|
+
- SSR-ready by default
|
|
20
|
+
- Easy extensible with plugins
|
|
21
|
+
- Tiny - 1 KB minified + gzipped
|
|
22
|
+
|
|
23
|
+
## Getting started
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npx @merkur/create-widget <name>
|
|
27
|
+
|
|
28
|
+
cd name
|
|
29
|
+
|
|
30
|
+
npm run dev // Point your browser at http://localhost:4444/
|
|
31
|
+
```
|
|
32
|
+

|
|
33
|
+
## Documentation
|
|
34
|
+
|
|
35
|
+
To check out [live demo](https://merkur.js.org/demo) and [docs](https://merkur.js.org/docs), visit [https://merkur.js.org](https://merkur.js.org).
|
|
36
|
+
|
|
37
|
+
## Contribution
|
|
38
|
+
|
|
39
|
+
Contribute to this project via [Pull-Requests](https://github.com/mjancarik/merkur/pulls).
|
|
40
|
+
|
|
41
|
+
We are following [Conventional Commits Specification](https://www.conventionalcommits.org/en/v1.0.0/#summary). To simplify the commit process, you can use `npm run commit` command. It opens an interactive interface, which should help you with commit message composition.
|
|
42
|
+
|
|
43
|
+
Thank you to all the people who already contributed to Merkur!
|
|
44
|
+
|
|
45
|
+
<a href="https://github.com/mjancarik/merkur/graphs/contributors">
|
|
46
|
+
<img src="https://contrib.rocks/image?repo=mjancarik/merkur" />
|
|
47
|
+
</a>
|
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@merkur/core');
|
|
4
|
+
var reselect = require('reselect');
|
|
5
|
+
var hooks = require('preact/hooks');
|
|
6
|
+
var preact = require('preact');
|
|
7
|
+
|
|
8
|
+
// eslint-disable-next-line no-unused-vars
|
|
9
|
+
const SelectContext = preact.createContext(null);
|
|
10
|
+
function SelectProvider({
|
|
11
|
+
widget,
|
|
12
|
+
children
|
|
13
|
+
}) {
|
|
14
|
+
return preact.h(SelectContext.Provider, {
|
|
15
|
+
value: widget
|
|
16
|
+
}, children);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const WIDGET_UPDATE_EVENT = '@merkur/plugin-select-preact.update';
|
|
20
|
+
let creatorOfStateSelector = createStateSelector;
|
|
21
|
+
function useSelect(data, ...selectors) {
|
|
22
|
+
const stateSelector = hooks.useRef(creatorOfStateSelector(...selectors));
|
|
23
|
+
const currentData = hooks.useRef(data);
|
|
24
|
+
const widget = hooks.useContext(SelectContext);
|
|
25
|
+
const resolveNewState = hooks.useRef(() => {
|
|
26
|
+
return stateSelector.current(widget.state, currentData.current);
|
|
27
|
+
});
|
|
28
|
+
const [state, setState] = hooks.useState(resolveNewState.current());
|
|
29
|
+
const afterChangeState = hooks.useRef(() => {
|
|
30
|
+
const newState = resolveNewState.current();
|
|
31
|
+
if (newState !== state) {
|
|
32
|
+
setState(newState);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
hooks.useEffect(() => {
|
|
36
|
+
widget.on(WIDGET_UPDATE_EVENT, afterChangeState.current);
|
|
37
|
+
return function () {
|
|
38
|
+
widget.off(WIDGET_UPDATE_EVENT, afterChangeState.current);
|
|
39
|
+
};
|
|
40
|
+
}, [widget]);
|
|
41
|
+
hooks.useEffect(() => {
|
|
42
|
+
currentData.current = data;
|
|
43
|
+
afterChangeState.current();
|
|
44
|
+
}, [data]);
|
|
45
|
+
return [state];
|
|
46
|
+
}
|
|
47
|
+
function createStateSelector(...selectors) {
|
|
48
|
+
const derivedState = reselect.createSelector(...selectors.map(selector => {
|
|
49
|
+
return (state, data) => {
|
|
50
|
+
return selector(state, data);
|
|
51
|
+
};
|
|
52
|
+
}), (...rest) => Object.assign({}, ...rest));
|
|
53
|
+
const passStateOnChange = (() => {
|
|
54
|
+
let memoizedSelector = null;
|
|
55
|
+
let selectorFunctions = null;
|
|
56
|
+
let memoizedState = null;
|
|
57
|
+
return state => {
|
|
58
|
+
if (Object.keys(state || {}).length !== Object.keys(memoizedState || {}).length) {
|
|
59
|
+
memoizedSelector = null;
|
|
60
|
+
selectorFunctions = null;
|
|
61
|
+
}
|
|
62
|
+
memoizedState = state;
|
|
63
|
+
if (!selectorFunctions) {
|
|
64
|
+
selectorFunctions = Object.keys(state).map(key => {
|
|
65
|
+
return currentState => {
|
|
66
|
+
return currentState[key] || false;
|
|
67
|
+
};
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
if (!memoizedSelector) {
|
|
71
|
+
memoizedSelector = reselect.createSelector(...selectorFunctions, () => {
|
|
72
|
+
return memoizedState;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return memoizedSelector(state);
|
|
76
|
+
};
|
|
77
|
+
})();
|
|
78
|
+
return reselect.createSelector(derivedState, passStateOnChange);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function selectPlugin() {
|
|
82
|
+
return {
|
|
83
|
+
create(widget) {
|
|
84
|
+
core.hookMethod(widget, 'update', (widget, originalFunction, ...args) => {
|
|
85
|
+
widget.emit(WIDGET_UPDATE_EVENT);
|
|
86
|
+
return originalFunction(...args);
|
|
87
|
+
});
|
|
88
|
+
return widget;
|
|
89
|
+
},
|
|
90
|
+
setup(widget) {
|
|
91
|
+
return widget;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
exports.SelectProvider = SelectProvider;
|
|
97
|
+
exports.selectPlugin = selectPlugin;
|
|
98
|
+
exports.useSelect = useSelect;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@merkur/core');
|
|
4
|
+
var reselect = require('reselect');
|
|
5
|
+
var hooks = require('preact/hooks');
|
|
6
|
+
var preact = require('preact');
|
|
7
|
+
|
|
8
|
+
// eslint-disable-next-line no-unused-vars
|
|
9
|
+
const SelectContext = preact.createContext(null);
|
|
10
|
+
function SelectProvider({
|
|
11
|
+
widget,
|
|
12
|
+
children
|
|
13
|
+
}) {
|
|
14
|
+
return preact.h(SelectContext.Provider, {
|
|
15
|
+
value: widget
|
|
16
|
+
}, children);
|
|
17
|
+
}
|
|
18
|
+
const WIDGET_UPDATE_EVENT = '@merkur/plugin-select-preact.update';
|
|
19
|
+
let creatorOfStateSelector = createStateSelector;
|
|
20
|
+
function useSelect(data, ...selectors) {
|
|
21
|
+
const stateSelector = hooks.useRef(creatorOfStateSelector(...selectors));
|
|
22
|
+
const currentData = hooks.useRef(data);
|
|
23
|
+
const widget = hooks.useContext(SelectContext);
|
|
24
|
+
const resolveNewState = hooks.useRef(() => {
|
|
25
|
+
return stateSelector.current(widget.state, currentData.current);
|
|
26
|
+
});
|
|
27
|
+
const [state, setState] = hooks.useState(resolveNewState.current());
|
|
28
|
+
const afterChangeState = hooks.useRef(() => {
|
|
29
|
+
const newState = resolveNewState.current();
|
|
30
|
+
if (newState !== state) {
|
|
31
|
+
setState(newState);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
hooks.useEffect(() => {
|
|
35
|
+
widget.on(WIDGET_UPDATE_EVENT, afterChangeState.current);
|
|
36
|
+
return function () {
|
|
37
|
+
widget.off(WIDGET_UPDATE_EVENT, afterChangeState.current);
|
|
38
|
+
};
|
|
39
|
+
}, [widget]);
|
|
40
|
+
hooks.useEffect(() => {
|
|
41
|
+
currentData.current = data;
|
|
42
|
+
afterChangeState.current();
|
|
43
|
+
}, [data]);
|
|
44
|
+
return [state];
|
|
45
|
+
}
|
|
46
|
+
function createStateSelector(...selectors) {
|
|
47
|
+
const derivedState = reselect.createSelector(...selectors.map(selector => {
|
|
48
|
+
return (state, data) => {
|
|
49
|
+
return selector(state, data);
|
|
50
|
+
};
|
|
51
|
+
}), (...rest) => Object.assign({}, ...rest));
|
|
52
|
+
const passStateOnChange = (() => {
|
|
53
|
+
let memoizedSelector = null;
|
|
54
|
+
let selectorFunctions = null;
|
|
55
|
+
let memoizedState = null;
|
|
56
|
+
return state => {
|
|
57
|
+
if (Object.keys(state || {}).length !== Object.keys(memoizedState || {}).length) {
|
|
58
|
+
memoizedSelector = null;
|
|
59
|
+
selectorFunctions = null;
|
|
60
|
+
}
|
|
61
|
+
memoizedState = state;
|
|
62
|
+
if (!selectorFunctions) {
|
|
63
|
+
selectorFunctions = Object.keys(state).map(key => {
|
|
64
|
+
return currentState => {
|
|
65
|
+
return currentState[key] || false;
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
if (!memoizedSelector) {
|
|
70
|
+
memoizedSelector = reselect.createSelector(...selectorFunctions, () => {
|
|
71
|
+
return memoizedState;
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
return memoizedSelector(state);
|
|
75
|
+
};
|
|
76
|
+
})();
|
|
77
|
+
return reselect.createSelector(derivedState, passStateOnChange);
|
|
78
|
+
}
|
|
79
|
+
function selectPlugin() {
|
|
80
|
+
return {
|
|
81
|
+
create(widget) {
|
|
82
|
+
core.hookMethod(widget, 'update', (widget, originalFunction, ...args) => {
|
|
83
|
+
widget.emit(WIDGET_UPDATE_EVENT);
|
|
84
|
+
return originalFunction(...args);
|
|
85
|
+
});
|
|
86
|
+
return widget;
|
|
87
|
+
},
|
|
88
|
+
setup(widget) {
|
|
89
|
+
return widget;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
exports.SelectProvider = SelectProvider;
|
|
94
|
+
exports.selectPlugin = selectPlugin;
|
|
95
|
+
exports.useSelect = useSelect;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { hookMethod } from '@merkur/core';
|
|
2
|
+
import { createSelector } from 'reselect';
|
|
3
|
+
import { useRef, useContext, useState, useEffect } from 'preact/hooks';
|
|
4
|
+
import { createContext, h } from 'preact';
|
|
5
|
+
|
|
6
|
+
// eslint-disable-next-line no-unused-vars
|
|
7
|
+
const SelectContext = createContext(null);
|
|
8
|
+
function SelectProvider({
|
|
9
|
+
widget,
|
|
10
|
+
children
|
|
11
|
+
}) {
|
|
12
|
+
return h(SelectContext.Provider, {
|
|
13
|
+
value: widget
|
|
14
|
+
}, children);
|
|
15
|
+
}
|
|
16
|
+
const WIDGET_UPDATE_EVENT = '@merkur/plugin-select-preact.update';
|
|
17
|
+
let creatorOfStateSelector = createStateSelector;
|
|
18
|
+
function useSelect(data, ...selectors) {
|
|
19
|
+
const stateSelector = useRef(creatorOfStateSelector(...selectors));
|
|
20
|
+
const currentData = useRef(data);
|
|
21
|
+
const widget = useContext(SelectContext);
|
|
22
|
+
const resolveNewState = useRef(() => {
|
|
23
|
+
return stateSelector.current(widget.state, currentData.current);
|
|
24
|
+
});
|
|
25
|
+
const [state, setState] = useState(resolveNewState.current());
|
|
26
|
+
const afterChangeState = useRef(() => {
|
|
27
|
+
const newState = resolveNewState.current();
|
|
28
|
+
if (newState !== state) {
|
|
29
|
+
setState(newState);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
widget.on(WIDGET_UPDATE_EVENT, afterChangeState.current);
|
|
34
|
+
return function () {
|
|
35
|
+
widget.off(WIDGET_UPDATE_EVENT, afterChangeState.current);
|
|
36
|
+
};
|
|
37
|
+
}, [widget]);
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
currentData.current = data;
|
|
40
|
+
afterChangeState.current();
|
|
41
|
+
}, [data]);
|
|
42
|
+
return [state];
|
|
43
|
+
}
|
|
44
|
+
function createStateSelector(...selectors) {
|
|
45
|
+
const derivedState = createSelector(...selectors.map(selector => {
|
|
46
|
+
return (state, data) => {
|
|
47
|
+
return selector(state, data);
|
|
48
|
+
};
|
|
49
|
+
}), (...rest) => Object.assign({}, ...rest));
|
|
50
|
+
const passStateOnChange = (() => {
|
|
51
|
+
let memoizedSelector = null;
|
|
52
|
+
let selectorFunctions = null;
|
|
53
|
+
let memoizedState = null;
|
|
54
|
+
return state => {
|
|
55
|
+
if (Object.keys(state || {}).length !== Object.keys(memoizedState || {}).length) {
|
|
56
|
+
memoizedSelector = null;
|
|
57
|
+
selectorFunctions = null;
|
|
58
|
+
}
|
|
59
|
+
memoizedState = state;
|
|
60
|
+
if (!selectorFunctions) {
|
|
61
|
+
selectorFunctions = Object.keys(state).map(key => {
|
|
62
|
+
return currentState => {
|
|
63
|
+
return currentState[key] || false;
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (!memoizedSelector) {
|
|
68
|
+
memoizedSelector = createSelector(...selectorFunctions, () => {
|
|
69
|
+
return memoizedState;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return memoizedSelector(state);
|
|
73
|
+
};
|
|
74
|
+
})();
|
|
75
|
+
return createSelector(derivedState, passStateOnChange);
|
|
76
|
+
}
|
|
77
|
+
function selectPlugin() {
|
|
78
|
+
return {
|
|
79
|
+
create(widget) {
|
|
80
|
+
hookMethod(widget, 'update', (widget, originalFunction, ...args) => {
|
|
81
|
+
widget.emit(WIDGET_UPDATE_EVENT);
|
|
82
|
+
return originalFunction(...args);
|
|
83
|
+
});
|
|
84
|
+
return widget;
|
|
85
|
+
},
|
|
86
|
+
setup(widget) {
|
|
87
|
+
return widget;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export { SelectProvider, selectPlugin, useSelect };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@merkur/core');
|
|
4
|
+
var reselect = require('reselect');
|
|
5
|
+
var hooks = require('preact/hooks');
|
|
6
|
+
var preact = require('preact');
|
|
7
|
+
|
|
8
|
+
// eslint-disable-next-line no-unused-vars
|
|
9
|
+
const SelectContext = preact.createContext(null);
|
|
10
|
+
function SelectProvider({
|
|
11
|
+
widget,
|
|
12
|
+
children
|
|
13
|
+
}) {
|
|
14
|
+
return preact.h(SelectContext.Provider, {
|
|
15
|
+
value: widget
|
|
16
|
+
}, children);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const WIDGET_UPDATE_EVENT = '@merkur/plugin-select-preact.update';
|
|
20
|
+
let creatorOfStateSelector = createStateSelector;
|
|
21
|
+
function useSelect(data, ...selectors) {
|
|
22
|
+
const stateSelector = hooks.useRef(creatorOfStateSelector(...selectors));
|
|
23
|
+
const currentData = hooks.useRef(data);
|
|
24
|
+
const widget = hooks.useContext(SelectContext);
|
|
25
|
+
const resolveNewState = hooks.useRef(() => {
|
|
26
|
+
return stateSelector.current(widget.state, currentData.current);
|
|
27
|
+
});
|
|
28
|
+
const [state, setState] = hooks.useState(resolveNewState.current());
|
|
29
|
+
const afterChangeState = hooks.useRef(() => {
|
|
30
|
+
const newState = resolveNewState.current();
|
|
31
|
+
if (newState !== state) {
|
|
32
|
+
setState(newState);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
hooks.useEffect(() => {
|
|
36
|
+
widget.on(WIDGET_UPDATE_EVENT, afterChangeState.current);
|
|
37
|
+
return function () {
|
|
38
|
+
widget.off(WIDGET_UPDATE_EVENT, afterChangeState.current);
|
|
39
|
+
};
|
|
40
|
+
}, [widget]);
|
|
41
|
+
hooks.useEffect(() => {
|
|
42
|
+
currentData.current = data;
|
|
43
|
+
afterChangeState.current();
|
|
44
|
+
}, [data]);
|
|
45
|
+
return [state];
|
|
46
|
+
}
|
|
47
|
+
function createStateSelector(...selectors) {
|
|
48
|
+
const derivedState = reselect.createSelector(...selectors.map(selector => {
|
|
49
|
+
return (state, data) => {
|
|
50
|
+
return selector(state, data);
|
|
51
|
+
};
|
|
52
|
+
}), (...rest) => Object.assign({}, ...rest));
|
|
53
|
+
const passStateOnChange = (() => {
|
|
54
|
+
let memoizedSelector = null;
|
|
55
|
+
let selectorFunctions = null;
|
|
56
|
+
let memoizedState = null;
|
|
57
|
+
return state => {
|
|
58
|
+
if (Object.keys(state || {}).length !== Object.keys(memoizedState || {}).length) {
|
|
59
|
+
memoizedSelector = null;
|
|
60
|
+
selectorFunctions = null;
|
|
61
|
+
}
|
|
62
|
+
memoizedState = state;
|
|
63
|
+
if (!selectorFunctions) {
|
|
64
|
+
selectorFunctions = Object.keys(state).map(key => {
|
|
65
|
+
return currentState => {
|
|
66
|
+
return currentState[key] || false;
|
|
67
|
+
};
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
if (!memoizedSelector) {
|
|
71
|
+
memoizedSelector = reselect.createSelector(...selectorFunctions, () => {
|
|
72
|
+
return memoizedState;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return memoizedSelector(state);
|
|
76
|
+
};
|
|
77
|
+
})();
|
|
78
|
+
return reselect.createSelector(derivedState, passStateOnChange);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function selectPlugin() {
|
|
82
|
+
return {
|
|
83
|
+
create(widget) {
|
|
84
|
+
core.hookMethod(widget, 'update', (widget, originalFunction, ...args) => {
|
|
85
|
+
widget.emit(WIDGET_UPDATE_EVENT);
|
|
86
|
+
return originalFunction(...args);
|
|
87
|
+
});
|
|
88
|
+
return widget;
|
|
89
|
+
},
|
|
90
|
+
setup(widget) {
|
|
91
|
+
return widget;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
exports.SelectProvider = SelectProvider;
|
|
97
|
+
exports.selectPlugin = selectPlugin;
|
|
98
|
+
exports.useSelect = useSelect;
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { hookMethod } from '@merkur/core';
|
|
2
|
+
import { createSelector } from 'reselect';
|
|
3
|
+
import { useRef, useContext, useState, useEffect } from 'preact/hooks';
|
|
4
|
+
import { createContext, h } from 'preact';
|
|
5
|
+
|
|
6
|
+
// eslint-disable-next-line no-unused-vars
|
|
7
|
+
const SelectContext = createContext(null);
|
|
8
|
+
function SelectProvider({
|
|
9
|
+
widget,
|
|
10
|
+
children
|
|
11
|
+
}) {
|
|
12
|
+
return h(SelectContext.Provider, {
|
|
13
|
+
value: widget
|
|
14
|
+
}, children);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const WIDGET_UPDATE_EVENT = '@merkur/plugin-select-preact.update';
|
|
18
|
+
let creatorOfStateSelector = createStateSelector;
|
|
19
|
+
function useSelect(data, ...selectors) {
|
|
20
|
+
const stateSelector = useRef(creatorOfStateSelector(...selectors));
|
|
21
|
+
const currentData = useRef(data);
|
|
22
|
+
const widget = useContext(SelectContext);
|
|
23
|
+
const resolveNewState = useRef(() => {
|
|
24
|
+
return stateSelector.current(widget.state, currentData.current);
|
|
25
|
+
});
|
|
26
|
+
const [state, setState] = useState(resolveNewState.current());
|
|
27
|
+
const afterChangeState = useRef(() => {
|
|
28
|
+
const newState = resolveNewState.current();
|
|
29
|
+
if (newState !== state) {
|
|
30
|
+
setState(newState);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
widget.on(WIDGET_UPDATE_EVENT, afterChangeState.current);
|
|
35
|
+
return function () {
|
|
36
|
+
widget.off(WIDGET_UPDATE_EVENT, afterChangeState.current);
|
|
37
|
+
};
|
|
38
|
+
}, [widget]);
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
currentData.current = data;
|
|
41
|
+
afterChangeState.current();
|
|
42
|
+
}, [data]);
|
|
43
|
+
return [state];
|
|
44
|
+
}
|
|
45
|
+
function createStateSelector(...selectors) {
|
|
46
|
+
const derivedState = createSelector(...selectors.map(selector => {
|
|
47
|
+
return (state, data) => {
|
|
48
|
+
return selector(state, data);
|
|
49
|
+
};
|
|
50
|
+
}), (...rest) => Object.assign({}, ...rest));
|
|
51
|
+
const passStateOnChange = (() => {
|
|
52
|
+
let memoizedSelector = null;
|
|
53
|
+
let selectorFunctions = null;
|
|
54
|
+
let memoizedState = null;
|
|
55
|
+
return state => {
|
|
56
|
+
if (Object.keys(state || {}).length !== Object.keys(memoizedState || {}).length) {
|
|
57
|
+
memoizedSelector = null;
|
|
58
|
+
selectorFunctions = null;
|
|
59
|
+
}
|
|
60
|
+
memoizedState = state;
|
|
61
|
+
if (!selectorFunctions) {
|
|
62
|
+
selectorFunctions = Object.keys(state).map(key => {
|
|
63
|
+
return currentState => {
|
|
64
|
+
return currentState[key] || false;
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (!memoizedSelector) {
|
|
69
|
+
memoizedSelector = createSelector(...selectorFunctions, () => {
|
|
70
|
+
return memoizedState;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return memoizedSelector(state);
|
|
74
|
+
};
|
|
75
|
+
})();
|
|
76
|
+
return createSelector(derivedState, passStateOnChange);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function selectPlugin() {
|
|
80
|
+
return {
|
|
81
|
+
create(widget) {
|
|
82
|
+
hookMethod(widget, 'update', (widget, originalFunction, ...args) => {
|
|
83
|
+
widget.emit(WIDGET_UPDATE_EVENT);
|
|
84
|
+
return originalFunction(...args);
|
|
85
|
+
});
|
|
86
|
+
return widget;
|
|
87
|
+
},
|
|
88
|
+
setup(widget) {
|
|
89
|
+
return widget;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export { SelectProvider, selectPlugin, useSelect };
|
package/lib/index.umd.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(e,r){if("function"==typeof define&&define.amd)define("@merkur/plugin-select-preact",["exports","@merkur/core","reselect","preact/hooks","preact"],r);else if("undefined"!=typeof exports)r(exports,require("@merkur/core"),require("reselect"),require("preact/hooks"),require("preact"));else{var t={exports:{}};r(t.exports,e.Merkur.Core,e.reselect,e.preactHooks,e.preact),e.merkurPluginSelectPreact=t.exports}}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,(function(e,r,t,n,u){function o(e){return function(e){if(Array.isArray(e))return a(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||c(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function c(e,r){if(e){if("string"==typeof e)return a(e,r);var t={}.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?a(e,r):void 0}}function a(e,r){(null==r||r>e.length)&&(r=e.length);for(var t=0,n=Array(r);t<r;t++)n[t]=e[t];return n}Object.defineProperty(e,"__esModule",{value:!0}),e.SelectProvider=function(e){var r=e.widget,t=e.children;return(0,u.h)(i.Provider,{value:r},t)},e.selectPlugin=function(){return{create:function(e){return(0,r.hookMethod)(e,"update",(function(e,r){e.emit(l);for(var t=arguments.length,n=new Array(t>2?t-2:0),u=2;u<t;u++)n[u-2]=arguments[u];return r.apply(void 0,n)})),e},setup:function(e){return e}}},e.useSelect=function(e){for(var r=arguments.length,t=new Array(r>1?r-1:0),u=1;u<r;u++)t[u-1]=arguments[u];var o=(0,n.useRef)(f.apply(void 0,t)),a=(0,n.useRef)(e),s=(0,n.useContext)(i),p=(0,n.useRef)((function(){return o.current(s.state,a.current)})),y=(m=(0,n.useState)(p.current()),b=2,function(e){if(Array.isArray(e))return e}(m)||function(e,r){var t=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=t){var n,u,o,c,a=[],i=!0,l=!1;try{if(o=(t=t.call(e)).next,0===r){if(Object(t)!==t)return;i=!1}else for(;!(i=(n=o.call(t)).done)&&(a.push(n.value),a.length!==r);i=!0);}catch(e){l=!0,u=e}finally{try{if(!i&&null!=t.return&&(c=t.return(),Object(c)!==c))return}finally{if(l)throw u}}return a}}(m,b)||c(m,b)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()),d=y[0],v=y[1],h=(0,n.useRef)((function(){var e=p.current();e!==d&&v(e)}));var m,b;return(0,n.useEffect)((function(){return s.on(l,h.current),function(){s.off(l,h.current)}}),[s]),(0,n.useEffect)((function(){a.current=e,h.current()}),[e]),[d]};var i=(0,u.createContext)(null);var l="@merkur/plugin-select-preact.update",f=function(){for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];var u=t.createSelector.apply(void 0,o(r.map((function(e){return function(r,t){return e(r,t)}}))).concat([function(){for(var e=arguments.length,r=new Array(e),t=0;t<e;t++)r[t]=arguments[t];return Object.assign.apply(Object,[{}].concat(r))}])),c=(a=null,i=null,l=null,function(e){return Object.keys(e||{}).length!==Object.keys(l||{}).length&&(a=null,i=null),l=e,i||(i=Object.keys(e).map((function(e){return function(r){return r[e]||!1}}))),a||(a=t.createSelector.apply(void 0,o(i).concat([function(){return l}]))),a(e)});var a,i,l;return(0,t.createSelector)(u,c)}}));
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@merkur/plugin-select-preact",
|
|
3
|
+
"version": "0.38.2",
|
|
4
|
+
"description": "Merkur select preact plugin.",
|
|
5
|
+
"main": "lib/index",
|
|
6
|
+
"module": "lib/index",
|
|
7
|
+
"types": "./types.d.ts",
|
|
8
|
+
"unpkg": "lib/index.umd.js",
|
|
9
|
+
"sideEffects": false,
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./types.d.ts",
|
|
13
|
+
"import": "./lib/index.mjs",
|
|
14
|
+
"require": "./lib/index.cjs"
|
|
15
|
+
},
|
|
16
|
+
"./lib/index.es9.mjs": "./lib/index.es9.mjs",
|
|
17
|
+
"./lib/index.es9.cjs": "./lib/index.es9.cjs"
|
|
18
|
+
},
|
|
19
|
+
"browser": {
|
|
20
|
+
"./lib/index.js": "./lib/index.js",
|
|
21
|
+
"./lib/index.cjs": "./lib/index.cjs",
|
|
22
|
+
"./lib/index.mjs": "./lib/index.mjs",
|
|
23
|
+
"./lib/index.es9.mjs": "./lib/index.es9.mjs"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"preversion": "npm test",
|
|
27
|
+
"test": "jest --no-watchman -c ./jest.config.js",
|
|
28
|
+
"test:es:version": "es-check es11 ./lib/index.mjs --module && es-check es9 ./lib/index.es9.mjs --module && es-check es9 ./lib/index.es9.cjs --module",
|
|
29
|
+
"build": "rollup -c rollup.config.mjs",
|
|
30
|
+
"prepare": "npm run build"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/mjancarik/merkur.git"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"merkur",
|
|
38
|
+
"plugin",
|
|
39
|
+
"microservices",
|
|
40
|
+
"microfrontends"
|
|
41
|
+
],
|
|
42
|
+
"author": "Miroslav Jancarik",
|
|
43
|
+
"license": "ISC",
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/mjancarik/merkur/issues"
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"registry": "https://registry.npmjs.org/",
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://merkur.js.org/",
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"reselect": "^4.1.8"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@merkur/core": "^0.38.0",
|
|
57
|
+
"@testing-library/preact": "^3.2.4"
|
|
58
|
+
},
|
|
59
|
+
"peerDependencies": {
|
|
60
|
+
"@merkur/core": "*",
|
|
61
|
+
"@merkur/plugin-component": "*",
|
|
62
|
+
"@merkur/plugin-event-emitter": "*",
|
|
63
|
+
"preact": "*"
|
|
64
|
+
},
|
|
65
|
+
"gitHead": "7c7a3f47ce06bd883ff950ab23c026f14ba46f3a"
|
|
66
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRollupESConfig,
|
|
3
|
+
createRollupES9Config,
|
|
4
|
+
createRollupUMDConfig,
|
|
5
|
+
} from '../../createRollupConfig.mjs';
|
|
6
|
+
|
|
7
|
+
let esConfig = createRollupESConfig(1);
|
|
8
|
+
let es9Config = createRollupES9Config(1);
|
|
9
|
+
let umdConfig = createRollupUMDConfig(1);
|
|
10
|
+
|
|
11
|
+
export default [esConfig, es9Config, umdConfig];
|
package/tsconfig.json
ADDED
package/types.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { WidgetState } from '@merkur/plugin-component';
|
|
2
|
+
type Selector<
|
|
3
|
+
D = null,
|
|
4
|
+
R extends Record<string, unknown> = Record<string, unknown>,
|
|
5
|
+
> = (state: WidgetState, data: D) => R;
|
|
6
|
+
type Intersection<
|
|
7
|
+
T extends {
|
|
8
|
+
[K in keyof T]: T[K];
|
|
9
|
+
},
|
|
10
|
+
> = T extends [infer Head, ...infer Tail] ? Head & Intersection<Tail> : unknown;
|
|
11
|
+
export declare function useSelect<
|
|
12
|
+
D,
|
|
13
|
+
R extends Record<string, any>[],
|
|
14
|
+
S extends {
|
|
15
|
+
[I in keyof R]: Selector<D, R[I]>;
|
|
16
|
+
},
|
|
17
|
+
>(
|
|
18
|
+
widget: any,
|
|
19
|
+
data: D,
|
|
20
|
+
...selectors: S
|
|
21
|
+
): [
|
|
22
|
+
Intersection<{
|
|
23
|
+
[K in keyof S]: ReturnType<S[K]>;
|
|
24
|
+
}>,
|
|
25
|
+
];
|
|
26
|
+
export declare function createStateSelector<
|
|
27
|
+
D,
|
|
28
|
+
R extends Record<string, any>[],
|
|
29
|
+
S extends {
|
|
30
|
+
[I in keyof R]: Selector<D, R[I]>;
|
|
31
|
+
},
|
|
32
|
+
>(
|
|
33
|
+
...selectors: S
|
|
34
|
+
): ((state: WidgetState, data: any) => any) &
|
|
35
|
+
import('reselect').OutputSelectorFields<
|
|
36
|
+
(args_0: any) => any,
|
|
37
|
+
{
|
|
38
|
+
clearCache: () => void;
|
|
39
|
+
}
|
|
40
|
+
> & {
|
|
41
|
+
clearCache: () => void;
|
|
42
|
+
};
|