ink-framework-lightos 0.1.0-alpha.12 → 0.1.0-alpha.13
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/ink-native.json +6 -0
- package/package.json +6 -4
- package/src/push-delivery.ts +44 -0
- package/src/push-hooks.ts +82 -0
- package/src/push.ts +2 -123
package/ink-native.json
CHANGED
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ink-framework-lightos",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.13",
|
|
4
4
|
"exports": {
|
|
5
5
|
".": "./src/index.ts",
|
|
6
6
|
"./ringtone": "./src/ringtone.ts",
|
|
7
|
-
"./push": "./src/push.ts"
|
|
7
|
+
"./push": "./src/push.ts",
|
|
8
|
+
"./push/delivery": "./src/push-delivery.ts"
|
|
8
9
|
},
|
|
9
10
|
"peerDependencies": {
|
|
10
|
-
"ink": "npm:ink-framework@0.1.0-alpha.
|
|
11
|
+
"ink": "npm:ink-framework@0.1.0-alpha.13",
|
|
11
12
|
"react": "19.2.8"
|
|
12
13
|
},
|
|
13
14
|
"license": "MIT",
|
|
@@ -19,6 +20,7 @@
|
|
|
19
20
|
"files": [
|
|
20
21
|
"src",
|
|
21
22
|
"*.json",
|
|
22
|
-
"*.ts"
|
|
23
|
+
"*.ts",
|
|
24
|
+
"*.md"
|
|
23
25
|
]
|
|
24
26
|
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { callNative, NativeError } from "ink/native";
|
|
2
|
+
|
|
3
|
+
export interface PushMessage {
|
|
4
|
+
id: string;
|
|
5
|
+
groupKey: string;
|
|
6
|
+
title: string;
|
|
7
|
+
body: string;
|
|
8
|
+
route: string;
|
|
9
|
+
receivedAt: number;
|
|
10
|
+
}
|
|
11
|
+
export function fields(value: unknown) {
|
|
12
|
+
if (typeof value !== "object" || value === null) throw new NativeError("protocol", "Invalid push state");
|
|
13
|
+
return {
|
|
14
|
+
text(name: string) {
|
|
15
|
+
const field: unknown = Reflect.get(value, name);
|
|
16
|
+
if (typeof field !== "string") throw new NativeError("protocol", `Invalid push ${name}`);
|
|
17
|
+
return field;
|
|
18
|
+
},
|
|
19
|
+
time(name: string) {
|
|
20
|
+
const field: unknown = Reflect.get(value, name);
|
|
21
|
+
if (typeof field !== "number" || !Number.isSafeInteger(field) || field < 0) throw new NativeError("protocol", `Invalid push ${name}`);
|
|
22
|
+
return field;
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function decodeMessage(value: unknown): PushMessage {
|
|
27
|
+
const item = fields(value);
|
|
28
|
+
return { id: item.text("id"), groupKey: item.text("groupKey"), title: item.text("title"), body: item.text("body"), route: item.text("route"), receivedAt: item.time("receivedAtMs") };
|
|
29
|
+
}
|
|
30
|
+
export interface PushDelivery { messages: readonly PushMessage[]; cancelled: readonly string[] }
|
|
31
|
+
export function decodePushDelivery(value: unknown): PushDelivery {
|
|
32
|
+
if (typeof value !== "object" || value === null || !("messages" in value) || !Array.isArray(value.messages)
|
|
33
|
+
|| !("cancelled" in value) || !Array.isArray(value.cancelled)) throw new TypeError("Invalid push delivery");
|
|
34
|
+
return {
|
|
35
|
+
messages: value.messages.map(decodeMessage),
|
|
36
|
+
cancelled: value.cancelled.map((key: unknown) => {
|
|
37
|
+
if (typeof key !== "string") throw new TypeError("Invalid cancelled push group");
|
|
38
|
+
return key;
|
|
39
|
+
}),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export async function setPushTask(task: { id: string } | null): Promise<void> {
|
|
43
|
+
await callNative("background", "set-push-task", { task: task?.id ?? null });
|
|
44
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { fields, decodeMessage, type PushMessage } from "./push-delivery";
|
|
2
|
+
import { useEffect, useMemo, useRef, useState } from "ink/internal/react";
|
|
3
|
+
import { NativeError } from "ink/native";
|
|
4
|
+
import { attachNativeController } from "ink/native/controller";
|
|
5
|
+
|
|
6
|
+
export interface PushState {
|
|
7
|
+
status: "idle" | "registering" | "synchronising" | "ready" | "error";
|
|
8
|
+
endpoint: string;
|
|
9
|
+
registeredAt: number;
|
|
10
|
+
openedKey: string;
|
|
11
|
+
messages: readonly PushMessage[];
|
|
12
|
+
error: Error | null;
|
|
13
|
+
}
|
|
14
|
+
export interface PushRegistration {
|
|
15
|
+
url: string;
|
|
16
|
+
bearerToken?: string;
|
|
17
|
+
encryptionKey?: string;
|
|
18
|
+
fetchOnWake?: boolean;
|
|
19
|
+
}
|
|
20
|
+
export interface PushOptions {
|
|
21
|
+
foregroundGroup?: string | null;
|
|
22
|
+
}
|
|
23
|
+
const initial: PushState = { status: "idle", endpoint: "", registeredAt: 0, openedKey: "", messages: [], error: null };
|
|
24
|
+
function decode(value: unknown): PushState {
|
|
25
|
+
const field = fields(value);
|
|
26
|
+
const status = field.text("status");
|
|
27
|
+
if (status !== "idle" && status !== "registering" && status !== "synchronising" && status !== "ready" && status !== "error") throw new NativeError("protocol", "Invalid push status");
|
|
28
|
+
if (typeof value !== "object" || value === null || !("messages" in value) || !Array.isArray(value.messages)) throw new NativeError("protocol", "Invalid push messages");
|
|
29
|
+
let error: Error | null = null;
|
|
30
|
+
if (status === "error") {
|
|
31
|
+
if (!("error" in value) || typeof value.error !== "object" || value.error === null) throw new NativeError("protocol", "Invalid push error");
|
|
32
|
+
const detail = fields(value.error);
|
|
33
|
+
error = new NativeError(detail.text("kind"), detail.text("message"), "retryable" in value.error && value.error.retryable === true);
|
|
34
|
+
}
|
|
35
|
+
return { status, endpoint: field.text("endpoint"), registeredAt: field.time("registeredAtMs"), openedKey: field.text("openedKey"), error,
|
|
36
|
+
messages: value.messages.map(decodeMessage) };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function usePush({ foregroundGroup = null }: PushOptions = {}) {
|
|
40
|
+
const [state, setState] = useState(initial);
|
|
41
|
+
const [ready, setReady] = useState(false);
|
|
42
|
+
const controller = useRef<ReturnType<typeof attachNativeController> | null>(null);
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
setState(initial);
|
|
45
|
+
setReady(false);
|
|
46
|
+
const fail = (error: unknown) => setState({ ...initial, status: "error", error: error instanceof Error ? error : new Error(String(error)) });
|
|
47
|
+
const attachment = attachNativeController("notifications", { kind: "light-push" }, value => {
|
|
48
|
+
try { setState(decode(value)); } catch (error) { fail(error); }
|
|
49
|
+
});
|
|
50
|
+
controller.current = attachment;
|
|
51
|
+
void attachment.ready.then(() => {
|
|
52
|
+
if (controller.current === attachment) setReady(true);
|
|
53
|
+
}, error => { if (controller.current === attachment) fail(error); });
|
|
54
|
+
return () => {
|
|
55
|
+
controller.current = null;
|
|
56
|
+
void attachment.dispose().catch(error => console.error("Could not release push inbox", error));
|
|
57
|
+
};
|
|
58
|
+
}, []);
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
const attachment = controller.current;
|
|
61
|
+
if (!attachment) return;
|
|
62
|
+
let active = true;
|
|
63
|
+
void attachment.call("foreground", { groupKey: foregroundGroup }).catch(error => {
|
|
64
|
+
if (active) setState(current => ({ ...current, status: "error", error: error instanceof Error ? error : new Error(String(error)) }));
|
|
65
|
+
});
|
|
66
|
+
return () => { active = false; };
|
|
67
|
+
}, [foregroundGroup]);
|
|
68
|
+
const commands = useMemo(() => {
|
|
69
|
+
const call = async (operation: string, payload: unknown = {}) => {
|
|
70
|
+
if (!controller.current) throw new NativeError("unavailable", "Push inbox is not attached");
|
|
71
|
+
await controller.current.call(operation, payload);
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
register: (options: PushRegistration) => call("register", options),
|
|
75
|
+
retry: () => call("retry"), unregister: () => call("unregister"),
|
|
76
|
+
dismiss: (groupKey: string) => call("dismiss", { groupKey }), clear: () => call("clear"),
|
|
77
|
+
notifyForeground: (groupKey: string) => call("foreground-message", { groupKey }),
|
|
78
|
+
};
|
|
79
|
+
}, []);
|
|
80
|
+
return { state, ready, ...commands };
|
|
81
|
+
}
|
|
82
|
+
|
package/src/push.ts
CHANGED
|
@@ -1,123 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { attachNativeController } from "ink/native/controller";
|
|
4
|
-
|
|
5
|
-
export interface PushMessage {
|
|
6
|
-
id: string;
|
|
7
|
-
groupKey: string;
|
|
8
|
-
title: string;
|
|
9
|
-
body: string;
|
|
10
|
-
route: string;
|
|
11
|
-
receivedAt: number;
|
|
12
|
-
}
|
|
13
|
-
export interface PushState {
|
|
14
|
-
status: "idle" | "registering" | "synchronising" | "ready" | "error";
|
|
15
|
-
endpoint: string;
|
|
16
|
-
registeredAt: number;
|
|
17
|
-
openedKey: string;
|
|
18
|
-
messages: readonly PushMessage[];
|
|
19
|
-
error: Error | null;
|
|
20
|
-
}
|
|
21
|
-
export interface PushRegistration {
|
|
22
|
-
url: string;
|
|
23
|
-
bearerToken?: string;
|
|
24
|
-
encryptionKey?: string;
|
|
25
|
-
fetchOnWake?: boolean;
|
|
26
|
-
}
|
|
27
|
-
export interface PushOptions {
|
|
28
|
-
foregroundGroup?: string | null;
|
|
29
|
-
}
|
|
30
|
-
const initial: PushState = { status: "idle", endpoint: "", registeredAt: 0, openedKey: "", messages: [], error: null };
|
|
31
|
-
function fields(value: unknown) {
|
|
32
|
-
if (typeof value !== "object" || value === null) throw new NativeError("protocol", "Invalid push state");
|
|
33
|
-
return {
|
|
34
|
-
text(name: string) {
|
|
35
|
-
const field: unknown = Reflect.get(value, name);
|
|
36
|
-
if (typeof field !== "string") throw new NativeError("protocol", `Invalid push ${name}`);
|
|
37
|
-
return field;
|
|
38
|
-
},
|
|
39
|
-
time(name: string) {
|
|
40
|
-
const field: unknown = Reflect.get(value, name);
|
|
41
|
-
if (typeof field !== "number" || !Number.isSafeInteger(field) || field < 0) throw new NativeError("protocol", `Invalid push ${name}`);
|
|
42
|
-
return field;
|
|
43
|
-
},
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
function decodeMessage(value: unknown): PushMessage {
|
|
47
|
-
const item = fields(value);
|
|
48
|
-
return { id: item.text("id"), groupKey: item.text("groupKey"), title: item.text("title"), body: item.text("body"), route: item.text("route"), receivedAt: item.time("receivedAtMs") };
|
|
49
|
-
}
|
|
50
|
-
function decode(value: unknown): PushState {
|
|
51
|
-
const field = fields(value);
|
|
52
|
-
const status = field.text("status");
|
|
53
|
-
if (status !== "idle" && status !== "registering" && status !== "synchronising" && status !== "ready" && status !== "error") throw new NativeError("protocol", "Invalid push status");
|
|
54
|
-
if (typeof value !== "object" || value === null || !("messages" in value) || !Array.isArray(value.messages)) throw new NativeError("protocol", "Invalid push messages");
|
|
55
|
-
let error: Error | null = null;
|
|
56
|
-
if (status === "error") {
|
|
57
|
-
if (!("error" in value) || typeof value.error !== "object" || value.error === null) throw new NativeError("protocol", "Invalid push error");
|
|
58
|
-
const detail = fields(value.error);
|
|
59
|
-
error = new NativeError(detail.text("kind"), detail.text("message"), "retryable" in value.error && value.error.retryable === true);
|
|
60
|
-
}
|
|
61
|
-
return { status, endpoint: field.text("endpoint"), registeredAt: field.time("registeredAtMs"), openedKey: field.text("openedKey"), error,
|
|
62
|
-
messages: value.messages.map(decodeMessage) };
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function usePush({ foregroundGroup = null }: PushOptions = {}) {
|
|
66
|
-
const [state, setState] = useState(initial);
|
|
67
|
-
const [ready, setReady] = useState(false);
|
|
68
|
-
const controller = useRef<ReturnType<typeof attachNativeController> | null>(null);
|
|
69
|
-
useEffect(() => {
|
|
70
|
-
setState(initial);
|
|
71
|
-
setReady(false);
|
|
72
|
-
const fail = (error: unknown) => setState({ ...initial, status: "error", error: error instanceof Error ? error : new Error(String(error)) });
|
|
73
|
-
const attachment = attachNativeController("notifications", { kind: "light-push" }, value => {
|
|
74
|
-
try { setState(decode(value)); } catch (error) { fail(error); }
|
|
75
|
-
});
|
|
76
|
-
controller.current = attachment;
|
|
77
|
-
void attachment.ready.then(() => {
|
|
78
|
-
if (controller.current === attachment) setReady(true);
|
|
79
|
-
}, error => { if (controller.current === attachment) fail(error); });
|
|
80
|
-
return () => {
|
|
81
|
-
controller.current = null;
|
|
82
|
-
void attachment.dispose().catch(error => console.error("Could not release push inbox", error));
|
|
83
|
-
};
|
|
84
|
-
}, []);
|
|
85
|
-
useEffect(() => {
|
|
86
|
-
const attachment = controller.current;
|
|
87
|
-
if (!attachment) return;
|
|
88
|
-
let active = true;
|
|
89
|
-
void attachment.call("foreground", { groupKey: foregroundGroup }).catch(error => {
|
|
90
|
-
if (active) setState(current => ({ ...current, status: "error", error: error instanceof Error ? error : new Error(String(error)) }));
|
|
91
|
-
});
|
|
92
|
-
return () => { active = false; };
|
|
93
|
-
}, [foregroundGroup]);
|
|
94
|
-
const commands = useMemo(() => {
|
|
95
|
-
const call = async (operation: string, payload: unknown = {}) => {
|
|
96
|
-
if (!controller.current) throw new NativeError("unavailable", "Push inbox is not attached");
|
|
97
|
-
await controller.current.call(operation, payload);
|
|
98
|
-
};
|
|
99
|
-
return {
|
|
100
|
-
register: (options: PushRegistration) => call("register", options),
|
|
101
|
-
retry: () => call("retry"), unregister: () => call("unregister"),
|
|
102
|
-
dismiss: (groupKey: string) => call("dismiss", { groupKey }), clear: () => call("clear"),
|
|
103
|
-
notifyForeground: (groupKey: string) => call("foreground-message", { groupKey }),
|
|
104
|
-
};
|
|
105
|
-
}, []);
|
|
106
|
-
return { state, ready, ...commands };
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export interface PushDelivery { messages: readonly PushMessage[]; cancelled: readonly string[] }
|
|
110
|
-
export function decodePushDelivery(value: unknown): PushDelivery {
|
|
111
|
-
if (typeof value !== "object" || value === null || !("messages" in value) || !Array.isArray(value.messages)
|
|
112
|
-
|| !("cancelled" in value) || !Array.isArray(value.cancelled)) throw new TypeError("Invalid push delivery");
|
|
113
|
-
return {
|
|
114
|
-
messages: value.messages.map(decodeMessage),
|
|
115
|
-
cancelled: value.cancelled.map((key: unknown) => {
|
|
116
|
-
if (typeof key !== "string") throw new TypeError("Invalid cancelled push group");
|
|
117
|
-
return key;
|
|
118
|
-
}),
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
export async function setPushTask(task: { id: string } | null): Promise<void> {
|
|
122
|
-
await callNative("background", "set-push-task", { task: task?.id ?? null });
|
|
123
|
-
}
|
|
1
|
+
export { usePush, type PushState, type PushRegistration, type PushOptions } from "./push-hooks";
|
|
2
|
+
export { decodePushDelivery, setPushTask, type PushDelivery, type PushMessage } from "./push-delivery";
|