Skip to content

Commit 0d8f7b3

Browse files
committed
Use abstract classes.
1 parent 992299b commit 0d8f7b3

File tree

5 files changed

+39
-37
lines changed

5 files changed

+39
-37
lines changed

.haxerc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"version": "4.1.3",
2+
"version": "4.2.1",
33
"resolveLibs": "scoped"
44
}

src/coconut/diffing/Cursor.hx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
package coconut.diffing;
22

3-
interface Cursor<Native> {
3+
abstract class Cursor<Native> {
44
/**
55
A reference back to the applicator that created this cursor.
66
**/
7-
final applicator:Applicator<Native>;
7+
public final applicator:Applicator<Native>;
8+
public function new(applicator)
9+
this.applicator = applicator;
810
/**
911
Inserts a native node at the current cursor position.
1012
1113
Please note that:
1214
13-
1. The native element may already be a child of the parent node being iterated over
14-
2. The native element may even be at the current cursor position.
15+
1. The native node may already be a child of the parent node being iterated over
16+
2. The native node may even be at the current cursor position.
1517
**/
16-
function insert(native:Native):Void;
18+
public abstract function insert(native:Native):Void;
1719
/**
18-
Delete `count` elements from the current position.
20+
Delete `count` nodes from the current position.
1921
**/
20-
function delete(count:Int):Void;
21-
// TODO: document
22-
function current():Null<Native>;
22+
public abstract function delete(count:Int):Void;
23+
/**
24+
Returns the current node. Only used for hydration (which is only truly relevant for coconut.vdom)
25+
**/
26+
public function current():Null<Native>
27+
return null;
2328
}

src/coconut/diffing/Factory.hx

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@ package coconut.diffing;
22

33
import coconut.ui.Ref;
44

5-
@:using(coconut.diffing.Factory.FactoryTools)
6-
interface Factory<Data, Native, Target:Native> {
7-
final type:TypeId;
8-
function create(data:Data):Target;
9-
function adopt(target:Native):Null<Target>;
10-
function hydrate(target:Target, data:Data):Void;
11-
function update(target:Target, next:Data, prev:Data):Void;
12-
}
5+
abstract class Factory<Data, Native, Target:Native> {
6+
public final type = new TypeId();
7+
8+
public abstract function create(data:Data):Target;
9+
public abstract function update(target:Target, next:Data, prev:Data):Void;
10+
11+
/**
12+
Only used in hydration (by coconut.vdom). The currently encountered native node is passed to `adopt`.
13+
Return `null` if the wrong type of node is encountered.
14+
**/
15+
public function adopt(target:Native):Null<Target> return null;
16+
/**
17+
The actual implementation of the hydration (only used by coconut.vdom)
18+
**/
19+
public function hydrate(target:Target, data:Data):Void {}
1320

14-
class FactoryTools {
15-
static public function vnode<Data, Native, Concrete:Native, RenderResult:VNode<Native>>(f:Factory<Data, Native, Concrete>, data:Data, ?key:Key, ?ref:Ref<Concrete>, ?children:Children<RenderResult>):VNode<Native>
16-
return new VNative<Data, Native, Concrete>(f, data, key, ref, children);
21+
public function vnode<RenderResult:VNode<Native>>(data:Data, ?key:Key, ?ref:Ref<Target>, ?children:Children<RenderResult>):VNode<Native>
22+
return new VNative<Data, Native, Target>(this, data, key, ref, children);
1723
}
1824

1925
private typedef Dict<T> = Null<haxe.DynamicAccess<Null<T>>>;
2026

21-
class Properties<Value, Native:{}, Target:Native> implements Factory<Dict<Value>, Native, Target> {
22-
23-
public final type = new TypeId();
27+
class Properties<Value, Native:{}, Target:Native> extends Factory<Dict<Value>, Native, Target> {
2428

2529
final construct:()->Target;
2630
final apply:(target:Target, name:String, nu:Null<Value>, old:Null<Value>)->Void;
@@ -30,11 +34,6 @@ class Properties<Value, Native:{}, Target:Native> implements Factory<Dict<Value>
3034
this.apply = apply;
3135
}
3236

33-
public function adopt(target)
34-
return null;
35-
36-
public function hydrate(target, data) {}
37-
3837
public function create(data:Dict<Value>):Target {
3938
var ret = construct();
4039
update(ret, data, null);

tests/coconut/fake/DummyApplicator.hx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package coconut.fake;
22

3-
class DummyCursor implements Cursor<Dummy> {
3+
class DummyCursor extends Cursor<Dummy> {
44

5-
public final applicator:Applicator<Dummy>;
65
static var idCounter = 0;
76
var index:Int;
87
final target:Dummy;
98

109
public function new(applicator, target, index) {
11-
this.applicator = applicator;
10+
super(applicator);
1211
this.target = target;
1312
this.index = index;
1413
}
@@ -17,7 +16,7 @@ class DummyCursor implements Cursor<Dummy> {
1716
target.insert(index++, native);
1817
}
1918

20-
public function current()
19+
override public function current()
2120
return target.getChild(index);
2221

2322
public function delete(count:Int) {

tests/coconut/fake/DummyFactory.hx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package coconut.fake;
22

3-
class DummyFactory implements Factory<Attr, Dummy, Dummy> {
3+
class DummyFactory extends Factory<Attr, Dummy, Dummy> {
44

55
public final tag:String;
6-
public final type:TypeId = new TypeId();
76

87
public function new(tag) {
98
this.tag = tag;
@@ -15,10 +14,10 @@ class DummyFactory implements Factory<Attr, Dummy, Dummy> {
1514
return ret;
1615
}
1716

18-
public function adopt(dummy)
17+
override public function adopt(dummy)
1918
return dummy;
2019

21-
public function hydrate(dummy:Dummy, _) {
20+
override public function hydrate(dummy:Dummy, _) {
2221
@:privateAccess dummy.wet = true;
2322
}
2423

0 commit comments

Comments
 (0)