Skip to content

Commit 155b761

Browse files
committed
Get editor working without bundling all of React
1 parent 40e09e0 commit 155b761

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

apps/components_guide_web/lib/components_guide_web/templates/react_typescript/editor.html.eex

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
<!-- See https://www.sanity.io/guides/server-side-rendering-deno-react -->
22

3+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/umd/react.profiling.min.js"></script>
4+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/umd/react-dom.profiling.min.js"></script>
5+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/umd/react-dom-server.browser.production.min.js"></script>
6+
37
<script src="https://unpkg.com/monaco-editor@latest/min/vs/loader.js"></script>
48

9+
10+
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js" integrity="sha512-c3Nl8+7g4LMSTdrm621y7kf9v3SDPnhxLNhcjFJbKECVnmZHTdo+IRO05sNLTH/D3vA6u1X32ehoLC7WFVdheg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>-->
11+
512
<script type="module">
13+
//import React, { useReducer, useCallback, useEffect, useState, useMemo } from "https://jspm.dev/[email protected]";
14+
//import ReactDOM from "https://jspm.dev/[email protected]/profiling";
15+
//import ReactDOMServer from "https://jspm.dev/[email protected]/server";
16+
17+
console.log("react", window.React);
18+
//import ReactDOM from "https://cdn.jsdelivr.net/npm/[email protected]/umd/react-dom.profiling.min.js/+esm";
19+
//import "https://cdn.jsdelivr.net/npm/[email protected]/umd/react-dom-server.browser.production.min.js";
20+
621
import * as esbuild from "https://cdn.jsdelivr.net/npm/[email protected]/esm/browser.min.js";
722
const esbuildPromise = Promise.resolve(esbuild.initialize({
823
wasmURL: 'https://cdn.jsdelivr.net/npm/[email protected]/esbuild.wasm',
@@ -249,7 +264,9 @@ require(["vs/editor/editor.main"], function () {
249264
bundle: true,
250265
minify: true,
251266
stdin: {
252-
contents: `${prefix}\n${body ?? ""}\n${suffix}`,
267+
//contents: `${prefix}\n${body ?? ""}\n${suffix}`,
268+
contents: `${body ?? ""}\n${suffix}`,
269+
//contents: body ?? "",
253270
loader: 'jsx',
254271
sourcefile: 'main.jsx',
255272
},
@@ -277,7 +294,11 @@ require(["vs/editor/editor.main"], function () {
277294
})
278295
.then(({ code, codeBytes, duration }) => {
279296
console.log("NEW CODE!");
280-
const executor = new Function(`${code}; return exports.Example();`);
297+
//window.useReducer = window.React.useReducer;
298+
//window.useMemo = window.React.useMemo;
299+
const hookNames = Object.keys(window.React).filter(name => name.startsWith('use'));
300+
const preamble = hookNames.map(hookName => `const ${hookName} = window.React.${hookName}`).join(';');
301+
const executor = new Function(`${preamble}; ${code}; return exports.Example();`);
281302
const result = executor();
282303
return new Map()
283304
.set('result', result)
@@ -326,7 +347,7 @@ require(["vs/editor/editor.main"], function () {
326347
}, 1000);
327348
}
328349

329-
reactRenderEl.textContent = `last ${detail.actualDuration}ms; ${renderCount} times rendered`;
350+
reactRenderEl.textContent = `last ${detail.actualDuration.toFixed(1)}ms; ${renderCount} times rendered`;
330351
});
331352
});
332353
</script>

apps/components_guide_web/lib/components_guide_web/templates/react_typescript/logical-clocks.html.md

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,35 @@ However, while React has certainly made the *what* easier with the declarative m
1010

1111
## What is a logical clock?
1212

13+
A logical clock is a monotonically increasing number. Or in other words, an incremented integer: `i++`.
14+
15+
We can implement once as a React hook using `useReducer()`:
16+
1317
```ts
14-
function useTick() {
18+
function useTicker() {
1519
return useReducer(n => n + 1, 0);
1620
}
1721
```
1822

23+
We can wire it up to a button like so:
24+
25+
```ts
26+
const [count, advance] = useTicker();
27+
28+
return <>
29+
<p>You have clicked {count} times</p>
30+
<button>Click me!</button>
31+
</>;
32+
```
33+
1934
## Debouncing
2035

2136
```ts
22-
export function useDebouncedTick(duration: number): readonly [number, EffectCallback] {
23-
const [count, tick] = useTick();
37+
export function useDebouncedTicker(duration: number): readonly [number, EffectCallback] {
38+
const [count, advance] = useTicker();
2439

2540
const callback = useMemo(() => {
26-
let timeout = null;
41+
let timeout: null | ReturnType<typeof setTimeout> = null;
2742
function clear() {
2843
if (timeout) {
2944
clearTimeout(timeout);
@@ -32,19 +47,25 @@ export function useDebouncedTick(duration: number): readonly [number, EffectCall
3247
}
3348
return () => {
3449
clear()
35-
timeout = setTimeout(tick, duration);
50+
timeout = setTimeout(advance, duration);
3651
return clear;
3752
};
38-
}, [duration, tick]);
53+
}, [duration, advance]);
3954

4055
return [count, callback];
4156
}
4257
```
4358

4459
```ts
4560
export function useDebouncedEffect(effect: EffectCallback, duration: number, deps: DependencyList): void {
46-
const [count, tick] = useDebouncedTick(duration);
47-
useEffect(tick, deps); // When our deps change, notify our debouncer.
61+
const [count, receive] = useDebouncedTicker(duration);
62+
useEffect(receive, deps); // When our deps change, notify our debouncer.
4863
useEffect(effect, [count]); // When our debouncer finishes, run our effect.
4964
}
65+
66+
export function useDebouncedMemo<T>(factory: () => T, duration: number, deps: DependencyList): T {
67+
const [tick, scheduleAdvance] = useDebouncedTicker(duration);
68+
useEffect(scheduleAdvance, deps); // When our deps change, notify our debouncer.
69+
return useMemo(factory, [tick]); // When our debouncer finishes, invalidate our memo.
70+
}
5071
```

0 commit comments

Comments
 (0)