UNPKG

12.2 kBSource Map (JSON)View Raw
1{"version":3,"file":"patella.iife.min.js","sources":["../lib/util.js","../lib/computed.js","../lib/reactive.js","../lib/dispose.js","../lib/ignore.js"],"sourcesContent":["// Global object/function references\nvar _Object = Object;\nvar _Object_hasOwnProperty = _Object.hasOwnProperty;\nvar _Array_isArray = Array.isArray;\nvar _Error = Error;\nvar _TypeError = TypeError;\n\n/** Reference to global Object.defineProperty */\nexport var defineProperty = _Object.defineProperty;\n\n/**\n * Checks if an object has a specified property as its own property (ignores prototype properties and `__proto__`)\n * @param object Object to check\n * @param key Property key\n * @returns Does `object` have the property `key`?\n */\nexport function hasOwnProperty(object, key) {\n return key !== \"__proto__\" && _Object_hasOwnProperty.call(object, key);\n}\n\n/**\n * Creates an ECMAScript 6 Symbol, falling back to a simple string in environments that do not support Symbols\n * @param description Symbol description\n * @returns Symbol object or string\n * @function\n */\n/* c8 ignore start */\n/* istanbul ignore next */\nvar createSymbol =\n typeof Symbol === \"function\"\n ? Symbol\n : function (description) {\n return \"__\" + description;\n };\n/* c8 ignore stop */\n\n/** Hint property to indicate if an object has been observed */\nexport var HINT_OBSERVE = createSymbol(\"observe\");\n/** Hint property to indicate if a function has been disposed */\nexport var HINT_DISPOSE = createSymbol(\"dispose\");\n/** Hint property that contains a function's dependency disposal callbacks */\nexport var HINT_DEPENDS = createSymbol(\"depends\");\n\n/**\n * Defines a hint property on an object\n * @param object Object to define property on\n * @param hint Property key\n * @param {*} [value] Property value, property will be made non-configurable if this is unset (`undefined`)\n */\nexport function defineHint(object, hint, value) {\n defineProperty(object, hint, {\n value: value,\n configurable: value !== void 0,\n enumerable: false,\n writable: false\n });\n}\n\n/**\n * Checks if a value is a normal object, ignores functions and arrays\n * @param value Value to check\n * @returns Is `value` a normal object?\n */\nexport function isObject(value) {\n return value !== null && typeof value === \"object\" && !_Array_isArray(value);\n}\n\n/**\n * Checks if a value is a function\n * @param value Value to check\n * @return Is `value` a function?\n */\nexport function isFunction(value) {\n return typeof value === \"function\";\n}\n\n/** Error message printed when an argument is of an incorrect type (not a normal object) */\nexport var MESSAGE_NOT_OBJECT = \"Argument 'object' is not an object\";\n/** Error message printed when an argument is of an incorrect type (not a function) */\nexport var MESSAGE_NOT_FUNCTION = \"Argument 'func' is not a function\";\n\n/**\n * Throws an error message\n * @param message Message to construct the error with\n * @param generic Should the more generic `Error` be thrown instead of `TypeError`?\n */\nexport function throwError(message, generic) {\n throw new (generic ? _Error : _TypeError)(message);\n}\n","import {\n isFunction,\n hasOwnProperty,\n HINT_DISPOSE, HINT_DEPENDS, defineHint,\n MESSAGE_NOT_FUNCTION, throwError\n} from \"./util.js\";\n\n/** Maximum queue length */\nvar MAX_QUEUE = 2000;\n\n/** Is the queue being executed? */\nexport var computedLock = false;\n/** Queue of computed functions to be called */\nexport var computedQueue = [];\n/** Current index into `computedQueue` */\nexport var computedI = 0;\n\n/**\n * Throws an error indicating that the computed queue has overflowed\n */\nfunction computedOverflow() {\n var message = \"Computed queue overflow! Last 10 functions in the queue:\";\n\n var length = computedQueue.length;\n for (var i = length - 11; i < length; i++) {\n var func = computedQueue[i];\n message +=\n \"\\n\"\n + (i + 1)\n + \": \"\n + (func.name || \"anonymous\");\n }\n\n throwError(message, true);\n}\n\n/**\n * Attempts to add a function to the computed queue, then attempts to lock and execute the computed queue\n * @param func Function to queue\n */\nexport function computedNotify(func) {\n if (hasOwnProperty(func, HINT_DISPOSE)) return;\n\n // Only add to the queue if not already pending execution\n if (computedQueue.lastIndexOf(func) >= computedI) return;\n computedQueue.push(func);\n\n // Make sure that the function in question has a depends hint\n if (!hasOwnProperty(func, HINT_DEPENDS)) {\n defineHint(func, HINT_DEPENDS, []);\n }\n\n // Attempt to lock and execute the queue\n if (!computedLock) {\n computedLock = true;\n\n try {\n for (; computedI < computedQueue.length; computedI++) {\n // Indirectly call the function to avoid leaking `computedQueue` as `this`\n (0, computedQueue[computedI])();\n if (computedI > MAX_QUEUE) /* @__NOINLINE */ computedOverflow();\n }\n } finally {\n computedLock = false;\n computedQueue = [];\n computedI = 0;\n }\n }\n}\n\n/** See lib/patella.d.ts */\nexport function computed(func) {\n if (!isFunction(func)) {\n throwError(MESSAGE_NOT_FUNCTION);\n }\n\n computedNotify(func);\n return func;\n}\n","import { computedQueue, computedI, computedNotify } from \"./computed.js\";\nimport {\n isObject, isFunction,\n hasOwnProperty, defineProperty,\n HINT_OBSERVE, HINT_DEPENDS, defineHint,\n MESSAGE_NOT_OBJECT, throwError\n} from \"./util.js\";\n\n/**\n * Generates a property descriptor for a reactive property\n * @param value Initial property value\n * @returns Property descriptor object\n */\nfunction reactiveProperty(value) {\n if (isObject(value)) reactiveObserve(value);\n\n // List of computed functions that depend on this property\n var depends = [];\n /**\n * Remove a computed function from this reactive property\n * @param func Computed function to remove\n */\n function dependsRemove(func) {\n var i = depends.lastIndexOf(func);\n if (i >= 0) depends.splice(i, 1);\n }\n\n return {\n get: function() {\n // Add the current executing computed function to this reactive property's dependencies\n var func = computedQueue[computedI];\n if (func) {\n var i = depends.lastIndexOf(func);\n if (i < 0) {\n // Add them to our dependencies\n depends.push(func);\n // Add us to their dependants\n func[HINT_DEPENDS].push(dependsRemove);\n }\n }\n\n return value;\n },\n set: function(newValue) {\n if (isObject(newValue)) reactiveObserve(newValue);\n value = newValue;\n\n // Notify all dependencies\n for (var i = 0; i < depends.length; i++) {\n computedNotify(depends[i]);\n }\n }\n };\n}\n\n/**\n * Observes an object by making all of its enumerable properties reactive\n * @param object Object to observe\n */\nfunction reactiveObserve(object) {\n if (hasOwnProperty(object, HINT_OBSERVE)) return;\n defineHint(object, HINT_OBSERVE);\n\n for (var key in object) {\n if (hasOwnProperty(object, key)) {\n try {\n defineProperty(object, key, reactiveProperty(object[key]));\n } catch (err) {}\n }\n }\n}\n\n/** See lib/patella.d.ts */\nexport function observe(object) {\n if (!isObject(object) && !isFunction(object)) {\n throwError(MESSAGE_NOT_OBJECT);\n }\n\n reactiveObserve(object);\n return object;\n}\n","import { computedLock, computedQueue, computedI } from \"./computed.js\";\nimport {\n isFunction,\n hasOwnProperty,\n HINT_DISPOSE, HINT_DEPENDS, defineHint,\n MESSAGE_NOT_FUNCTION, throwError\n} from \"./util.js\";\n\n/** See lib/patella.d.ts */\nexport function dispose(func, clean) {\n if (func == null) {\n func = computedQueue[computedI];\n if (!func) {\n throwError(\"Tried to dispose of current computed function while not running a computed function\", true);\n }\n } else if (!isFunction(func)) {\n throwError(MESSAGE_NOT_FUNCTION);\n }\n\n // Only execute if the function has not been disposed yet\n if (!hasOwnProperty(func, HINT_DISPOSE)) {\n // Only define disposed property if we aren't cleaning\n if (!clean) defineHint(func, HINT_DISPOSE);\n\n // Remove from dependant reactive objects\n var depends = func[HINT_DEPENDS];\n if (depends) {\n defineHint(func, HINT_DEPENDS, clean ? [] : void 0);\n for (var i = 0; i < depends.length; i++) {\n depends[i](func);\n }\n }\n\n // Remove from the queue if locked and pending execution\n if (computedLock) { // Not required, but saves a `lastIndexOf` call on an empty array for like 6 bytes\n var i = computedQueue.lastIndexOf(func);\n if (i > computedI) computedQueue.splice(i, 1);\n }\n }\n\n // Only return the function if it was specified as an argument\n if (!computedLock) return func;\n}\n","import {\n isObject, isFunction,\n hasOwnProperty,\n HINT_OBSERVE, defineHint,\n MESSAGE_NOT_OBJECT, throwError\n} from \"./util.js\";\n\n/** See lib/patella.d.ts */\nexport function ignore(object) {\n if (!isObject(object) && !isFunction(object)) {\n throwError(MESSAGE_NOT_OBJECT);\n }\n\n if (!hasOwnProperty(object, HINT_OBSERVE)) {\n defineHint(object, HINT_OBSERVE);\n }\n\n return object;\n}\n"],"names":["hasOwnProperty","object","key","_Object_hasOwnProperty","call","defineHint","hint","value","defineProperty","configurable","enumerable","writable","isObject","_Array_isArray","isFunction","throwError","message","generic","_Error","_TypeError","computedOverflow","length","computedQueue","i","name","computedNotify","func","HINT_DISPOSE","lastIndexOf","computedI","push","HINT_DEPENDS","computedLock","MAX_QUEUE","reactiveProperty","dependsRemove","depends","splice","reactiveObserve","get","set","newValue","HINT_OBSERVE","err","_Object","Object","Array","isArray","Error","TypeError","createSymbol","Symbol","description","MESSAGE_NOT_OBJECT","MESSAGE_NOT_FUNCTION","clean"],"mappings":"iCAgBgBA,EAAeC,EAAQC,GACrC,MAAe,cAARA,GAAuBC,EAAuBC,KAAKH,EAAQC,YAgCpDG,EAAWJ,EAAQK,EAAMC,GACvCC,EAAeP,EAAQK,EAAM,CAC3BC,MAAOA,EACPE,sBAAcF,EACdG,cACAC,cASG,SAASC,EAASL,GACvB,OAAiB,OAAVA,GAAmC,iBAAVA,IAAuBM,EAAeN,GAQjE,SAASO,EAAWP,GACzB,MAAwB,mBAAVA,WAaAQ,EAAWC,EAASC,GAClC,MAAM,IAAKA,EAAUC,EAASC,GAAYH,YCnEnCI,IAIP,IAJF,IACMJ,EAAU,2DAEVK,EAASC,EAAcD,OAClBE,EAAIF,EAAS,GAAQA,EAAJE,EAAYA,IAEpCP,GACE,MACGO,EAAI,GACL,MAJOD,EAAcC,GAKfC,MAAQ,aAGpBT,EAAWC,MAON,SAASS,EAAeC,GAC7B,IAAI1B,EAAe0B,EAAMC,IAGrBL,EAAcM,YAAYF,GAASG,IACvCP,EAAcQ,KAAKJ,KAGCA,EAAMK,IACxB1B,EAAWqB,EAAMK,EAAc,KAI5BC,GAAc,CACjBA,KAEA,IACE,KAAOH,EAAYP,EAAcD,OAAQQ,OAEnCP,EAAcO,MACdA,EAAYI,GAA6Bb,YAG/CY,KACAV,EAAgB,GAChBO,EAAY,aCpDTK,EAAiB3B,YASf4B,EAAcT,GACrB,IAAIH,EAAIa,EAAQR,YAAYF,GACnB,EAALH,GAAQa,EAAQC,OAAOd,EAAG,GAV5BX,EAASL,IAAQ+B,EAAgB/B,OAGjC6B,EAAU,GAUd,MAAO,CACLG,IAAK,WAAA,IAECb,EAAOJ,EAAcO,GAWzB,OAVIH,GAEM,EADAU,EAAQR,YAAYF,OAGlBI,KAAKJ,KAERK,GAAcD,KAAKK,IAIrB5B,GAETiC,IAAK,SAAS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