-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(bloc): 0.3.0 CHANGELOG and example improvements
- Loading branch information
Showing
3 changed files
with
53 additions
and
40 deletions.
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
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,4 +1,4 @@ | ||
{ | ||
"tabWidth": 4, | ||
"tabWidth": 2, | ||
"singleQuote": true | ||
} |
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,60 +1,60 @@ | ||
import { Bloc, BlocObserver, Transition } from '@felangel/bloc'; | ||
|
||
enum CounterEvent { | ||
increment = 'INCREMENT', | ||
decrement = 'DECREMENT' | ||
increment = 'INCREMENT', | ||
decrement = 'DECREMENT' | ||
} | ||
|
||
class MyBlocObserver extends BlocObserver { | ||
onEvent(_: Bloc<any, any>, event: CounterEvent) { | ||
console.log(`added ${event}`); | ||
} | ||
onEvent(_: Bloc<any, any>, event: CounterEvent) { | ||
console.log(`added ${event}`); | ||
} | ||
|
||
onTransition(_: Bloc<any, any>, transition: Transition<any, any>) { | ||
console.log(transition); | ||
} | ||
onTransition(_: Bloc<any, any>, transition: Transition<any, any>) { | ||
console.log(transition); | ||
} | ||
|
||
onError(_: Bloc<any, any>, error: any) { | ||
console.log(`error: ${error}`); | ||
} | ||
onError(_: Bloc<any, any>, error: any) { | ||
console.log(`error: ${error}`); | ||
} | ||
} | ||
|
||
class CounterBloc extends Bloc<CounterEvent, number> { | ||
constructor() { | ||
super(0); | ||
} | ||
|
||
async *mapEventToState(event: CounterEvent) { | ||
switch (event) { | ||
case CounterEvent.increment: | ||
await wait(1000); // Simulating Latency | ||
yield this.state + 1; | ||
break; | ||
case CounterEvent.decrement: | ||
await wait(500); // Simulating Latency | ||
yield this.state - 1; | ||
break; | ||
} | ||
constructor() { | ||
super(0); | ||
} | ||
|
||
async *mapEventToState(event: CounterEvent) { | ||
switch (event) { | ||
case CounterEvent.increment: | ||
await wait(1000); // Simulating Latency | ||
yield this.state + 1; | ||
break; | ||
case CounterEvent.decrement: | ||
await wait(500); // Simulating Latency | ||
yield this.state - 1; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
(async function main() { | ||
Bloc.observer = new MyBlocObserver(); | ||
const counterBloc = new CounterBloc(); | ||
Bloc.observer = new MyBlocObserver(); | ||
const counterBloc = new CounterBloc(); | ||
|
||
counterBloc.add(CounterEvent.increment); | ||
counterBloc.add(CounterEvent.increment); | ||
counterBloc.add(CounterEvent.increment); | ||
counterBloc.add(CounterEvent.increment); | ||
counterBloc.add(CounterEvent.increment); | ||
counterBloc.add(CounterEvent.increment); | ||
|
||
counterBloc.add(CounterEvent.decrement); | ||
counterBloc.add(CounterEvent.decrement); | ||
counterBloc.add(CounterEvent.decrement); | ||
counterBloc.add(CounterEvent.decrement); | ||
counterBloc.add(CounterEvent.decrement); | ||
counterBloc.add(CounterEvent.decrement); | ||
})(); | ||
|
||
async function wait(ms: number): Promise<void> { | ||
return new Promise<void>((resolve, _) => { | ||
setTimeout(() => { | ||
resolve(); | ||
}, ms); | ||
}); | ||
return new Promise<void>((resolve, _) => { | ||
setTimeout(() => { | ||
resolve(); | ||
}, ms); | ||
}); | ||
} |