-
-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: React Bloc: BlocProvider & BlocConsumer #29
Open
prateek14
wants to merge
5
commits into
felangel:master
Choose a base branch
from
prateek14:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './src/bloc-builder'; | ||
export * from './src/bloc-builder'; | ||
export * from './src/bloc-provider'; | ||
export * from './src/bloc-consumer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as React from 'react' | ||
import { Bloc } from '@felangel/bloc' | ||
import { BlocProvider } from '../react-bloc' | ||
|
||
export type BlocConsumerBuilder<B> = (bloc: B) => JSX.Element | ||
|
||
export type BlocConsumerProps<B extends Bloc<any, any>> = { | ||
type: string | ||
consumer: BlocConsumerBuilder<B> | ||
} | ||
|
||
export function BlocConsumer<B extends Bloc<any, any>>(props: BlocConsumerProps<B>): JSX.Element { | ||
const context = BlocProvider.context<B>(props.type) | ||
return <context.Consumer>{props.consumer}</context.Consumer> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Bloc } from '@felangel/bloc' | ||
import * as React from 'react' | ||
|
||
export type BlocCreator<B extends Bloc<any, any>> = () => B | ||
|
||
export interface BlocProviderProps<B extends Bloc<any, any>> { | ||
bloc?: B | ||
create?: BlocCreator<B> | ||
type: string | ||
} | ||
|
||
export interface BlocProviderState<B extends Bloc<any, any>> { | ||
bloc: B | null | ||
blocContext: React.Context<B | null> | ||
} | ||
|
||
export class BlocProvider<B extends Bloc<any, any>> extends React.Component< | ||
React.PropsWithChildren<BlocProviderProps<B>>, | ||
BlocProviderState<B> | ||
> { | ||
constructor(props: BlocProviderProps<B>) { | ||
super(props) | ||
|
||
this.state = this.getStateFromProps() | ||
} | ||
|
||
private getStateFromProps(): BlocProviderState<B> { | ||
let bloc: B | null = null | ||
|
||
if (this.props.bloc) { | ||
bloc = this.props.bloc | ||
} else if (this.props.create) { | ||
bloc = this.props.create() | ||
} else { | ||
throw Error('BlocProvider: Expected either "bloc" or "create" property to be not null.') | ||
} | ||
|
||
let blocContext = BlocProvider.contextTypeMap[this.props.type] as React.Context<B | null> | ||
if (!blocContext) { | ||
blocContext = React.createContext<B | null>(bloc) | ||
blocContext.displayName = this.props.type | ||
BlocProvider.contextTypeMap[this.props.type] = blocContext as React.Context<unknown> | ||
} | ||
return { bloc, blocContext } | ||
} | ||
|
||
private subscribe(): void { | ||
const state = this.getStateFromProps() | ||
this.setState(state) | ||
} | ||
|
||
private unsubscribe(): void { | ||
if (!this.props.bloc && this.state.bloc) { | ||
// close only if BlocProvider was the creator | ||
this.state.bloc.close() | ||
} | ||
} | ||
|
||
componentDidUpdate(prevProps: BlocProviderProps<B>) { | ||
if ( | ||
prevProps.bloc !== this.props.bloc || | ||
prevProps.type !== this.props.type || | ||
prevProps.create !== this.props.create | ||
) { | ||
this.unsubscribe() | ||
this.subscribe() | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
this.unsubscribe() | ||
} | ||
|
||
render() { | ||
return ( | ||
<this.state.blocContext.Provider value={this.state.bloc}> | ||
{this.props.children} | ||
</this.state.blocContext.Provider> | ||
) | ||
} | ||
|
||
static clear(): void { | ||
BlocProvider.contextTypeMap = {} | ||
} | ||
|
||
static context<B>(type: string): React.Context<B> { | ||
const context = BlocProvider.contextTypeMap[type] | ||
if (context) { | ||
return context as React.Context<B> | ||
} | ||
|
||
throw Error('BlocProvider: BlocContext of type ' + type + ' not found!') | ||
} | ||
|
||
private static contextTypeMap = {} as Record<string, React.Context<unknown> | null> | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also noticed we are passing in the type manually. Can we get the name of the bloc using something like
props.bloc.constructor.name
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
props.bloc.constructor.name will be minified with any good builder, eg Webpack.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, my first iteration used constructor.name, but it doesnt work in following cases:
The new react library, Recoil also uses a unique key method to track a set of state nodes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to note, there is one more possibility, see this implementation https://github.com/cartant/ts-action/blob/master/packages/ts-action/source/action.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think we should consider trying to implement something that removes the need to pass in the type. I think someone also mentions using metadata in https://stackoverflow.com/questions/13613524/get-an-objects-class-name-at-runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could merge the PR now and have this as a separate issue to look into. @felangel what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delayed response! Going to take a closer look at this tomorrow and let y'all know what my thoughts are 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@erickjtorres Even if we were to implement an instance property called "type" as suggested by Lonli or use something like constructor.name, they will be available only in the instance context.
We need to be able to access the key in a static context