Skip to content

Commit a9c9f57

Browse files
Changes:
- Updated ESLint config to avoid false posititves - Renamed Context to IdContext - Added useServiceState hook - Added reset prop to ServiceProvider - Removed useReducer and useState from public API
1 parent 1675983 commit a9c9f57

33 files changed

+474
-454
lines changed

.eslintrc.json

Lines changed: 109 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,109 @@
1-
{
2-
"globals": {
3-
"JSX": "readonly"
4-
},
5-
"env": {
6-
"jest/globals": true
7-
},
8-
"extends": [
9-
"eslint:recommended",
10-
"plugin:@typescript-eslint/recommended",
11-
"plugin:react/recommended",
12-
"plugin:react-hooks/recommended",
13-
"airbnb"
14-
],
15-
"parser": "@typescript-eslint/parser",
16-
"parserOptions": {
17-
"ecmaFeatures": {
18-
"jsx": true
19-
},
20-
"sourceType": "module"
21-
},
22-
"plugins": [
23-
"@typescript-eslint",
24-
"jest",
25-
"react",
26-
"react-hooks"
27-
],
28-
"rules": {
29-
"@typescript-eslint/consistent-type-definitions": [
30-
"error",
31-
"interface"
32-
],
33-
"@typescript-eslint/member-delimiter-style": "error",
34-
"@typescript-eslint/no-explicit-any": "off",
35-
"@typescript-eslint/no-non-null-assertion": "off",
36-
"@typescript-eslint/semi": "error",
37-
"import/extensions": [
38-
"error",
39-
"never",
40-
{
41-
"json": "always"
42-
}
43-
],
44-
"import/no-extraneous-dependencies": [
45-
"error",
46-
{
47-
"devDependencies": [
48-
"*.js",
49-
"**/*.test.ts",
50-
"**/*.test.tsx"
51-
]
52-
}
53-
],
54-
"no-unused-vars": "off",
55-
"react/jsx-filename-extension": [
56-
"error",
57-
{
58-
"extensions": [
59-
".tsx"
60-
]
61-
}
62-
],
63-
"react/jsx-props-no-spreading": "off",
64-
"react/state-in-constructor": [
65-
"error",
66-
"never"
67-
],
68-
"semi": "off"
69-
},
70-
"settings": {
71-
"import/resolver": {
72-
"node": {
73-
"extensions": [
74-
".js",
75-
".ts",
76-
".tsx"
77-
]
78-
}
79-
}
80-
}
81-
}
1+
{
2+
"globals": {
3+
"JSX": "readonly"
4+
},
5+
"env": {
6+
"jest/globals": true
7+
},
8+
"extends": [
9+
"eslint:recommended",
10+
"plugin:@typescript-eslint/recommended",
11+
"plugin:react/recommended",
12+
"plugin:react-hooks/recommended",
13+
"airbnb"
14+
],
15+
"parser": "@typescript-eslint/parser",
16+
"parserOptions": {
17+
"ecmaFeatures": {
18+
"jsx": true
19+
},
20+
"sourceType": "module"
21+
},
22+
"plugins": [
23+
"@typescript-eslint",
24+
"jest",
25+
"react",
26+
"react-hooks"
27+
],
28+
"rules": {
29+
"@typescript-eslint/consistent-type-definitions": [
30+
"error",
31+
"interface"
32+
],
33+
"@typescript-eslint/member-delimiter-style": "error",
34+
"@typescript-eslint/no-explicit-any": "off",
35+
"@typescript-eslint/no-non-null-assertion": "off",
36+
"@typescript-eslint/no-redeclare": "error",
37+
"@typescript-eslint/no-shadow": "error",
38+
"@typescript-eslint/semi": "error",
39+
"@typescript-eslint/space-before-function-paren": [
40+
"error",
41+
{
42+
"named": "never",
43+
"asyncArrow": "always"
44+
}
45+
],
46+
"function-paren-newline": [
47+
"error",
48+
"consistent"
49+
],
50+
"import/extensions": [
51+
"error",
52+
"never",
53+
{
54+
"json": "always"
55+
}
56+
],
57+
"import/no-extraneous-dependencies": [
58+
"error",
59+
{
60+
"devDependencies": [
61+
"*.js",
62+
"**/*.test.ts",
63+
"**/*.test.tsx"
64+
]
65+
}
66+
],
67+
"no-plusplus": [
68+
"error",
69+
{
70+
"allowForLoopAfterthoughts": true
71+
}
72+
],
73+
"no-redeclare": "off",
74+
"no-shadow": "off",
75+
"no-unused-vars": "off",
76+
"object-curly-newline": [
77+
"error",
78+
{
79+
"consistent": true
80+
}
81+
],
82+
"react/jsx-filename-extension": [
83+
"error",
84+
{
85+
"extensions": [
86+
".tsx"
87+
]
88+
}
89+
],
90+
"react/jsx-props-no-spreading": "off",
91+
"react/state-in-constructor": [
92+
"error",
93+
"never"
94+
],
95+
"semi": "off",
96+
"space-before-function-paren": "off"
97+
},
98+
"settings": {
99+
"import/resolver": {
100+
"node": {
101+
"extensions": [
102+
".js",
103+
".ts",
104+
".tsx"
105+
]
106+
}
107+
}
108+
}
109+
}

src/Context/index.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/Context/Consumer/Props.ts renamed to src/IdContext/Consumer/Props.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { ReactNode } from 'react';
22
import Id from '../Id';
33

4-
export default interface ContextConsumerProps<T> {
4+
export default interface IdContextConsumerProps<T> {
55
/**
6-
* Which ContextProvider to use
6+
* The {@link IdContextProvider} to use
77
* @default null
88
*/
99
id?: Id;

src/Context/Consumer/index.tsx renamed to src/IdContext/Consumer/index.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import React, {
2-
Context, ComponentType, memo, useCallback, useMemo,
3-
} from 'react';
1+
import React, { Context, ComponentType, memo, useCallback, useMemo } from 'react';
42
import Environment, { unwrap } from '../Environment';
5-
import Props, { defaultProps } from './Props';
3+
import IdContextConsumerProps, { defaultProps } from './Props';
64

7-
type ContextConsumer<T> = ComponentType<Props<T>>;
5+
type IdContextConsumer<T> = ComponentType<IdContextConsumerProps<T>>;
86

9-
export default ContextConsumer;
7+
export default IdContextConsumer;
8+
export { IdContextConsumerProps };
109

1110
/** @ignore */
12-
export function createConsumer<T>(
11+
export function createIdContextConsumer<T>(
1312
{ Consumer }: Context<Environment<T>>,
14-
): ContextConsumer<T> {
15-
const EnvironmentConsumer: ContextConsumer<T> = ({ id, children }) => {
13+
): IdContextConsumer<T> {
14+
const EnvironmentConsumer: IdContextConsumer<T> = ({ id, children }) => {
1615
const render = useCallback((env: Environment<T>) => {
1716
const value = unwrap(env, id);
1817

src/Context/Environment.ts renamed to src/IdContext/Environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function wrap<T>(
1313
value: T,
1414
id: Id = null,
1515
): Environment<T> {
16-
return new Map([...env, [id, value], [null, value]]);
16+
return new Map(env).set(id, value).set(null, value);
1717
}
1818

1919
/** @ignore */

src/Context/Id.ts renamed to src/IdContext/Id.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Provider id type.
2+
* {@link IdContextProviderProps.id | Provider id} type.
33
* Using the id `null` specifies the closest Provider.
44
*/
55
type Id = string | number | symbol | null;

src/Context/Provider/Props.ts renamed to src/IdContext/Provider/Props.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { ReactNode } from 'react';
22
import Id from '../Id';
33

4-
export default interface ContextProviderProps<T> {
4+
export default interface IdContextProviderProps<T> {
55
/**
6-
* A value to provide
6+
* The value to provide
77
*/
88
value: T;
99
/**
10-
* A key that allows nested Providers to be used
10+
* The key that identifies the {@link IdContextProvider} to be consumed
1111
* @default null
1212
*/
1313
id?: Id;
14+
/**
15+
* @default null
16+
*/
1417
children?: ReactNode;
1518
}
1619

src/Context/Provider/index.tsx renamed to src/IdContext/Provider/index.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
import React, {
2-
ComponentType, Context, memo, useContext, useMemo,
3-
} from 'react';
1+
import React, { ComponentType, Context, memo, useContext, useMemo } from 'react';
42
import Environment, { wrap } from '../Environment';
5-
import Props, { defaultProps } from './Props';
3+
import IdContextProviderProps, { defaultProps } from './Props';
64

7-
type ContextProvider<T> = ComponentType<Props<T>>;
5+
type IdContextProvider<T> = ComponentType<IdContextProviderProps<T>>;
86

9-
export default ContextProvider;
7+
export default IdContextProvider;
8+
export { IdContextProviderProps };
109

1110
/** @ignore */
12-
export function createProvider<T>(
11+
export function createIdContextProvider<T>(
1312
EnvironmentContext: Context<Environment<T>>,
14-
): ContextProvider<T> {
13+
): IdContextProvider<T> {
1514
const { Provider } = EnvironmentContext;
16-
const EnvironmentProvider: ContextProvider<T> = ({ value, id, children }) => {
15+
const EnvironmentProvider: IdContextProvider<T> = ({ value, id, children }) => {
1716
const prev = useContext(EnvironmentContext);
1817
const next = useMemo(() => (
1918
wrap(prev, value, id)

0 commit comments

Comments
 (0)