Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
patrickroberts committed Oct 30, 2020
1 parent 1675983 commit a9c9f57
Show file tree
Hide file tree
Showing 33 changed files with 474 additions and 454 deletions.
190 changes: 109 additions & 81 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,81 +1,109 @@
{
"globals": {
"JSX": "readonly"
},
"env": {
"jest/globals": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react/recommended",
"plugin:react-hooks/recommended",
"airbnb"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"sourceType": "module"
},
"plugins": [
"@typescript-eslint",
"jest",
"react",
"react-hooks"
],
"rules": {
"@typescript-eslint/consistent-type-definitions": [
"error",
"interface"
],
"@typescript-eslint/member-delimiter-style": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/semi": "error",
"import/extensions": [
"error",
"never",
{
"json": "always"
}
],
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": [
"*.js",
"**/*.test.ts",
"**/*.test.tsx"
]
}
],
"no-unused-vars": "off",
"react/jsx-filename-extension": [
"error",
{
"extensions": [
".tsx"
]
}
],
"react/jsx-props-no-spreading": "off",
"react/state-in-constructor": [
"error",
"never"
],
"semi": "off"
},
"settings": {
"import/resolver": {
"node": {
"extensions": [
".js",
".ts",
".tsx"
]
}
}
}
}
{
"globals": {
"JSX": "readonly"
},
"env": {
"jest/globals": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react/recommended",
"plugin:react-hooks/recommended",
"airbnb"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"sourceType": "module"
},
"plugins": [
"@typescript-eslint",
"jest",
"react",
"react-hooks"
],
"rules": {
"@typescript-eslint/consistent-type-definitions": [
"error",
"interface"
],
"@typescript-eslint/member-delimiter-style": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-redeclare": "error",
"@typescript-eslint/no-shadow": "error",
"@typescript-eslint/semi": "error",
"@typescript-eslint/space-before-function-paren": [
"error",
{
"named": "never",
"asyncArrow": "always"
}
],
"function-paren-newline": [
"error",
"consistent"
],
"import/extensions": [
"error",
"never",
{
"json": "always"
}
],
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": [
"*.js",
"**/*.test.ts",
"**/*.test.tsx"
]
}
],
"no-plusplus": [
"error",
{
"allowForLoopAfterthoughts": true
}
],
"no-redeclare": "off",
"no-shadow": "off",
"no-unused-vars": "off",
"object-curly-newline": [
"error",
{
"consistent": true
}
],
"react/jsx-filename-extension": [
"error",
{
"extensions": [
".tsx"
]
}
],
"react/jsx-props-no-spreading": "off",
"react/state-in-constructor": [
"error",
"never"
],
"semi": "off",
"space-before-function-paren": "off"
},
"settings": {
"import/resolver": {
"node": {
"extensions": [
".js",
".ts",
".tsx"
]
}
}
}
}
56 changes: 0 additions & 56 deletions src/Context/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ReactNode } from 'react';
import Id from '../Id';

export default interface ContextConsumerProps<T> {
export default interface IdContextConsumerProps<T> {
/**
* Which ContextProvider to use
* The {@link IdContextProvider} to use
* @default null
*/
id?: Id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import React, {
Context, ComponentType, memo, useCallback, useMemo,
} from 'react';
import React, { Context, ComponentType, memo, useCallback, useMemo } from 'react';
import Environment, { unwrap } from '../Environment';
import Props, { defaultProps } from './Props';
import IdContextConsumerProps, { defaultProps } from './Props';

type ContextConsumer<T> = ComponentType<Props<T>>;
type IdContextConsumer<T> = ComponentType<IdContextConsumerProps<T>>;

export default ContextConsumer;
export default IdContextConsumer;
export { IdContextConsumerProps };

/** @ignore */
export function createConsumer<T>(
export function createIdContextConsumer<T>(
{ Consumer }: Context<Environment<T>>,
): ContextConsumer<T> {
const EnvironmentConsumer: ContextConsumer<T> = ({ id, children }) => {
): IdContextConsumer<T> {
const EnvironmentConsumer: IdContextConsumer<T> = ({ id, children }) => {
const render = useCallback((env: Environment<T>) => {
const value = unwrap(env, id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function wrap<T>(
value: T,
id: Id = null,
): Environment<T> {
return new Map([...env, [id, value], [null, value]]);
return new Map(env).set(id, value).set(null, value);
}

/** @ignore */
Expand Down
2 changes: 1 addition & 1 deletion src/Context/Id.ts → src/IdContext/Id.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Provider id type.
* {@link IdContextProviderProps.id | Provider id} type.
* Using the id `null` specifies the closest Provider.
*/
type Id = string | number | symbol | null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { ReactNode } from 'react';
import Id from '../Id';

export default interface ContextProviderProps<T> {
export default interface IdContextProviderProps<T> {
/**
* A value to provide
* The value to provide
*/
value: T;
/**
* A key that allows nested Providers to be used
* The key that identifies the {@link IdContextProvider} to be consumed
* @default null
*/
id?: Id;
/**
* @default null
*/
children?: ReactNode;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import React, {
ComponentType, Context, memo, useContext, useMemo,
} from 'react';
import React, { ComponentType, Context, memo, useContext, useMemo } from 'react';
import Environment, { wrap } from '../Environment';
import Props, { defaultProps } from './Props';
import IdContextProviderProps, { defaultProps } from './Props';

type ContextProvider<T> = ComponentType<Props<T>>;
type IdContextProvider<T> = ComponentType<IdContextProviderProps<T>>;

export default ContextProvider;
export default IdContextProvider;
export { IdContextProviderProps };

/** @ignore */
export function createProvider<T>(
export function createIdContextProvider<T>(
EnvironmentContext: Context<Environment<T>>,
): ContextProvider<T> {
): IdContextProvider<T> {
const { Provider } = EnvironmentContext;
const EnvironmentProvider: ContextProvider<T> = ({ value, id, children }) => {
const EnvironmentProvider: IdContextProvider<T> = ({ value, id, children }) => {
const prev = useContext(EnvironmentContext);
const next = useMemo(() => (
wrap(prev, value, id)
Expand Down
Loading

0 comments on commit a9c9f57

Please sign in to comment.