tauri-plugin-live-activity 0.5.1 → 0.7.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/README.ko.md +201 -234
- package/README.md +196 -226
- package/dist-js/index.d.ts +54 -1
- package/dist-js/index.js +103 -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/index.d.ts +2 -1
- package/dist-js/react/index.js +26 -2
- package/dist-js/react/session.d.ts +18 -1
- package/dist-js/react/session.js +40 -10
- package/dist-js/types.d.ts +508 -15
- package/package.json +2 -2
package/dist-js/index.js
CHANGED
|
@@ -68,3 +68,106 @@ export function writeImage(name, png) {
|
|
|
68
68
|
export function onAction(handler) {
|
|
69
69
|
return listen('live-activity://action', event => handler(event.payload));
|
|
70
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* The push tokens the plugin holds since launch (iOS): the newest token of each of the last 32 activities that got
|
|
73
|
+
* one, and the app's push-to-start token. The events report each token as it comes, but an event reaches only the
|
|
74
|
+
* listeners there are when it goes out, and a token may come before the page listens, at launch above all:
|
|
75
|
+
* `onPushToken` and `onPushToStartToken` read this right after subscribing. Empty where there are no pushes. The
|
|
76
|
+
* read is in the plugin's `default` permission.
|
|
77
|
+
*/
|
|
78
|
+
export function pushTokens() {
|
|
79
|
+
return invoke('plugin:live-activity|push_tokens');
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Receive the push token of each activity that asked for one (`push: 'token'`), when ActivityKit gives it and each
|
|
83
|
+
* time it changes (iOS), and first those the plugin already holds, each token once. Send it to the server, which
|
|
84
|
+
* pushes to it with activitykit's `push::Payload`. Rejects, subscribed to nothing, when the plugin cannot be asked
|
|
85
|
+
* for the tokens it holds.
|
|
86
|
+
*/
|
|
87
|
+
export async function onPushToken(handler) {
|
|
88
|
+
const delivered = new Map();
|
|
89
|
+
const deliver = (event) => {
|
|
90
|
+
if (delivered.get(event.activityId) === event.token)
|
|
91
|
+
return;
|
|
92
|
+
delivered.set(event.activityId, event.token);
|
|
93
|
+
handler(event);
|
|
94
|
+
};
|
|
95
|
+
const unlisten = await listen('live-activity://push-token', event => deliver(event.payload));
|
|
96
|
+
const held = await heldTokens(unlisten);
|
|
97
|
+
// An event that came while the plugin was asked is newer than what it held for that activity.
|
|
98
|
+
for (const event of held.activities)
|
|
99
|
+
if (!delivered.has(event.activityId))
|
|
100
|
+
deliver(event);
|
|
101
|
+
return unlisten;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Receive the app's push-to-start token, with `pushToStart: true` in the plugin configuration (iOS 17.2), when the
|
|
105
|
+
* system gives it and each time it changes, and first the one the plugin already holds, each token once. The server
|
|
106
|
+
* starts activities with it. Rejects, subscribed to nothing, when the plugin cannot be asked for the token it holds.
|
|
107
|
+
*/
|
|
108
|
+
export async function onPushToStartToken(handler) {
|
|
109
|
+
let last;
|
|
110
|
+
const deliver = (event) => {
|
|
111
|
+
if (event.token === last)
|
|
112
|
+
return;
|
|
113
|
+
last = event.token;
|
|
114
|
+
handler(event);
|
|
115
|
+
};
|
|
116
|
+
const unlisten = await listen('live-activity://push-to-start-token', event => deliver(event.payload));
|
|
117
|
+
const held = await heldTokens(unlisten);
|
|
118
|
+
if (last === undefined && held.pushToStart !== undefined)
|
|
119
|
+
deliver({ token: held.pushToStart });
|
|
120
|
+
return unlisten;
|
|
121
|
+
}
|
|
122
|
+
/** What the plugin holds, or the subscription given up and the failure passed on. */
|
|
123
|
+
async function heldTokens(unlisten) {
|
|
124
|
+
try {
|
|
125
|
+
return await pushTokens();
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
unlisten();
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Receive the record of an activity whose content a push changed (iOS): content above every revision this app sent
|
|
134
|
+
* or accepted, so the app's own updates do not come this way. Content pushed before the page listened is in the
|
|
135
|
+
* activity's record, as `listLayout` gives it.
|
|
136
|
+
*/
|
|
137
|
+
export function onUpdated(handler) {
|
|
138
|
+
return listen('live-activity://updated', event => handler(event.payload));
|
|
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
|
+
/**
|
|
160
|
+
* Receive each opening of the app from the notification of an activity (Android): which activity the person
|
|
161
|
+
* 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
|
|
162
|
+
* link. iOS hands the URL to the app and reports nothing here.
|
|
163
|
+
*/
|
|
164
|
+
export function onOpened(handler) {
|
|
165
|
+
return listen('live-activity://opened', event => handler(event.payload));
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Receive the permissions after the person changed them in Settings (iOS): Live Activities turned on or off, or
|
|
169
|
+
* frequent pushes. `checkPermissions` gives them at any time.
|
|
170
|
+
*/
|
|
171
|
+
export function onPermissions(handler) {
|
|
172
|
+
return listen('live-activity://permissions', event => handler(event.payload));
|
|
173
|
+
}
|
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 };
|
package/dist-js/react/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export interface UseLiveActivityOptions extends SessionOptions {
|
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
9
|
* Starts a layout activity on mount, sends `values` as an update whenever they change, and ends the activity on
|
|
10
|
-
* unmount or when `enabled` turns false.
|
|
10
|
+
* unmount or when `enabled` turns false. The state carries the activity's push token and takes content a push
|
|
11
|
+
* delivered (iOS).
|
|
11
12
|
*/
|
|
12
13
|
export declare function useLiveActivity(options: UseLiveActivityOptions): LiveActivityState;
|
package/dist-js/react/index.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
// `useLiveActivity`: one layout activity kept in step with a React component. Only this entry imports React.
|
|
2
2
|
import { useEffect, useRef, useState } from 'react';
|
|
3
|
-
import { endLayout, onAction, startLayout, updateLayout } from '../index.js';
|
|
3
|
+
import { endLayout, onAction, onPushToken, onUpdated, startLayout, updateLayout } from '../index.js';
|
|
4
4
|
import { IDLE, Session } from './session.js';
|
|
5
5
|
const commands = { startLayout, updateLayout, endLayout };
|
|
6
6
|
/**
|
|
7
7
|
* Starts a layout activity on mount, sends `values` as an update whenever they change, and ends the activity on
|
|
8
|
-
* unmount or when `enabled` turns false.
|
|
8
|
+
* unmount or when `enabled` turns false. The state carries the activity's push token and takes content a push
|
|
9
|
+
* delivered (iOS).
|
|
9
10
|
*/
|
|
10
11
|
export function useLiveActivity(options) {
|
|
11
12
|
const [state, setState] = useState(IDLE);
|
|
@@ -15,6 +16,8 @@ export function useLiveActivity(options) {
|
|
|
15
16
|
const handler = useRef(options.onAction);
|
|
16
17
|
handler.current = options.onAction;
|
|
17
18
|
const listening = options.onAction !== undefined;
|
|
19
|
+
// Read at start, as `push` is: an activity that asks for no push gets no token and no pushed content.
|
|
20
|
+
const pushing = options.push !== undefined;
|
|
18
21
|
useEffect(() => {
|
|
19
22
|
session.current?.sync(options);
|
|
20
23
|
});
|
|
@@ -24,6 +27,25 @@ export function useLiveActivity(options) {
|
|
|
24
27
|
current?.keep();
|
|
25
28
|
return () => current?.release();
|
|
26
29
|
}, []);
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (!pushing)
|
|
32
|
+
return undefined;
|
|
33
|
+
let open = true;
|
|
34
|
+
const stops = [
|
|
35
|
+
onPushToken((event) => { if (open)
|
|
36
|
+
session.current?.token(event); }),
|
|
37
|
+
onUpdated((event) => { if (open)
|
|
38
|
+
session.current?.pushed(event); }),
|
|
39
|
+
];
|
|
40
|
+
for (const stop of stops)
|
|
41
|
+
stop.catch((error) => { if (open)
|
|
42
|
+
session.current?.failed(error); });
|
|
43
|
+
return () => {
|
|
44
|
+
open = false;
|
|
45
|
+
for (const stop of stops)
|
|
46
|
+
stop.then((unlisten) => unlisten(), () => { });
|
|
47
|
+
};
|
|
48
|
+
}, [pushing]);
|
|
27
49
|
useEffect(() => {
|
|
28
50
|
if (!listening)
|
|
29
51
|
return undefined;
|
|
@@ -33,6 +55,8 @@ export function useLiveActivity(options) {
|
|
|
33
55
|
if (open && running !== undefined && event.activityId === running)
|
|
34
56
|
handler.current?.(event);
|
|
35
57
|
});
|
|
58
|
+
stop.catch((error) => { if (open)
|
|
59
|
+
session.current?.failed(error); });
|
|
36
60
|
return () => {
|
|
37
61
|
open = false;
|
|
38
62
|
stop.then((unlisten) => unlisten(), () => { });
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { EndPolicy, Fallback, Layout, LayoutActivityRecord, LayoutAndroidOptions, LayoutEndInput, LayoutStartInput, LayoutStyle, LayoutUpdateInput, LayoutValues, LiveActivityError } 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,10 @@ 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;
|
|
19
|
+
/** Read at start: what the activity asks ActivityKit for, a push token or a channel (iOS). */
|
|
20
|
+
readonly push?: LayoutPush;
|
|
17
21
|
/** False ends the activity, and true starts it again. Defaults to true. */
|
|
18
22
|
readonly enabled?: boolean;
|
|
19
23
|
/** How the activity ends on unmount or when `enabled` turns false. Defaults to `{ kind: 'immediate' }`. */
|
|
@@ -26,6 +30,8 @@ export interface LiveActivityState {
|
|
|
26
30
|
readonly record: LayoutActivityRecord | undefined;
|
|
27
31
|
/** The last refusal; an accepted request clears it. */
|
|
28
32
|
readonly error: LiveActivityError | undefined;
|
|
33
|
+
/** The running activity's latest push token (iOS, with `push: 'token'`), until the activity ends. */
|
|
34
|
+
readonly pushToken: string | undefined;
|
|
29
35
|
}
|
|
30
36
|
/** The commands the session sends. */
|
|
31
37
|
export interface SessionCommands {
|
|
@@ -46,11 +52,22 @@ export declare class Session {
|
|
|
46
52
|
private refused;
|
|
47
53
|
/** The key whose start was refused: not started again until `enabled` turns off or the key changes. */
|
|
48
54
|
private failedKey;
|
|
55
|
+
/** The latest push token of each activity seen, by id: one may come before the start it belongs to has answered. */
|
|
56
|
+
private readonly tokens;
|
|
49
57
|
private busy;
|
|
50
58
|
private mounts;
|
|
51
59
|
constructor(commands: SessionCommands, changed: (state: LiveActivityState) => void);
|
|
52
60
|
/** The id of the running activity. */
|
|
53
61
|
get activityId(): string | undefined;
|
|
62
|
+
/** A push token: the running activity's goes into the state; one that comes before its start answered waits. */
|
|
63
|
+
token(event: PushTokenEvent): void;
|
|
64
|
+
/**
|
|
65
|
+
* Content a push delivered: the running activity's record takes it. The component's values stand as last sent, so
|
|
66
|
+
* the pushed content stays until they change, and that change goes above the pushed revision.
|
|
67
|
+
*/
|
|
68
|
+
pushed(event: UpdatedEvent): void;
|
|
69
|
+
/** A failure outside a request, as a subscription the plugin refused: it lands in `error`. */
|
|
70
|
+
failed(error: unknown): void;
|
|
54
71
|
/** The options of a render. */
|
|
55
72
|
sync(options: SessionOptions): void;
|
|
56
73
|
/** The component is gone: the activity ends, now or once the request in flight has answered. */
|
package/dist-js/react/session.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export const IDLE = Object.freeze({ status: 'idle', record: undefined, error: undefined });
|
|
1
|
+
export const IDLE = Object.freeze({ status: 'idle', record: undefined, error: undefined, pushToken: undefined });
|
|
2
2
|
const IMMEDIATE = Object.freeze({ kind: 'immediate' });
|
|
3
3
|
export class Session {
|
|
4
4
|
constructor(commands, changed) {
|
|
@@ -6,6 +6,8 @@ export class Session {
|
|
|
6
6
|
this.changed = changed;
|
|
7
7
|
this.state = IDLE;
|
|
8
8
|
this.endPolicy = IMMEDIATE;
|
|
9
|
+
/** The latest push token of each activity seen, by id: one may come before the start it belongs to has answered. */
|
|
10
|
+
this.tokens = new Map();
|
|
9
11
|
this.busy = false;
|
|
10
12
|
this.mounts = 0;
|
|
11
13
|
}
|
|
@@ -13,6 +15,28 @@ export class Session {
|
|
|
13
15
|
get activityId() {
|
|
14
16
|
return this.running?.record.id;
|
|
15
17
|
}
|
|
18
|
+
/** A push token: the running activity's goes into the state; one that comes before its start answered waits. */
|
|
19
|
+
token(event) {
|
|
20
|
+
this.tokens.set(event.activityId, event.token);
|
|
21
|
+
if (this.running?.record.id === event.activityId)
|
|
22
|
+
this.set({ ...this.state, pushToken: event.token });
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Content a push delivered: the running activity's record takes it. The component's values stand as last sent, so
|
|
26
|
+
* the pushed content stays until they change, and that change goes above the pushed revision.
|
|
27
|
+
*/
|
|
28
|
+
pushed(event) {
|
|
29
|
+
const { record } = event;
|
|
30
|
+
if (!this.running || record.id !== this.running.record.id)
|
|
31
|
+
return;
|
|
32
|
+
const revision = Math.max(this.running.revision, record.snapshot.revision);
|
|
33
|
+
this.running = { ...this.running, record, revision };
|
|
34
|
+
this.set({ ...this.state, record });
|
|
35
|
+
}
|
|
36
|
+
/** A failure outside a request, as a subscription the plugin refused: it lands in `error`. */
|
|
37
|
+
failed(error) {
|
|
38
|
+
this.set({ ...this.state, error: asError(error) });
|
|
39
|
+
}
|
|
16
40
|
/** The options of a render. */
|
|
17
41
|
sync(options) {
|
|
18
42
|
this.endPolicy = options.endPolicy ?? IMMEDIATE;
|
|
@@ -71,18 +95,21 @@ export class Session {
|
|
|
71
95
|
snapshot: { revision: 0, values },
|
|
72
96
|
...(options.styles === undefined ? {} : { styles: options.styles }),
|
|
73
97
|
...(options.android === undefined ? {} : { android: options.android }),
|
|
98
|
+
...(options.ios === undefined ? {} : { ios: options.ios }),
|
|
99
|
+
...(options.push === undefined ? {} : { push: options.push }),
|
|
74
100
|
};
|
|
75
101
|
this.failedKey = undefined;
|
|
76
102
|
this.refused = undefined;
|
|
77
|
-
this.set({ status: 'starting', record: undefined, error: undefined });
|
|
103
|
+
this.set({ status: 'starting', record: undefined, error: undefined, pushToken: undefined });
|
|
78
104
|
this.send(this.commands.startLayout(input), (record) => {
|
|
79
|
-
|
|
80
|
-
this.
|
|
105
|
+
// A repeated keyed start, after a relaunch, answers the running activity at its own revision.
|
|
106
|
+
this.running = { record, values, revision: record.snapshot.revision };
|
|
107
|
+
this.set({ status: 'active', record, error: undefined, pushToken: this.tokens.get(record.id) });
|
|
81
108
|
}, (error) => {
|
|
82
109
|
// Blocks only the key still wanted: `enabled` off meanwhile, or another key, starts again.
|
|
83
110
|
if (this.wanted?.activityKey === options.activityKey)
|
|
84
111
|
this.failedKey = options.activityKey;
|
|
85
|
-
this.set({ status: 'failed', record: undefined, error });
|
|
112
|
+
this.set({ status: 'failed', record: undefined, error, pushToken: undefined });
|
|
86
113
|
});
|
|
87
114
|
}
|
|
88
115
|
update(running, values) {
|
|
@@ -91,22 +118,25 @@ export class Session {
|
|
|
91
118
|
this.running = { ...running, revision };
|
|
92
119
|
this.send(this.commands.updateLayout({ id: running.record.id, snapshot: { revision, values } }), (record) => {
|
|
93
120
|
this.refused = undefined;
|
|
94
|
-
|
|
95
|
-
|
|
121
|
+
// A push may have moved the activity on meanwhile: the next change goes above it.
|
|
122
|
+
const taken = Math.max(revision, record.snapshot.revision, this.running?.revision ?? 0);
|
|
123
|
+
this.running = { record, values, revision: taken };
|
|
124
|
+
this.set({ status: 'active', record, error: undefined, pushToken: this.state.pushToken });
|
|
96
125
|
}, (error) => {
|
|
97
126
|
this.refused = values;
|
|
98
127
|
this.set({ ...this.state, error });
|
|
99
128
|
});
|
|
100
129
|
}
|
|
101
130
|
end(record) {
|
|
102
|
-
this.set({ status: 'ending', record, error: this.state.error });
|
|
131
|
+
this.set({ status: 'ending', record, error: this.state.error, pushToken: this.state.pushToken });
|
|
132
|
+
this.tokens.delete(record.id);
|
|
103
133
|
this.send(this.commands.endLayout({ id: record.id, policy: this.endPolicy }), (ended) => {
|
|
104
134
|
this.running = undefined;
|
|
105
|
-
this.set({ status: 'ended', record: ended, error: undefined });
|
|
135
|
+
this.set({ status: 'ended', record: ended, error: undefined, pushToken: undefined });
|
|
106
136
|
}, (error) => {
|
|
107
137
|
// Not tried again: the activity is left to the system.
|
|
108
138
|
this.running = undefined;
|
|
109
|
-
this.set({ status: 'failed', record, error });
|
|
139
|
+
this.set({ status: 'failed', record, error, pushToken: undefined });
|
|
110
140
|
});
|
|
111
141
|
}
|
|
112
142
|
send(request, accepted, refused) {
|