-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This MR brings reactivity out of the box via signals based libraries. You can bring your ogn signals based library or use the `uhtml/preactive` export which already bundles `@preact/signals-core` in it. The main difference with the *reactive* export, or its *preactive* one, is that the *render* function needs the "what to render" second argument to be a callback, otherwise signals don't get a chance to side effect.
- Loading branch information
1 parent
5ef2bb7
commit 1d43475
Showing
15 changed files
with
197 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/*! (c) Andrea Giammarchi - MIT */ | ||
import { unroll } from './rabbit.js'; | ||
import { Hole } from './rabbit.js'; | ||
import { attr } from './handler.js'; | ||
import { cache } from './literals.js'; | ||
import { empty, set } from './utils.js'; | ||
import { html, svg } from './index.js'; | ||
|
||
import reactive from './render/reactive.js'; | ||
|
||
/** @typedef {import("./literals.js").Cache} Cache */ | ||
/** @typedef {import("./literals.js").Target} Target */ | ||
/** @typedef {import("./literals.js").Value} Value */ | ||
|
||
/** @typedef {(ref:Object, key:string | number) => Tag} Bound */ | ||
|
||
/** | ||
* @callback Tag | ||
* @param {TemplateStringsArray} template | ||
* @param {...Value} values | ||
* @returns {Target} | ||
*/ | ||
|
||
const keyed = new WeakMap; | ||
const createRef = svg => /** @type {Bound} */ (ref, key) => { | ||
/** @type {Tag} */ | ||
function tag(template, ...values) { | ||
return unroll(this, new Hole(svg, template, values)); | ||
} | ||
|
||
const memo = keyed.get(ref) || set(keyed, ref, new Map); | ||
return memo.get(key) || set(memo, key, tag.bind(cache(empty))); | ||
}; | ||
|
||
/** @type {Bound} Returns a bound tag to render HTML content. */ | ||
const htmlFor = createRef(false); | ||
|
||
/** @type {Bound} Returns a bound tag to render SVG content. */ | ||
const svgFor = createRef(true); | ||
|
||
export { Hole, reactive, html, svg, htmlFor, svgFor, attr }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { effect } from '@preact/signals-core'; | ||
export * from '@preact/signals-core'; | ||
|
||
import { Hole, reactive, html, svg, htmlFor, svgFor, attr } from '../reactive.js'; | ||
|
||
const render = reactive(effect); | ||
|
||
export { Hole, render, html, svg, htmlFor, svgFor, attr }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { create, drop } from 'gc-hook'; | ||
|
||
import render from './keyed.js'; | ||
|
||
/** @type {WeakMap<Element | DocumentFragment, Function>} */ | ||
const effects = new WeakMap; | ||
|
||
/** | ||
* @param {Function} dispose | ||
* @returns {void} | ||
*/ | ||
const onGC = dispose => dispose(); | ||
|
||
export default effect => { | ||
/** | ||
* Render with smart updates within a generic container. | ||
* @template T | ||
* @param {T} where the DOM node where to render content | ||
* @param {() => Hole} what the hole to render | ||
* @returns {T} | ||
*/ | ||
return (where, what) => { | ||
let dispose = effects.get(where); | ||
if (dispose) { | ||
drop(dispose); | ||
dispose(); | ||
} | ||
const wr = new WeakRef(where); | ||
dispose = effect(() => { render(wr.deref(), what) }); | ||
effects.set(where, dispose); | ||
return create(dispose, onGC, { return: where }); | ||
}; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.