Skip to content

Commit

Permalink
feat(vdom): remove support for null nodes returned from mapIterable()
Browse files Browse the repository at this point in the history
Useless feature, it is easy to generate next node instead of returning
null node.
  • Loading branch information
localvoid committed May 31, 2018
1 parent 5c75401 commit e3c88a5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 48 deletions.
36 changes: 5 additions & 31 deletions packages/ivi/src/vdom/__tests__/vnode_collections_iterator.spec.ts
@@ -1,9 +1,11 @@
import { mapIterable, VNodeFlags, fragment } from "ivi";
import { mapIterable, VNodeFlags, fragment, VNode } from "ivi";
import * as h from "ivi-html";

test(`null`, () => {
expect(mapIterable(function* () {
yield null;
if (0) {
yield h.div();
}
}())).toBeNull();
});

Expand Down Expand Up @@ -37,34 +39,6 @@ test(`two nodes`, () => {
expect(v2._r).toBeNull();
});

test(`first null`, () => {
const v1 = h.div().k(5);

const first = mapIterable(function* () {
yield null;
yield v1;
}());

expect(first).toBe(v1);
expect(v1._f & VNodeFlags.KeyedList).toBeTruthy();
expect(v1._l).toBe(v1);
expect(v1._r).toBeNull();
});

test(`second null`, () => {
const v1 = h.div().k(5);

const first = mapIterable(function* () {
yield v1;
yield null;
}());

expect(first).toBe(v1);
expect(v1._f & VNodeFlags.KeyedList).toBeTruthy();
expect(v1._l).toBe(v1);
expect(v1._r).toBeNull();
});

test(`raise an exception when VNode doesn't have an explicit key (first node)`, () => {
expect(() => {
mapIterable((function* () {
Expand All @@ -85,7 +59,7 @@ test(`raise an exception when VNode doesn't have an explicit key (second node)`,
test(`raise an exception when function returns children collection`, () => {
expect(() => {
mapIterable(function* () {
yield fragment(h.div().k(0), h.div().k(1));
yield fragment(h.div().k(0), h.div().k(1)) as VNode;
}());
}).toThrowError("singular");
});
30 changes: 14 additions & 16 deletions packages/ivi/src/vdom/vnode_collections.ts
Expand Up @@ -194,29 +194,27 @@ export function mapRange<T>(start: number, end: number, fn: (idx: number) => VNo
* @param iterable - Iterable iterator
* @returns Virtual DOM collection
*/
export function mapIterable<T>(iterable: IterableIterator<VNode<T> | null>): VNode<T> | null {
export function mapIterable<T>(iterable: IterableIterator<VNode<T>>): VNode<T> | null {
let first: VNode<any> | null = null;
let prev: VNode<any> | null = null;

for (const n of iterable) {
if (n !== null) {
/* istanbul ignore else */
if (DEBUG) {
if ((n._f & VNodeFlags.Key) === 0) {
throw new Error(`VNodes created with a mapIterable() function should have an explicit key`);
}
if (n._l !== n) {
throw new Error(`VNodes created with a mapIterable() function should be a singular nodes`);
}
/* istanbul ignore else */
if (DEBUG) {
if ((n._f & VNodeFlags.Key) === 0) {
throw new Error(`VNodes created with a mapIterable() function should have an explicit key`);
}
if (prev !== null) {
n._l = prev;
prev._r = n;
} else {
first = n;
if (n._l !== n) {
throw new Error(`VNodes created with a mapIterable() function should be a singular nodes`);
}
prev = n;
}
if (prev !== null) {
n._l = prev;
prev._r = n;
} else {
first = n;
}
prev = n;
}

if (first !== null) {
Expand Down
3 changes: 2 additions & 1 deletion packages/ivi/tsconfig.build.module.json
Expand Up @@ -3,6 +3,7 @@
"compilerOptions": {
"target": "es5",
"module": "es2015",
"outDir": "dist/module"
"outDir": "dist/module",
"downlevelIteration": true
}
}

0 comments on commit e3c88a5

Please sign in to comment.