Skip to content

Commit

Permalink
fix api and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ragokan committed Mar 27, 2021
1 parent fa077a6 commit 1257d84
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 19 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# okito
## The simplest state management solution ever, at least I think so. It depends on nothing, works really fast with minimum code usage.
---
### With okito, you don't need to wrap your material app for state, you don't need any complex libraries and most importantly your don't need context to have a state or update state.
---

 


## State Management Usage
---

#### Create Controller
```dart
Expand All @@ -22,7 +26,7 @@ class CounterController extends OkitoController {
CounterController counterController = CounterController();
```


---
#### Watch Controller
```dart
// That simple!
Expand All @@ -32,6 +36,7 @@ OkitoBuilder(
);
```

---
#### Update Controller
```dart
main(){
Expand All @@ -54,7 +59,8 @@ OkitoBuilder(



#### How to contribute okito
---
## How to contribute okito
- okito needs tests.
- okito needs a better readme file.
- okito needs more examples.
File renamed without changes.
8 changes: 3 additions & 5 deletions lib/bin/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,29 @@ class _OkitoBuilderState extends State<OkitoBuilder> {
List<Function> unmountFunctions = [];

@protected
@mustCallSuper
@override
void initState() {
super.initState();

/// Here, we mount the [watch] function to re-render state on changes.
final unmount =
controllerXstate.watch(widget.controller, () => setState(() {}));
controllerXview.watch(widget.controller, () => setState(() {}));
unmountFunctions.add(unmount);

/// Just like the above example, here we mount all of the controllers
/// that build method wants to watch.
widget.otherControllers.forEach((controller) {
final unmount = controllerXstate.watch(controller, () => setState(() {}));
final unmount = controllerXview.watch(controller, () => setState(() {}));
unmountFunctions.add(unmount);
});
}

@protected
@mustCallSuper
@override
void dispose() {
super.dispose();

/// On dispose, we would like to unmount the listeners, so that
/// On dispose, we would like to unmount the watchers, so that
/// we don't leak the memory and the [notify] function don't
/// call the [watch] function.
unmountFunctions.forEach((unmount) => unmount());
Expand Down
2 changes: 1 addition & 1 deletion lib/bin/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class OkitoController {
///```
///
@mustCallSuper
void update() => controllerXstate.notify(this);
void update() => controllerXview.notify(this);

/// [setState] method notifies all the builders to re-build the state
/// after the callback ends.
Expand Down
1 change: 1 addition & 0 deletions lib/bin/okito.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class _Okito {}

// To implement in future
// ignore: non_constant_identifier_names
final _Okito Okito = _Okito();
26 changes: 17 additions & 9 deletions lib/modules/communication.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
import '../bin/controller.dart';
import '../types/callback_types.dart';
import 'listener.dart';
import 'watcher.dart';

class _ControllerCommunication<Okito> {
final _listeners = <Listener>{};
final _watchers = <Watcher>{};

/// [notify] method is the method called when you use 'update' or 'setState'.
void notify<T extends Okito>(T type) {
for (var listener in _listeners) {
if (listener.event == type) {
listener.callback();
for (var watcher in _watchers) {
if (watcher.event == type) {
watcher.callback();
}
}
}

/// With [watch] method, state watches changes that are coming from notify.
Function watch<T extends Okito>(T type, VoidCallback<Object> callback) {
var listener = Listener(type, callback);
_listeners.add(listener);
var watcher = Watcher(type, callback);
_watchers.add(watcher);

return () => _listeners.remove(listener);
return () => _watchers.remove(watcher);
}
}

final _ControllerCommunication controllerXstate =
/// ### For Developers
///
/// The [controllerXview] is a bridge between controller
/// and view(stateful widget).
///
/// In the okito, we don't use Dependency Injection, instead we use
/// set with watchers which is triggered whenever a controller
/// notifies the state to update/re-build.
final _ControllerCommunication controllerXview =
_ControllerCommunication<OkitoController>();
4 changes: 2 additions & 2 deletions lib/modules/listener.dart → lib/modules/watcher.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import '../types/callback_types.dart';

class Listener<T> {
class Watcher<T> {
final T event;
final VoidCallback callback;

const Listener(this.event, this.callback);
const Watcher(this.event, this.callback);
}

0 comments on commit 1257d84

Please sign in to comment.