{ content }
11 |18 | Test Content 19 |
20 | {
63 | render() {
64 | const { props } = this;
65 |
66 | return (
67 | = (state: S) => any;
5 |
6 | // Heavily inspired by the react-redux implementation
7 | // https://github.com/reduxjs/react-redux/blob/master/src/hooks/useSelector.js
8 |
9 | const useIsomorphicLayoutEffect =
10 | typeof window !== "undefined" ? useLayoutEffect : useEffect;
11 |
12 | export function useSelector(selector: selector): any {
13 | const store = useStore();
14 | const [, forceRerender] = useReducer(s => s + 1, 0);
15 |
16 | const selectorRef = useRef(undefined);
17 | const selectedStateRef = useRef(undefined);
18 | const onChangeErrorRef = useRef(undefined);
19 |
20 | let selectedState;
21 |
22 | try {
23 | if (selectorRef.current !== selector || onChangeErrorRef.current) {
24 | selectedState = selector(store.getState());
25 | } else {
26 | selectedState = selectedStateRef.current;
27 | }
28 | } catch (err) {
29 | let errorMessage = `An error occurred while selecting the store state: ${
30 | err.message
31 | }.`;
32 |
33 | if (onChangeErrorRef.current) {
34 | errorMessage +=
35 | `\nThe error may be related with this previous error:\n${
36 | onChangeErrorRef.current.stack
37 | }` + `\n\nOriginal stack trace:`;
38 | }
39 |
40 | throw new Error(errorMessage);
41 | }
42 |
43 | useIsomorphicLayoutEffect(() => {
44 | selectorRef.current = selector;
45 | selectedStateRef.current = selectedState;
46 | onChangeErrorRef.current = undefined;
47 | });
48 |
49 | useIsomorphicLayoutEffect(
50 | () => {
51 | const checkForUpdates = () => {
52 | try {
53 | const newSelectedState = selectorRef.current(store.getState());
54 |
55 | if (newSelectedState === selectedStateRef.current) {
56 | return;
57 | }
58 |
59 | selectedStateRef.current = newSelectedState;
60 | } catch (err) {
61 | onChangeErrorRef.current = err;
62 | }
63 |
64 | forceRerender({});
65 | };
66 |
67 | const unsubscribe = store.subscribe(checkForUpdates);
68 | checkForUpdates();
69 |
70 | return () => unsubscribe();
71 | },
72 | [store]
73 | );
74 |
75 | return selectedState;
76 | }
77 |
--------------------------------------------------------------------------------
/src/svelte/components/svelte.spec.ts:
--------------------------------------------------------------------------------
1 | const Svelte = function(options?: { data: any }) {
2 | return {
3 | _state: (options ? options.data : null) || {},
4 |
5 | set: function(newState) {
6 | this._set(this.assign({}, newState));
7 | },
8 |
9 | _set: function(newState) {
10 | let oldState = this._state,
11 | changed = {},
12 | dirty = false;
13 |
14 | for (let key in newState) {
15 | if (this.differs(newState[key], oldState[key]))
16 | changed[key] = dirty = true;
17 | }
18 | if (!dirty) return;
19 |
20 | this._state = this.assign({}, oldState, newState);
21 | },
22 |
23 | get: function(key?: string) {
24 | return key ? this._state[key] : this._state;
25 | },
26 |
27 | on: function(eventName, eventHandler) {
28 | // dummy for now
29 | },
30 |
31 | differs: function(a, b) {
32 | return (
33 | a !== b || ((a && typeof a === "object") || typeof a === "function")
34 | );
35 | },
36 |
37 | assign: function(target) {
38 | let k,
39 | source,
40 | i = 1,
41 | len = arguments.length;
42 | for (; i < len; i++) {
43 | source = arguments[i];
44 | for (k in source) target[k] = source[k];
45 | }
46 |
47 | return target;
48 | }
49 | };
50 | };
51 |
52 | describe("redux-zero - svelte fake component", () => {
53 | const listener = jest.fn();
54 |
55 | beforeEach(() => {
56 | listener.mockReset();
57 | });
58 |
59 | test("set - get state", () => {
60 | const svt = Svelte();
61 | const mapToProps = ({ message }) => ({ message });
62 | const state = { message: "hello" };
63 |
64 | svt.set(state);
65 |
66 | expect(svt.get("message")).toEqual(state.message);
67 | expect(svt.get()).toEqual(state);
68 | });
69 |
70 | test("update state", () => {
71 | const svt = Svelte();
72 | const mapToProps = ({ message }) => ({ message });
73 | const state = { message: "hello" };
74 |
75 | svt.set(state);
76 |
77 | const newState = { message: "hello world" };
78 | svt.set(newState);
79 |
80 | expect(svt.get("message")).toEqual(newState.message);
81 | expect(svt.get()).toEqual(newState);
82 | });
83 | });
84 |
85 | export default Svelte;
86 |
--------------------------------------------------------------------------------
/examples/react/ssr/webpack/client.config.js:
--------------------------------------------------------------------------------
1 | const webpack = require('webpack')
2 | const commonConfig = require('./common.config')
3 | const PATHS = require('./paths')
4 | const isProduction = process.env.NODE_ENV === 'production'
5 | const babelConfig = require('../babelConfig')
6 | const clientConfig = Object.assign({}, {
7 | name: 'client',
8 |
9 | target: 'web',
10 |
11 | devtool: isProduction ? 'source-map' : 'source-map',
12 |
13 | entry: isProduction ? {
14 | main: PATHS.CLIENT
15 | } : [
16 | 'webpack-hot-middleware/client?name=client&path=/__webpack_hmr&timeout=20000&reload=false&quiet=false',
17 | PATHS.CLIENT
18 | ],
19 |
20 | devServer: isProduction ? {} : {
21 | contentBase: PATHS.PUBLIC,
22 | compress: true,
23 | port: 8081,
24 | color: true,
25 | disableHostCheck: true,
26 | host: "0.0.0.0",
27 | hot: true,
28 | noInfo: true,
29 | overlay: true
30 | },
31 |
32 | output: Object.assign({},{
33 | path: PATHS.PUBLIC,
34 | publicPath:'/assets/'
35 | }, isProduction ? {
36 | filename: 'js/[name].[chunkhash].js',
37 | chunkFilename: 'js/[name].[chunkhash].js'
38 | } : {
39 | filename: '[name].js',
40 | chunkFilename:'[name].js'
41 | }),
42 |
43 | module: {
44 | rules: [
45 | {
46 | test: /\.js$/,
47 | exclude: /node_modules/,
48 | use: {
49 | loader: 'babel-loader',
50 | options: babelConfig()
51 | }
52 | }
53 | ]
54 | },
55 |
56 | plugins: [
57 | new webpack.optimize.CommonsChunkPlugin({
58 | name: "vendor",
59 | minChunks: module => {
60 | return (
61 | module.context &&
62 | module.context.indexOf("node_modules") !== -1
63 | )
64 | }
65 | }),
66 | new webpack.optimize.CommonsChunkPlugin({
67 | name: "bootstrap",
68 | minChunks: Infinity
69 | })
70 | ].concat(isProduction ? [
71 | new webpack.DefinePlugin({
72 | __DEV__: false,
73 | __PROD__: true,
74 | __SERVER__: false,
75 | __CLIENT__: true,
76 | 'process.env.NODE_ENV': JSON.stringify('production')
77 | }),
78 | new webpack.optimize.UglifyJsPlugin()
79 | ] : [
80 | new webpack.HotModuleReplacementPlugin(),
81 | new webpack.DefinePlugin({
82 | __DEV__: true,
83 | __PROD__: false,
84 | __SERVER__: false,
85 | __CLIENT__: true,
86 | 'process.env.NODE_ENV': JSON.stringify('development')
87 | })
88 | ])
89 | },commonConfig)
90 |
91 | module.exports = clientConfig
92 |
93 |
--------------------------------------------------------------------------------
/examples/react-native/counter/ios/counterTests/counterTests.m:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2015-present, Facebook, Inc.
3 | * All rights reserved.
4 | *
5 | * This source code is licensed under the BSD-style license found in the
6 | * LICENSE file in the root directory of this source tree. An additional grant
7 | * of patent rights can be found in the PATENTS file in the same directory.
8 | */
9 |
10 | #import {{message}}
`;
20 | const Component = {
21 | template,
22 | data() {
23 | return {
24 | message: "hello"
25 | };
26 | },
27 | created() {
28 | connect(this, store, mapToProps);
29 | }
30 | };
31 | $mounted = new Vue(Component).$mount();
32 |
33 | let html = $mounted.$el.outerHTML;
34 | expect(html).toEqual("hello
");
35 |
36 | store.setState({ message: "bye" });
37 | Vue.nextTick()
38 | .then(() => {
39 | const html = $mounted.$el.outerHTML;
40 | expect(html).toEqual("bye
");
41 | done();
42 | })
43 | .catch(done);
44 | });
45 |
46 | it("should provide the actions and subscribe to changes", done => {
47 | store.setState({ count: 0 });
48 |
49 | const mapToProps = ({ count }) => ({ count });
50 | const actions = store => ({
51 | increment: state => ({ count: state.count + 1 })
52 | });
53 |
54 | const template = `{{count}}
1
{{message}}
`;
91 | const ChildComp = {
92 | template: childTemplate,
93 | data() {
94 | return {
95 | message: "hello"
96 | };
97 | },
98 | created() {
99 | connect(this, store, mapToProps);
100 | }
101 | };
102 |
103 | const template = `{{message}}
hello
hello
bye
bye