@turbox3d/shared
Advanced tools
Comparing version 1.1.4 to 1.1.5
@@ -9,4 +9,4 @@ import _typeof from "@babel/runtime/helpers/esm/typeof"; | ||
return value && typeof value.then === 'function'; | ||
} // {} | ||
} | ||
// {} | ||
export function isPlainObject(value) { | ||
@@ -20,6 +20,4 @@ if (value === null || _typeof(value) !== 'object') return false; | ||
*/ | ||
export function sortBy(arr, func) { | ||
var length = arr.length; | ||
for (var i = 0; i < length; i++) { | ||
@@ -31,3 +29,2 @@ for (var j = i + 1; j < length; j++) { | ||
var indicatorJ = func ? func(itemJ) : itemJ; | ||
if (indicatorJ < indicatorI) { | ||
@@ -39,3 +36,2 @@ arr[i] = itemJ; | ||
} | ||
return arr; | ||
@@ -48,3 +44,2 @@ } | ||
var index = array.indexOf(item); | ||
if (index > -1) { | ||
@@ -57,3 +52,2 @@ array.splice(index, 1); | ||
var index = array.indexOf(item); | ||
if (index > -1) { | ||
@@ -68,4 +62,4 @@ array.splice(index, 1); | ||
}); | ||
} // boolean, string, number, undefined, null | ||
} | ||
// boolean, string, number, undefined, null | ||
export function isPrimitive(value) { | ||
@@ -78,3 +72,2 @@ return value === null || _typeof(value) !== 'object' && typeof value !== 'function'; | ||
} | ||
return key.toString() + generateUUID(); | ||
@@ -89,3 +82,2 @@ } | ||
*/ | ||
export function nextTick(fn) { | ||
@@ -96,10 +88,9 @@ return fn ? promise.then(fn) : promise; | ||
return Object.prototype.hasOwnProperty.call(val, key); | ||
}; // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js | ||
}; | ||
// From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js | ||
export function is(x, y) { | ||
if (x === y) { | ||
return x !== 0 || 1 / x === 1 / y; | ||
} // eslint-disable-next-line no-self-compare | ||
} | ||
// eslint-disable-next-line no-self-compare | ||
return x !== x && y !== y; | ||
@@ -109,22 +100,19 @@ } | ||
function boundFn(a) { | ||
var l = arguments.length; // eslint-disable-next-line no-nested-ternary | ||
return l ? l > 1 // eslint-disable-next-line prefer-rest-params | ||
var l = arguments.length; | ||
// eslint-disable-next-line no-nested-ternary | ||
return l ? l > 1 | ||
// eslint-disable-next-line prefer-rest-params | ||
? fn.apply(ctx, arguments) : fn.call(ctx, a) : fn.call(ctx); | ||
} | ||
return boundFn; | ||
} // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js | ||
} | ||
// From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js | ||
export function shallowEqual(objA, objB) { | ||
if (is(objA, objB)) return true; | ||
if (_typeof(objA) !== 'object' || objA === null || _typeof(objB) !== 'object' || objB === null) { | ||
return false; | ||
} | ||
var keysA = Object.keys(objA); | ||
var keysB = Object.keys(objB); | ||
if (keysA.length !== keysB.length) return false; | ||
for (var i = 0; i < keysA.length; i++) { | ||
@@ -135,6 +123,6 @@ if (!objB.hasOwnProperty(keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) { | ||
} | ||
return true; | ||
} | ||
export var dummy = function dummy() {// | ||
export var dummy = function dummy() { | ||
// | ||
}; | ||
@@ -146,3 +134,2 @@ export function loadJSON(url) { | ||
xhr.open('GET', url, true); | ||
xhr.onreadystatechange = function () { | ||
@@ -153,3 +140,2 @@ if (xhr.readyState === 4 && xhr.status === 200) { | ||
}; | ||
xhr.send(null); | ||
@@ -162,11 +148,22 @@ }); | ||
} | ||
var re = new RegExp("([?&])".concat(key, "=.*?(&|$)"), 'i'); | ||
var separator = url.indexOf('?') !== -1 ? '&' : '?'; | ||
if (url.match(re)) { | ||
return url.replace(re, "$1".concat(key, "=").concat(value, "$2")); | ||
} | ||
return "".concat(url).concat(separator).concat(key, "=").concat(value); | ||
} | ||
export function getContextEnv() { | ||
return typeof window !== 'undefined' ? 'browser' : 'vm'; | ||
} | ||
export function getContextParam(key) { | ||
var value; | ||
var env = getContextEnv(); | ||
if (env === 'browser') { | ||
value = window[key]; | ||
} | ||
if (env === 'vm') { | ||
value = global[key]; | ||
} | ||
return value; | ||
} |
@@ -8,3 +8,2 @@ /** | ||
} | ||
if (funcs.length === 0) { | ||
@@ -15,7 +14,5 @@ return function (arg) { | ||
} | ||
if (funcs.length === 1) { | ||
return funcs[0]; | ||
} | ||
return funcs.reduce(function (a, b) { | ||
@@ -22,0 +19,0 @@ return function () { |
@@ -14,11 +14,9 @@ var now = Date.now; | ||
var trailing = true; | ||
if (typeof func !== 'function') { | ||
throw new TypeError('Expected a function'); | ||
} | ||
function invokeFunc(time) { | ||
var args = lastArgs; | ||
var thisArg = lastThis; // eslint-disable-next-line no-multi-assign | ||
var thisArg = lastThis; | ||
// eslint-disable-next-line no-multi-assign | ||
lastArgs = lastThis = undefined; | ||
@@ -29,12 +27,10 @@ lastInvokeTime = time; | ||
} | ||
function shouldInvoke(time) { | ||
var timeSinceLastCall = time - lastCallTime; | ||
var timeSinceLastInvoke = time - lastInvokeTime; // Either this is the first call, activity has stopped and we're at the | ||
var timeSinceLastInvoke = time - lastInvokeTime; | ||
// Either this is the first call, activity has stopped and we're at the | ||
// trailing edge, the system time has gone backwards and we're treating | ||
// it as the trailing edge, or we've hit the `maxWait` limit. | ||
return lastCallTime === undefined || timeSinceLastCall >= wait || timeSinceLastCall < 0 || maxing && timeSinceLastInvoke >= maxWait; | ||
} | ||
function remainingWait(time) { | ||
@@ -46,36 +42,29 @@ var timeSinceLastCall = time - lastCallTime; | ||
} | ||
function trailingEdge(time) { | ||
timerId = undefined; // Only invoke if we have `lastArgs` which means `func` has been | ||
timerId = undefined; | ||
// Only invoke if we have `lastArgs` which means `func` has been | ||
// debounced at least once. | ||
if (trailing && lastArgs) { | ||
return invokeFunc(time); | ||
} // eslint-disable-next-line no-multi-assign | ||
} | ||
// eslint-disable-next-line no-multi-assign | ||
lastArgs = lastThis = undefined; | ||
return result; | ||
} | ||
function timerExpired() { | ||
var time = now(); | ||
if (shouldInvoke(time)) { | ||
return trailingEdge(time); | ||
} // Restart the timer. | ||
} | ||
// Restart the timer. | ||
timerId = setTimeout(timerExpired, remainingWait(time)); | ||
} | ||
function leadingEdge(time) { | ||
// Reset any `maxWait` timer. | ||
lastInvokeTime = time; // Start the timer for the trailing edge. | ||
timerId = setTimeout(timerExpired, wait); // Invoke the leading edge. | ||
lastInvokeTime = time; | ||
// Start the timer for the trailing edge. | ||
timerId = setTimeout(timerExpired, wait); | ||
// Invoke the leading edge. | ||
return leading ? invokeFunc(time) : result; | ||
} | ||
function cancel() { | ||
@@ -85,20 +74,16 @@ if (timerId !== undefined) { | ||
} | ||
lastInvokeTime = 0; // eslint-disable-next-line no-multi-assign | ||
lastInvokeTime = 0; | ||
// eslint-disable-next-line no-multi-assign | ||
lastArgs = lastCallTime = lastThis = timerId = undefined; | ||
} | ||
function flush() { | ||
return timerId === undefined ? result : trailingEdge(now()); | ||
} | ||
function debounced() { | ||
var time = now(); | ||
var isInvoking = shouldInvoke(time); // eslint-disable-next-line prefer-rest-params | ||
var isInvoking = shouldInvoke(time); | ||
// eslint-disable-next-line prefer-rest-params | ||
lastArgs = arguments; | ||
lastThis = this; | ||
lastCallTime = time; | ||
if (isInvoking) { | ||
@@ -108,3 +93,2 @@ if (timerId === undefined) { | ||
} | ||
if (maxing) { | ||
@@ -117,10 +101,7 @@ // Handle invocations in a tight loop. | ||
} | ||
if (timerId === undefined) { | ||
timerId = setTimeout(timerExpired, wait); | ||
} | ||
return result; | ||
} | ||
debounced.cancel = cancel; | ||
@@ -127,0 +108,0 @@ debounced.flush = flush; |
import { toObjectTypeString, stateDecoRegExp } from './lang'; | ||
export function quacksLikeADecorator(args) { | ||
return (args.length === 2 || args.length === 3) && typeof args[1] === 'string'; | ||
} // prop could be decorated by @reactor | ||
} | ||
// prop could be decorated by @reactor | ||
export var canObserve = function canObserve(value) { | ||
return stateDecoRegExp.test(toObjectTypeString(value)); | ||
}; |
import _typeof from "@babel/runtime/helpers/esm/typeof"; | ||
function isMergeableObject(val) { | ||
@@ -7,13 +6,10 @@ var nonNullObject = val && _typeof(val) === 'object'; | ||
} | ||
function emptyTarget(val) { | ||
return Array.isArray(val) ? [] : {}; | ||
} | ||
function cloneIfNecessary(value, optionsArgument) { | ||
var clone = optionsArgument && optionsArgument.clone === true; // eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
var clone = optionsArgument && optionsArgument.clone === true; | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
return clone && isMergeableObject(value) ? deepMerge(emptyTarget(value), value, optionsArgument) : value; | ||
} | ||
function defaultArrayMerge(target, source, optionsArgument) { | ||
@@ -33,6 +29,4 @@ var destination = target.slice(); | ||
} | ||
function mergeObject(target, source, optionsArgument) { | ||
var destination = {}; | ||
if (isMergeableObject(target)) { | ||
@@ -43,3 +37,2 @@ Object.keys(target).forEach(function (key) { | ||
} | ||
Object.keys(source).forEach(function (key) { | ||
@@ -55,3 +48,2 @@ if (!isMergeableObject(source[key]) || !target[key]) { | ||
} | ||
export function deepMerge(target, source, optionsArgument) { | ||
@@ -63,8 +55,6 @@ var array = Array.isArray(source); | ||
var arrayMerge = options.arrayMerge || defaultArrayMerge; | ||
if (array) { | ||
return Array.isArray(target) ? arrayMerge(target, source, optionsArgument) : cloneIfNecessary(source, optionsArgument); | ||
} | ||
return mergeObject(target, source, optionsArgument); | ||
} |
@@ -6,4 +6,4 @@ export var OBFUSCATED_ERROR = 'An invariant failed, however the error is obfuscated because this is an production build.'; | ||
export function fail(message) { | ||
invariant(false, message); // eslint-disable-next-line no-throw-literal | ||
invariant(false, message); | ||
// eslint-disable-next-line no-throw-literal | ||
throw 'X'; | ||
@@ -10,0 +10,0 @@ } |
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; | ||
import _createClass from "@babel/runtime/helpers/esm/createClass"; | ||
var Emitter = /*#__PURE__*/function () { | ||
function Emitter() { | ||
_classCallCheck(this, Emitter); | ||
this.listeners = {}; | ||
} | ||
_createClass(Emitter, [{ | ||
@@ -22,7 +19,5 @@ key: "on", | ||
var _this = this; | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
var listeners = this.listeners[eventName]; | ||
@@ -44,10 +39,7 @@ if (!Array.isArray(listeners)) return; | ||
} | ||
this.listeners[eventName] = []; | ||
} | ||
}]); | ||
return Emitter; | ||
}(); | ||
export var emitter = new Emitter(); |
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; | ||
/** 判断点是否在矩形内 */ | ||
export function pointInRect(p, rect) { | ||
var _rect = _slicedToArray(rect, 2), | ||
r1 = _rect[0], | ||
r2 = _rect[1]; | ||
r1 = _rect[0], | ||
r2 = _rect[1]; | ||
var c1 = (p.x - r1.x) * (p.x - r2.x); | ||
@@ -19,15 +17,11 @@ var c2 = (p.y - r1.y) * (p.y - r2.y); | ||
*/ | ||
export function getRelativePositionFromEvent(vec, canvas) { | ||
var rect = canvas.getBoundingClientRect(); | ||
export function getRelativePositionFromEvent(vec, rect) { | ||
var point = { | ||
x: vec.x - rect.left, | ||
y: vec.y - rect.top | ||
x: vec.x - rect.x, | ||
y: vec.y - rect.y | ||
}; | ||
if (point.x <= 0 || point.y <= 0 || point.x > rect.width || point.y > rect.height) { | ||
return undefined; | ||
} | ||
return point; | ||
} |
import _typeof from "@babel/runtime/helpers/esm/typeof"; | ||
/** | ||
@@ -4,0 +3,0 @@ * Check if a string starts with $ or _ |
@@ -7,5 +7,3 @@ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; | ||
*/ | ||
export var TaskPriority; | ||
(function (TaskPriority) { | ||
@@ -19,3 +17,2 @@ /** | ||
*/ | ||
TaskPriority[TaskPriority["UpdateState"] = 1] = "UpdateState"; | ||
@@ -25,3 +22,2 @@ /** | ||
*/ | ||
TaskPriority[TaskPriority["RenderReact"] = 2] = "RenderReact"; | ||
@@ -33,3 +29,2 @@ /** | ||
*/ | ||
TaskPriority[TaskPriority["RenderScene"] = -1] = "RenderScene"; | ||
@@ -43,6 +38,4 @@ })(TaskPriority || (TaskPriority = {})); | ||
// 存储的任务队列 | ||
var tasks = []; // 是否注册了任务待执行 | ||
var tasks = []; | ||
// 是否注册了任务待执行 | ||
var readyToExecute = false; | ||
@@ -52,3 +45,2 @@ /** | ||
*/ | ||
function executeTask() { | ||
@@ -59,6 +51,6 @@ if (readyToExecute) { | ||
return task.priority; | ||
}); // 清空存储的数据 | ||
tasks = []; // 按顺序执行当前任务队列 | ||
}); | ||
// 清空存储的数据 | ||
tasks = []; | ||
// 按顺序执行当前任务队列 | ||
currentTasks.forEach(function (task) { | ||
@@ -76,10 +68,6 @@ var fn = task.fn; | ||
*/ | ||
function pushTask(fn, priority, maxFPS) { | ||
tasks.push(Task.createTask(fn, priority)); | ||
if (!readyToExecute) { | ||
readyToExecute = true; | ||
if (maxFPS === 60) { | ||
@@ -97,4 +85,2 @@ requestAnimationFrame(executeTask); | ||
*/ | ||
export function throttleInAFrame(func) { | ||
@@ -105,3 +91,2 @@ var priority = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : TaskPriority.UserAction; | ||
var isPending = false; | ||
function execute() { | ||
@@ -111,3 +96,2 @@ isPending = false; | ||
} | ||
return function () { | ||
@@ -117,5 +101,3 @@ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
} | ||
cacheArgs = args; | ||
if (!isPending) { | ||
@@ -122,0 +104,0 @@ isPending = true; |
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; | ||
import _createClass from "@babel/runtime/helpers/esm/createClass"; | ||
var Task = /*#__PURE__*/function () { | ||
@@ -8,7 +7,5 @@ // eslint-disable-next-line no-useless-constructor | ||
_classCallCheck(this, Task); | ||
this.fn = fn; | ||
this.priority = priority; | ||
} | ||
_createClass(Task, null, [{ | ||
@@ -20,6 +17,4 @@ key: "createTask", | ||
}]); | ||
return Task; | ||
}(); | ||
export { Task }; |
@@ -6,6 +6,4 @@ var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''); | ||
*/ | ||
export function generateUUID() { | ||
var rnd = 0; | ||
for (var i = 0; i < 36; i++) { | ||
@@ -20,3 +18,2 @@ if (i === 8 || i === 13 || i === 18 || i === 23) { | ||
} | ||
var r = rnd & 0xf; | ||
@@ -27,4 +24,3 @@ rnd >>= 4; | ||
} | ||
return uuid.join(''); | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -12,3 +11,6 @@ value: true | ||
exports.convert2UniqueString = convert2UniqueString; | ||
exports.hasOwn = exports.dummy = exports.deduplicate = void 0; | ||
exports.dummy = exports.deduplicate = void 0; | ||
exports.getContextEnv = getContextEnv; | ||
exports.getContextParam = getContextParam; | ||
exports.hasOwn = void 0; | ||
exports.includes = includes; | ||
@@ -26,18 +28,12 @@ exports.is = is; | ||
exports.updateQueryStringParameter = updateQueryStringParameter; | ||
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); | ||
var _lang = require("./lang"); | ||
var _uuid = require("./uuid"); | ||
function isObject(value) { | ||
return value !== null && (0, _typeof2["default"])(value) === 'object'; | ||
} | ||
function isPromise(value) { | ||
return value && typeof value.then === 'function'; | ||
} // {} | ||
} | ||
// {} | ||
function isPlainObject(value) { | ||
@@ -51,7 +47,4 @@ if (value === null || (0, _typeof2["default"])(value) !== 'object') return false; | ||
*/ | ||
function sortBy(arr, func) { | ||
var length = arr.length; | ||
for (var i = 0; i < length; i++) { | ||
@@ -63,3 +56,2 @@ for (var j = i + 1; j < length; j++) { | ||
var indicatorJ = func ? func(itemJ) : itemJ; | ||
if (indicatorJ < indicatorI) { | ||
@@ -71,13 +63,9 @@ arr[i] = itemJ; | ||
} | ||
return arr; | ||
} | ||
function includes(array, item) { | ||
return array.indexOf(item) > -1; | ||
} | ||
function remove(array, item) { | ||
var index = array.indexOf(item); | ||
if (index > -1) { | ||
@@ -87,7 +75,5 @@ array.splice(index, 1); | ||
} | ||
function batchRemove(array, items) { | ||
items.forEach(function (item) { | ||
var index = array.indexOf(item); | ||
if (index > -1) { | ||
@@ -98,3 +84,2 @@ array.splice(index, 1); | ||
} | ||
function batchRemoveFromSet(set, items) { | ||
@@ -104,9 +89,7 @@ items.forEach(function (item) { | ||
}); | ||
} // boolean, string, number, undefined, null | ||
} | ||
// boolean, string, number, undefined, null | ||
function isPrimitive(value) { | ||
return value === null || (0, _typeof2["default"])(value) !== 'object' && typeof value !== 'function'; | ||
} | ||
function convert2UniqueString(key) { | ||
@@ -116,10 +99,7 @@ if (!(0, _lang.isSymbol)(key)) { | ||
} | ||
return key.toString() + (0, _uuid.generateUUID)(); | ||
} | ||
var deduplicate = function deduplicate(array) { | ||
return Array.from(new Set(array)); | ||
}; | ||
exports.deduplicate = deduplicate; | ||
@@ -130,46 +110,36 @@ var promise = Promise.resolve(); | ||
*/ | ||
function nextTick(fn) { | ||
return fn ? promise.then(fn) : promise; | ||
} | ||
var hasOwn = function hasOwn(val, key) { | ||
return Object.prototype.hasOwnProperty.call(val, key); | ||
}; // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js | ||
}; | ||
// From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js | ||
exports.hasOwn = hasOwn; | ||
function is(x, y) { | ||
if (x === y) { | ||
return x !== 0 || 1 / x === 1 / y; | ||
} // eslint-disable-next-line no-self-compare | ||
} | ||
// eslint-disable-next-line no-self-compare | ||
return x !== x && y !== y; | ||
} | ||
function bind(fn, ctx) { | ||
function boundFn(a) { | ||
var l = arguments.length; // eslint-disable-next-line no-nested-ternary | ||
return l ? l > 1 // eslint-disable-next-line prefer-rest-params | ||
var l = arguments.length; | ||
// eslint-disable-next-line no-nested-ternary | ||
return l ? l > 1 | ||
// eslint-disable-next-line prefer-rest-params | ||
? fn.apply(ctx, arguments) : fn.call(ctx, a) : fn.call(ctx); | ||
} | ||
return boundFn; | ||
} // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js | ||
} | ||
// From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js | ||
function shallowEqual(objA, objB) { | ||
if (is(objA, objB)) return true; | ||
if ((0, _typeof2["default"])(objA) !== 'object' || objA === null || (0, _typeof2["default"])(objB) !== 'object' || objB === null) { | ||
return false; | ||
} | ||
var keysA = Object.keys(objA); | ||
var keysB = Object.keys(objB); | ||
if (keysA.length !== keysB.length) return false; | ||
for (var i = 0; i < keysA.length; i++) { | ||
@@ -180,11 +150,8 @@ if (!objB.hasOwnProperty(keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) { | ||
} | ||
return true; | ||
} | ||
var dummy = function dummy() {// | ||
var dummy = function dummy() { | ||
// | ||
}; | ||
exports.dummy = dummy; | ||
function loadJSON(url) { | ||
@@ -195,3 +162,2 @@ return new Promise(function (resolve) { | ||
xhr.open('GET', url, true); | ||
xhr.onreadystatechange = function () { | ||
@@ -202,7 +168,5 @@ if (xhr.readyState === 4 && xhr.status === 200) { | ||
}; | ||
xhr.send(null); | ||
}); | ||
} | ||
function updateQueryStringParameter(url, key, value) { | ||
@@ -212,11 +176,22 @@ if (!value) { | ||
} | ||
var re = new RegExp("([?&])".concat(key, "=.*?(&|$)"), 'i'); | ||
var separator = url.indexOf('?') !== -1 ? '&' : '?'; | ||
if (url.match(re)) { | ||
return url.replace(re, "$1".concat(key, "=").concat(value, "$2")); | ||
} | ||
return "".concat(url).concat(separator).concat(key, "=").concat(value); | ||
} | ||
function getContextEnv() { | ||
return typeof window !== 'undefined' ? 'browser' : 'vm'; | ||
} | ||
function getContextParam(key) { | ||
var value; | ||
var env = getContextEnv(); | ||
if (env === 'browser') { | ||
value = window[key]; | ||
} | ||
if (env === 'vm') { | ||
value = global[key]; | ||
} | ||
return value; | ||
} |
@@ -7,3 +7,2 @@ "use strict"; | ||
exports.compose = compose; | ||
/** | ||
@@ -16,3 +15,2 @@ * from redux compose | ||
} | ||
if (funcs.length === 0) { | ||
@@ -23,7 +21,5 @@ return function (arg) { | ||
} | ||
if (funcs.length === 1) { | ||
return funcs[0]; | ||
} | ||
return funcs.reduce(function (a, b) { | ||
@@ -30,0 +26,0 @@ return function () { |
@@ -8,3 +8,2 @@ "use strict"; | ||
var now = Date.now; | ||
function debounce(func) { | ||
@@ -22,11 +21,9 @@ var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 300; | ||
var trailing = true; | ||
if (typeof func !== 'function') { | ||
throw new TypeError('Expected a function'); | ||
} | ||
function invokeFunc(time) { | ||
var args = lastArgs; | ||
var thisArg = lastThis; // eslint-disable-next-line no-multi-assign | ||
var thisArg = lastThis; | ||
// eslint-disable-next-line no-multi-assign | ||
lastArgs = lastThis = undefined; | ||
@@ -37,12 +34,10 @@ lastInvokeTime = time; | ||
} | ||
function shouldInvoke(time) { | ||
var timeSinceLastCall = time - lastCallTime; | ||
var timeSinceLastInvoke = time - lastInvokeTime; // Either this is the first call, activity has stopped and we're at the | ||
var timeSinceLastInvoke = time - lastInvokeTime; | ||
// Either this is the first call, activity has stopped and we're at the | ||
// trailing edge, the system time has gone backwards and we're treating | ||
// it as the trailing edge, or we've hit the `maxWait` limit. | ||
return lastCallTime === undefined || timeSinceLastCall >= wait || timeSinceLastCall < 0 || maxing && timeSinceLastInvoke >= maxWait; | ||
} | ||
function remainingWait(time) { | ||
@@ -54,36 +49,29 @@ var timeSinceLastCall = time - lastCallTime; | ||
} | ||
function trailingEdge(time) { | ||
timerId = undefined; // Only invoke if we have `lastArgs` which means `func` has been | ||
timerId = undefined; | ||
// Only invoke if we have `lastArgs` which means `func` has been | ||
// debounced at least once. | ||
if (trailing && lastArgs) { | ||
return invokeFunc(time); | ||
} // eslint-disable-next-line no-multi-assign | ||
} | ||
// eslint-disable-next-line no-multi-assign | ||
lastArgs = lastThis = undefined; | ||
return result; | ||
} | ||
function timerExpired() { | ||
var time = now(); | ||
if (shouldInvoke(time)) { | ||
return trailingEdge(time); | ||
} // Restart the timer. | ||
} | ||
// Restart the timer. | ||
timerId = setTimeout(timerExpired, remainingWait(time)); | ||
} | ||
function leadingEdge(time) { | ||
// Reset any `maxWait` timer. | ||
lastInvokeTime = time; // Start the timer for the trailing edge. | ||
timerId = setTimeout(timerExpired, wait); // Invoke the leading edge. | ||
lastInvokeTime = time; | ||
// Start the timer for the trailing edge. | ||
timerId = setTimeout(timerExpired, wait); | ||
// Invoke the leading edge. | ||
return leading ? invokeFunc(time) : result; | ||
} | ||
function cancel() { | ||
@@ -93,20 +81,16 @@ if (timerId !== undefined) { | ||
} | ||
lastInvokeTime = 0; // eslint-disable-next-line no-multi-assign | ||
lastInvokeTime = 0; | ||
// eslint-disable-next-line no-multi-assign | ||
lastArgs = lastCallTime = lastThis = timerId = undefined; | ||
} | ||
function flush() { | ||
return timerId === undefined ? result : trailingEdge(now()); | ||
} | ||
function debounced() { | ||
var time = now(); | ||
var isInvoking = shouldInvoke(time); // eslint-disable-next-line prefer-rest-params | ||
var isInvoking = shouldInvoke(time); | ||
// eslint-disable-next-line prefer-rest-params | ||
lastArgs = arguments; | ||
lastThis = this; | ||
lastCallTime = time; | ||
if (isInvoking) { | ||
@@ -116,3 +100,2 @@ if (timerId === undefined) { | ||
} | ||
if (maxing) { | ||
@@ -125,10 +108,7 @@ // Handle invocations in a tight loop. | ||
} | ||
if (timerId === undefined) { | ||
timerId = setTimeout(timerExpired, wait); | ||
} | ||
return result; | ||
} | ||
debounced.cancel = cancel; | ||
@@ -135,0 +115,0 @@ debounced.flush = flush; |
@@ -8,14 +8,10 @@ "use strict"; | ||
exports.quacksLikeADecorator = quacksLikeADecorator; | ||
var _lang = require("./lang"); | ||
function quacksLikeADecorator(args) { | ||
return (args.length === 2 || args.length === 3) && typeof args[1] === 'string'; | ||
} // prop could be decorated by @reactor | ||
} | ||
// prop could be decorated by @reactor | ||
var canObserve = function canObserve(value) { | ||
return _lang.stateDecoRegExp.test((0, _lang.toObjectTypeString)(value)); | ||
}; | ||
exports.canObserve = canObserve; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -9,5 +8,3 @@ value: true | ||
exports.deepMerge = deepMerge; | ||
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); | ||
function isMergeableObject(val) { | ||
@@ -17,13 +14,10 @@ var nonNullObject = val && (0, _typeof2["default"])(val) === 'object'; | ||
} | ||
function emptyTarget(val) { | ||
return Array.isArray(val) ? [] : {}; | ||
} | ||
function cloneIfNecessary(value, optionsArgument) { | ||
var clone = optionsArgument && optionsArgument.clone === true; // eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
var clone = optionsArgument && optionsArgument.clone === true; | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
return clone && isMergeableObject(value) ? deepMerge(emptyTarget(value), value, optionsArgument) : value; | ||
} | ||
function defaultArrayMerge(target, source, optionsArgument) { | ||
@@ -43,6 +37,4 @@ var destination = target.slice(); | ||
} | ||
function mergeObject(target, source, optionsArgument) { | ||
var destination = {}; | ||
if (isMergeableObject(target)) { | ||
@@ -53,3 +45,2 @@ Object.keys(target).forEach(function (key) { | ||
} | ||
Object.keys(source).forEach(function (key) { | ||
@@ -65,3 +56,2 @@ if (!isMergeableObject(source[key]) || !target[key]) { | ||
} | ||
function deepMerge(target, source, optionsArgument) { | ||
@@ -73,8 +63,6 @@ var array = Array.isArray(source); | ||
var arrayMerge = options.arrayMerge || defaultArrayMerge; | ||
if (array) { | ||
return Array.isArray(target) ? arrayMerge(target, source, optionsArgument) : cloneIfNecessary(source, optionsArgument); | ||
} | ||
return mergeObject(target, source, optionsArgument); | ||
} |
@@ -12,15 +12,12 @@ "use strict"; | ||
exports.OBFUSCATED_ERROR = OBFUSCATED_ERROR; | ||
function invariant(check, message) { | ||
if (!check) throw new Error("[turbox]: ".concat(message || OBFUSCATED_ERROR)); | ||
} | ||
function fail(message) { | ||
invariant(false, message); // eslint-disable-next-line no-throw-literal | ||
invariant(false, message); | ||
// eslint-disable-next-line no-throw-literal | ||
throw 'X'; | ||
} | ||
function warn(message) { | ||
console.warn("[turbox]: ".concat(message)); | ||
} |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -9,7 +8,4 @@ value: true | ||
exports.emitter = void 0; | ||
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); | ||
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); | ||
var Emitter = /*#__PURE__*/function () { | ||
@@ -20,3 +16,2 @@ function Emitter() { | ||
} | ||
(0, _createClass2["default"])(Emitter, [{ | ||
@@ -33,7 +28,5 @@ key: "on", | ||
var _this = this; | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
var listeners = this.listeners[eventName]; | ||
@@ -55,3 +48,2 @@ if (!Array.isArray(listeners)) return; | ||
} | ||
this.listeners[eventName] = []; | ||
@@ -62,4 +54,3 @@ } | ||
}(); | ||
var emitter = new Emitter(); | ||
exports.emitter = emitter; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -10,11 +9,8 @@ value: true | ||
exports.pointInRect = pointInRect; | ||
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); | ||
/** 判断点是否在矩形内 */ | ||
function pointInRect(p, rect) { | ||
var _rect = (0, _slicedToArray2["default"])(rect, 2), | ||
r1 = _rect[0], | ||
r2 = _rect[1]; | ||
r1 = _rect[0], | ||
r2 = _rect[1]; | ||
var c1 = (p.x - r1.x) * (p.x - r2.x); | ||
@@ -30,16 +26,11 @@ var c2 = (p.y - r1.y) * (p.y - r2.y); | ||
*/ | ||
function getRelativePositionFromEvent(vec, canvas) { | ||
var rect = canvas.getBoundingClientRect(); | ||
function getRelativePositionFromEvent(vec, rect) { | ||
var point = { | ||
x: vec.x - rect.left, | ||
y: vec.y - rect.top | ||
x: vec.x - rect.x, | ||
y: vec.y - rect.y | ||
}; | ||
if (point.x <= 0 || point.y <= 0 || point.x > rect.width || point.y > rect.height) { | ||
return undefined; | ||
} | ||
return point; | ||
} |
@@ -6,5 +6,3 @@ "use strict"; | ||
}); | ||
var _uuid = require("./uuid"); | ||
Object.keys(_uuid).forEach(function (key) { | ||
@@ -20,5 +18,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _type = require("./type"); | ||
Object.keys(_type).forEach(function (key) { | ||
@@ -34,5 +30,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _task = require("./task"); | ||
Object.keys(_task).forEach(function (key) { | ||
@@ -48,5 +42,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _rafTask = require("./rafTask"); | ||
Object.keys(_rafTask).forEach(function (key) { | ||
@@ -62,5 +54,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _lang = require("./lang"); | ||
Object.keys(_lang).forEach(function (key) { | ||
@@ -76,5 +66,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _graphic = require("./graphic"); | ||
Object.keys(_graphic).forEach(function (key) { | ||
@@ -90,5 +78,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _event = require("./event"); | ||
Object.keys(_event).forEach(function (key) { | ||
@@ -104,5 +90,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _error = require("./error"); | ||
Object.keys(_error).forEach(function (key) { | ||
@@ -118,5 +102,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _deepMerge = require("./deep-merge"); | ||
Object.keys(_deepMerge).forEach(function (key) { | ||
@@ -132,5 +114,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _decorator = require("./decorator"); | ||
Object.keys(_decorator).forEach(function (key) { | ||
@@ -146,5 +126,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _debounce = require("./debounce"); | ||
Object.keys(_debounce).forEach(function (key) { | ||
@@ -160,5 +138,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _compose = require("./compose"); | ||
Object.keys(_compose).forEach(function (key) { | ||
@@ -174,5 +150,3 @@ if (key === "default" || key === "__esModule") return; | ||
}); | ||
var _common = require("./common"); | ||
Object.keys(_common).forEach(function (key) { | ||
@@ -179,0 +153,0 @@ if (key === "default" || key === "__esModule") return; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -13,5 +12,3 @@ value: true | ||
exports.toObjectTypeString = exports.stateDecoRegExp = void 0; | ||
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); | ||
/** | ||
@@ -24,21 +21,16 @@ * Check if a string starts with $ or _ | ||
} | ||
function isSupportProxy() { | ||
return typeof Proxy !== 'undefined'; | ||
} | ||
function isSymbol(symbol) { | ||
return (0, _typeof2["default"])(symbol) === 'symbol'; | ||
} | ||
function isSupportSymbol() { | ||
return typeof Symbol !== 'undefined'; | ||
} | ||
var toObjectTypeString = function toObjectTypeString(value) { | ||
return Object.prototype.toString.call(value); | ||
}; | ||
exports.toObjectTypeString = toObjectTypeString; | ||
var stateDecoRegExp = /^\[object (?:Object|Array|Map|Set|WeakMap|WeakSet)\]$/; | ||
exports.stateDecoRegExp = stateDecoRegExp; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -10,9 +9,5 @@ value: true | ||
exports.throttleInAFrame = throttleInAFrame; | ||
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); | ||
var _task = require("./task"); | ||
var _common = require("./common"); | ||
/** | ||
@@ -23,3 +18,2 @@ * 任务优先级。数据越小,优先级越高 | ||
exports.TaskPriority = TaskPriority; | ||
(function (TaskPriority) { | ||
@@ -33,3 +27,2 @@ /** | ||
*/ | ||
TaskPriority[TaskPriority["UpdateState"] = 1] = "UpdateState"; | ||
@@ -39,3 +32,2 @@ /** | ||
*/ | ||
TaskPriority[TaskPriority["RenderReact"] = 2] = "RenderReact"; | ||
@@ -47,3 +39,2 @@ /** | ||
*/ | ||
TaskPriority[TaskPriority["RenderScene"] = -1] = "RenderScene"; | ||
@@ -57,6 +48,4 @@ })(TaskPriority || (exports.TaskPriority = TaskPriority = {})); | ||
// 存储的任务队列 | ||
var tasks = []; // 是否注册了任务待执行 | ||
var tasks = []; | ||
// 是否注册了任务待执行 | ||
var readyToExecute = false; | ||
@@ -66,3 +55,2 @@ /** | ||
*/ | ||
function executeTask() { | ||
@@ -73,6 +61,6 @@ if (readyToExecute) { | ||
return task.priority; | ||
}); // 清空存储的数据 | ||
tasks = []; // 按顺序执行当前任务队列 | ||
}); | ||
// 清空存储的数据 | ||
tasks = []; | ||
// 按顺序执行当前任务队列 | ||
currentTasks.forEach(function (task) { | ||
@@ -90,10 +78,6 @@ var fn = task.fn; | ||
*/ | ||
function pushTask(fn, priority, maxFPS) { | ||
tasks.push(_task.Task.createTask(fn, priority)); | ||
if (!readyToExecute) { | ||
readyToExecute = true; | ||
if (maxFPS === 60) { | ||
@@ -111,4 +95,2 @@ requestAnimationFrame(executeTask); | ||
*/ | ||
function throttleInAFrame(func) { | ||
@@ -119,3 +101,2 @@ var priority = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : TaskPriority.UserAction; | ||
var isPending = false; | ||
function execute() { | ||
@@ -125,3 +106,2 @@ isPending = false; | ||
} | ||
return function () { | ||
@@ -131,5 +111,3 @@ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
} | ||
cacheArgs = args; | ||
if (!isPending) { | ||
@@ -136,0 +114,0 @@ isPending = true; |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -9,7 +8,4 @@ value: true | ||
exports.Task = void 0; | ||
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); | ||
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); | ||
var Task = /*#__PURE__*/function () { | ||
@@ -22,3 +18,2 @@ // eslint-disable-next-line no-useless-constructor | ||
} | ||
(0, _createClass2["default"])(Task, null, [{ | ||
@@ -32,3 +27,2 @@ key: "createTask", | ||
}(); | ||
exports.Task = Task; |
@@ -7,5 +7,4 @@ "use strict"; | ||
exports.getValue = getValue; | ||
function getValue(obj, key) { | ||
return obj[key]; | ||
} |
@@ -12,6 +12,4 @@ "use strict"; | ||
*/ | ||
function generateUUID() { | ||
var rnd = 0; | ||
for (var i = 0; i < 36; i++) { | ||
@@ -26,3 +24,2 @@ if (i === 8 || i === 13 || i === 18 || i === 23) { | ||
} | ||
var r = rnd & 0xf; | ||
@@ -33,4 +30,3 @@ rnd >>= 4; | ||
} | ||
return uuid.join(''); | ||
} |
{ | ||
"name": "@turbox3d/shared", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"description": "turbox shared internal utility", | ||
@@ -65,3 +65,3 @@ "main": "./lib/index.js", | ||
}, | ||
"gitHead": "5dbc698db60060bbe7207a3e96a86bd4067de248" | ||
"gitHead": "cc344341bde5c462382b7cda62d07955d2d7f1d3" | ||
} |
@@ -163,1 +163,17 @@ import { isSymbol } from './lang'; | ||
} | ||
export function getContextEnv() { | ||
return typeof window !== 'undefined' ? 'browser' : 'vm'; | ||
} | ||
export function getContextParam(key: string) { | ||
let value: any; | ||
const env = getContextEnv(); | ||
if (env === 'browser') { | ||
value = window[key]; | ||
} | ||
if (env === 'vm') { | ||
value = global[key]; | ||
} | ||
return value; | ||
} |
@@ -20,2 +20,9 @@ export interface Vec2 { | ||
interface ICanvasRect { | ||
width: number; | ||
height: number; | ||
x: number; | ||
y: number; | ||
} | ||
/** | ||
@@ -27,7 +34,6 @@ * 获得点击位置相对于 canvas 的坐标 | ||
*/ | ||
export function getRelativePositionFromEvent(vec: Vec2, canvas: HTMLCanvasElement) { | ||
const rect = canvas.getBoundingClientRect(); | ||
export function getRelativePositionFromEvent(vec: Vec2, rect: ICanvasRect) { | ||
const point = { | ||
x: vec.x - rect.left, | ||
y: vec.y - rect.top, | ||
x: vec.x - rect.x, | ||
y: vec.y - rect.y, | ||
}; | ||
@@ -34,0 +40,0 @@ if (point.x <= 0 || point.y <= 0 || point.x > rect.width || point.y > rect.height) { |
@@ -26,1 +26,3 @@ export declare function isObject(value: any): boolean; | ||
export declare function updateQueryStringParameter(url: string, key: string, value: string): string; | ||
export declare function getContextEnv(): "browser" | "vm"; | ||
export declare function getContextParam(key: string): any; |
@@ -12,2 +12,8 @@ export interface Vec2 { | ||
export declare function pointInRect(p: Vec2, rect: Vec2[]): boolean; | ||
interface ICanvasRect { | ||
width: number; | ||
height: number; | ||
x: number; | ||
y: number; | ||
} | ||
/** | ||
@@ -19,5 +25,6 @@ * 获得点击位置相对于 canvas 的坐标 | ||
*/ | ||
export declare function getRelativePositionFromEvent(vec: Vec2, canvas: HTMLCanvasElement): { | ||
export declare function getRelativePositionFromEvent(vec: Vec2, rect: ICanvasRect): { | ||
x: number; | ||
y: number; | ||
} | undefined; | ||
export {}; |
75002
2337