mygo-runtime 0.1.2 → 0.1.4
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/README.md +12 -0
- package/dist/index.d.ts +19 -1
- package/dist/index.js +4 -0
- package/dist/types.d.ts +26 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,6 +25,18 @@ if (isMyGo() && runtime().platform === "darwin") {
|
|
|
25
25
|
await currentWindow.toggleMaximize();
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
+
A Go method that streams values through a `*mygo.Channel[T]` parameter takes
|
|
29
|
+
a `Channel` in its place:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { Channel } from "mygo-runtime";
|
|
33
|
+
|
|
34
|
+
const lines = new Channel<string>();
|
|
35
|
+
const done = Shell.tail("ls -R", lines);
|
|
36
|
+
for await (const line of lines) console.log(line);
|
|
37
|
+
await done;
|
|
38
|
+
```
|
|
39
|
+
|
|
28
40
|
A Go method returning an error rejects with a `CallError` (see
|
|
29
41
|
`isCallError`). Outside a MyGo window, e.g. when the dev server is opened in a
|
|
30
42
|
browser, calls reject and `isMyGo()` is false.
|
package/dist/index.d.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
*
|
|
14
14
|
* @module
|
|
15
15
|
*/
|
|
16
|
-
import type { Runtime, WindowControls } from "./types";
|
|
16
|
+
import type { Channel as ChannelType, Runtime, WindowControls } from "./types";
|
|
17
17
|
export type { Platform, Runtime, WindowControls } from "./types";
|
|
18
18
|
declare global {
|
|
19
19
|
interface Window {
|
|
@@ -34,6 +34,24 @@ export declare function runtime(): Runtime;
|
|
|
34
34
|
* with a {@link CallError}.
|
|
35
35
|
*/
|
|
36
36
|
export declare function call<T = unknown>(method: string, ...args: unknown[]): Promise<T>;
|
|
37
|
+
/**
|
|
38
|
+
* A stream of values from a Go method that takes a `*mygo.Channel[T]`.
|
|
39
|
+
* Create one, pass it to the call in place of that parameter, and iterate
|
|
40
|
+
* it or handle its values with `onmessage`:
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* const lines = new Channel<string>();
|
|
44
|
+
* const done = Shell.tail("ping -c 3 example.com", lines);
|
|
45
|
+
* for await (const line of lines) output.append(line + "\n");
|
|
46
|
+
* await done;
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* It closes when the method returns; `close()`, or breaking out of the
|
|
50
|
+
* loop, stops the method. A channel serves one call.
|
|
51
|
+
*/
|
|
52
|
+
export type Channel<T = unknown> = ChannelType<T>;
|
|
53
|
+
/** Creates a {@link Channel}, optionally with its `onmessage` handler. */
|
|
54
|
+
export declare const Channel: new <T = unknown>(onmessage?: (value: T) => void) => Channel<T>;
|
|
37
55
|
/** Subscribes to a Go event by name. Returns a function that unsubscribes. */
|
|
38
56
|
export declare function on<T = unknown>(name: string, listener: (payload: T) => void): () => void;
|
|
39
57
|
/** Like {@link on}, but unsubscribes after the first event. */
|
package/dist/index.js
CHANGED
|
@@ -15,6 +15,9 @@ function call(method, ...args) {
|
|
|
15
15
|
return Promise.reject(err);
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
+
var Channel = function(onmessage) {
|
|
19
|
+
return runtime().channel(onmessage);
|
|
20
|
+
};
|
|
18
21
|
function on(name, listener) {
|
|
19
22
|
return runtime().on(name, listener);
|
|
20
23
|
}
|
|
@@ -45,6 +48,7 @@ var currentWindow = {
|
|
|
45
48
|
setTitle: async (title) => runtime().window.setTitle(title)
|
|
46
49
|
};
|
|
47
50
|
export {
|
|
51
|
+
Channel,
|
|
48
52
|
call,
|
|
49
53
|
currentWindow,
|
|
50
54
|
event,
|
package/dist/types.d.ts
CHANGED
|
@@ -11,10 +11,36 @@ export interface WindowControls {
|
|
|
11
11
|
close(): Promise<void>;
|
|
12
12
|
setTitle(title: string): Promise<void>;
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* A stream of values from a Go method that takes a `*mygo.Channel[T]`,
|
|
16
|
+
* passed to the call in its place. Iterate it, or handle the values with
|
|
17
|
+
* `onmessage`: values that arrive before either wait for it. The channel
|
|
18
|
+
* closes when the method returns, or calls `Close`, once its values are
|
|
19
|
+
* taken; `close()` stops the method.
|
|
20
|
+
*/
|
|
21
|
+
export interface Channel<T = unknown> extends AsyncIterable<T> {
|
|
22
|
+
/** Called with each value, instead of iterating. */
|
|
23
|
+
onmessage: ((value: T) => void) | null;
|
|
24
|
+
/** Called when the channel closes. */
|
|
25
|
+
onclose: (() => void) | null;
|
|
26
|
+
/** Whether the channel is closed. */
|
|
27
|
+
readonly closed: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Closes the channel, dropping values not taken yet: the Go method's
|
|
30
|
+
* `Send` fails and its context is canceled. Breaking out of a
|
|
31
|
+
* `for await` loop over the channel closes it too.
|
|
32
|
+
*/
|
|
33
|
+
close(): void;
|
|
34
|
+
}
|
|
14
35
|
/** The runtime MyGo injects into every page as `window.mygo`. */
|
|
15
36
|
export interface Runtime {
|
|
16
37
|
/** Calls a bound Go method, e.g. `call("Greeter.Greet", "Ada")`. */
|
|
17
38
|
call<T = unknown>(method: string, ...args: unknown[]): Promise<T>;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a channel to pass to a call of a Go method that streams values
|
|
41
|
+
* through a `*mygo.Channel[T]` parameter. A channel serves one call.
|
|
42
|
+
*/
|
|
43
|
+
channel<T = unknown>(onmessage?: (value: T) => void): Channel<T>;
|
|
18
44
|
/** Subscribes to a Go event. Returns a function that unsubscribes. */
|
|
19
45
|
on<T = unknown>(event: string, listener: (payload: T) => void): () => void;
|
|
20
46
|
/** Like `on`, but unsubscribes after the first event. */
|