Extract EngineService into separate service
This commit is contained in:
parent
04d61bfa02
commit
a92b864dfa
8 changed files with 86 additions and 67 deletions
|
@ -5,6 +5,7 @@ import 'package:rules_of_living/components/controls_component.dart';
|
|||
import 'package:rules_of_living/components/header_component.dart';
|
||||
import 'package:rules_of_living/components/simulation_component.dart';
|
||||
import 'package:rules_of_living/service/configuration_service.dart';
|
||||
import 'package:rules_of_living/service/control_service.dart';
|
||||
import 'package:rules_of_living/service/engine_service.dart';
|
||||
|
||||
@Component(
|
||||
|
@ -20,8 +21,16 @@ import 'package:rules_of_living/service/engine_service.dart';
|
|||
ControlsComponent,
|
||||
ConfigurationComponent
|
||||
],
|
||||
providers: [materialProviders, ClassProvider(EngineService), ClassProvider(ConfigurationService)],
|
||||
styleUrls: const ['package:angular_components/app_layout/layout.scss.css', 'app_component.css'],
|
||||
providers: [
|
||||
materialProviders,
|
||||
ClassProvider(EngineService),
|
||||
ClassProvider(ConfigurationService),
|
||||
ClassProvider(EngineService)
|
||||
],
|
||||
styleUrls: const [
|
||||
'package:angular_components/app_layout/layout.scss.css',
|
||||
'app_component.css'
|
||||
],
|
||||
)
|
||||
class AppComponent {
|
||||
var name = "World";
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import 'package:angular/angular.dart';
|
||||
import 'package:angular_components/angular_components.dart';
|
||||
import 'package:rules_of_living/service/engine_service.dart';
|
||||
import 'package:rules_of_living/service/control_service.dart';
|
||||
|
||||
@Component(
|
||||
selector: 'sim-controls',
|
||||
|
@ -15,27 +15,27 @@ import 'package:rules_of_living/service/engine_service.dart';
|
|||
styleUrls: const ["controls_component.css"],
|
||||
)
|
||||
class ControlsComponent {
|
||||
final EngineService engine;
|
||||
final ControlService ctrl;
|
||||
|
||||
ControlsComponent(this.engine);
|
||||
ControlsComponent(this.ctrl);
|
||||
|
||||
void onStartClicked() {
|
||||
engine.toggleRunning();
|
||||
ctrl.toggleRunning();
|
||||
}
|
||||
|
||||
void onStepClicked() {
|
||||
engine.step();
|
||||
ctrl.step();
|
||||
}
|
||||
|
||||
void onResetClicked() {
|
||||
engine.reset();
|
||||
ctrl.reset();
|
||||
}
|
||||
|
||||
void onRandomClicked() {
|
||||
engine.addRandomPattern();
|
||||
ctrl.addRandomPattern();
|
||||
}
|
||||
|
||||
void onClearClicked() {
|
||||
engine.clear();
|
||||
ctrl.clear();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import 'dart:html' as html;
|
||||
|
||||
import 'package:angular/angular.dart';
|
||||
import 'package:rules_of_living/service/engine_service.dart';
|
||||
import 'package:rules_of_living/service/control_service.dart';
|
||||
|
||||
@Component(
|
||||
selector: 'gol-simulation',
|
||||
|
@ -10,9 +10,9 @@ import 'package:rules_of_living/service/engine_service.dart';
|
|||
providers: [],
|
||||
)
|
||||
class SimulationComponent implements OnInit {
|
||||
final EngineService engineService;
|
||||
final ControlService ctrl;
|
||||
|
||||
SimulationComponent(this.engineService);
|
||||
SimulationComponent(this.ctrl);
|
||||
|
||||
@override
|
||||
void ngOnInit() {
|
||||
|
@ -29,6 +29,6 @@ class SimulationComponent implements OnInit {
|
|||
|
||||
the canvas did not load correctly :(
|
||||
''', canvas.width / 2 - 50, canvas.height / 2);
|
||||
engineService.canvas = canvas;
|
||||
ctrl.canvas = canvas;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ import 'dart:math';
|
|||
import 'package:rules_of_living/service/engine_service.dart';
|
||||
|
||||
class ConfigurationService {
|
||||
final EngineService engineService;
|
||||
final EngineService _es;
|
||||
|
||||
bool showGrid;
|
||||
|
||||
|
@ -17,10 +17,10 @@ class ConfigurationService {
|
|||
int get simSpeed => _simSpeed;
|
||||
void set simSpeed(int val) {
|
||||
_simSpeed = val;
|
||||
engineService.engine.stepsPerSecond = simSpeed;
|
||||
_es.engine.stepsPerSecond = simSpeed;
|
||||
}
|
||||
|
||||
ConfigurationService(this.engineService) {
|
||||
ConfigurationService(this._es) {
|
||||
showGrid = false;
|
||||
simSpeed = 5;
|
||||
}
|
||||
|
@ -30,8 +30,8 @@ class ConfigurationService {
|
|||
}
|
||||
|
||||
void setGridSize({int x, int y}) {
|
||||
x = x ?? engineService.engine.gridSize.x;
|
||||
y = y ?? engineService.engine.gridSize.y;
|
||||
engineService.engine.gridSize = Point(x, y);
|
||||
x = x ?? _es.engine.gridSize.x;
|
||||
y = y ?? _es.engine.gridSize.y;
|
||||
_es.engine.gridSize = Point(x, y);
|
||||
}
|
||||
}
|
||||
|
|
43
lib/service/control_service.dart
Normal file
43
lib/service/control_service.dart
Normal file
|
@ -0,0 +1,43 @@
|
|||
import 'dart:html' as html;
|
||||
|
||||
import 'package:rules_of_living/service/engine_service.dart';
|
||||
|
||||
class ControlService {
|
||||
EngineService _es;
|
||||
|
||||
ControlService(this._es);
|
||||
|
||||
void set canvas(html.CanvasElement canvas) => _es.engine.canvas = canvas;
|
||||
html.CanvasElement get canvas => _es.engine.canvas;
|
||||
|
||||
void run() {
|
||||
_es.engine.running = true;
|
||||
}
|
||||
|
||||
void stop() {
|
||||
_es.engine.running = false;
|
||||
}
|
||||
|
||||
void toggleRunning() {
|
||||
_es.engine.running = !_es.engine.running;
|
||||
}
|
||||
|
||||
void step() {
|
||||
_es.engine.step();
|
||||
}
|
||||
|
||||
void reset() {
|
||||
_es.engine.reset();
|
||||
}
|
||||
|
||||
void addRandomPattern() {
|
||||
_es.engine.running = false;
|
||||
_es.engine.addPattern();
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_es.engine.clear();
|
||||
}
|
||||
|
||||
bool get isRunning => _es.engine.running;
|
||||
}
|
|
@ -1,48 +1,15 @@
|
|||
import 'dart:html' as html;
|
||||
|
||||
import 'package:rules_of_living/src/Engine.dart';
|
||||
|
||||
class EngineService {
|
||||
Engine _engine;
|
||||
Engine _uncachedEngineAccess;
|
||||
|
||||
Engine get engine => _engine ?? getEngine(Engine());
|
||||
|
||||
Engine getEngine(Engine engine) {
|
||||
_engine = engine;
|
||||
return _engine;
|
||||
Engine get engine => _uncachedEngineAccess ?? _setCachedAndReturn(Engine());
|
||||
void set engine(Engine newEngine) {
|
||||
_uncachedEngineAccess = newEngine;
|
||||
}
|
||||
|
||||
void set canvas(html.CanvasElement canvas) => engine.canvas = canvas;
|
||||
html.CanvasElement get canvas => engine.canvas;
|
||||
|
||||
void run() {
|
||||
engine.running = true;
|
||||
Engine _setCachedAndReturn(Engine newEngine) {
|
||||
engine = newEngine;
|
||||
return newEngine;
|
||||
}
|
||||
|
||||
void stop() {
|
||||
engine.running = false;
|
||||
}
|
||||
|
||||
void toggleRunning() {
|
||||
engine.running = !engine.running;
|
||||
}
|
||||
|
||||
void step() {
|
||||
engine.step();
|
||||
}
|
||||
|
||||
void reset() {
|
||||
engine.reset();
|
||||
}
|
||||
|
||||
void addRandomPattern() {
|
||||
engine.running = false;
|
||||
engine.addPattern();
|
||||
}
|
||||
|
||||
void clear() {
|
||||
engine.clear();
|
||||
}
|
||||
|
||||
bool get isRunning => engine.running;
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import 'dart:math';
|
||||
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:rules_of_living/service/configuration_service.dart';
|
||||
import 'package:rules_of_living/service/engine_service.dart';
|
||||
import 'package:rules_of_living/src/Engine.dart';
|
||||
@TestOn('browser')
|
||||
import 'package:test/test.dart';
|
||||
import 'package:rules_of_living/service/configuration_service.dart';
|
||||
import 'package:rules_of_living/service/engine_service.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
class MockEngine extends Mock implements Engine {}
|
||||
|
||||
|
@ -16,7 +16,7 @@ void main() {
|
|||
setUp(() {
|
||||
me = MockEngine();
|
||||
engineService = EngineService();
|
||||
engineService.getEngine(me);
|
||||
engineService.engine = me;
|
||||
sut = ConfigurationService(engineService);
|
||||
});
|
||||
|
||||
|
|
|
@ -14,8 +14,8 @@ void main() {
|
|||
sut = EngineService();
|
||||
});
|
||||
group("Dependency Injection", () {
|
||||
test("EngineService accesses the Engine defined in getEngine", () {
|
||||
sut.getEngine(me);
|
||||
test("EngineService can be passed a custom Engine", () {
|
||||
sut.engine = me;
|
||||
|
||||
Engine result = sut.engine;
|
||||
expect(result, equals(me));
|
||||
|
@ -33,7 +33,7 @@ void main() {
|
|||
});
|
||||
test("caching can be overriden by providing a custom engine", () {
|
||||
Engine first = sut.engine;
|
||||
sut.getEngine(me);
|
||||
sut.engine = me;
|
||||
Engine second = sut.engine;
|
||||
expect(second, isNot(equals(first)));
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue