nativeApp.ts
TLDR
This file, nativeApp.ts
, is responsible for handling the import and export of user settings in the native app. It includes methods for importing native app settings, notifying changes in settings, and posting webkit messages.
Methods
importNativeAppSettings
This method imports the user settings from the NativeApp
object in the native app. If the NativeApp
object is undefined or does not have a valid settingsBlob
, the method returns without performing any action. If the NativeApp
object has a valid settingsBlob
, the method imports the user settings using the importUserSettingsBlob
function, determines the message type based on whether any keys were imported, and then posts a webkit message containing the message type, updated settings blob, and imported keys. Finally, it sets the settingsBlob
property of the NativeApp
object to null.
notifySettingsChanged
This method notifies changes in user settings by posting a webkit message containing the message type 'didChangeUserSettings'
, the updated settings blob obtained from the exportUserSettingsBlob
function, and the key
parameter provided.
postWebKitMessage
This internal helper function posts a webkit message using the window.webkit.messageHandlers.nativeApp.postMessage
method. It takes a generic message
parameter and passes it to the webkit message handler.
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {IAppWindow} from './types'
import {exportUserSettingsBlob, importUserSettingsBlob} from './userSettings'
declare interface INativeApp {
settingsBlob: string | null
}
declare const NativeApp: INativeApp
declare let window: IAppWindow
export function importNativeAppSettings(): void {
if (typeof NativeApp === 'undefined' || !NativeApp.settingsBlob) {
return
}
const importedKeys = importUserSettingsBlob(NativeApp.settingsBlob)
const messageType = importedKeys.length ? 'didImportUserSettings' : 'didNotImportUserSettings'
postWebKitMessage({type: messageType, settingsBlob: exportUserSettingsBlob(), keys: importedKeys})
NativeApp.settingsBlob = null
}
export function notifySettingsChanged(key: string): void {
postWebKitMessage({type: 'didChangeUserSettings', settingsBlob: exportUserSettingsBlob(), key})
}
function postWebKitMessage<T>(message: T) {
window.webkit?.messageHandlers.nativeApp?.postMessage(message)
}