-
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(web): add online atom * chore(web): fix atoms names --------- Co-authored-by: Arutiunian Artem <[email protected]>
- Loading branch information
1 parent
042a77c
commit 6413696
Showing
5 changed files
with
45 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './event' | ||
export * from './online' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { atom, type Atom } from '@reatom/core' | ||
import { onConnect } from '@reatom/hooks' | ||
import { withAssign } from '@reatom/primitives' | ||
import { onEvent } from './event' | ||
|
||
type OnlineAtom = Atom<boolean> & { | ||
/** Time stamp of transition to online mode. */ | ||
offlineAtAtom: Atom<number | undefined> | ||
/** Time stamp of transition to offline mode. */ | ||
onlineAtAtom: Atom<number | undefined> | ||
} | ||
|
||
/** | ||
* @note https://issues.chromium.org/issues/338514113 | ||
*/ | ||
export const createOnlineAtom = (): OnlineAtom => { | ||
const onlineAtom = atom(navigator.onLine, 'onLine') | ||
.pipe(withAssign(() => ({ | ||
offlineAtAtom: atom<number | undefined>(undefined, 'onLine.offlineAtAtom'), | ||
onlineAtAtom: atom<number | undefined>(undefined, 'onLine.onlineAtAtom'), | ||
}))) | ||
|
||
onConnect(onlineAtom, (ctx) => { | ||
onlineAtom(ctx, navigator.onLine) | ||
onEvent(ctx, window, 'online', () => { | ||
onlineAtom(ctx, true) | ||
onlineAtom.onlineAtAtom(ctx, Date.now()) | ||
}) | ||
onEvent(ctx, window, 'offline', () => { | ||
onlineAtom(ctx, false) | ||
onlineAtom.offlineAtAtom(ctx, Date.now()) | ||
}) | ||
}) | ||
|
||
return onlineAtom | ||
} |