-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Configure CozyClient to use CozyPouchLink
- Loading branch information
Showing
9 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { platformReactNative } from '/pouchdb/platformReactNative' | ||
|
||
import { default as PouchLink } from 'cozy-pouch-link' | ||
|
||
export const offlineDoctypes = [ | ||
'io.cozy.accounts', | ||
'io.cozy.apps', | ||
'io.cozy.contacts', | ||
'io.cozy.files', | ||
'io.cozy.jobs', | ||
'io.cozy.konnectors', | ||
'io.cozy.permissions', | ||
'io.cozy.settings', | ||
'io.cozy.tags', | ||
'io.cozy.triggers' | ||
] | ||
|
||
export const getLinks = () => { | ||
const pouchLinkOptions = { | ||
doctypes: offlineDoctypes, | ||
initialSync: true, | ||
platform: platformReactNative | ||
} | ||
|
||
const pouchLink = new PouchLink({ | ||
...pouchLinkOptions | ||
}) | ||
|
||
return [pouchLink] | ||
} |
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,34 @@ | ||
import EventEmitter from 'events' | ||
|
||
import { AppState, AppStateStatus, NativeEventSubscription } from 'react-native' | ||
|
||
import Minilog from 'cozy-minilog' | ||
|
||
const log = Minilog('🛋️ PlatormReactNative.appState') | ||
|
||
let appState = AppState.currentState | ||
let appStateHandler: NativeEventSubscription | undefined = undefined | ||
|
||
export const listenAppState = (eventEmitter: EventEmitter): void => { | ||
appStateHandler = AppState.addEventListener('change', nextAppState => { | ||
log.debug('🛋️ AppState event', nextAppState) | ||
if (isGoingToSleep(nextAppState)) { | ||
eventEmitter.emit('resume') | ||
} | ||
if (isGoingToWakeUp(nextAppState)) { | ||
eventEmitter.emit('pause') | ||
} | ||
|
||
appState = nextAppState | ||
}) | ||
} | ||
|
||
export const stopListeningAppState = (): void => { | ||
appStateHandler?.remove() | ||
} | ||
|
||
const isGoingToSleep = (nextAppState: AppStateStatus): boolean => | ||
Boolean(appState.match(/active/) && nextAppState === 'background') | ||
|
||
const isGoingToWakeUp = (nextAppState: AppStateStatus): boolean => | ||
Boolean(appState.match(/background/) && nextAppState === 'active') |
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,28 @@ | ||
import { EventEmitter } from 'events' | ||
|
||
import { listenAppState } from '/pouchdb/platformReactNative.appState' | ||
import { listenNetInfo } from '/pouchdb/platformReactNative.netInfo' | ||
|
||
export const pouchDbEmitter = new EventEmitter() | ||
|
||
const listenPouchEvents = (): void => { | ||
listenAppState(pouchDbEmitter) | ||
listenNetInfo(pouchDbEmitter) | ||
} | ||
|
||
listenPouchEvents() | ||
|
||
export const events = { | ||
addEventListener: ( | ||
eventName: string, | ||
handler: (...args: unknown[]) => void | ||
): void => { | ||
pouchDbEmitter.addListener(eventName, handler) | ||
}, | ||
removeEventListener: ( | ||
eventName: string, | ||
handler: (...args: unknown[]) => void | ||
): void => { | ||
pouchDbEmitter.removeListener(eventName, handler) | ||
} | ||
} |
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,5 @@ | ||
import { NetService } from '/libs/services/NetService' | ||
|
||
export const isOnline = (): Promise<boolean | null> => { | ||
return NetService.isConnected() | ||
} |
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,24 @@ | ||
import EventEmitter from 'events' | ||
|
||
import NetInfo, { NetInfoSubscription } from '@react-native-community/netinfo' | ||
|
||
import Minilog from 'cozy-minilog' | ||
|
||
const log = Minilog('🛋️ PlatormReactNative.netInfo') | ||
|
||
let netInfoHandler: NetInfoSubscription | undefined = undefined | ||
|
||
export const listenNetInfo = (eventEmitter: EventEmitter): void => { | ||
netInfoHandler = NetInfo.addEventListener(state => { | ||
log.debug('🛋️ NetInfo event', state.isConnected) | ||
if (state.isConnected) { | ||
eventEmitter.emit('online') | ||
} else { | ||
eventEmitter.emit('offline') | ||
} | ||
}) | ||
} | ||
|
||
export const stopListeningNetInfo = (): void => { | ||
netInfoHandler?.() | ||
} |
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,14 @@ | ||
import AsyncStorage from '@react-native-async-storage/async-storage' | ||
|
||
export const storage = { | ||
getItem: async (key: string): Promise<string | null> => { | ||
return AsyncStorage.getItem(key) | ||
}, | ||
setItem: async (key: string, value: string | undefined): Promise<void> => { | ||
if (value === undefined) return | ||
return AsyncStorage.setItem(key, value) | ||
}, | ||
removeItem: async (key: string): Promise<void> => { | ||
return AsyncStorage.removeItem(key) | ||
} | ||
} |
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,12 @@ | ||
import { events } from '/pouchdb/platformReactNative.events' | ||
import { isOnline } from '/pouchdb/platformReactNative.isOnline' | ||
import { storage } from '/pouchdb/platformReactNative.storage' | ||
import PouchDB from '/pouchdb/pouchdb' | ||
|
||
export const platformReactNative = { | ||
storage, | ||
events, | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
pouchAdapter: PouchDB, | ||
isOnline | ||
} |