Add cancellation examples

This commit is contained in:
Fabio Massaioli 2025-02-25 03:33:24 +01:00
commit 0dfc2b2a53
12 changed files with 440 additions and 0 deletions

View file

@ -0,0 +1,19 @@
package main
import (
"context"
"time"
)
type Service struct {
}
// A long running operation of specified duration.
func (*Service) LongRunning(ctx context.Context, milliseconds int) error {
select {
case <-time.After(time.Duration(milliseconds) * time.Millisecond):
return nil
case <-ctx.Done():
return ctx.Err()
}
}

View file

@ -0,0 +1,5 @@
# Binding Call Cancelling Example
This example demonstrates how to cancel binding calls in async/await style.
To regenerate bindings, run `wails3 generate bindings -clean -b -d assets/bindings` in this directory.

View file

@ -0,0 +1,8 @@
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import * as Service from "./service.js";
export {
Service
};

View file

@ -0,0 +1,16 @@
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Unused imports
import {Call as $Call, CancellablePromise as $CancellablePromise, Create as $Create} from "/wails/runtime.js";
/**
* A long running operation of specified duration.
* @param {number} milliseconds
* @returns {$CancellablePromise<void>}
*/
export function LongRunning(milliseconds) {
return $Call.ByID(298191698, milliseconds);
}

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,37 @@
package main
import (
"embed"
"log"
"github.com/wailsapp/wails/v3/pkg/application"
)
//go:embed assets/*
var assets embed.FS
func main() {
app := application.New(application.Options{
Services: []application.Service{
application.NewService(&Service{}),
},
Assets: application.AssetOptions{
Handler: application.BundledAssetFileServer(assets),
},
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
},
})
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
URL: "/",
DevToolsEnabled: true,
})
err := app.Run()
if err != nil {
log.Fatal(err)
}
}

View file

@ -0,0 +1,19 @@
package main
import (
"context"
"time"
)
type Service struct {
}
// A long running operation of specified duration.
func (*Service) LongRunning(ctx context.Context, milliseconds int) error {
select {
case <-time.After(time.Duration(milliseconds) * time.Millisecond):
return nil
case <-ctx.Done():
return ctx.Err()
}
}

View file

@ -0,0 +1,5 @@
# Binding Call Cancelling Example
This example demonstrates how to cancel binding calls in promise chaining style.
To regenerate bindings, run `wails3 generate bindings -clean -b -d assets/bindings` in this directory.

View file

@ -0,0 +1,8 @@
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import * as Service from "./service.js";
export {
Service
};

View file

@ -0,0 +1,16 @@
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Unused imports
import {Call as $Call, CancellablePromise as $CancellablePromise, Create as $Create} from "/wails/runtime.js";
/**
* A long running operation of specified duration.
* @param {number} milliseconds
* @returns {$CancellablePromise<void>}
*/
export function LongRunning(milliseconds) {
return $Call.ByID(298191698, milliseconds);
}

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,37 @@
package main
import (
"embed"
"log"
"github.com/wailsapp/wails/v3/pkg/application"
)
//go:embed assets/*
var assets embed.FS
func main() {
app := application.New(application.Options{
Services: []application.Service{
application.NewService(&Service{}),
},
Assets: application.AssetOptions{
Handler: application.BundledAssetFileServer(assets),
},
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
},
})
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
URL: "/",
DevToolsEnabled: true,
})
err := app.Run()
if err != nil {
log.Fatal(err)
}
}