Skip to content

Commit

Permalink
feat(react-bloc)!: v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Sep 8, 2020
1 parent 6837162 commit 99985a1
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 45 deletions.
6 changes: 6 additions & 0 deletions packages/react-bloc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.3.0

- **BREAKING**: upgrade to `bloc ^0.3.0`
- docs: inline documentation improvements
- docs: minor improvements to example

# 0.2.1

Fix compatibility/upgrade issues
Expand Down
2 changes: 1 addition & 1 deletion packages/react-bloc/example/.prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"tabWidth": 4,
"tabWidth": 2,
"singleQuote": true
}
5 changes: 2 additions & 3 deletions packages/react-bloc/example/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ button {

body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu',
'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
56 changes: 28 additions & 28 deletions packages/react-bloc/example/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,42 @@ import { BlocBuilder } from '@felangel/react-bloc';
import './index.css';

enum CounterEvent {
increment = 'INCREMENT',
decrement = 'DECREMENT'
increment = 'INCREMENT',
decrement = 'DECREMENT'
}

class CounterBloc extends Bloc<CounterEvent, number> {
initialState(): number {
return 0;
}
constructor() {
super(0);
}

async *mapEventToState(event: CounterEvent) {
switch (event) {
case CounterEvent.increment:
yield this.state + 1;
break;
case CounterEvent.decrement:
yield this.state - 1;
break;
}
async *mapEventToState(event: CounterEvent) {
switch (event) {
case CounterEvent.increment:
yield this.state + 1;
break;
case CounterEvent.decrement:
yield this.state - 1;
break;
}
}
}

const App: React.FC = () => {
let bloc = new CounterBloc();
return (
<div className="App">
<p>Counter</p>
<BlocBuilder
bloc={bloc}
builder={(count: number) => {
return <p>{count}</p>;
}}
/>
<button onClick={() => bloc.add(CounterEvent.increment)}>Increase Counter</button>
<button onClick={() => bloc.add(CounterEvent.decrement)}>Decrement Counter</button>
</div>
);
let bloc = new CounterBloc();
return (
<div className="App">
<p>Counter</p>
<BlocBuilder
bloc={bloc}
builder={(count: number) => {
return <p>{count}</p>;
}}
/>
<button onClick={() => bloc.add(CounterEvent.increment)}>Increase Counter</button>
<button onClick={() => bloc.add(CounterEvent.decrement)}>Decrement Counter</button>
</div>
);
};

ReactDOM.render(<App />, document.getElementById('root'));
19 changes: 14 additions & 5 deletions packages/react-bloc/lib/src/bloc-builder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ export type BlocStateType<S> = {
blocState: S
}

/**
* `BlocBuilder` handles building a component in response to new `states`.
*
* @export
* @class BlocBuilder
* @extends {React.Component<BlocBuilderProps<B, S>, BlocStateType<S>>}
* @template B
* @template S
*/
export class BlocBuilder<B extends Bloc<any, S>, S> extends React.Component<
BlocBuilderProps<B, S>,
BlocStateType<S>
Expand All @@ -34,7 +43,7 @@ export class BlocBuilder<B extends Bloc<any, S>, S> extends React.Component<
this.subscription = Subscription.EMPTY
this.state = {
blocState: this.bloc.state
}
}
}

private subscribe(): void {
Expand All @@ -53,7 +62,7 @@ export class BlocBuilder<B extends Bloc<any, S>, S> extends React.Component<
this.subscription.unsubscribe()
}

componentDidUpdate(prevProps: BlocBuilderProps<B, S>) {
componentDidUpdate(prevProps: BlocBuilderProps<B, S>): void {
if (prevProps.bloc !== this.props.bloc) {
this.unsubscribe()
this.bloc = this.props.bloc
Expand All @@ -63,15 +72,15 @@ export class BlocBuilder<B extends Bloc<any, S>, S> extends React.Component<
}
}

componentDidMount() {
componentDidMount(): void {
this.subscribe()
}

componentWillUnmount() {
componentWillUnmount(): void {
this.unsubscribe()
}

render() {
render(): JSX.Element {
return this.builder(this.state.blocState)
}
}
8 changes: 4 additions & 4 deletions packages/react-bloc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/react-bloc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@felangel/react-bloc",
"version": "0.2.1",
"version": "0.3.0",
"description": "React Components that make it easy to implement the BLoC (Business Logic Component) design pattern. Built to be used with the bloc.js state management package.",
"keywords": [
"react",
Expand Down Expand Up @@ -119,7 +119,7 @@
"typescript": "^3.0.3"
},
"dependencies": {
"@felangel/bloc": "^0.2.3",
"@felangel/bloc": "^0.3.0",
"react": "^16.9.0",
"rxjs": "^6.5.2"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/react-bloc/test/bloc-builder.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ enum CounterEvent {
decrement
}
class CounterBloc extends Bloc<CounterEvent, number> {
initialState(): number {
return 0
constructor() {
super(0)
}

async *mapEventToState(event: CounterEvent) {
Expand Down

0 comments on commit 99985a1

Please sign in to comment.