108 lines
3.0 KiB
HTML
108 lines
3.0 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Document</title>
|
|
<script src="dist/copy.js"></script>
|
|
<script src="dist/select.js"></script>
|
|
<script src="dist/form.js"></script>
|
|
<script src="dist/popup.js"></script>
|
|
<script src="dist/switch.js"></script>
|
|
<script src="dist/checkbox.js"></script>
|
|
<script src="dist/radio.js"></script>
|
|
<script src="dist/input.js"></script>
|
|
<script src="dist/textarea.js"></script>
|
|
<script src="dist/text-field.js"></script>
|
|
<script src="dist/button.js"></script>
|
|
<script src="dist/menu.js"></script>
|
|
<script src="dist/cube-loader.js"></script>
|
|
<script src="dist/tags-input.js"></script>
|
|
<script src="dist/strip-select.js"></script>
|
|
<script src="dist/collapsed-text.js"></script>
|
|
<script src="dist/notifications.js"></script>
|
|
<script src="dist/syntax-highlighter.js"></script>
|
|
<link rel="stylesheet" href="css/main.min.css">
|
|
<style>
|
|
div {
|
|
display: flex;
|
|
padding: 4vmax;
|
|
}
|
|
|
|
body {
|
|
overflow: auto;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<sm-copy value="hi dhf jsbdjf sjdb"></sm-copy>
|
|
<syntax-highlighter language="html">
|
|
<sm-copy value="hi dhf jsbdjf sjdb">
|
|
<sm-button>
|
|
hi
|
|
</sm-button>
|
|
</sm-copy>
|
|
</syntax-highlighter>
|
|
</body>
|
|
<script>
|
|
let currentSubscriber = null;
|
|
/**
|
|
* @param {any} initialValue - initial value for the signal
|
|
* @returns {array} - array containing getter and setter for the signal
|
|
* @example
|
|
* const [getCount, setCount] = $signal(0);
|
|
*/
|
|
function $signal(initialValue) {
|
|
let value = initialValue;
|
|
const subscribers = new Set();
|
|
|
|
function getter() {
|
|
if (currentSubscriber) {
|
|
const weakRef = new WeakRef({ func: currentSubscriber });
|
|
subscribers.add(weakRef);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function setter(newValue) {
|
|
if (newValue !== value) {
|
|
value = newValue;
|
|
for (const subscriber of subscribers) {
|
|
const ref = subscriber.deref();
|
|
if (ref) {
|
|
ref.func();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return [getter, setter];
|
|
}
|
|
/**
|
|
*
|
|
* @param {function} fn - function that will run if any of its dependent signals change
|
|
* @example
|
|
* $effect(() => {
|
|
* console.log(count());
|
|
* }
|
|
* @returns {void}
|
|
*/
|
|
async function $effect(fn) {
|
|
currentSubscriber = fn;
|
|
const result = fn();
|
|
try {
|
|
if (result instanceof Promise) {
|
|
await result;
|
|
}
|
|
} catch (e) {
|
|
console.error(e)
|
|
} finally {
|
|
currentSubscriber = null;
|
|
}
|
|
}
|
|
</script>
|
|
|
|
</html> |