5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-09 18:31:29 +08:00

WIP Events.On

This commit is contained in:
Lea Anthony 2020-10-17 15:10:13 +11:00
parent c9bf4e3d48
commit fb0ccfc8e6
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
7 changed files with 101 additions and 8 deletions

View File

@ -29,7 +29,7 @@ export default {
// Embed text files
string({
include: ["**/*.jsx","**/*.go"],
include: ["**/*.jsx","**/*.go", "**/*.txt"],
}),
svelte({

View File

@ -26,7 +26,7 @@
showCode = !showCode;
}
export let id;
export let id = "toggle-" + Date.now().toString() + Math.random().toString();
// Handle hiding example
let showRun = true;
@ -47,7 +47,7 @@
</span>
</span>
{#if description}
<div class="description">{description}</div>
<div class="description">{@html description}</div>
{/if}
</div>
<div class="run">

View File

@ -1,12 +1,27 @@
<script>
import { afterUpdate } from 'svelte';
import { darkMode } from '../Store';
$: termClass = $darkMode ? 'faketerm-dark' : 'faketerm-light';
let termElement;
afterUpdate( () => {
termElement.scrollTop = termElement.scrollHeight;
});
export let text = "";
export let style = null;
</script>
<div class="common {termClass}">
<div bind:this={termElement} class="common {termClass}" {style}>
<pre>
{#if text && text.length > 0}
{text}
{:else}
<slot></slot>
{/if}
</pre>
</div>
@ -23,6 +38,7 @@
margin-top: 10px;
margin-bottom: 10px;
border: 1px solid #5555;
overflow: auto;
}
.faketerm-dark {

View File

@ -1,7 +1,5 @@
<script>
import CodeBlock from '../../components/CodeBlock.svelte';
import On from './On/On.svelte';
</script>
<div>
@ -20,7 +18,8 @@
<div style="padding: 15px"></div>
<CodeBlock title="Events.On(eventName, callback)" description="etc etc etc "></CodeBlock>
<On></On>
</div>
<style>

View File

@ -0,0 +1,73 @@
<script>
import { Events } from '@wails/runtime';
import { writable } from 'svelte/store';
import CodeBlock from '../../../components/CodeBlock.svelte';
import description from './description.txt';
import { UniqueID } from '../../../utils/utils';
import FakeTerm from '../../../components/FakeTerm.svelte';
let isJs = false;
let jsCode = "js";
let goCode = "go";
$: lang = isJs ? 'Javascript' : 'Go';
// Listeners
let listeners = writable([]);
let id = UniqueID('events');
let eventName = "";
let loggingOutput = writable("");
function subscribe() {
if (eventName.length == 0) {
return
}
// Add eventName to listeners list
listeners.update( (current) => {
// Don't add twice
if( current.includes(eventName) ) {
return current;
}
return current.concat(eventName);
});
if( isJs ) {
console.log("Adding listener for " + eventName);
Events.On(eventName, (data) => {
console.log("CALLED! " + eventName);
loggingOutput.update( (log) => {
let datatext = (data ? JSON.stringify(data) : "(No data given)");
return log + "[" + eventName + "] " + datatext + "\n";
});
})
} else {
console.log("go!");
}
}
</script>
<CodeBlock bind:isJs={isJs} {jsCode} {goCode} {id} title="Events.On(eventName, callback)" {description}>
<div class="logging-form">
<form data-wails-no-drag class="mw-full">
Subscribed to:
<div class="form-group">
<ul class="list">
{#each $listeners as listener}
<li>"{listener}"</li>
{/each}
</div>
<div class="form-group">
<label for="{id}-eventName" class="required">Event Name to subscribe to</label>
<input type="text" class="form-control" id="{id}-eventName" placeholder="MyEventName" bind:value="{eventName}" required="required">
</div>
<input class="btn btn-primary" type="button" on:click="{subscribe}" value="Subscribe using {lang} runtime">
<FakeTerm text={$loggingOutput} style="height: 300px; overflow: scroll"></FakeTerm>
</form>
</div>
</CodeBlock>

View File

@ -0,0 +1 @@
Events.On is used to subscribe to events. The given callback will be called whenever an event matching the given event name is received.

View File

@ -0,0 +1,4 @@
export function UniqueID(text) {
return text + "-" + Date.now().toString() + Math.random().toString();
}