From 7113bd30cd3b01473bcca36f7f2cac781db6358b Mon Sep 17 00:00:00 2001 From: kasperskei Date: Fri, 11 Oct 2024 17:31:11 +0300 Subject: [PATCH] feat(web): add online atom --- package-lock.json | 4 +++- packages/web/package.json | 4 +++- packages/web/src/event.ts | 2 ++ packages/web/src/index.ts | 1 + packages/web/src/online.ts | 45 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 packages/web/src/online.ts diff --git a/package-lock.json b/package-lock.json index ee550b9bd..94fe4d7a8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11657,7 +11657,9 @@ "version": "3.5.3", "license": "MIT", "dependencies": { - "@reatom/core": ">=3.5.0" + "@reatom/core": ">=3.5.0", + "@reatom/hooks": "^3.2.0", + "@reatom/primitives": "^3.5.0" } }, "packages/web-fetch": { diff --git a/packages/web/package.json b/packages/web/package.json index 9ae1d52cc..b536536ff 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -27,7 +27,9 @@ "test:watch": "tsx watch src/index.test.ts" }, "dependencies": { - "@reatom/core": ">=3.5.0" + "@reatom/core": ">=3.5.0", + "@reatom/hooks": "^3.2.0", + "@reatom/primitives": "^3.5.0" }, "author": "artalar", "maintainers": [ diff --git a/packages/web/src/event.ts b/packages/web/src/event.ts index 793dcf3f8..3b20fa41e 100644 --- a/packages/web/src/event.ts +++ b/packages/web/src/event.ts @@ -31,12 +31,14 @@ export const onEvent: { target: Target, type: Type, cb: (value: EventOfTarget) => any, + options?: AddEventListenerOptions ): Unsubscribe ( ctx: Ctx, target: EventTarget, type: string, cb: (value: Event) => any, + options?: AddEventListenerOptions ): Unsubscribe } = (ctx: Ctx, target: EventTarget, type: string, listener: Fn) => { let un diff --git a/packages/web/src/index.ts b/packages/web/src/index.ts index c475080bb..f2ff23a11 100644 --- a/packages/web/src/index.ts +++ b/packages/web/src/index.ts @@ -1 +1,2 @@ export * from './event' +export * from './online' diff --git a/packages/web/src/online.ts b/packages/web/src/online.ts new file mode 100644 index 000000000..abfd8a1a4 --- /dev/null +++ b/packages/web/src/online.ts @@ -0,0 +1,45 @@ +import { + atom, + type Atom, +} from '@reatom/core' +import { + onConnect, +} from '@reatom/hooks' +import { + withAssign, +} from '@reatom/primitives' +import { + onEvent, +} from './event' + +type OnlineAtom = Atom & { + /** Time stamp of transition to online mode. */ + offlineAtAtom: Atom + /** Time stamp of transition to offline mode. */ + onlineAtAtom: Atom +} + +/** + * @note https://issues.chromium.org/issues/338514113 + */ +export const createOnlineAtom = (): OnlineAtom => { + const onlineAtom = atom(navigator.onLine, 'onLine') + .pipe(withAssign(() => ({ + offlineAtAtom: atom(undefined), + onlineAtAtom: atom(undefined), + }))) + + 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 +}