Skip to content

Commit

Permalink
fix(hdom-canvas): update __normalize handling, rename fns
Browse files Browse the repository at this point in the history
- update canvas() component fn
- rename drawTree() => createTree()
- update releaseTree() call sites
- remove warning from hydrateTree() (just do nothing)
  • Loading branch information
postspectacular committed Sep 16, 2018
1 parent 382c45c commit a52f83c
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions packages/hdom-canvas/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { isArrayLike } from "@thi.ng/checks/is-arraylike";
import { isFunction } from "@thi.ng/checks/is-function";
import { isNotStringAndIterable } from "@thi.ng/checks/is-not-string-iterable";
import { isString } from "@thi.ng/checks/is-string";
import { HDOMImplementation, HDOMOpts } from "@thi.ng/hdom/api";
import { diffArray } from "@thi.ng/diff/array";
import { releaseDeep, equiv } from "@thi.ng/hdom/diff";
import { HDOMImplementation, HDOMOpts } from "@thi.ng/hdom/api";
import { equiv, releaseTree } from "@thi.ng/hdom/diff";
import { ReadonlyVec } from "@thi.ng/vectors/api";
import { TAU } from "@thi.ng/vectors/math";

Expand Down Expand Up @@ -105,6 +105,7 @@ const CTX_ATTRIBS = {
export const canvas = (_, attribs, ...body: any[]) => {
const cattribs = { ...attribs };
delete cattribs.__diff;
delete cattribs.__normalize;
const dpr = window.devicePixelRatio || 1;
if (dpr !== 1) {
!cattribs.style && (cattribs.style = {});
Expand All @@ -117,13 +118,14 @@ export const canvas = (_, attribs, ...body: any[]) => {
["g", {
__impl: IMPL,
__diff: attribs.__diff !== false,
__normalize: attribs.__normalize !== false,
__release: attribs.__release === true,
__clear: attribs.__clear,
scale: dpr !== 1 ? dpr : null,
}, ...body]]
};

export const drawTree = (_: Partial<HDOMOpts>, canvas: HTMLCanvasElement, tree: any) => {
export const createTree = (_: Partial<HDOMOpts>, canvas: HTMLCanvasElement, tree: any) => {
// console.log(Date.now(), "draw");
const ctx = canvas.getContext("2d");
const attribs = tree[1];
Expand Down Expand Up @@ -176,24 +178,24 @@ export const diffTree = (opts: Partial<HDOMOpts>,
child: number) => {
const attribs = curr[1];
if (attribs.__diff === false) {
releaseDeep(prev);
return drawTree(opts, parent, curr);
releaseTree(prev);
return createTree(opts, parent, curr);
}
// delegate to branch-local implementation
if (attribs.__impl && attribs.__impl !== IMPL) {
return attribs.__impl.diffTree(opts, attribs.__impl, parent, prev, curr, child);
}
const delta = diffArray(prev, curr, equiv, true);
if (delta.distance > 0) {
return drawTree(opts, parent, curr);
return createTree(opts, parent, curr);
}
}

export const IMPL: HDOMImplementation<any> = {
createTree: drawTree,
createTree,
normalizeTree,
diffTree,
hydrateTree: () => console.warn("hydrate not-supported for hdom-canvas"),
hydrateTree: () => { },
};

const walk = (ctx: CanvasRenderingContext2D, shape: any[], pstate: DrawState) => {
Expand Down

0 comments on commit a52f83c

Please sign in to comment.