import {
call as e,
fork as a,
take as t,
put as i,
getContext as o,
spawn as l,
delay as s,
} from "redux-saga/effects"
import { channelFromMessages as r } from "../lib"
import { taskErrorSaga as d } from "../error"
import { roomInvitationHandler as n } from "../room"
import { PLATFORM_CONTEXT_KEY as p } from "../constants"
import {
WAKEUP_MESSAGE as y,
CLICK_AVATAR_MESSAGE as c,
PLATFORM_UPDATE_STATUS as f,
PEAR_RUNTIME_UPDATED as m,
PEAR_RUNTIME_UPDATING as g,
} from "./platform.constants"
import { UI_PANEL as u, toggleUIPanel as P } from "../ui-desktop"
import {
APP_UPDATE_REASONS as A,
APP_DELAY_BEFORE_UPDATE as k,
appRoomInvitationLinkServed as D,
setUpdateRequired as x,
setUpdateStatus as S,
} from "../app"
import { userInfoNotification as U } from "../notification"
export function* platformFocusHandler() {
const { windowFocus: a } = yield o(p)
try {
yield e(a)
} catch (e) {
yield d("platformFocusHandler", e)
}
}
export function* wakeupMessagesSaga() {
const { messages: e } = yield o(p),
a = r(e, y)
try {
for (;;) {
const e = yield t(a)
;(e?.linkData && (yield i(D()), yield l(n, { link: e.linkData, skipParse: !0 })),
yield l(platformFocusHandler))
}
} finally {
a?.close()
}
}
function* w() {
const { updated: a } = yield o(p)
if (!(yield e(a))) return
const { restart: t } = yield o(p)
;(yield i(x({ reason: A.APP_UPGRADE, timestamp: Date.now() })),
yield i(
U({
title: "A new update has been released",
message: "Please update the app for a better experience. ",
}),
),
yield s(k),
yield e(t))
}
function* b() {
const { updating: a } = yield o(p)
;(yield e(a)) && (yield i(S(f.UPDATING)))
}
export function* appUpdatingSaga() {
const { messages: e } = yield o(p),
a = r(e, g)
try {
for (;;) (yield t(a), yield i(S(f.UPDATING)))
} finally {
a?.close()
}
}
export function* appUpdatedSaga() {
const { messages: a, restart: l } = yield o(p),
d = r(a, m)
try {
;(yield t(d),
yield i(S(f.UPDATED)),
yield i(x({ reason: A.APP_UPGRADE, timestamp: Date.now() })),
yield i(
U({
title: "A new update has been released",
message: "Please update the app for a better experience. ",
}),
),
yield s(k),
yield e(l, {}))
} finally {
d?.close()
}
}
export function* clickAvatarMessagesSaga() {
const { messages: e } = yield o(p),
a = r(e, c)
try {
for (;;) (yield t(a), yield i(P({ key: u.PROFILE })))
} finally {
a?.close()
}
}
export function* platformSaga() {
;(yield a(wakeupMessagesSaga),
yield a(clickAvatarMessagesSaga),
yield a(appUpdatingSaga),
yield a(appUpdatedSaga),
yield a(w),
yield a(b))
}