tauri-plugin-live-activity 0.6.0 → 0.7.1
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.ko.md +200 -248
- package/README.md +195 -240
- package/dist-js/index.d.ts +33 -1
- package/dist-js/index.js +131 -0
- package/dist-js/jsx/index.d.ts +14 -1
- package/dist-js/jsx/index.js +22 -6
- package/dist-js/layout.d.ts +15 -4
- package/dist-js/layout.js +21 -4
- package/dist-js/react/session.d.ts +3 -1
- package/dist-js/react/session.js +1 -0
- package/dist-js/types.d.ts +467 -15
- package/package.json +3 -3
package/dist-js/index.js
CHANGED
|
@@ -137,3 +137,134 @@ async function heldTokens(unlisten) {
|
|
|
137
137
|
export function onUpdated(handler) {
|
|
138
138
|
return listen('live-activity://updated', event => handler(event.payload));
|
|
139
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* Receive the record of a layout activity that began while the app ran (iOS): one the app started, or one a push
|
|
142
|
+
* started with the app's push-to-start token. An activity that began before the page listened is in `listLayout`.
|
|
143
|
+
*/
|
|
144
|
+
export function onStarted(handler) {
|
|
145
|
+
return listen('live-activity://started', event => handler(event.payload));
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Receive each new lifecycle state of an activity, of a template or a layout. iOS reports the changes ActivityKit
|
|
149
|
+
* tells of while the app runs. Android reports where an activity stands when the plugin looks, each state once:
|
|
150
|
+
* `active` once Android shows the notification, `stale` after the snapshot's `staleAt`, `ended` and `dismissed`.
|
|
151
|
+
* The plugin looks with every start, update, end and list, when Android stores a tap or a dismissal, and at the
|
|
152
|
+
* stale time of an activity while the app runs. A state that came while the app did not run is reported when it
|
|
153
|
+
* starts. An activity is over at `ended` and at `dismissed`: an end that removes an activity at once may be reported
|
|
154
|
+
* as `dismissed` alone.
|
|
155
|
+
*/
|
|
156
|
+
export function onState(handler) {
|
|
157
|
+
return listen('live-activity://state', event => handler(event.payload));
|
|
158
|
+
}
|
|
159
|
+
/** The handlers of this page for openings. They share one subscription. */
|
|
160
|
+
const openingHandlers = new Set();
|
|
161
|
+
/** The page's subscription to the event that says an opening came, while the page has handlers. */
|
|
162
|
+
let openingEvents;
|
|
163
|
+
/** The questions of this page to the plugin, one after another, so that the openings keep their order. */
|
|
164
|
+
let openingTakes = Promise.resolve();
|
|
165
|
+
/** The number of the newest opening that this page has handed to its handlers. */
|
|
166
|
+
let openingsHanded = 0;
|
|
167
|
+
/** The number up to which this page has told the plugin to let the openings go. */
|
|
168
|
+
let openingsLetGo = 0;
|
|
169
|
+
/**
|
|
170
|
+
* Hands `opening` to `handler`. A handler that throws is reported, as the browser reports a listener that throws,
|
|
171
|
+
* and keeps the opening from nobody else.
|
|
172
|
+
*/
|
|
173
|
+
function hand(handler, opening) {
|
|
174
|
+
try {
|
|
175
|
+
handler(opening);
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
const report = globalThis.reportError;
|
|
179
|
+
if (typeof report === 'function')
|
|
180
|
+
report(error);
|
|
181
|
+
else
|
|
182
|
+
console.error(error);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Asks the plugin for the openings that wait and hands each new one to the handlers there are, until none is left.
|
|
187
|
+
* Every question also says how far this page has handed them over, and the plugin lets those go. So an opening
|
|
188
|
+
* that no handler got stays with the plugin, for a handler that comes later or for the page that loads next.
|
|
189
|
+
*/
|
|
190
|
+
function takeOpenings() {
|
|
191
|
+
const taken = openingTakes.then(async () => {
|
|
192
|
+
let answered = false;
|
|
193
|
+
// With nobody to hand to, the plugin is only told what it may let go.
|
|
194
|
+
while (openingHandlers.size > 0 || openingsLetGo < openingsHanded) {
|
|
195
|
+
const handed = openingsHanded;
|
|
196
|
+
let waiting;
|
|
197
|
+
try {
|
|
198
|
+
waiting = await invoke('plugin:live-activity|openings', { taken: handed });
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
// Once the plugin has answered, a question that fails only leaves it for the next one to say what was
|
|
202
|
+
// handed over.
|
|
203
|
+
if (answered)
|
|
204
|
+
return;
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
answered = true;
|
|
208
|
+
openingsLetGo = Math.max(openingsLetGo, handed);
|
|
209
|
+
const first = waiting.through - waiting.openings.length + 1;
|
|
210
|
+
for (const [index, opening] of waiting.openings.entries()) {
|
|
211
|
+
// The plugin answers what comes after `handed`. One that this page has handed over is not handed again.
|
|
212
|
+
if (first + index <= openingsHanded)
|
|
213
|
+
continue;
|
|
214
|
+
if (openingHandlers.size === 0)
|
|
215
|
+
break;
|
|
216
|
+
// A handler that another one unsubscribed meanwhile is not called.
|
|
217
|
+
for (const handler of [...openingHandlers])
|
|
218
|
+
if (openingHandlers.has(handler))
|
|
219
|
+
hand(handler, opening);
|
|
220
|
+
openingsHanded = first + index;
|
|
221
|
+
}
|
|
222
|
+
if (openingsHanded === handed)
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
// A question that failed does not stop the next one: what it left still waits in the plugin.
|
|
227
|
+
openingTakes = taken.catch(() => undefined);
|
|
228
|
+
return taken;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Receive each opening of the app from the notification of an activity (Android): which activity the person
|
|
232
|
+
* opened it from, and by which action when it was one that opens a URL. The URL itself comes to the app as any deep
|
|
233
|
+
* link. iOS hands the URL to the app and reports nothing here.
|
|
234
|
+
*
|
|
235
|
+
* The opening that launched the app is reported before the page has loaded, so the plugin keeps every opening until
|
|
236
|
+
* a page has handed it to a handler, the newest 16. The handler first gets those that waited, oldest first, and
|
|
237
|
+
* then each one as it comes, each once: subscribe when the page starts. The handlers of a page share the openings;
|
|
238
|
+
* one that subscribes later gets what is handed over from then on. Rejects, subscribed to nothing, when the plugin
|
|
239
|
+
* does not answer the first question for what waits.
|
|
240
|
+
*/
|
|
241
|
+
export async function onOpened(handler) {
|
|
242
|
+
// A function may subscribe twice: each subscription is its own and ends alone.
|
|
243
|
+
const subscribed = (event) => handler(event);
|
|
244
|
+
const leave = () => {
|
|
245
|
+
if (!openingHandlers.delete(subscribed) || openingHandlers.size > 0)
|
|
246
|
+
return;
|
|
247
|
+
const events = openingEvents;
|
|
248
|
+
openingEvents = undefined;
|
|
249
|
+
void events?.then(unlisten => unlisten()).catch(() => undefined);
|
|
250
|
+
};
|
|
251
|
+
openingHandlers.add(subscribed);
|
|
252
|
+
try {
|
|
253
|
+
// The event says that an opening came. The plugin hands it over when it is asked.
|
|
254
|
+
openingEvents ??= listen('live-activity://opened', () => { void takeOpenings().catch(() => undefined); });
|
|
255
|
+
await openingEvents;
|
|
256
|
+
await takeOpenings();
|
|
257
|
+
}
|
|
258
|
+
catch (error) {
|
|
259
|
+
leave();
|
|
260
|
+
throw error;
|
|
261
|
+
}
|
|
262
|
+
return leave;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Receive the permissions after the person changed them in Settings (iOS): Live Activities turned on or off, or
|
|
266
|
+
* frequent pushes. `checkPermissions` gives them at any time.
|
|
267
|
+
*/
|
|
268
|
+
export function onPermissions(handler) {
|
|
269
|
+
return listen('live-activity://permissions', event => handler(event.payload));
|
|
270
|
+
}
|
package/dist-js/jsx/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { LayoutButtonNode, LayoutCustomNode, LayoutDividerNode, LayoutGaugeNode, LayoutHStackNode, LayoutImageNode, LayoutNode, LayoutPathNode, LayoutProgressNode, LayoutSpacerNode, LayoutSymbolNode, LayoutTextNode, LayoutTimerTextNode, LayoutVStackNode, LayoutZStackNode } from '../types.js';
|
|
1
|
+
import type { LayoutButtonNode, LayoutCustomNode, LayoutDividerNode, LayoutGaugeNode, LayoutHStackNode, LayoutImageNode, LayoutLinkNode, LayoutNode, LayoutPathNode, LayoutProgressNode, LayoutSpacerNode, LayoutSymbolNode, LayoutTextNode, LayoutTimerTextNode, LayoutToggleNode, LayoutVStackNode, LayoutZStackNode } from '../types.js';
|
|
2
2
|
export { bind } from '../layout.js';
|
|
3
3
|
/** What a stack takes as children: nodes, arrays of them, and booleans, `null` or `undefined`, which are left out. */
|
|
4
4
|
export type LayoutChild = LayoutNode | readonly LayoutChild[] | boolean | null | undefined;
|
|
@@ -29,6 +29,12 @@ export type ImageProps = Props<LayoutImageNode, 'source'> & ({
|
|
|
29
29
|
export type ButtonProps = Props<LayoutButtonNode, 'child'> & {
|
|
30
30
|
readonly children: LayoutNode;
|
|
31
31
|
};
|
|
32
|
+
export type LinkProps = Props<LayoutLinkNode, 'child'> & {
|
|
33
|
+
readonly children: LayoutNode;
|
|
34
|
+
};
|
|
35
|
+
export type ToggleProps = Props<LayoutToggleNode, 'child'> & {
|
|
36
|
+
readonly children: LayoutNode;
|
|
37
|
+
};
|
|
32
38
|
/** Children from top to bottom. */
|
|
33
39
|
export declare function VStack({ children, ...options }: VStackProps): LayoutVStackNode;
|
|
34
40
|
/** Children side by side. */
|
|
@@ -55,5 +61,12 @@ export declare function Gauge({ bind, ...options }: Props<LayoutGaugeNode>): Lay
|
|
|
55
61
|
export declare function TimerText({ start, end, ...options }: Props<LayoutTimerTextNode>): LayoutTimerTextNode;
|
|
56
62
|
/** A button around its one child; a tap reports `actionId` to `onAction`. */
|
|
57
63
|
export declare function Button({ actionId, children, ...options }: ButtonProps): LayoutButtonNode;
|
|
64
|
+
/** A link around its one child; a tap opens the app with `url`, a URL or `bind(key)` for a text value that is one. */
|
|
65
|
+
export declare function Link({ url, children, ...options }: LinkProps): LayoutLinkNode;
|
|
66
|
+
/**
|
|
67
|
+
* A switch that shows the boolean value of `bind` beside its one child, the label; a tap reports `actionId` to
|
|
68
|
+
* `onAction` with `on`, the state the person asked for.
|
|
69
|
+
*/
|
|
70
|
+
export declare function Toggle({ actionId, bind, children, ...options }: ToggleProps): LayoutToggleNode;
|
|
58
71
|
/** A SwiftUI view that the app registers under `name`; `props` are its inputs. */
|
|
59
72
|
export declare function Custom({ name, ...options }: Props<LayoutCustomNode>): LayoutCustomNode;
|
package/dist-js/jsx/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Layout components for TSX. Each returns the plain node object of its `layout` builder; nothing here
|
|
2
|
-
// imports Tauri or React, so a server can build the same layout.
|
|
3
|
-
|
|
2
|
+
// imports Tauri or React, so a server can build the same layout. Every component takes the members any node
|
|
3
|
+
// may carry as props: `style`, `if` or `unless`, `ifStale`, `accessibility`, `transition` and `privacy`.
|
|
4
|
+
import { bound, button, custom, divider, gauge, hstack, image, link, path, progress, spacer, symbol, text, timerText, toggle, vstack, zstack, } from '../layout.js';
|
|
4
5
|
export { bind } from '../layout.js';
|
|
5
6
|
/** Children from top to bottom. */
|
|
6
7
|
export function VStack({ children, ...options }) {
|
|
@@ -61,10 +62,18 @@ export function TimerText({ start, end, ...options }) {
|
|
|
61
62
|
}
|
|
62
63
|
/** A button around its one child; a tap reports `actionId` to `onAction`. */
|
|
63
64
|
export function Button({ actionId, children, ...options }) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
65
|
+
return button(actionId, only('Button', children), options);
|
|
66
|
+
}
|
|
67
|
+
/** A link around its one child; a tap opens the app with `url`, a URL or `bind(key)` for a text value that is one. */
|
|
68
|
+
export function Link({ url, children, ...options }) {
|
|
69
|
+
return link(url, only('Link', children), options);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* A switch that shows the boolean value of `bind` beside its one child, the label; a tap reports `actionId` to
|
|
73
|
+
* `onAction` with `on`, the state the person asked for.
|
|
74
|
+
*/
|
|
75
|
+
export function Toggle({ actionId, bind, children, ...options }) {
|
|
76
|
+
return toggle(actionId, bind, only('Toggle', children), options);
|
|
68
77
|
}
|
|
69
78
|
/** A SwiftUI view that the app registers under `name`; `props` are its inputs. */
|
|
70
79
|
export function Custom({ name, ...options }) {
|
|
@@ -74,6 +83,13 @@ function isNode(value) {
|
|
|
74
83
|
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
75
84
|
&& typeof value.type === 'string';
|
|
76
85
|
}
|
|
86
|
+
/** The one child of a button, a link or a toggle; it may come as an array of one. */
|
|
87
|
+
function only(component, children) {
|
|
88
|
+
const child = Array.isArray(children) && children.length === 1 ? children[0] : children;
|
|
89
|
+
if (!isNode(child))
|
|
90
|
+
throw new TypeError(`${component}: needs exactly one node as its child`);
|
|
91
|
+
return child;
|
|
92
|
+
}
|
|
77
93
|
function nodes(component, children) {
|
|
78
94
|
const found = [];
|
|
79
95
|
const visit = (child) => {
|
package/dist-js/layout.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { LayoutBound, LayoutButtonNode, LayoutCustomNode, LayoutDividerNode, LayoutGaugeNode, LayoutHStackNode, LayoutImageNode, LayoutImageSource, LayoutNode, LayoutPathNode, LayoutProgressNode, LayoutSpacerNode, LayoutSymbolNode, LayoutTextNode, LayoutTimerTextNode, LayoutVStackNode, LayoutZStackNode } from './types.js';
|
|
1
|
+
import type { LayoutBindable, LayoutBound, LayoutButtonNode, LayoutCustomNode, LayoutDividerNode, LayoutGaugeNode, LayoutHStackNode, LayoutImageNode, LayoutImageSource, LayoutLinkNode, LayoutNode, LayoutPathNode, LayoutProgressNode, LayoutSpacerNode, LayoutSymbolNode, LayoutTextNode, LayoutTimerTextNode, LayoutToggleNode, LayoutVStackNode, LayoutZStackNode } from './types.js';
|
|
2
2
|
/** The members of a node that its builder does not take as arguments. */
|
|
3
3
|
type Options<N extends LayoutNode, Argument extends keyof N = never> = N extends unknown ? Omit<N, 'type' | Argument> : never;
|
|
4
4
|
/** Children from top to bottom. */
|
|
@@ -32,12 +32,23 @@ export declare function gauge(key: string, options?: Options<LayoutGaugeNode, 'b
|
|
|
32
32
|
export declare function timerText(start: string, end: string, options?: Options<LayoutTimerTextNode, 'start' | 'end'>): LayoutTimerTextNode;
|
|
33
33
|
/** A button around `child`; a tap reports `actionId` to `onAction`. */
|
|
34
34
|
export declare function button(actionId: string, child: LayoutNode, options?: Options<LayoutButtonNode, 'actionId' | 'child'>): LayoutButtonNode;
|
|
35
|
+
/**
|
|
36
|
+
* A link around `child`: a tap opens the app with `url`, a URL such as `myapp://delivery/7` or `bind(key)` for a
|
|
37
|
+
* text value that is one.
|
|
38
|
+
*/
|
|
39
|
+
export declare function link(url: LayoutBindable<string>, child: LayoutNode, options?: Options<LayoutLinkNode, 'url' | 'child'>): LayoutLinkNode;
|
|
40
|
+
/**
|
|
41
|
+
* A switch that shows the boolean value of `key` beside `child`, its label; a tap reports `actionId` to `onAction`
|
|
42
|
+
* with `on`, the state the person asked for.
|
|
43
|
+
*/
|
|
44
|
+
export declare function toggle(actionId: string, key: string, child: LayoutNode, options?: Options<LayoutToggleNode, 'actionId' | 'bind' | 'child'>): LayoutToggleNode;
|
|
35
45
|
/** A SwiftUI view that the app registers under `name` in LiveActivityCustomViews.swift; `props` are its inputs. */
|
|
36
46
|
export declare function custom(name: string, options?: Options<LayoutCustomNode, 'name'>): LayoutCustomNode;
|
|
37
47
|
/**
|
|
38
|
-
* A
|
|
39
|
-
*
|
|
40
|
-
* `
|
|
48
|
+
* A member that reads the value of `key` on every render, so an update can change it: `{ color: bind('tone') }`.
|
|
49
|
+
* Only these can be bound: `color`, `background`, `tint`, `opacity`, `size`, `weight` and `cornerRadius` of a style,
|
|
50
|
+
* `layout.background`, `layout.keyline` and `layout.url`, the `url` of a link, the `label`, `value` and `hint` of a
|
|
51
|
+
* node's `accessibility`, and the props of a `custom` node.
|
|
41
52
|
*/
|
|
42
53
|
export declare function bind(key: string): LayoutBound;
|
|
43
54
|
export {};
|
package/dist-js/layout.js
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
function given(options) {
|
|
3
3
|
return Object.fromEntries(Object.entries(options ?? {}).filter(([, value]) => value !== undefined));
|
|
4
4
|
}
|
|
5
|
-
// The builders return plain node objects. They do not validate: the plugin checks the whole request.
|
|
5
|
+
// The builders return plain node objects. They do not validate: the plugin checks the whole request. The options
|
|
6
|
+
// of every builder take the members any node may carry: `style`, `if` or `unless`, `ifStale`, `accessibility`,
|
|
7
|
+
// `transition` and `privacy`.
|
|
6
8
|
/** Children from top to bottom. */
|
|
7
9
|
export function vstack(children, options) {
|
|
8
10
|
return { type: 'vstack', children, ...given(options) };
|
|
@@ -62,14 +64,29 @@ export function timerText(start, end, options) {
|
|
|
62
64
|
export function button(actionId, child, options) {
|
|
63
65
|
return { type: 'button', actionId, child, ...given(options) };
|
|
64
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* A link around `child`: a tap opens the app with `url`, a URL such as `myapp://delivery/7` or `bind(key)` for a
|
|
69
|
+
* text value that is one.
|
|
70
|
+
*/
|
|
71
|
+
export function link(url, child, options) {
|
|
72
|
+
return { type: 'link', url, child, ...given(options) };
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A switch that shows the boolean value of `key` beside `child`, its label; a tap reports `actionId` to `onAction`
|
|
76
|
+
* with `on`, the state the person asked for.
|
|
77
|
+
*/
|
|
78
|
+
export function toggle(actionId, key, child, options) {
|
|
79
|
+
return { type: 'toggle', actionId, bind: key, child, ...given(options) };
|
|
80
|
+
}
|
|
65
81
|
/** A SwiftUI view that the app registers under `name` in LiveActivityCustomViews.swift; `props` are its inputs. */
|
|
66
82
|
export function custom(name, options) {
|
|
67
83
|
return { type: 'custom', name, ...given(options) };
|
|
68
84
|
}
|
|
69
85
|
/**
|
|
70
|
-
* A
|
|
71
|
-
*
|
|
72
|
-
* `
|
|
86
|
+
* A member that reads the value of `key` on every render, so an update can change it: `{ color: bind('tone') }`.
|
|
87
|
+
* Only these can be bound: `color`, `background`, `tint`, `opacity`, `size`, `weight` and `cornerRadius` of a style,
|
|
88
|
+
* `layout.background`, `layout.keyline` and `layout.url`, the `url` of a link, the `label`, `value` and `hint` of a
|
|
89
|
+
* node's `accessibility`, and the props of a `custom` node.
|
|
73
90
|
*/
|
|
74
91
|
export function bind(key) {
|
|
75
92
|
return { bind: key };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { EndPolicy, Fallback, Layout, LayoutActivityRecord, LayoutAndroidOptions, LayoutEndInput, LayoutPush, LayoutStartInput, LayoutStyle, LayoutUpdateInput, LayoutValues, LiveActivityError, PushTokenEvent, UpdatedEvent } from '../types.js';
|
|
1
|
+
import type { EndPolicy, Fallback, Layout, LayoutActivityRecord, LayoutAndroidOptions, LayoutEndInput, LayoutIosOptions, LayoutPush, LayoutStartInput, LayoutStyle, LayoutUpdateInput, LayoutValues, LiveActivityError, PushTokenEvent, UpdatedEvent } from '../types.js';
|
|
2
2
|
export interface SessionOptions {
|
|
3
3
|
/** The app's key of the activity; another key ends the running activity and starts one for it. */
|
|
4
4
|
readonly activityKey: string;
|
|
@@ -14,6 +14,8 @@ export interface SessionOptions {
|
|
|
14
14
|
readonly fallback?: Fallback;
|
|
15
15
|
/** Read at start. */
|
|
16
16
|
readonly android?: LayoutAndroidOptions;
|
|
17
|
+
/** Read at start: what only iOS reads, such as a transient activity. */
|
|
18
|
+
readonly ios?: LayoutIosOptions;
|
|
17
19
|
/** Read at start: what the activity asks ActivityKit for, a push token or a channel (iOS). */
|
|
18
20
|
readonly push?: LayoutPush;
|
|
19
21
|
/** False ends the activity, and true starts it again. Defaults to true. */
|
package/dist-js/react/session.js
CHANGED
|
@@ -95,6 +95,7 @@ export class Session {
|
|
|
95
95
|
snapshot: { revision: 0, values },
|
|
96
96
|
...(options.styles === undefined ? {} : { styles: options.styles }),
|
|
97
97
|
...(options.android === undefined ? {} : { android: options.android }),
|
|
98
|
+
...(options.ios === undefined ? {} : { ios: options.ios }),
|
|
98
99
|
...(options.push === undefined ? {} : { push: options.push }),
|
|
99
100
|
};
|
|
100
101
|
this.failedKey = undefined;
|