Finish events page

This commit is contained in:
Lea Anthony 2020-10-20 06:45:46 +11:00
commit 307e07b4c8
No known key found for this signature in database
GPG key ID: 33DAF7BB90A58405
8 changed files with 169 additions and 9 deletions

View file

@ -40,6 +40,19 @@ func (e *Events) Once(eventName string) {
})
}
// OnMultiple will subscribe to the given event name, with a maximum
// set by maxCallbacks
func (e *Events) OnMultiple(eventName string, maxCallbacks int) {
e.runtime.Events.OnMultiple(eventName, func(args ...interface{}) {
type callbackData struct {
Name string
Data []interface{}
}
result := callbackData{Name: eventName, Data: args}
e.runtime.Events.Emit("onmultiple event fired by go subscriber", result)
}, maxCallbacks)
}
// Emit will emit
func (e *Events) Emit(eventName string, data []interface{}) {
e.runtime.Events.Emit(eventName, data...)

View file

@ -2,6 +2,7 @@
import On from './On/On.svelte';
import Emit from './Emit/Emit.svelte';
import Once from './Once/Once.svelte';
import OnMultiple from './OnMultiple/OnMultiple.svelte';
</script>
<div>
@ -22,6 +23,8 @@
<br/>
<Once></Once>
<br/>
<OnMultiple></OnMultiple>
<br/>
<Emit></Emit>
</div>

View file

@ -0,0 +1,120 @@
<script>
import { Events } from '@wails/runtime';
import { writable } from 'svelte/store';
import CodeBlock from '../../../components/CodeBlock.svelte';
import CodeSnippet from '../../../components/CodeSnippet.svelte';
import description from './description.txt';
import { UniqueID } from '../../../utils/utils';
import FakeTerm from '../../../components/FakeTerm.svelte';
import jsCode from './code.jsx';
import goCode from './code.go';
import { loggingOutput } from '../On/store';
let isJs = false;
$: lang = isJs ? 'Javascript' : 'Go';
// Listeners
let listeners = writable([]);
let counts = {};
let id = UniqueID('events');
let eventName = "";
let maxCallbacks = 1;
function removeSubscriber(eventName, source) {
listeners.update( (current) => {
let name = '"' + eventName + '" (' + source + ')';
const index = current.indexOf(name);
if (index > -1) {
current.splice(index, 1);
}
return current;
});
}
function updateLog(eventName, data, source) {
let name = '"' + eventName + '" (' + source + ")";
console.log(counts);
counts[name].current = counts[name].current + 1;
console.log(counts);
let countText = counts[name].current + "/" + counts[name].max;
if( counts[name].current === counts[name].max) {
removeSubscriber(name);
}
loggingOutput.update( (log) => {
let datatext = (data ? JSON.stringify(data) : "(No data given)");
let destroyText = "";
if( counts[name].current === counts[name].max) {
destroyText = " (Listener Now Destroyed) ";
}
return log + "[" + eventName + " (" + source + ") " + countText + "] data: " + datatext + destroyText + "\n";
});
}
// Subscribe to the Go event calls
Events.On("onmultiple event fired by go subscriber", (input) => {
// Format the data for printing
updateLog(input.Name, input.Data, "Go");
});
function subscribe() {
if (eventName.length == 0) {
return
}
let name = '"' + eventName + '" (' + (isJs ? 'JS' : 'Go') + ")"
if( $listeners.includes(name) ) {
return
}
// Add eventName to listeners list
listeners.update( (current) => {
return current.concat(name);
});
counts[name] = { max: maxCallbacks, current: 0 }
if( isJs ) {
Events.OnMultiple(eventName, (...data) => {
updateLog(eventName, data, "JS");
}, maxCallbacks);
} else {
// We call a function in Go to register a subscriber
// for us
backend.main.Events.OnMultiple(eventName, maxCallbacks);
}
}
$: testcodeJs = "import { Events } from '@wails/runtime';\nEvents.OnMultiple('" + eventName + "', callback, " + maxCallbacks + ");";
$: testcodeGo = '// runtime is given through WailsInit()\nruntime.Events.OnMultiple("' + eventName + '", func(optionalData ...interface{} {\n // Process data\n}), " + maxCallbacks + ")';
</script>
<CodeBlock bind:isJs={isJs} {jsCode} {goCode} {id} title="Events.OnMultiple(eventName, callback)" {description}>
<div class="logging-form">
<form data-wails-no-drag class="mw-full">
<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>
<div class="form-group">
<label for="{id}-maxTimes" class="required">Number of times the callback should fire</label>
<input type="number" class="form-control" id="{id}-maxTimes" placeholder="1" bind:value="{maxCallbacks}">
</div>
<input class="btn btn-primary" type="button" on:click="{subscribe}" value="Subscribe using {lang} runtime">
<CodeSnippet bind:isJs={isJs} jsCode={testcodeJs} goCode={testcodeGo}></CodeSnippet>
{#if $listeners.length > 0 }
<div class="form-group" style="margin-top:10px">
Subscribed to:
<ul class="list">
{#each $listeners as listener}
<li>{listener}</li>
{/each}
</div>
Now use <code>Events.Emit</code> to trigger the subscribers!<br/>
<div style="margin-top: 10px">Subscriber output will be printed below:</div>
<FakeTerm text={$loggingOutput} style="height: 300px; overflow: scroll"></FakeTerm>
{/if}
</form>
</div>
</CodeBlock>

View file

@ -0,0 +1,18 @@
package main
import wails "github.com/wailsapp/wails/v2"
type MyStruct struct {
runtime *wails.Runtime
}
func (m *MyStruct) WailsInit(runtime *wails.Runtime) error {
maxAttempts := 3
runtime.Events.OnMultiple("unlock attempts", func(optionalData ...interface{}) {
// Do something (at most) maxAttempts times
}, maxAttempts)
m.runtime = runtime
return nil
}

View file

@ -0,0 +1,8 @@
import { Events } from '@wails/runtime';
let notes = [];
// Do some things
Events.On("notes loaded", (newNotes) => {
notes = newNotes;
});

View file

@ -0,0 +1,3 @@
<code>Events.OnMultiple()</code> is used to subscribe to events. It takes a number which is the
total number of times that the callback will be fired when an event matching the given event
name is received.

View file

@ -8,10 +8,8 @@ type MyStruct struct {
func (m *MyStruct) WailsInit(runtime *wails.Runtime) error {
runtime.Events.On("notes updated", func(optionalData ...interface{}) {
// Get notes
notes := optionalData[0].(*Notes)
// Save the notes to disk
runtime.Events.Once("initialised", func(optionalData ...interface{}) {
// Do something once
})
m.runtime = runtime

View file

@ -1,8 +1,5 @@
import { Events } from '@wails/runtime';
let notes = [];
// Do some things
Events.On("notes loaded", (newNotes) => {
notes = newNotes;
Events.Once("initialised", () => {
// Do something once
});