Skip to content

Commit 225c6cd

Browse files
committed
Optimized memory usage for increased efficiency
1 parent 70c1be1 commit 225c6cd

10 files changed

+71
-27
lines changed

README.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class MyApp extends StatelessWidget {
9292
9393
void incrementCounter() {
9494
// You can call state from anywhere.
95-
var state = June.getState(CounterVM());
95+
var state = June.getState(() => CounterVM());
9696
state.count++;
9797
9898
state.setState();
@@ -140,7 +140,7 @@ class MyApp extends StatelessWidget {
140140
floatingActionButton: FloatingActionButton(
141141
onPressed: () {
142142
// This way, you can call actions inside the model.
143-
June.getState(CounterVM()).incrementCounter();
143+
June.getState(() => CounterVM()).incrementCounter();
144144
},
145145
child: const Icon(Icons.add),
146146
),
@@ -232,13 +232,13 @@ class MyApp extends StatelessWidget {
232232
}
233233
234234
void increaseBasicInstance() {
235-
var state = June.getState(CounterVM());
235+
var state = June.getState(() => CounterVM());
236236
state.count++;
237237
state.setState();
238238
}
239239
240240
void increaseObjectInstanceCreatedWithTags() {
241-
var state = June.getState(CounterVM(), tag: "SomeId");
241+
var state = June.getState(() => CounterVM(), tag: "SomeId");
242242
state.count++;
243243
state.setState();
244244
}
@@ -248,6 +248,18 @@ class CounterVM extends JuneState {
248248
}
249249
```
250250

251+
## Migration
252+
### 0.8.x to 1.0.0
253+
#### Before
254+
```dart
255+
June.getState(CounterVM());
256+
```
257+
258+
#### After
259+
```dart
260+
June.getState(() => CounterVM());
261+
```
262+
251263
## Acknowledgements
252264

253265
This project has been significantly inspired by the native state management system of Flutter, the Provider package, the GetX library, the Bloc pattern, and the state management approach in Svelte. Each of these tools and methodologies has provided substantial inspiration and direction in the development of our own state management solution. We sincerely express our gratitude to all the developers and contributors of Flutter's native state management, Provider, GetX, Bloc, and Svelte's state management. Their innovative approaches and dedication to open source have had a profound impact on shaping our library. We are deeply thankful for the insights, ideas, and principles shared by these communities, which have greatly enriched our project and the wider open source community.

example/lib/main.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class MyApp extends StatelessWidget {
3636
}
3737

3838
void incrementCounter() {
39-
var state = June.getState(CounterVM());
39+
var state = June.getState(() => CounterVM());
4040
state.count++;
4141

4242
state.setState();

example/lib/main2.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MyApp extends StatelessWidget {
3131
),
3232
floatingActionButton: FloatingActionButton(
3333
onPressed: () {
34-
June.getState(CounterVM()).incrementCounter();
34+
June.getState(() => CounterVM()).incrementCounter();
3535
},
3636
child: const Icon(Icons.add),
3737
),

example/lib/main_http_instance.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class MyApp extends StatelessWidget {
2121
children: <Widget>[
2222
FilledButton(
2323
onPressed: () {
24-
June.getState(MyController()).getBoredApi();
24+
June.getState(() => MyController()).getBoredApi();
2525
},
2626
child: const Text("Get boredApi"),
2727
),
@@ -80,15 +80,15 @@ class MyApp extends StatelessWidget {
8080
}
8181

8282
checkInstanceWithTag() {
83-
var c = June.getState(MyController());
84-
var c2 = June.getState(MyController(), tag: "new");
83+
var c = June.getState(() => MyController());
84+
var c2 = June.getState(() => MyController(), tag: "new");
8585
c2.instance = c == c2;
8686
c2.setState();
8787
}
8888

8989
checkInstanceNoTag() {
90-
var c = June.getState(MyController());
91-
var c2 = June.getState(MyController());
90+
var c = June.getState(() => MyController());
91+
var c2 = June.getState(() => MyController());
9292
c.instance = c == c2;
9393
c.setState();
9494
}

example/lib/multi_instance_example.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ class MyApp extends StatelessWidget {
6262
}
6363

6464
void increaseBasicInstance() {
65-
var state = June.getState(CounterVM());
65+
var state = June.getState(() => CounterVM());
6666
state.count++;
6767
state.setState();
6868
}
6969

7070
void increaseObjectInstanceCreatedWithTags() {
71-
var state = June.getState(CounterVM(), tag: "SomeId");
71+
var state = June.getState(() => CounterVM(), tag: "SomeId");
7272
state.count++;
7373
state.setState();
7474
}

example/pubspec.lock

+35-3
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ packages:
4545
dependency: "direct main"
4646
description:
4747
name: cupertino_icons
48-
sha256: d57953e10f9f8327ce64a508a355f0b1ec902193f66288e8cb5070e7c47eeb2d
48+
sha256: ba631d1c7f7bef6b729a622b7b752645a2d076dba9976925b8f25725a30e1ee6
4949
url: "https://pub.dev"
5050
source: hosted
51-
version: "1.0.6"
51+
version: "1.0.8"
5252
fake_async:
5353
dependency: transitive
5454
description:
@@ -75,6 +75,22 @@ packages:
7575
description: flutter
7676
source: sdk
7777
version: "0.0.0"
78+
http:
79+
dependency: "direct overridden"
80+
description:
81+
name: http
82+
sha256: "761a297c042deedc1ffbb156d6e2af13886bb305c2a343a4d972504cd67dd938"
83+
url: "https://pub.dev"
84+
source: hosted
85+
version: "1.2.1"
86+
http_parser:
87+
dependency: transitive
88+
description:
89+
name: http_parser
90+
sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b"
91+
url: "https://pub.dev"
92+
source: hosted
93+
version: "4.0.2"
7894
june:
7995
dependency: "direct overridden"
8096
description:
@@ -199,6 +215,14 @@ packages:
199215
url: "https://pub.dev"
200216
source: hosted
201217
version: "0.6.1"
218+
typed_data:
219+
dependency: transitive
220+
description:
221+
name: typed_data
222+
sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c
223+
url: "https://pub.dev"
224+
source: hosted
225+
version: "1.3.2"
202226
vector_math:
203227
dependency: transitive
204228
description:
@@ -215,5 +239,13 @@ packages:
215239
url: "https://pub.dev"
216240
source: hosted
217241
version: "13.0.0"
242+
web:
243+
dependency: transitive
244+
description:
245+
name: web
246+
sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27"
247+
url: "https://pub.dev"
248+
source: hosted
249+
version: "0.5.1"
218250
sdks:
219-
dart: ">=3.2.6 <4.0.0"
251+
dart: ">=3.3.0 <4.0.0"

example/pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ environment:
2424
dependency_overrides:
2525
june:
2626
path: ../
27+
http: ^1.2.1
2728

2829

2930
dependencies:

lib/instance/src/extension_instance.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ extension Instant on JuneInterface {
7373
// }
7474

7575
S put<S>(
76-
S dependency, {
76+
S Function() dependencyBuilder, {
7777
String? tag,
7878
bool permanent = true,
7979
}) {
8080
_insert(
8181
isSingleton: true,
8282
name: tag,
8383
permanent: permanent,
84-
builder: (() => dependency));
84+
builder: (dependencyBuilder));
8585
return find<S>(tag: tag);
8686
}
8787

@@ -264,7 +264,7 @@ extension Instant on JuneInterface {
264264
}
265265

266266
S getState<S>(
267-
S dependency, {
267+
S Function() dependencyBuilder, {
268268
String? tag,
269269
bool permanent = true,
270270
}) {
@@ -273,7 +273,7 @@ extension Instant on JuneInterface {
273273
if (_singl.containsKey(key)) {
274274
return _singl[key]!.getDependency() as S;
275275
} else {
276-
return put(dependency, tag: tag, permanent: permanent);
276+
return put(dependencyBuilder, tag: tag, permanent: permanent);
277277
}
278278
}
279279

@@ -321,7 +321,7 @@ extension Instant on JuneInterface {
321321
final info = getInstanceDetails<P>(tag: tag);
322322
final permanent = (info.isPermanent ?? false);
323323
delete<P>(tag: tag, force: permanent);
324-
put(child, tag: tag, permanent: permanent);
324+
put(() => child, tag: tag, permanent: permanent);
325325
}
326326

327327
/// Replaces a parent instance with a new Instance<P> lazily from the

lib/state_manager/src/simple/state.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class JuneBuilder<T extends JuneState> extends StatelessWidget {
7070

7171
@override
7272
Widget build(BuildContext context) {
73-
June.getState(createInstance(), tag: tag, permanent: global);
73+
June.getState(() => createInstance(), tag: tag, permanent: global);
7474

7575
return Binder(
7676
init: init == null ? null : () => init!,
@@ -132,7 +132,7 @@ abstract class Bind<T> extends StatelessWidget {
132132
String? tag,
133133
bool permanent = false,
134134
}) {
135-
June.put<S>(dependency, tag: tag, permanent: permanent);
135+
June.put<S>(() => dependency, tag: tag, permanent: permanent);
136136
return _FactoryBind<S>(
137137
autoRemove: permanent,
138138
assignId: true,
@@ -199,7 +199,7 @@ abstract class Bind<T> extends StatelessWidget {
199199
final info = June.getInstanceDetails<P>(tag: tag);
200200
final permanent = (info.isPermanent ?? false);
201201
delete<P>(tag: tag, force: permanent);
202-
June.put(child, tag: tag, permanent: permanent);
202+
June.put(() => child, tag: tag, permanent: permanent);
203203
}
204204

205205
static void lazyReplace<P>(BuilderPattern<P> builder,
@@ -477,7 +477,7 @@ class BindElement<T> extends InheritedElement {
477477
if (widget.lazy) {
478478
June.lazyPut<T>(_controllerBuilder!, tag: widget.tag);
479479
} else {
480-
June.put<T>(_controllerBuilder!(), tag: widget.tag);
480+
June.put<T>(() => _controllerBuilder!(), tag: widget.tag);
481481
}
482482
}
483483
} else {

pubspec.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: june
22
description: "June is a lightweight and modern state management library that focuses on providing a pattern very similar to Flutter's native state management."
3-
version: 0.8.8
3+
version: 1.0.0
44
homepage: https://github.com/melodysdreamj/june
55

66

@@ -15,7 +15,6 @@ dependencies:
1515
dev_dependencies:
1616
flutter_test:
1717
sdk: flutter
18-
flutter_lints: ^2.0.0
1918

2019
# For information on the generic Dart part of this file, see the
2120
# following page: https://dart.dev/tools/pub/pubspec

0 commit comments

Comments
 (0)