Skip to content

Commit

Permalink
Radical JS runtime overhaul. New @wailsio/runtime package
Browse files Browse the repository at this point in the history
  • Loading branch information
leaanthony committed Dec 28, 2023
1 parent d1255d3 commit b08126d
Show file tree
Hide file tree
Showing 61 changed files with 1,809 additions and 4,726 deletions.
2 changes: 1 addition & 1 deletion v3/internal/runtime/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Runtime

To rebuild the runtime run `task build-runtime` or if you have Wails v3 CLI, you can use `wails task build-runtime`.
To rebuild the runtime run `task build-runtime` or if you have Wails v3 CLI, you can use `wails3 task build-runtime`.
72 changes: 4 additions & 68 deletions v3/internal/runtime/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,82 +21,18 @@ tasks:
build:debug:
internal: true
cmds:
- npx esbuild desktop/main.js --bundle --tree-shaking=true --sourcemap=inline --outfile=runtime_debug_desktop_{{.PLATFORM}}.js --define:DEBUG=true --define:WINDOWS={{.WINDOWS}} --define:DARWIN={{.DARWIN}} --define:LINUX={{.LINUX}} --define:PLATFORM={{.PLATFORM}} --define:INVOKE={{.INVOKE}}

build:debug:windows:
cmds:
- task: build:debug
vars:
WINDOWS: true
DARWIN: false
LINUX: false
PLATFORM: windows
INVOKE: "chrome.webview.postMessage"

build:debug:linux:
cmds:
- task: build:debug
vars:
WINDOWS: false
DARWIN: false
LINUX: true
PLATFORM: linux
INVOKE: "webkit.messageHandlers.external.postMessage"

build:debug:darwin:
cmds:
- task: build:debug
vars:
WINDOWS: false
DARWIN: true
LINUX: false
PLATFORM: darwin
INVOKE: "webkit.messageHandlers.external.postMessage"
- npx esbuild@latest desktop/main.js --bundle --tree-shaking=true --sourcemap=inline --outfile=runtime_debug.js --define:DEBUG=true

build:production:
internal: true
cmds:
- npx esbuild desktop/main.js --bundle --tree-shaking=true --minify --outfile=runtime_production_desktop_{{.PLATFORM}}.js --define:DEBUG=false --define:WINDOWS={{.WINDOWS}} --define:DARWIN={{.DARWIN}} --define:LINUX={{.LINUX}} --define:PLATFORM={{.PLATFORM}} --define:INVOKE={{.INVOKE}}

build:production:windows:
cmds:
- task: build:production
vars:
WINDOWS: true
DARWIN: false
LINUX: false
PLATFORM: windows
INVOKE: "chrome.webview.postMessage"

build:production:linux:
cmds:
- task: build:production
vars:
WINDOWS: false
DARWIN: false
LINUX: true
PLATFORM: linux
INVOKE: "webkit.messageHandlers.external.postMessage"

build:production:darwin:
cmds:
- task: build:production
vars:
WINDOWS: false
DARWIN: true
LINUX: false
PLATFORM: darwin
INVOKE: "webkit.messageHandlers.external.postMessage"
- npx esbuild@latest desktop/main.js --bundle --tree-shaking=true --minify --outfile=runtime.js --drop:console

build:all:
internal: true
deps:
- build:debug:windows
- build:debug:linux
- build:debug:darwin
- build:production:windows
- build:production:linux
- build:production:darwin
- build:debug
- build:production

cmds:
- cmd: echo "Build Complete."
Expand Down
5 changes: 5 additions & 0 deletions v3/internal/runtime/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

package runtime

import _ "embed"

//go:embed runtime.js
var DesktopRuntime []byte

var RuntimeAssetsBundle = &RuntimeAssets{
runtimeDesktopJS: DesktopRuntime,
}
Expand Down
5 changes: 5 additions & 0 deletions v3/internal/runtime/assets_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

package runtime

import _ "embed"

//go:embed runtime_debug.js
var DesktopRuntime []byte

var RuntimeAssetsBundle = &RuntimeAssets{
runtimeDesktopJS: DesktopRuntime,
}
Expand Down
46 changes: 46 additions & 0 deletions v3/internal/runtime/desktop/@wailsio/runtime/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
_ __ _ __
| | / /___ _(_) /____
| | /| / / __ `/ / / ___/
| |/ |/ / /_/ / / (__ )
|__/|__/\__,_/_/_/____/
The electron alternative for Go
(c) Lea Anthony 2019-present
*/

/* jshint esversion: 9 */

import { newRuntimeCallerWithID, objectNames } from "./runtime";
const call = newRuntimeCallerWithID(objectNames.Application);

const HideMethod = 0;
const ShowMethod = 1;
const QuitMethod = 2;

/**
* Hides a certain method by calling the HideMethod function.
*
* @return {Promise<void>}
*
*/
export function Hide() {
return call(HideMethod);
}

/**
* Calls the ShowMethod and returns the result.
*
* @return {Promise<void>}
*/
export function Show() {
return call(ShowMethod);
}

/**
* Calls the QuitMethod to terminate the program.
*
* @return {Promise<void>}
*/
export function Quit() {
return call(QuitMethod);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ The electron alternative for Go
*/

/* jshint esversion: 9 */

import {newRuntimeCallerWithID, objectNames} from "./runtime";

let call = newRuntimeCallerWithID(objectNames.Browser);

let BrowserOpenURL = 0;
const call = newRuntimeCallerWithID(objectNames.Browser, '');
const BrowserOpenURL = 0;

/**
* Open a browser window to the given URL
* @param {string} url - The URL to open
* @returns {Promise<string>}
*/
export function OpenURL(url) {
void call(BrowserOpenURL, {url});
return call(BrowserOpenURL, {url});
}
123 changes: 123 additions & 0 deletions v3/internal/runtime/desktop/@wailsio/runtime/calls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
_ __ _ __
| | / /___ _(_) /____
| | /| / / __ `/ / / ___/
| |/ |/ / /_/ / / (__ )
|__/|__/\__,_/_/_/____/
The electron alternative for Go
(c) Lea Anthony 2019-present
*/

/* jshint esversion: 9 */
import { newRuntimeCallerWithID, objectNames } from "./runtime";
import { nanoid } from 'nanoid/non-secure';

const CallBinding = 0;
const call = newRuntimeCallerWithID(objectNames.Call, '');
let callResponses = new Map();

window._wails = window._wails || {};
window._wails.callCallback = resultHandler;
window._wails.callErrorCallback = errorHandler;

function generateID() {
let result;
do {
result = nanoid();
} while (callResponses.has(result));
return result;
}

export function resultHandler(id, data, isJSON) {
const promiseHandler = getAndDeleteResponse(id);
if (promiseHandler) {
promiseHandler.resolve(isJSON ? JSON.parse(data) : data);
}
}

export function errorHandler(id, message) {
const promiseHandler = getAndDeleteResponse(id);
if (promiseHandler) {
promiseHandler.reject(message);
}
}

function getAndDeleteResponse(id) {
const response = callResponses.get(id);
callResponses.delete(id);
return response;
}

function callBinding(type, options = {}) {
return new Promise((resolve, reject) => {
const id = generateID();
options["call-id"] = id;
callResponses.set(id, { resolve, reject });
call(type, options).catch((error) => {
reject(error);
callResponses.delete(id);
});
});
}

/**
* Call method.
*
* @param {Object} options - The options for the method.
* @returns {Object} - The result of the call.
*/
export function Call(options) {
return callBinding(CallBinding, options);
}

/**
* Executes a method by name.
*
* @param {string} name - The name of the method in the format 'package.struct.method'.
* @param {...*} args - The arguments to pass to the method.
* @throws {Error} If the name is not a string or is not in the correct format.
* @returns {*} The result of the method execution.
*/
export function ByName(name, ...args) {
if (typeof name !== "string" || name.split(".").length !== 3) {
throw new Error("CallByName requires a string in the format 'package.struct.method'");
}
let [packageName, structName, methodName] = name.split(".");
return callBinding(CallBinding, {
packageName,
structName,
methodName,
args
});
}

/**
* Calls a method by its ID with the specified arguments.
*
* @param {string} methodID - The ID of the method to call.
* @param {...*} args - The arguments to pass to the method.
* @return {*} - The result of the method call.
*/
export function ByID(methodID, ...args) {
return callBinding(CallBinding, {
methodID,
args
});
}

/**
* Calls a method on a plugin.
*
* @param {string} pluginName - The name of the plugin.
* @param {string} methodName - The name of the method to call.
* @param {...*} args - The arguments to pass to the method.
* @returns {*} - The result of the method call.
*/
export function Plugin(pluginName, methodName, ...args) {
return callBinding(CallBinding, {
packageName: "wails-plugins",
structName: pluginName,
methodName,
args
});
}
35 changes: 35 additions & 0 deletions v3/internal/runtime/desktop/@wailsio/runtime/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
_ __ _ __
| | / /___ _(_) /____
| | /| / / __ `/ / / ___/
| |/ |/ / /_/ / / (__ )
|__/|__/\__,_/_/_/____/
The electron alternative for Go
(c) Lea Anthony 2019-present
*/

/* jshint esversion: 9 */

import {newRuntimeCallerWithID, objectNames} from "./runtime";

const call = newRuntimeCallerWithID(objectNames.Clipboard, '');
const ClipboardSetText = 0;
const ClipboardText = 1;

/**
* Sets the text to the Clipboard.
*
* @param {string} text - The text to be set to the Clipboard.
* @return {Promise} - A Promise that resolves when the operation is successful.
*/
export function SetText(text) {
return call(ClipboardSetText, {text});
}

/**
* Get the Clipboard text
* @returns {Promise<string>} A promise that resolves with the text from the Clipboard.
*/
export function Text() {
return call(ClipboardText);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import {newRuntimeCallerWithID, objectNames} from "./runtime";
/*
_ __ _ __
| | / /___ _(_) /____
| | /| / / __ `/ / / ___/
| |/ |/ / /_/ / / (__ )
|__/|__/\__,_/_/_/____/
The electron alternative for Go
(c) Lea Anthony 2019-present
*/

let call = newRuntimeCallerWithID(objectNames.ContextMenu);
/* jshint esversion: 9 */

import {newRuntimeCallerWithID, objectNames} from "./runtime";

let ContextMenuOpen = 0;
const call = newRuntimeCallerWithID(objectNames.ContextMenu, '');
const ContextMenuOpen = 0;

function openContextMenu(id, x, y, data) {
void call(ContextMenuOpen, {id, x, y, data});
Expand Down
Loading

0 comments on commit b08126d

Please sign in to comment.