Skip to content

Commit a9757a4

Browse files
authored
Merge pull request #20 from MVCoconut/hydrate
Hydrate
2 parents 5d90ecb + 043d085 commit a9757a4

File tree

23 files changed

+224
-90
lines changed

23 files changed

+224
-90
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ jobs:
1515
haxe-version:
1616
- stable
1717
- nightly
18-
target:
18+
target:
1919
- interp
2020
- neko
2121
- node
2222
- js
23+
- js -lib coconut.vdom
2324
- cpp
2425
- php
2526
- jvm
@@ -29,7 +30,7 @@ jobs:
2930
steps:
3031
- name: Check out repo
3132
uses: actions/checkout@v2
32-
33+
3334
- name: Get yarn cache directory path
3435
id: yarn-cache-dir-path
3536
run: echo "::set-output name=dir::$(yarn cache dir)"
@@ -41,34 +42,34 @@ jobs:
4142
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
4243
restore-keys: |
4344
${{ runner.os }}-yarn-
44-
45+
4546
- name: Cache Haxe
4647
uses: actions/cache@v1
4748
with:
4849
path: ${{ startsWith(runner.os, 'windows') && '%AppData%' || '~/haxe' }}
4950
key: ${{ runner.os }}-haxe
50-
51+
5152
- name: Install Lix
5253
uses: lix-pm/setup-lix@master
53-
54+
5455
- name: Install Haxe
5556
run: lix install haxe ${{ matrix.haxe-version }}
56-
57+
5758
- name: Install Haxe Libraries
5859
run: lix download
59-
60+
6061
- name: Run Test
6162
run: lix run travix ${{ matrix.target }}
6263

6364
release:
6465
runs-on: ubuntu-latest
6566
needs: test
6667
if: startsWith(github.ref, 'refs/tags/') # consider using the "release" event. see: https://docs.github.com/en/actions/reference/events-that-trigger-workflows#release
67-
68+
6869
steps:
6970
- name: Check out repo
7071
uses: actions/checkout@v2
71-
72+
7273
- name: Get yarn cache directory path
7374
id: yarn-cache-dir-path
7475
run: echo "::set-output name=dir::$(yarn cache dir)"
@@ -80,24 +81,23 @@ jobs:
8081
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
8182
restore-keys: |
8283
${{ runner.os }}-yarn-
83-
84+
8485
- name: Cache Haxe
8586
uses: actions/cache@v1
8687
with:
8788
path: ${{ startsWith(runner.os, 'windows') && '%AppData%' || '~/haxe' }}
8889
key: ${{ runner.os }}-haxe
89-
90+
9091
- name: Install Lix
9192
uses: lix-pm/setup-lix@master
92-
93+
9394
- name: Install Haxe
9495
run: lix install haxe stable
95-
96+
9697
- name: Install Haxe Libraries
9798
run: lix download
98-
99+
99100
- name: Release to Haxelib
100101
run: lix run travix release
101102
env:
102103
HAXELIB_AUTH: ${{ secrets.HAXELIB_AUTH }}
103-

.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
}

dev.hxml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
-cp tests
2-
-lib tink_unittest
3-
-main TodoMvc
1+
tests.hxml
2+
-lib travix
43
-lib coconut.diffing
5-
-D no-deprecation-warnings
6-
coconut.diffing.Implicit
7-
--interp
8-
# -js bin/js/tests.js
9-
# -dce full
4+
--interp

haxe_libraries/coconut.vdom.hxml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# @install: lix --silent download "gh://github.com/MVCoconut/coconut.vdom#058fbb7b86580043bea3335ab0a58f863c143b72" into coconut.vdom/0.8.1/github/058fbb7b86580043bea3335ab0a58f863c143b72
1+
# @install: lix --silent download "gh://github.com/MVCoconut/coconut.vdom#c549f6902e337eca6fa9f8fc88729c44dea907b6" into coconut.vdom/0.9.0/github/c549f6902e337eca6fa9f8fc88729c44dea907b6
22
-lib coconut.diffing
33
-lib xDOM
4-
-cp ${HAXE_LIBCACHE}/coconut.vdom/0.8.1/github/058fbb7b86580043bea3335ab0a58f863c143b72/src
5-
-D coconut.vdom=0.8.1
4+
-cp ${HAXE_LIBCACHE}/coconut.vdom/0.9.0/github/c549f6902e337eca6fa9f8fc88729c44dea907b6/src
5+
-D coconut.vdom=0.9.0

src/coconut/diffing/Cursor.hx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +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;
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;
2128
}

src/coconut/diffing/Factory.hx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +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 update(target:Target, next:Data, prev:Data):Void;
10-
}
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;
1110

12-
class FactoryTools {
13-
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>
14-
return new VNative<Data, Native, Concrete>(f, data, key, ref, children);
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 {}
20+
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);
1523
}
1624

