@firebolt-js/sdk
Version:
The Firebolt JS SDK
123 lines (107 loc) • 2.66 kB
JavaScript
/*
* Copyright 2021 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
import Transport from '../Transport/index.mjs'
import Events from '../Events/index.mjs'
import { registerEvents } from '../Events/index.mjs'
import { ready as logReady } from '../Metrics/index.mjs'
import { prioritize } from '../Events/index.mjs'
registerEvents('Lifecycle', [
'background',
'foreground',
'inactive',
'suspended',
'unloading',
])
export const store = {
_current: 'initializing',
get current() {
return this._current
},
}
async function ready() {
let readyRes
await prioritize('Lifecycle', (event, value) => {
store._current = event
})
readyRes = await Transport.send('lifecycle', 'ready', {})
setTimeout((_) => {
logReady()
})
return readyRes
}
// Methods
function clear(...args) {
return Events.clear('Lifecycle', ...args)
}
function close(reason) {
const transforms = null
return Transport.send('Lifecycle', 'close', { reason }, transforms)
}
function listen(...args) {
return Events.listen('Lifecycle', ...args)
}
function once(...args) {
return Events.once('Lifecycle', ...args)
}
function state() {
return store.current
}
function finished() {
if (store.current === 'unloading') {
return Transport.send('lifecycle', 'finished')
} else {
throw 'Cannot call finished() except when in the unloading transition'
}
}
// public API
export default {
Events: {
INACTIVE: 'inactive',
FOREGROUND: 'foreground',
BACKGROUND: 'background',
SUSPENDED: 'suspended',
UNLOADING: 'unloading',
},
/**
* The application close reason
*/
CloseReason: {
REMOTE_BUTTON: 'remoteButton',
USER_EXIT: 'userExit',
DONE: 'done',
ERROR: 'error',
},
/**
* The application lifecycle state
*/
LifecycleState: {
INITIALIZING: 'initializing',
INACTIVE: 'inactive',
FOREGROUND: 'foreground',
BACKGROUND: 'background',
UNLOADING: 'unloading',
SUSPENDED: 'suspended',
},
ready,
state,
finished,
clear,
close,
listen,
once,
}