From 977f61f1eb1c481b19ed2f45404172d6e6100698 Mon Sep 17 00:00:00 2001 From: Richard Harris Date: Sat, 2 Mar 2019 12:23:33 -0500 Subject: [PATCH] use context for handling changes --- .../components/Repl/Input/ModuleEditor.svelte | 4 +- site/src/components/Repl/Input/index.svelte | 1 - site/src/components/Repl/index.svelte | 41 +++++++++---------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/site/src/components/Repl/Input/ModuleEditor.svelte b/site/src/components/Repl/Input/ModuleEditor.svelte index 596fafcd41..13a741732c 100644 --- a/site/src/components/Repl/Input/ModuleEditor.svelte +++ b/site/src/components/Repl/Input/ModuleEditor.svelte @@ -2,7 +2,7 @@ import { getContext } from 'svelte'; import CodeMirror from '../CodeMirror.svelte'; - const { selected } = getContext('REPL'); + const { selected, handle_change } = getContext('REPL'); export let error; export let errorLoc; @@ -33,7 +33,7 @@ {error} {errorLoc} {warningCount} - on:change + on:change={handle_change} /> {/if} \ No newline at end of file diff --git a/site/src/components/Repl/Input/index.svelte b/site/src/components/Repl/Input/index.svelte index f9aa5905f0..a8a5b9e374 100644 --- a/site/src/components/Repl/Input/index.svelte +++ b/site/src/components/Repl/Input/index.svelte @@ -13,5 +13,4 @@ {error} {errorLoc} {warningCount} - on:change /> \ No newline at end of file diff --git a/site/src/components/Repl/index.svelte b/site/src/components/Repl/index.svelte index de4b647d96..d26e2e542d 100644 --- a/site/src/components/Repl/index.svelte +++ b/site/src/components/Repl/index.svelte @@ -44,6 +44,26 @@ // if (selected.name === name) return; // selected = components.find(c => c.name === name); + }, + + handle_change: event => { + selected.update(component => { + // TODO this is a bit hacky — we're relying on mutability + // so that updating components works... might be better + // if a) components had unique IDs, b) we tracked selected + // *index* rather than component, and c) `selected` was + // derived from `components` and `index` + component.source = event.detail.value; + return component; + }); + + components.update(c => c); + + // recompile selected component + compile($selected, compile_options); + + // regenerate bundle (TODO do this in a separate worker?) + workers.bundler.postMessage({ type: 'bundle', components: $components }); } }); @@ -123,26 +143,6 @@ } } - function handleChange(event) { - selected.update(component => { - // TODO this is a bit hacky — we're relying on mutability - // so that updating components works... might be better - // if a) components had unique IDs, b) we tracked selected - // *index* rather than component, and c) `selected` was - // derived from `components` and `index` - component.source = event.detail.value; - return component; - }); - - components.update(c => c); - - // recompile selected component - compile($selected, compile_options); - - // regenerate bundle (TODO do this in a separate worker?) - workers.bundler.postMessage({ type: 'bundle', components: $components }); - } - $: if (sourceError && $selected) { sourceErrorLoc = sourceError.filename === `${$selected.name}.${$selected.type}` ? sourceError.start @@ -275,7 +275,6 @@ error={sourceError} errorLoc="{sourceErrorLoc || runtimeErrorLoc}" {warningCount} - on:change="{handleChange}" />