Skip to content

Commit

Permalink
fix(transducers-hdom): support hdom user context
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 2, 2018
1 parent feae35b commit 949a5d4
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions packages/transducers-hdom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,32 @@ import { scan } from "@thi.ng/transducers/xform/scan";
/**
* Side-effecting & stateful transducer which receives @thi.ng/hdom
* component trees, diffs each against previous value and applies
* required changes to browser DOM starting at given root element. By
* default, incoming values are first normalized using hdom's
* `normalizeTree()` function.
* required changes to browser DOM starting at given root element.
*
* By default, incoming values are first normalized using hdom's
* `normalizeTree()` function and the given (optional) `ctx` object is
* provided to all embedded component functions in the tree.
*
* This transducer is primarily intended for @thi.ng/rstream dataflow
* graph based applications, where this transducer can be used as final
* leaf subscription to reactively reflect UI changes back to the user,
* without using the standard RAF update loop used by hdom by default.
* In this setup, UI updates will only be performed if the stream this
* transducer is attached too receives new values (i.e. hdom component
* In this setup, UI updates will only be performed when the stream this
* transducer is attached to emits new values (i.e. hdom component
* trees).
*
* @param el
* @param root root element (or ID)
* @param ctx hdom user context
* @param normalize
*/
export const updateUI = (el: string | Element, normalize = true): Transducer<any, any[]> => {
const root = isString(el) ? document.getElementById(el) : el;
export const updateUI = (root: string | Element, ctx?: any, normalize = true): Transducer<any, any[]> => {
root = isString(root) ? document.getElementById(root) : root;
return scan<any, any[]>(
reducer(
() => [],
(prev, curr) => {
normalize && (curr = normalizeTree(curr, {}));
diffElement(root, prev, curr);
normalize && (curr = normalizeTree(curr, ctx));
diffElement(<Element>root, prev, curr);
return curr;
}
)
Expand Down

0 comments on commit 949a5d4

Please sign in to comment.