import CallbackQueue = require ( './CallbackQueue' ) ; import core = require ( './interfaces' ) ; import has = require ( './has' ) ; declare var process ; has . add ( 'dom-mutationobserver' , function ( global ) : boolean { return has ( 'host-browser' ) && Boolean ( global . MutationObserver || global . WebKitMutationObserver ) ; } ) ; function noop ( ) { } var nextTick ; if ( typeof setImmediate !== 'undefined' && ( ! has ( 'host-node' ) || ( has ( 'host-node' ) && process . version . indexOf ( 'v0.10.' ) === 0 ) ) ) { nextTick = function ( callback ) : core . IHandle { var timer = setImmediate ( callback ) ; return { remove : function ( ) : void { this . remove = noop ; clearImmediate ( timer ) ; } } ; } ; } else if ( has ( 'host-node' ) ) { nextTick = function ( callback ) : core . IHandle { var removed = false ; process . nextTick ( function ( ) : void { if ( removed ) { return ; } callback ( ) ; } ) ; return { remove : function ( ) : void { this . remove = noop ; removed = true ; } } ; } ; } else { var queue = new CallbackQueue < ( ) => void > ( ) ; if ( has ( 'dom-mutationobserver' ) ) { nextTick = ( function ( ) : typeof nextTick { var MutationObserver = this . MutationObserver || this . WebKitMutationObserver ; var element = document . createElement ( 'div' ) ; var observer = new MutationObserver ( function ( ) : void { queue . drain ( ) ; } ) ; observer . observe ( element , { attributes : true } ) ; return function ( callback ) : core . IHandle { var handle = queue . add ( callback ) ; element . setAttribute ( 'drainQueue' , '1' ) ; return handle ; } ; } ) ( ) ; } else { nextTick = ( function ( ) : typeof nextTick { var timer ; return function ( callback ) : core . IHandle { var handle = queue . add ( callback ) ; if ( ! timer ) { timer = setTimeout ( function ( ) : void { clearTimeout ( timer ) ; timer = null ; queue . drain ( ) ; } , 0 ) ; } return handle ; } ; } ) ( ) ; } } export = nextTick ;