Skip to content

Commit

Permalink
Finish scaffolding for demo
Browse files Browse the repository at this point in the history
  • Loading branch information
spautz committed Jul 25, 2020
1 parent 3e44c31 commit c3ee076
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 48 deletions.
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@
"test:watch": "jest --coverage --watch",
"types": "tsc --noEmit --p tsconfig.json"
},
"dependencies": {
"recoil": "^0.0.10"
},
"dependencies": {},
"devDependencies": {
"@types/jest": "^26.0.4",
"@types/react-redux": "^7.1.9",
"@types/react": "^16.9.43",
"@typescript-eslint/eslint-plugin": "^3.6.0",
"@typescript-eslint/parser": "^3.6.0",
"coveralls": "^3.1.0",
Expand All @@ -67,15 +65,21 @@
"lint-staged": "^10.2.11",
"microbundle": "^0.12.2",
"prettier": "^2.0.5",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"recoil": "^0.0.10",
"redux": "^4.0.5",
"rimraf": "^3.0.2",
"standard-version": "^8.0.0",
"ts-jest": "^26.1.1",
"tslib": "^2.0.0",
"typescript": "^3.9.6",
"yarn-or-npm": "^3.0.1"
},
"peerDependencies": {},
"peerDependencies": {
"recoil": "^0.0.10",
"redux": "^4.0.0"
},
"lint-staged": {
"**/*.{css,html,js,jsx,json,less,md,scss,ts,tsx}": [
"prettier --write"
Expand Down
15 changes: 15 additions & 0 deletions src/SyncReduxToRecoil.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';

interface SyncReduxToRecoilProps {
enabled?: boolean;
}

const SyncReduxToRecoil: React.FC<SyncReduxToRecoilProps> = () => {
throw new Error('SyncReduxToRecoil: not implemented');
};

SyncReduxToRecoil.defaultProps = {
enabled: true,
};

export default SyncReduxToRecoil;
26 changes: 26 additions & 0 deletions src/atomFromRedux.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { RecoilState, selector } from 'recoil';

import { getReduxSourceAtom, getValueAtPath } from './util';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const atomFromRedux = <ReturnType = any>(namespace: string): RecoilState<ReturnType> => {
// The leading dot is just a convention to make things easier to understand: empty parts are ignored
const namespaceParts = namespace.split('.').filter((word) => !!word);

// Although named "atomFromRedux", each instance is actually just a selector. They all pull from a single atom
const selectorFromRedux = selector<ReturnType>({
key: `redux-to-recoil:${namespaceParts.join('.')}`,
get: ({ get }) => {
const reduxSourceAtom = getReduxSourceAtom();
const reduxState = get(reduxSourceAtom);
return getValueAtPath(reduxState, namespaceParts);
},
set: ({ get, set }) => {
console.log('TODO: Bidirectional support', namespace, { get, set });
},
});

return selectorFromRedux;
};

export default atomFromRedux;
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export default {};
export { default as SyncReduxToRecoil } from './SyncReduxToRecoil';
export * from './SyncReduxToRecoil';

export { default as atomFromRedux } from './atomFromRedux';
export * from './atomFromRedux';

export { default as selectorFromReselect } from './selectorFromReselect';
export * from './selectorFromReselect';
5 changes: 5 additions & 0 deletions src/selectorFromReselect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const selectorFromReselect = (): never => {
throw new Error('selectorFromReselect: not implemented');
};

export default selectorFromReselect;
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Microbundle manages this: it's not a real variable
declare const __DEV__: boolean;
40 changes: 40 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { RecoilState, atom } from 'recoil';

type ReduxState = unknown;
let reduxSourceAtom: RecoilState<ReduxState>;

const getReduxSourceAtom = (): typeof reduxSourceAtom => {
if (__DEV__ && !reduxSourceAtom) {
throw new Error('Cannot access redux state: SyncReduxToRecoil must be mounted');
}
return reduxSourceAtom;
};

const initializeReduxSourceAtom = (reduxState: ReduxState): void => {
if (__DEV__ && !reduxSourceAtom) {
throw new Error('Cannot set redux state: SyncReduxToRecoil is already mounted');
}

reduxSourceAtom = atom({
key: '__redux-to-recoil',
default: reduxState,
});
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getValueAtPath = (state: ReduxState, pathParts: Array<string>): any => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let currentResult: any = state;
let i = 0;
while (currentResult && i < pathParts.length) {
currentResult = currentResult[pathParts[i]];
i++;
}

if (i === pathParts.length) {
return currentResult;
}
return undefined;
};

export { getReduxSourceAtom, initializeReduxSourceAtom, getValueAtPath };
11 changes: 11 additions & 0 deletions tests/atomFromRedux.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-env jest */

import atomFromRedux from '../src/atomFromRedux';

describe('TODO', () => {
it('@TODO', () => {
const sampleAtom = atomFromRedux('.sample');

console.log('@TODO', sampleAtom);
});
});
9 changes: 0 additions & 9 deletions tests/index.test.ts

This file was deleted.

1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"lib": ["dom", "ES2019"],
"allowJs": true,
"declaration": true,
"jsx": "react",
"sourceMap": true,
"removeComments": false,
/* Strict Type-Checking Options */
Expand Down
77 changes: 44 additions & 33 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1217,14 +1217,6 @@
dependencies:
"@types/node" "*"

"@types/hoist-non-react-statics@^3.3.0":
version "3.3.1"
resolved "https://registry.yarnpkg.com/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.1.tgz#1124aafe5118cb591977aeb1ceaaed1070eb039f"
integrity sha512-iMIqiko6ooLrTh1joXodJK5X9xeEALT1kM5G3ZLhD3hszxBdIEd5C75U834D9mLcINgD4OyZf5uQXjkuYydWvA==
dependencies:
"@types/react" "*"
hoist-non-react-statics "^3.3.0"

"@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1":
version "2.0.3"
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.3.tgz#4ba8ddb720221f432e443bd5f9117fd22cfd4762"
Expand Down Expand Up @@ -1293,20 +1285,10 @@
resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.4.tgz#15925414e0ad2cd765bfef58842f7e26a7accb24"
integrity sha512-1HcDas8SEj4z1Wc696tH56G8OlRaH/sqZOynNNB+HF0WOeXPaxTtbYzJY2oEfiUxjSKjhCKr+MvR7dCHcEelug==

"@types/react-redux@^7.1.9":
version "7.1.9"
resolved "https://registry.yarnpkg.com/@types/react-redux/-/react-redux-7.1.9.tgz#280c13565c9f13ceb727ec21e767abe0e9b4aec3"
integrity sha512-mpC0jqxhP4mhmOl3P4ipRsgTgbNofMRXJb08Ms6gekViLj61v1hOZEKWDCyWsdONr6EjEA6ZHXC446wdywDe0w==
dependencies:
"@types/hoist-non-react-statics" "^3.3.0"
"@types/react" "*"
hoist-non-react-statics "^3.3.0"
redux "^4.0.0"

"@types/react@*":
version "16.9.42"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.42.tgz#9776508d59c1867bbf9bd7f036dab007fdaa1cb7"
integrity sha512-iGy6HwfVfotqJ+PfRZ4eqPHPP5NdPZgQlr0lTs8EfkODRBV9cYy8QMKcC9qPCe1JrESC1Im6SrCFR6tQgg74ag==
"@types/react@^16.9.43":
version "16.9.43"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.43.tgz#c287f23f6189666ee3bebc2eb8d0f84bcb6cdb6b"
integrity sha512-PxshAFcnJqIWYpJbLPriClH53Z2WlJcVZE+NP2etUtWQs2s7yIMj3/LDKZT/5CHJ/F62iyjVCDu2H3jHEXIxSg==
dependencies:
"@types/prop-types" "*"
csstype "^2.2.0"
Expand Down Expand Up @@ -3654,13 +3636,6 @@ hex-color-regex@^1.1.0:
resolved "https://registry.yarnpkg.com/hex-color-regex/-/hex-color-regex-1.1.0.tgz#4c06fccb4602fe2602b3c93df82d7e7dbf1a8a8e"
integrity sha512-l9sfDFsuqtOqKDsQdqrMRk0U85RZc0RtOR9yPI7mRVOa4FsR/BVnZ0shmQRM96Ji99kYZP/7hn1cedc1+ApsTQ==

hoist-non-react-statics@^3.3.0:
version "3.3.2"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz#ece0acaf71d62c2969c2ec59feff42a4b1a85b45"
integrity sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==
dependencies:
react-is "^16.7.0"

hosted-git-info@^2.1.4:
version "2.8.8"
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.8.tgz#7539bd4bc1e0e0a895815a2e0262420b12858488"
Expand Down Expand Up @@ -4928,7 +4903,7 @@ log-update@^4.0.0:
slice-ansi "^4.0.0"
wrap-ansi "^6.2.0"

loose-envify@^1.0.0, loose-envify@^1.4.0:
loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
Expand Down Expand Up @@ -5400,7 +5375,7 @@ oauth-sign@~0.9.0:
resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455"
integrity sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ==

object-assign@^4.0.1, object-assign@^4.1.0:
object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
Expand Down Expand Up @@ -6188,6 +6163,15 @@ prompts@^2.0.1:
kleur "^3.0.3"
sisteransi "^1.0.4"

prop-types@^15.6.2:
version "15.7.2"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.7.2.tgz#52c41e75b8c87e72b9d9360e0206b99dcbffa6c5"
integrity sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==
dependencies:
loose-envify "^1.4.0"
object-assign "^4.1.1"
react-is "^16.8.1"

psl@^1.1.28:
version "1.8.0"
resolved "https://registry.yarnpkg.com/psl/-/psl-1.8.0.tgz#9326f8bcfb013adcc005fdff056acce020e51c24"
Expand Down Expand Up @@ -6226,11 +6210,30 @@ quick-lru@^4.0.1:
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-4.0.1.tgz#5b8878f113a58217848c6482026c73e1ba57727f"
integrity sha512-ARhCpm70fzdcvNQfPoy49IaanKkTlRWF2JMzqhcJbhSFRZv7nPTvZJdcY7301IPmvW+/p0RgIWnQDLJxifsQ7g==

react-is@^16.12.0, react-is@^16.7.0:
react-dom@^16.13.1:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-16.13.1.tgz#c1bd37331a0486c078ee54c4740720993b2e0e7f"
integrity sha512-81PIMmVLnCNLO/fFOQxdQkvEq/+Hfpv24XNJfpyZhTRfO0QcmQIF/PgCa1zCOj2w1hrn12MFLyaJ/G0+Mxtfag==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
prop-types "^15.6.2"
scheduler "^0.19.1"

react-is@^16.12.0, react-is@^16.8.1:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==

react@^16.13.1:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react/-/react-16.13.1.tgz#2e818822f1a9743122c063d6410d85c1e3afe48e"
integrity sha512-YMZQQq32xHLX0bz5Mnibv1/LHb3Sqzngu7xstSM+vrkE5Kzr9xE0yMByK5kMoTK30YVJE61WfbxIFFvfeDKT1w==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
prop-types "^15.6.2"

read-pkg-up@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-1.0.1.tgz#9d63c13276c065918d57f002a57f40a1b643fb02"
Expand Down Expand Up @@ -6342,7 +6345,7 @@ redent@^3.0.0:
indent-string "^4.0.0"
strip-indent "^3.0.0"

redux@^4.0.0:
redux@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/redux/-/redux-4.0.5.tgz#4db5de5816e17891de8a80c424232d06f051d93f"
integrity sha512-VSz1uMAH24DM6MF72vcojpYPtrTUu3ByVWfPL1nPfVRb5mZVTve5GnNCUV53QM/BZ66xfWrm0CTWoM+Xlz8V1w==
Expand Down Expand Up @@ -6716,6 +6719,14 @@ saxes@^5.0.0:
dependencies:
xmlchars "^2.2.0"

scheduler@^0.19.1:
version "0.19.1"
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.19.1.tgz#4f3e2ed2c1a7d65681f4c854fa8c5a1ccb40f196"
integrity sha512-n/zwRWRYSUj0/3g/otKDRPMh6qv2SYMWNq85IEa8iZyAv8od9zDYpGSnpBEjNgcMNq6Scbu5KfIPxNF72R/2EA==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"

semver-compare@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/semver-compare/-/semver-compare-1.0.0.tgz#0dee216a1c941ab37e9efb1788f6afc5ff5537fc"
Expand Down

0 comments on commit c3ee076

Please sign in to comment.