import { SHARE } from './constants' ; import { isFunction } from './utils' ; interface AppCycles { [ key ] : Function [ ] ; } export const appCycles = { } ; param { string } cycle param { object } context param { ... any } args export function emit ( cycle , context ? , ... args ) { if ( Object . prototype . hasOwnProperty . call ( appCycles , cycle ) ) { const cycles = appCycles [ cycle ] ; if ( cycle === SHARE ) { args [ 0 ] . content = context ? cycles [ 0 ] . call ( context , args [ 1 ] ) : cycles [ 0 ] ( args [ 1 ] ) ; } else { cycles . forEach ( fn => { context ? fn . apply ( context , args ) : fn ( ... args ) ; } ) ; } } } param { string } cycle param { function } callback export function addAppLifeCycle ( cycle , callback ) { if ( isFunction ( callback ) ) { appCycles [ cycle ] = appCycles [ cycle ] || [ ] ; appCycles [ cycle ] . push ( callback ) ; } }