Skip to content
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
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

prateek14
Copy link

@prateek14 prateek14 commented Sep 8, 2020

Status

IN DEVELOPMENT

Breaking Changes

NO

Description

APIs for BlocProvider and BlocConsumer using React Context API

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

Copy link
Collaborator

@erickjtorres erickjtorres left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is really cool. looks like test phase is failing in the build because they have not been updated to use the new bloc api

packages/react-bloc/lib/src/bloc-provider.tsx Outdated Show resolved Hide resolved
@felangel felangel added the enhancement New feature or request label Sep 8, 2020
@prateek14
Copy link
Author

I'll update the test phase

@codecov-commenter
Copy link

codecov-commenter commented Sep 10, 2020

Codecov Report

Merging #29 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #29   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            4         3    -1     
  Lines          111        84   -27     
  Branches         9        13    +4     
=========================================
- Hits           111        84   -27     
Impacted Files Coverage Δ
packages/react-bloc/lib/src/bloc-builder.tsx 100.00% <100.00%> (ø)
packages/react-bloc/lib/src/bloc-consumer.tsx 100.00% <100.00%> (ø)
packages/react-bloc/lib/src/bloc-provider.tsx 100.00% <100.00%> (ø)
packages/bloc/lib/src/transition.ts
packages/bloc/lib/src/bloc-observer.ts
packages/bloc/lib/src/bloc.ts

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8980710...8c5d91b. Read the comment docs.

<BlocBuilderInternal bloc={props.bloc} builder={props.builder} condition={props.condition} />
)
} else if (props.type) {
const context = BlocProvider.context<B>(props.type)
Copy link
Collaborator

@erickjtorres erickjtorres Sep 10, 2020

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 ?

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.

Copy link
Author

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:

  1. Minification
  2. Old browsers, <IE9
  3. Classes defined using Prototypical inheritance es5

The new react library, Recoil also uses a unique key method to track a set of state nodes.

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

Copy link
Collaborator

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.

Copy link
Collaborator

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?

Copy link
Owner

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 👍

Copy link
Author

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

const context = BlocProvider.context<B>(props.type) 
// No way to access instance properties of B without creating an object first

@prateek14 prateek14 changed the title React Bloc: BlocProvider & BlocConsumer feat: React Bloc: BlocProvider & BlocConsumer Sep 24, 2020
@MrSquaare
Copy link

👋 What's the current status of this PR?

@felangel
Copy link
Owner

👋 What's the current status of this PR?

Hey thanks for bringing this back to my attention! I'll try to take another this this week and hopefully clean up and get something release very soon 👍

@Lonli-Lokli
Copy link

Hei @felangel did you have a chance to check it?

@Rukkaitto
Copy link

This is the only absent feature that is keeping me from using bloc.js in production. Would be great if this was merged.

@felangel
Copy link
Owner

I'll try to take a look over the weekend and get this merged, sorry for the delay here!

@lgoudin
Copy link

lgoudin commented Jan 19, 2022

news on this? would be a nice alternative to passing the bloc through useContext

@felangel
Copy link
Owner

news on this? would be a nice alternative to passing the bloc through useContext

Sorry keep getting side-tracked! Will try to make time today 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants