mygo-runtime 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/index.d.ts +81 -0
- package/dist/index.js +57 -0
- package/dist/types.d.ts +30 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MyGo contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# mygo-runtime
|
|
2
|
+
|
|
3
|
+
Typed access to the runtime of [MyGo](https://github.com/egoist/mygo) apps,
|
|
4
|
+
for their web frontends.
|
|
5
|
+
|
|
6
|
+
MyGo injects its runtime into every page of an app as `window.mygo`. This
|
|
7
|
+
package gives it types and imports. The client that `mygo generate` writes
|
|
8
|
+
for your bound Go services is built on it, so you mostly use that:
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { Greeter, events } from "./mygo"; // generated
|
|
12
|
+
|
|
13
|
+
const greeting = await Greeter.greet("Ada");
|
|
14
|
+
events.tick.on((time) => console.log(time));
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Use the package directly for the window and the platform:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { currentWindow, isCallError, isMyGo, runtime } from "mygo-runtime";
|
|
21
|
+
|
|
22
|
+
if (isMyGo() && runtime().platform === "darwin") {
|
|
23
|
+
document.body.classList.add("mac");
|
|
24
|
+
}
|
|
25
|
+
await currentWindow.toggleMaximize();
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
A Go method returning an error rejects with a `CallError` (see
|
|
29
|
+
`isCallError`). Outside a MyGo window, e.g. when the dev server is opened in a
|
|
30
|
+
browser, calls reject and `isMyGo()` is false.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed access to the MyGo runtime, which MyGo injects into every page of
|
|
3
|
+
* an app as `window.mygo`. The client that `mygo generate` writes for your Go
|
|
4
|
+
* services is built on it; use it directly for window controls and the
|
|
5
|
+
* platform:
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { currentWindow, runtime } from "mygo-runtime";
|
|
9
|
+
*
|
|
10
|
+
* if (runtime().platform === "darwin") document.body.classList.add("mac");
|
|
11
|
+
* await currentWindow.toggleMaximize();
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
import type { Runtime, WindowControls } from "./types";
|
|
17
|
+
export type { Platform, Runtime, WindowControls } from "./types";
|
|
18
|
+
declare global {
|
|
19
|
+
interface Window {
|
|
20
|
+
/** The MyGo runtime; missing outside MyGo windows. */
|
|
21
|
+
mygo?: Runtime;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Reports whether the page runs inside a MyGo window. */
|
|
25
|
+
export declare function isMyGo(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Returns the MyGo runtime. Throws when the page is not running inside a
|
|
28
|
+
* MyGo window, e.g. when the dev server is opened in a regular browser.
|
|
29
|
+
*/
|
|
30
|
+
export declare function runtime(): Runtime;
|
|
31
|
+
/**
|
|
32
|
+
* Calls a bound Go method by name, e.g. `call("Greeter.Greet", "Ada")`.
|
|
33
|
+
* Prefer the typed client generated by `mygo generate`. A Go error rejects
|
|
34
|
+
* with a {@link CallError}.
|
|
35
|
+
*/
|
|
36
|
+
export declare function call<T = unknown>(method: string, ...args: unknown[]): Promise<T>;
|
|
37
|
+
/** Subscribes to a Go event by name. Returns a function that unsubscribes. */
|
|
38
|
+
export declare function on<T = unknown>(name: string, listener: (payload: T) => void): () => void;
|
|
39
|
+
/** Like {@link on}, but unsubscribes after the first event. */
|
|
40
|
+
export declare function once<T = unknown>(name: string, listener: (payload: T) => void): () => void;
|
|
41
|
+
/** A typed event sent by the Go side, declared with `mygo.NewEvent`. */
|
|
42
|
+
export interface GoEvent<T> {
|
|
43
|
+
readonly name: string;
|
|
44
|
+
/** Subscribes to the event and returns a function that unsubscribes. */
|
|
45
|
+
on(listener: (payload: T) => void): () => void;
|
|
46
|
+
/** Like on, but unsubscribes after the first event. */
|
|
47
|
+
once(listener: (payload: T) => void): () => void;
|
|
48
|
+
}
|
|
49
|
+
/** Returns the typed event `name`; the generated client declares them. */
|
|
50
|
+
export declare function event<T>(name: string): GoEvent<T>;
|
|
51
|
+
/** The error of a call whose Go method returned an error. */
|
|
52
|
+
export interface CallError extends Error {
|
|
53
|
+
readonly name: "CallError";
|
|
54
|
+
/** The method that failed, e.g. "Greeter.Greet". */
|
|
55
|
+
readonly method: string;
|
|
56
|
+
}
|
|
57
|
+
/** Reports whether err comes from a Go method that returned an error. */
|
|
58
|
+
export declare function isCallError(err: unknown): err is CallError;
|
|
59
|
+
/** Files dropped on the window, e.g. from Finder or Explorer. */
|
|
60
|
+
export interface FileDrop {
|
|
61
|
+
/** Absolute paths of the dropped files and directories. */
|
|
62
|
+
paths: string[];
|
|
63
|
+
/** Where they were dropped, in CSS pixels, like `clientX`. */
|
|
64
|
+
x: number;
|
|
65
|
+
/** Where they were dropped, in CSS pixels, like `clientY`. */
|
|
66
|
+
y: number;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Subscribes to files dropped on the window, with their paths, which DOM
|
|
70
|
+
* drop events do not tell. Only the app's own pages get them. Returns a
|
|
71
|
+
* function that unsubscribes.
|
|
72
|
+
*
|
|
73
|
+
* ```ts
|
|
74
|
+
* onFileDrop(({ paths, x, y }) => {
|
|
75
|
+
* const target = document.elementFromPoint(x, y);
|
|
76
|
+
* });
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export declare function onFileDrop(listener: (drop: FileDrop) => void): () => void;
|
|
80
|
+
/** The window hosting the page, e.g. for a custom title bar. */
|
|
81
|
+
export declare const currentWindow: WindowControls;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function isMyGo() {
|
|
3
|
+
return typeof globalThis === "object" && globalThis.mygo !== undefined;
|
|
4
|
+
}
|
|
5
|
+
function runtime() {
|
|
6
|
+
const rt = globalThis.mygo;
|
|
7
|
+
if (!rt)
|
|
8
|
+
throw new Error("mygo: runtime not found; is this page running inside a MyGo window?");
|
|
9
|
+
return rt;
|
|
10
|
+
}
|
|
11
|
+
function call(method, ...args) {
|
|
12
|
+
try {
|
|
13
|
+
return runtime().call(method, ...args);
|
|
14
|
+
} catch (err) {
|
|
15
|
+
return Promise.reject(err);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function on(name, listener) {
|
|
19
|
+
return runtime().on(name, listener);
|
|
20
|
+
}
|
|
21
|
+
function once(name, listener) {
|
|
22
|
+
return runtime().once(name, listener);
|
|
23
|
+
}
|
|
24
|
+
function event(name) {
|
|
25
|
+
return {
|
|
26
|
+
name,
|
|
27
|
+
on: (listener) => on(name, listener),
|
|
28
|
+
once: (listener) => once(name, listener)
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function isCallError(err) {
|
|
32
|
+
return err instanceof Error && err.name === "CallError";
|
|
33
|
+
}
|
|
34
|
+
function onFileDrop(listener) {
|
|
35
|
+
return on("mygo:file-drop", listener);
|
|
36
|
+
}
|
|
37
|
+
var currentWindow = {
|
|
38
|
+
minimize: async () => runtime().window.minimize(),
|
|
39
|
+
maximize: async () => runtime().window.maximize(),
|
|
40
|
+
unmaximize: async () => runtime().window.unmaximize(),
|
|
41
|
+
toggleMaximize: async () => runtime().window.toggleMaximize(),
|
|
42
|
+
isMaximized: async () => runtime().window.isMaximized(),
|
|
43
|
+
toggleFullScreen: async () => runtime().window.toggleFullScreen(),
|
|
44
|
+
close: async () => runtime().window.close(),
|
|
45
|
+
setTitle: async (title) => runtime().window.setTitle(title)
|
|
46
|
+
};
|
|
47
|
+
export {
|
|
48
|
+
call,
|
|
49
|
+
currentWindow,
|
|
50
|
+
event,
|
|
51
|
+
isCallError,
|
|
52
|
+
isMyGo,
|
|
53
|
+
on,
|
|
54
|
+
onFileDrop,
|
|
55
|
+
once,
|
|
56
|
+
runtime
|
|
57
|
+
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** The operating system, using Node.js' `process.platform` names. */
|
|
2
|
+
export type Platform = "darwin" | "linux" | "win32";
|
|
3
|
+
/** Controls for the window hosting the page, handy for custom title bars. */
|
|
4
|
+
export interface WindowControls {
|
|
5
|
+
minimize(): Promise<void>;
|
|
6
|
+
maximize(): Promise<void>;
|
|
7
|
+
unmaximize(): Promise<void>;
|
|
8
|
+
toggleMaximize(): Promise<void>;
|
|
9
|
+
isMaximized(): Promise<boolean>;
|
|
10
|
+
toggleFullScreen(): Promise<void>;
|
|
11
|
+
close(): Promise<void>;
|
|
12
|
+
setTitle(title: string): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
/** The runtime MyGo injects into every page as `window.mygo`. */
|
|
15
|
+
export interface Runtime {
|
|
16
|
+
/** Calls a bound Go method, e.g. `call("Greeter.Greet", "Ada")`. */
|
|
17
|
+
call<T = unknown>(method: string, ...args: unknown[]): Promise<T>;
|
|
18
|
+
/** Subscribes to a Go event. Returns a function that unsubscribes. */
|
|
19
|
+
on<T = unknown>(event: string, listener: (payload: T) => void): () => void;
|
|
20
|
+
/** Like `on`, but unsubscribes after the first event. */
|
|
21
|
+
once<T = unknown>(event: string, listener: (payload: T) => void): () => void;
|
|
22
|
+
/** The operating system. */
|
|
23
|
+
readonly platform: Platform;
|
|
24
|
+
/** Id of the Go `*mygo.Window` hosting this page. */
|
|
25
|
+
readonly windowId: number;
|
|
26
|
+
/** MyGo version. */
|
|
27
|
+
readonly version: string;
|
|
28
|
+
/** The window hosting this page. */
|
|
29
|
+
readonly window: WindowControls;
|
|
30
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mygo-runtime",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Typed access to the MyGo runtime in the pages of MyGo desktop apps",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"mygo",
|
|
7
|
+
"desktop",
|
|
8
|
+
"webview",
|
|
9
|
+
"go"
|
|
10
|
+
],
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/egoist/mygo.git",
|
|
14
|
+
"directory": "packages/runtime"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"type": "module",
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"README.md"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "bun run build.ts",
|
|
32
|
+
"test": "bun test"
|
|
33
|
+
}
|
|
34
|
+
}
|