1725
private typedef Dict<T> = Null<haxe.DynamicAccess<Null<T>>>;
1826

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

2329
final construct:()->Target;
2430
final apply:(target:Target, name:String, nu:Null<Value>, old:Null<Value>)->Void;

src/coconut/diffing/Implicit.hx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class Implicit<Native, RenderResult:VNode<Native>> implements VNode<Native> {
1919
this.defaults = attr.defaults;
2020
}
2121

22-
public function render(parent:Parent, cursor:Cursor<Native>, later):RNode<Native>
23-
return new RImplicit(this, parent, cursor, later);
22+
public function render(parent:Parent, cursor:Cursor<Native>, later, hydrate:Bool):RNode<Native>
23+
return new RImplicit(this, parent, cursor, later, hydrate);
2424

2525
}
2626

@@ -29,10 +29,10 @@ private class RImplicit<Native> extends Parent implements RNode<Native> {
2929
public final type = Implicit.TYPE;
3030

3131
final children:VMany.RMany<Native>;
32-
public function new<RenderResult:VNode<Native>>(v:Implicit<Native, RenderResult>, parent:Parent, cursor, later) {
32+
public function new<RenderResult:VNode<Native>>(v:Implicit<Native, RenderResult>, parent:Parent, cursor, later, hydrate) {
3333
super(new ImplicitContext(parent.context), parent);
3434
this.context.update(v.defaults);
35-
this.children = new VMany.RMany(this, v.children, cursor, later);
35+
this.children = new VMany.RMany(this, v.children, cursor, later, hydrate);
3636
}
3737

3838
public function reiterate(applicator)

src/coconut/diffing/Root.hx

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

33
import coconut.ui.internal.ImplicitContext;
44

5+
enum abstract Hydration(Int) {
6+
var No;
7+
var Into;
8+
var Onto;
9+
}
10+
511
class Root<Native> extends Parent {
612
final rendered:RCell<Native>;
713

8-
public function new(parent, applicator:Applicator<Native>, ?content) {
14+
public function new(parent, applicator:Applicator<Native>, ?content, hydration = No) {
915
super(new ImplicitContext());
10-
var rendered = Parent.withLater(later -> new RCell(this, content, applicator.children(parent), later));
16+
var rendered = Parent.withLater(later -> new RCell(this, content, if (hydration == Onto) applicator.siblings(parent) else applicator.children(parent), later, hydration != No));
1117
this.rendered = rendered;
1218
}
1319

src/coconut/diffing/internal/RCell.hx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ class RCell<Native> {
77
var virtual:VNode<Native>;
88
var rendered:RNode<Native>;
99

10-
public function new(parent, virtual, cursor, later) {
10+
public function new(parent, virtual, cursor, later, hydrate:Bool) {
1111
this.parent = parent;
1212
if (virtual == null)
1313
virtual = empty;
1414
this.virtual = virtual;
15-
this.rendered = virtual.render(parent, cursor, later);
15+
this.rendered = virtual.render(parent, cursor, later, hydrate);
1616
this.applicator = cursor.applicator;
1717
}
1818

@@ -38,7 +38,7 @@ class RCell<Native> {
3838
this.rendered.update(virtual, cursor, later);
3939
else {
4040
var old = this.rendered;
41-
this.rendered = virtual.render(parent, cursor, later);
41+
this.rendered = virtual.render(parent, cursor, later, false);
4242
cursor.delete(old.destroy(applicator));
4343
}
4444
}

src/coconut/diffing/internal/RChildren.hx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class RChildren<Native> {
99

1010
public final parent:Parent;
1111

12-
public function new(parent:Parent, children:Children<VNode<Native>>, cursor:Cursor<Native>, later) {
12+
public function new(parent:Parent, children:Children<VNode<Native>>, cursor:Cursor<Native>, later, hydrate) {
1313
this.parent = parent;
1414
for (c in children) if (c != null) {
15-
var r = c.render(parent, cursor, later);
15+
var r = c.render(parent, cursor, later, hydrate);
1616
switch [c.key, byType[r.type]] {
1717
case [null, null]: byType[r.type] = [r];
1818
case [null, a]: a.push(r);
@@ -45,7 +45,7 @@ class RChildren<Native> {
4545
}
4646

4747
inline function insert(v:VNode<Native>)
48-
return byType[v.type][counts[v.type]++] = v.render(parent, cursor, later);
48+
return byType[v.type][counts[v.type]++] = v.render(parent, cursor, later, false);
4949

5050
var deleteCount = 0,
5151
applicator = cursor.applicator;
@@ -71,7 +71,7 @@ class RChildren<Native> {
7171
}
7272
case [k, _]:
7373
inline function insert(v:VNode<Native>)
74-
return setKey(k, v.render(parent, cursor, later));
74+
return setKey(k, v.render(parent, cursor, later, false));
7575
switch getKey(k) {
7676
case null:
7777
insert(v);

0 commit comments

Comments
 (0)