示例工程 28-project-approval-panel

28-project-approval-panel/app/.tsc/src/lib/store.js

javascript · 1.6 KB

const EMPTY = {
    pending: [],
    history: [],
    policies: [],
    now: Date.now(),
};
export function createApprovalStore() {
    let state = { ...EMPTY, now: Date.now() };
    const listeners = new Set();
    const emit = () => {
        for (const listener of [...listeners])
            listener();
    };
    const replace = (next) => {
        state = next;
        emit();
    };
    const unsubscribe = window.api.approval.onChange(replace);
    void window.api.approval.get().then(replace);
    const ticker = setInterval(() => {
        if (state.pending.length === 0)
            return;
        state = { ...state, now: Date.now() };
        emit();
    }, 250);
    return {
        getState: () => state,
        subscribe: (fn) => {
            listeners.add(fn);
            return () => {
                listeners.delete(fn);
            };
        },
        decide: async (id, decision) => {
            replace(await window.api.approval.decide(id, decision));
        },
        request: async (input) => {
            replace(await window.api.approval.request(input));
        },
        reset: async () => {
            replace(await window.api.approval.reset());
        },
        dispose: () => {
            clearInterval(ticker);
            unsubscribe();
            listeners.clear();
        },
    };
}
export function remainingSeconds(expiresAt, now) {
    return Math.max(0, Math.ceil((expiresAt - now) / 1000));
}
export function riskLabel(risk) {
    if (risk === "exec")
        return "执行";
    if (risk === "write")
        return "写入";
    return "只读";
}
//# sourceMappingURL=store.js.map