@lightningjs/sdk
Version:
The Lightning-SDK helps you build great Lightning-based TV apps!
96 lines (85 loc) • 2.94 kB
JavaScript
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2020 Metrological
*
* 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.
*
* Code from: https://github.com/jashkenas/underscore is
* Copyright (c) 2009-2022 Jeremy Ashkenas, Julian Gonggrijp, and DocumentCloud and Investigative Reporters & Editors
* Licensed under the MIT License based off:
* http://unscriptable.com/2009/03/20/debouncing-javascript-methods/ which is:
* Copyright (c) 2007-2009 unscriptable.com and John M. Hann
* Licensed under the MIT License (with X11 advertising exception)
*/
export function getElmName(elm) {
return elm.ref || elm.constructor.name
}
/**
* Returns a function, that, as long as it continues to be invoked, will not
* be triggered. The function will be called after it stops being called for
* N milliseconds. If `immediate` is passed, trigger the function on the
* leading edge, instead of the trailing. The function also has a property 'clear'
* that is a function which will clear the timer to prevent previously scheduled executions.
*
* @source underscore.js
* @see http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
* @param {Function} function to wrap
* @param {Number} timeout in ms (`100`)
* @param {Boolean} whether to execute at the beginning (`false`)
* @api public
*/
export function debounce(func, wait, immediate) {
var timeout, args, context, timestamp, result
if (null == wait) wait = 100
function later() {
var last = Date.now() - timestamp
if (last < wait && last >= 0) {
timeout = setTimeout(later, wait - last)
} else {
timeout = null
if (!immediate) {
result = func.apply(context, args)
context = args = null
}
}
}
var debounced = function() {
context = this
args = arguments
timestamp = Date.now()
var callNow = immediate && !timeout
if (!timeout) timeout = setTimeout(later, wait)
if (callNow) {
result = func.apply(context, args)
context = args = null
}
return result
}
debounced.clear = function() {
if (timeout) {
clearTimeout(timeout)
timeout = null
}
}
debounced.flush = function() {
if (timeout) {
result = func.apply(context, args)
context = args = null
clearTimeout(timeout)
timeout = null
}
}
return debounced
}