Repository: fractaltech/react-native-timer
Branch: master
Commit: 754ebe1d7931
Files: 10
Total size: 42.3 KB
Directory structure:
gitextract__3rsdz2t/
├── .babelrc
├── .eslintrc
├── .gitignore
├── .npmignore
├── LICENSE
├── lib/
│ └── index.js
├── package.json
├── readme.md
├── src/
│ └── index.js
└── test/
└── main.js
================================================
FILE CONTENTS
================================================
================================================
FILE: .babelrc
================================================
{
"presets": ["es2015", "react-native", "react-native-stage-0"]
}
================================================
FILE: .eslintrc
================================================
{
"parser": "babel-eslint",
"env": {
"browser": true,
"node": true,
"es6": true
},
"plugins": ["react"],
"ecmaFeatures": {
"arrowFunctions": true,
"binaryLiterals": true,
"blockBindings": true,
"classes": true,
"defaultParams": true,
"destructuring": true,
"forOf": true,
"generators": true,
"modules": true,
"objectLiteralComputedProperties": true,
"objectLiteralDuplicateProperties": true,
"objectLiteralShorthandMethods": true,
"objectLiteralShorthandProperties": true,
"octalLiterals": true,
"regexUFlag": true,
"regexYFlag": true,
"spread": true,
"superInFunctions": true,
"templateStrings": true,
"unicodeCodePointEscapes": true,
"globalReturn": true,
"jsx": true
},
"rules": {
//
//Possible Errors
//
// The following rules point out areas where you might have made mistakes.
//
"comma-dangle": 2, // disallow or enforce trailing commas
"no-cond-assign": 2, // disallow assignment in conditional expressions
"no-console": 0, // disallow use of console (off by default in the node environment)
"no-constant-condition": 2, // disallow use of constant expressions in conditions
"no-control-regex": 2, // disallow control characters in regular expressions
"no-debugger": 2, // disallow use of debugger
"no-dupe-args": 2, // disallow duplicate arguments in functions
"no-dupe-keys": 2, // disallow duplicate keys when creating object literals
"no-duplicate-case": 2, // disallow a duplicate case label.
"no-empty": 2, // disallow empty statements
"no-empty-character-class": 2, // disallow the use of empty character classes in regular expressions
"no-ex-assign": 2, // disallow assigning to the exception in a catch block
"no-extra-boolean-cast": 2, // disallow double-negation boolean casts in a boolean context
"no-extra-parens": 0, // disallow unnecessary parentheses (off by default)
"no-extra-semi": 2, // disallow unnecessary semicolons
"no-func-assign": 2, // disallow overwriting functions written as function declarations
"no-inner-declarations": 2, // disallow function or variable declarations in nested blocks
"no-invalid-regexp": 2, // disallow invalid regular expression strings in the RegExp constructor
"no-irregular-whitespace": 2, // disallow irregular whitespace outside of strings and comments
"no-negated-in-lhs": 2, // disallow negation of the left operand of an in expression
"no-obj-calls": 2, // disallow the use of object properties of the global object (Math and JSON) as functions
"no-regex-spaces": 2, // disallow multiple spaces in a regular expression literal
"no-sparse-arrays": 2, // disallow sparse arrays
"no-unreachable": 2, // disallow unreachable statements after a return, throw, continue, or break statement
"use-isnan": 2, // disallow comparisons with the value NaN
"valid-jsdoc": 2, // Ensure JSDoc comments are valid (off by default)
"valid-typeof": 2, // Ensure that the results of typeof are compared against a valid string
//
// Best Practices
//
// These are rules designed to prevent you from making mistakes.
// They either prescribe a better way of doing something or help you avoid footguns.
//
"block-scoped-var": 0, // treat var statements as if they were block scoped (off by default). 0: deep destructuring is not compatible https://github.com/eslint/eslint/issues/1863
"complexity": 0, // specify the maximum cyclomatic complexity allowed in a program (off by default)
"consistent-return": 2, // require return statements to either always or never specify values
"curly": 2, // specify curly brace conventions for all control statements
"default-case": 2, // require default case in switch statements (off by default)
"dot-notation": 2, // encourages use of dot notation whenever possible
"eqeqeq": 2, // require the use of === and !==
"guard-for-in": 2, // make sure for-in loops have an if statement (off by default)
"no-alert": 2, // disallow the use of alert, confirm, and prompt
"no-caller": 2, // disallow use of arguments.caller or arguments.callee
"no-div-regex": 2, // disallow division operators explicitly at beginning of regular expression (off by default)
"no-eq-null": 2, // disallow comparisons to null without a type-checking operator (off by default)
"no-eval": 2, // disallow use of eval()
"no-extend-native": 2, // disallow adding to native types
"no-extra-bind": 2, // disallow unnecessary function binding
"no-fallthrough": 2, // disallow fallthrough of case statements
"no-floating-decimal": 2, // disallow the use of leading or trailing decimal points in numeric literals (off by default)
"no-implied-eval": 2, // disallow use of eval()-like methods
"no-iterator": 2, // disallow usage of __iterator__ property
"no-labels": 2, // disallow use of labeled statements
"no-lone-blocks": 2, // disallow unnecessary nested blocks
/*"no-loop-func": 2, // disallow creation of functions within loops*/
"no-multi-spaces": 2, // disallow use of multiple spaces
"no-multi-str": 2, // disallow use of multiline strings
"no-native-reassign": 2, // disallow reassignments of native objects
"no-new": 2, // disallow use of new operator when not part of the assignment or comparison
"no-new-func": 2, // disallow use of new operator for Function object
"no-new-wrappers": 2, // disallows creating new instances of String,Number, and Boolean
"no-octal": 2, // disallow use of octal literals
"no-octal-escape": 2, // disallow use of octal escape sequences in string literals, such as var foo = "Copyright \251";
"no-process-env": 2, // disallow use of process.env (off by default)
"no-proto": 2, // disallow usage of __proto__ property
"no-redeclare": 2, // disallow declaring the same variable more then once
"no-return-assign": 2, // disallow use of assignment in return statement
"no-script-url": 2, // disallow use of javascript: urls.
"no-self-compare": 2, // disallow comparisons where both sides are exactly the same (off by default)
"no-sequences": 2, // disallow use of comma operator
"no-throw-literal": 2, // restrict what can be thrown as an exception (off by default)
"no-unused-expressions": 2, // disallow usage of expressions in statement position
"no-void": 2, // disallow use of void operator (off by default)
"no-warning-comments": [0, {"terms": ["todo", "fixme"], "location": "start"}], // disallow usage of configurable warning terms in comments": 2, // e.g. TODO or FIXME (off by default)
"no-with": 2, // disallow use of the with statement
"radix": 2, // require use of the second argument for parseInt() (off by default)
"vars-on-top": 2, // requires to declare all vars on top of their containing scope (off by default)
"wrap-iife": 2, // require immediate function invocation to be wrapped in parentheses (off by default)
"yoda": 2, // require or disallow Yoda conditions
//
// Strict Mode
//
// These rules relate to using strict mode.
//
"strict": 0, // controls location of Use Strict Directives. 0: required by `babel-eslint`
//
// Variables
//
// These rules have to do with variable declarations.
//
"no-catch-shadow": 2, // disallow the catch clause parameter name being the same as a variable in the outer scope (off by default in the node environment)
"no-delete-var": 2, // disallow deletion of variables
"no-label-var": 2, // disallow labels that share a name with a variable
"no-undef": 2, // disallow use of undeclared variables unless mentioned in a /*global */ block
"no-undef-init": 2, // disallow use of undefined when initializing variables
"no-undefined": 2, // disallow use of undefined variable (off by default)
"no-unused-vars": 2, // disallow declaration of variables that are not used in the code
//
//Stylistic Issues
//
// These rules are purely matters of style and are quite subjective.
//
"indent": [1, 2], // this option sets a specific tab width for your code (off by default)
/*"brace-style": 1, // enforce one true brace style (off by default)*/
"comma-spacing": [1, {"before": false, "after": true}], // enforce spacing before and after comma
"comma-style": [1, "last"], // enforce one true comma style (off by default)
"consistent-this": [1, "_this"], // enforces consistent naming when capturing the current execution context (off by default)
"eol-last": 1, // enforce newline at the end of file, with no multiple empty lines
"func-names": 0, // require function expressions to have a name (off by default)
"func-style": 0, // enforces use of function declarations or expressions (off by default)
"key-spacing": [1, {"beforeColon": false, "afterColon": true}], // enforces spacing between keys and values in object literal properties
"max-nested-callbacks": [1, 3], // specify the maximum depth callbacks can be nested (off by default)
"new-cap": [1, {newIsCap: true, capIsNew: false}], // require a capital letter for constructors
"new-parens": 1, // disallow the omission of parentheses when invoking a constructor with no arguments
"newline-after-var": 0, // allow/disallow an empty newline after var statement (off by default)
"no-array-constructor": 1, // disallow use of the Array constructor
"no-inline-comments": 1, // disallow comments inline after code (off by default)
"no-lonely-if": 1, // disallow if as the only statement in an else block (off by default)
"no-mixed-spaces-and-tabs": 1, // disallow mixed spaces and tabs for indentation
"no-multiple-empty-lines": [1, {"max": 2}], // disallow multiple empty lines (off by default)
"no-new-object": 1, // disallow use of the Object constructor
"no-spaced-func": 1, // disallow space between function identifier and application
"no-ternary": 0, // disallow the use of ternary operators (off by default)
"no-trailing-spaces": 1, // disallow trailing whitespace at the end of lines
// "no-underscore-dangle": 1, // disallow dangling underscores in identifiers
"one-var": [1, "never"], // allow just one var statement per function (off by default)
"operator-assignment": [1, "never"], // require assignment operator shorthand where possible or prohibit it entirely (off by default)
"padded-blocks": [1, "never"], // enforce padding within blocks (off by default)
"quote-props": [1, "as-needed"], // require quotes around object literal property names (off by default)
"quotes": [1, "single"], // specify whether double or single quotes should be used
"semi": [1, "always"], // require or disallow use of semicolons instead of ASI
"semi-spacing": [1, {"before": false, "after": true}], // enforce spacing before and after semicolons
"sort-vars": 0, // sort variables within the same declaration block (off by default)
"space-before-blocks": [1, "always"], // require or disallow space before blocks (off by default)
"space-before-function-paren": [1, {"anonymous": "always", "named": "never"}], // require or disallow space before function opening parenthesis (off by default)
"computed-property-spacing": [2, "never"],
"array-bracket-spacing": [2, "never"],
"object-curly-spacing": [2, "never"],
"space-in-parens": [1, "never"], // require or disallow spaces inside parentheses (off by default)
"space-unary-ops": [1, {"words": true, "nonwords": false}], // Require or disallow spaces before/after unary operators (words on by default, nonwords off by default)
"spaced-comment": [2, "always"], // require or disallow a space immediately following the // in a line comment (off by default)
"wrap-regex": 0, // require regex literals to be wrapped in parentheses (off by default)
//
// ECMAScript 6
//
// These rules are only relevant to ES6 environments and are off by default.
//
"no-var": 2, // require let or const instead of var (off by default)
"generator-star-spacing": [2, "before"], // enforce the spacing around the * in generator functions (off by default)
//
// Legacy
//
// The following rules are included for compatibility with JSHint and JSLint.
// While the names of the rules may not match up with the JSHint/JSLint counterpart,
// the functionality is the same.
//
"max-depth": [2, 3], // specify the maximum depth that blocks can be nested (off by default)
"max-len": [2, 100, 2], // specify the maximum length of a line in your program (off by default)
"max-params": [2, 5], // limits the number of parameters that can be used in the function declaration. (off by default)
"max-statements": 0, // specify the maximum number of statement allowed in a function (off by default)
"no-bitwise": 0, // disallow use of bitwise operators (off by default)
"no-plusplus": 2, // disallow use of unary operators, ++ and -- (off by default)
//
// eslint-plugin-react
//
// React specific linting rules for ESLint
//
"react/display-name": 0, // Prevent missing displayName in a React component definition
"jsx-quotes": [2, "prefer-double"], // Enforce quote style for JSX attributes
"react/jsx-no-undef": 2, // Disallow undeclared variables in JSX
"react/jsx-sort-props": 0, // Enforce props alphabetical sorting
"react/jsx-uses-react": 2, // Prevent React to be incorrectly marked as unused
"react/jsx-uses-vars": 2, // Prevent variables used in JSX to be incorrectly marked as unused
"react/no-did-mount-set-state": 2, // Prevent usage of setState in componentDidMount
"react/no-did-update-set-state": 2, // Prevent usage of setState in componentDidUpdate
"react/no-multi-comp": 0, // Prevent multiple component definition per file
"react/no-unknown-property": 2, // Prevent usage of unknown DOM property
"react/prop-types": 2, // Prevent missing props validation in a React component definition
"react/react-in-jsx-scope": 2, // Prevent missing React when using JSX
"react/self-closing-comp": 2, // Prevent extra closing tags for components without children
"react/wrap-multilines": 2, // Prevent missing parentheses around multilines JSX
}
}
================================================
FILE: .gitignore
================================================
node_modules
npm-debug.log
================================================
FILE: .npmignore
================================================
.babelrc
================================================
FILE: LICENSE
================================================
ISC License (ISC)
Copyright <2019> <Kapil Verma>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
================================================
FILE: lib/index.js
================================================
'use strict';var _typeof=typeof Symbol==="function"&&typeof(typeof Symbol==='function'?Symbol.iterator:'@@iterator')==="symbol"?function(obj){return typeof obj;}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==(typeof Symbol==='function'?Symbol.prototype:'@@prototype')?"symbol":typeof obj;};var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}var Timer=function(){function Timer(){_classCallCheck(this,Timer);this.
timeouts=new Map();this.
intervals=new Map();this.
immediates=new Map();this.
animationFrames=new Map();this.
executedTimeouts=new Set();this.
executedImmediates=new Set();this.
executedAnimationFrames=new Set();this.
contextTimers=new WeakMap();}_createClass(Timer,[{key:'contextTimer',value:function contextTimer(
ctx){
if(!this.contextTimers.has(ctx)){
this.contextTimers.set(ctx,new Timer());
}
return this.contextTimers.get(ctx);
}},{key:'setTimeout',value:function setTimeout()
{
if(_typeof(arguments.length<=0?undefined:arguments[0])==='object'){
return this._setTimeoutContext.apply(this,arguments);
}else{
return this._setTimeoutVanilla.apply(this,arguments);
}
}},{key:'_setTimeoutContext',value:function _setTimeoutContext(
ctx,name,fn,interval){
this.contextTimer(ctx).setTimeout(name,fn,interval);
return this;
}},{key:'_setTimeoutVanilla',value:function _setTimeoutVanilla(
name,fn,interval){var _this=this;
this.clearTimeout(name);
this.timeouts.set(name,setTimeout(function(){
_this.clearTimeout(name);
_this.executedTimeouts.add(name);
fn();
_this.executedTimeouts.delete(name);
},interval));
return this;
}},{key:'clearTimeout',value:function clearTimeout()
{
if(_typeof(arguments.length<=0?undefined:arguments[0])==='object'){
return this._clearTimeoutContext.apply(this,arguments);
}else{
return this._clearTimeoutVanilla.apply(this,arguments);
}
}},{key:'_clearTimeoutContext',value:function _clearTimeoutContext(
ctx){var _this2=this;
if(!this.contextTimers.has(ctx)){
return this;
}for(var _len=arguments.length,args=Array(_len>1?_len-1:0),_key=1;_key<_len;_key++){args[_key-1]=arguments[_key];}
if(args.length===0){
Array.from(this.contextTimer(ctx).timeouts.keys()).forEach(function(timeout){
_this2.contextTimer(ctx).clearTimeout(timeout);
});
}else{var
timeout=args[0];
this.contextTimer(ctx).clearTimeout(timeout);
}
return this;
}},{key:'_clearTimeoutVanilla',value:function _clearTimeoutVanilla(
name){
if(this.timeouts.has(name)){
clearTimeout(this.timeouts.get(name));
this.timeouts.delete(name);
}
return this;
}},{key:'timeoutExists',value:function timeoutExists()
{
if(_typeof(arguments.length<=0?undefined:arguments[0])==='object'){
return this._timeoutExistsContext.apply(this,arguments);
}else{
return this._timeoutExistsVanilla.apply(this,arguments);
}
}},{key:'_timeoutExistsContext',value:function _timeoutExistsContext(
ctx,name){
return this.contextTimers.has(ctx)&&this.contextTimer(ctx).timeoutExists(name);
}},{key:'_timeoutExistsVanilla',value:function _timeoutExistsVanilla(
name){
return this.timeouts.has(name)||this.executedTimeouts.has(name);
}},{key:'setInterval',value:function setInterval()
{
if(_typeof(arguments.length<=0?undefined:arguments[0])==='object'){
return this._setIntervalContext.apply(this,arguments);
}else{
return this._setIntervalVanilla.apply(this,arguments);
}
}},{key:'_setIntervalContext',value:function _setIntervalContext(
ctx,name,fn,interval){
this.contextTimer(ctx).setInterval(name,fn,interval);
return this;
}},{key:'_setIntervalVanilla',value:function _setIntervalVanilla(
name,fn,interval){
this.clearInterval(name);
this.intervals.set(name,setInterval(fn,interval));
return this;
}},{key:'clearInterval',value:function clearInterval()
{
if(_typeof(arguments.length<=0?undefined:arguments[0])==='object'){
return this._clearIntervalContext.apply(this,arguments);
}else{
return this._clearIntervalVanilla.apply(this,arguments);
}
}},{key:'_clearIntervalContext',value:function _clearIntervalContext(
ctx){var _this3=this;
if(!this.contextTimers.has(ctx)){
return this;
}for(var _len2=arguments.length,args=Array(_len2>1?_len2-1:0),_key2=1;_key2<_len2;_key2++){args[_key2-1]=arguments[_key2];}
if(args.length===0){
Array.from(this.contextTimer(ctx).intervals.keys()).forEach(function(interval){
_this3.contextTimer(ctx).clearInterval(interval);
});
}else{var
interval=args[0];
this.contextTimer(ctx).clearInterval(interval);
}
return this;
}},{key:'_clearIntervalVanilla',value:function _clearIntervalVanilla(
name){
if(this.intervals.has(name)){
clearInterval(this.intervals.get(name));
this.intervals.delete(name);
}
return this;
}},{key:'intervalExists',value:function intervalExists()
{
if(_typeof(arguments.length<=0?undefined:arguments[0])==='object'){
return this._intervalExistsContext.apply(this,arguments);
}else{
return this._intervalExistsVanilla.apply(this,arguments);
}
}},{key:'_intervalExistsContext',value:function _intervalExistsContext(
ctx,name){
return this.contextTimers.has(ctx)&&this.contextTimer(ctx).intervalExists(name);
}},{key:'_intervalExistsVanilla',value:function _intervalExistsVanilla(
name){
return this.intervals.has(name);
}},{key:'setImmediate',value:function setImmediate()
{
if(_typeof(arguments.length<=0?undefined:arguments[0])==='object'){
return this._setImmediateContext.apply(this,arguments);
}else{
return this._setImmediateVanilla.apply(this,arguments);
}
}},{key:'_setImmediateContext',value:function _setImmediateContext(
ctx,name,fn){
this.contextTimer(ctx).setImmediate(name,fn);
return this;
}},{key:'_setImmediateVanilla',value:function _setImmediateVanilla(
name,fn){var _this4=this;
this.clearImmediate(name);
this.immediates.set(name,setImmediate(function(){
_this4.clearImmediate(name);
_this4.executedImmediates.add(name);
fn();
_this4.executedImmediates.delete(name);
}));
return this;
}},{key:'clearImmediate',value:function clearImmediate()
{
if(_typeof(arguments.length<=0?undefined:arguments[0])==='object'){
return this._clearImmediateContext.apply(this,arguments);
}else{
return this._clearImmediateVanilla.apply(this,arguments);
}
}},{key:'_clearImmediateContext',value:function _clearImmediateContext(
ctx){var _this5=this;
if(!this.contextTimers.has(ctx)){
return this;
}for(var _len3=arguments.length,args=Array(_len3>1?_len3-1:0),_key3=1;_key3<_len3;_key3++){args[_key3-1]=arguments[_key3];}
if(args.length===0){
Array.from(this.contextTimer(ctx).immediates.keys()).forEach(function(immediate){
_this5.contextTimer(ctx).clearImmediate(immediate);
});
}else{var
immediate=args[0];
this.contextTimer(ctx).clearImmediate(immediate);
}
return this;
}},{key:'_clearImmediateVanilla',value:function _clearImmediateVanilla(
name){
if(this.immediates.has(name)){
clearImmediate(this.immediates.get(name));
this.immediates.delete(name);
}
return this;
}},{key:'immediateExists',value:function immediateExists()
{
if(_typeof(arguments.length<=0?undefined:arguments[0])==='object'){
return this._immediateExistsContext.apply(this,arguments);
}else{
return this._immediateExistsVanilla.apply(this,arguments);
}
}},{key:'_immediateExistsContext',value:function _immediateExistsContext(
ctx,name){
return this.contextTimers.has(ctx)&&this.contextTimer(ctx).immediateExists(name);
}},{key:'_immediateExistsVanilla',value:function _immediateExistsVanilla(
name){
return this.immediates.has(name)||this.executedImmediates.has(name);
}},{key:'requestAnimationFrame',value:function requestAnimationFrame()
{
if(_typeof(arguments.length<=0?undefined:arguments[0])==='object'){
return this._requestAnimationFrameContext.apply(this,arguments);
}else{
return this._requestAnimationFrameVanilla.apply(this,arguments);
}
}},{key:'_requestAnimationFrameContext',value:function _requestAnimationFrameContext(
ctx,name,fn){
this.contextTimer(ctx).requestAnimationFrame(name,fn);
return this;
}},{key:'_requestAnimationFrameVanilla',value:function _requestAnimationFrameVanilla(
name,fn){var _this6=this;
this.cancelAnimationFrame(name);
this.animationFrames.set(name,requestAnimationFrame(function(){
_this6.cancelAnimationFrame(name);
_this6.executedAnimationFrames.add(name);
fn();
_this6.executedAnimationFrames.delete(name);
}));
return this;
}},{key:'cancelAnimationFrame',value:function cancelAnimationFrame()
{
if(_typeof(arguments.length<=0?undefined:arguments[0])==='object'){
return this._cancelAnimationFrameContext.apply(this,arguments);
}else{
return this._cancelAnimationFrameVanilla.apply(this,arguments);
}
}},{key:'_cancelAnimationFrameContext',value:function _cancelAnimationFrameContext(
ctx){var _this7=this;
if(!this.contextTimers.has(ctx)){
return this;
}for(var _len4=arguments.length,args=Array(_len4>1?_len4-1:0),_key4=1;_key4<_len4;_key4++){args[_key4-1]=arguments[_key4];}
if(args.length===0){
Array.from(this.contextTimer(ctx).animationFrames.keys()).forEach(function(animationFrame){
_this7.contextTimer(ctx).cancelAnimationFrame(animationFrame);
});
}else{var
animationFrame=args[0];
this.contextTimer(ctx).cancelAnimationFrame(animationFrame);
}
return this;
}},{key:'_cancelAnimationFrameVanilla',value:function _cancelAnimationFrameVanilla(
name){
if(this.animationFrames.has(name)){
cancelAnimationFrame(this.animationFrames.get(name));
this.animationFrames.delete(name);
}
return this;
}},{key:'animationFrameExists',value:function animationFrameExists()
{
if(_typeof(arguments.length<=0?undefined:arguments[0])==='object'){
return this._animationFrameExistsContext.apply(this,arguments);
}else{
return this._animationFrameExistsVanilla.apply(this,arguments);
}
}},{key:'_animationFrameExistsContext',value:function _animationFrameExistsContext(
ctx,name){
return this.contextTimers.has(ctx)&&this.contextTimer(ctx).animationFrameExists(name);
}},{key:'_animationFrameExistsVanilla',value:function _animationFrameExistsVanilla(
name){
return this.animationFrames.has(name)||this.executedAnimationFrames.has(name);
}}]);return Timer;}();
module.exports=new Timer();
================================================
FILE: package.json
================================================
{
"name": "react-native-timer",
"version": "1.3.6",
"description": "something to manage timers in react native",
"main": "lib/index.js",
"scripts": {
"test": "babel-node test/main.js",
"build": "npm run clean && babel src/ --out-dir lib/",
"watch": "npm run clean && babel -w src/ --out-dir lib/",
"clean": "rm -rf lib/"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/fractaltech/react-native-timer.git"
},
"author": "Kapil <kapv89@gmail.com>",
"license": "ISC",
"bugs": {
"url": "https://github.com/fractaltech/react-native-timer/issues"
},
"homepage": "https://github.com/fractaltech/react-native-timer#readme",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-eslint": "^8.2.6",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.5.0",
"babel-preset-react-native-stage-0": "^1.0.1",
"eslint": "^5.15.1",
"eslint-plugin-babel": "^3.1.0",
"eslint-plugin-react": "^4.2.3"
}
}
================================================
FILE: readme.md
================================================
# react-native-timer
## We follow [breaking].[feature].[fix] versioning
`npm install --save react-native-timer`
#### A better way to manage timers in react-native with ES6 components, using __WeakMap__.
#### Version 1.3.6
1. Often you need to do things like show a message for a few seconds, and then hide it, or run an operation again and again at a specific interval. These things will usually happen *inside* a React Component, and will start *after* a component has mounted. So, you really cannot *just* do a `setTimeout(fn, 2000)` for non trivial things. You need to do a `this.timer = setTimeout(fn, 2000)`, and then `clearTimeout(this.timer)` in `componentWillUnmount`.
2. When a component unmounts, these timers have to be cleared and, so that you are not left with zombie timers doing things when you did not expect them to be there.
3. React, right now, offers a solution using the `react-native-timer-mixin` for this. However, mixins are not part of ES6-7 standard, and probably will never be as they get in the way of good software design. And this brings us to the package in question, `react-native-timer`.
4. With `react-native-timer`, you can set different timers, like `timeout`, `interval` etc in the context of a react component, and unmount all of them when the component unmounts, at context level.
Generic API:
```js
const timer = require('react-native-timer');
// timers maintained in the Map timer.timeouts
timer.setTimeout(name, fn, interval);
timer.clearTimeout(name);
timer.timeoutExists(name);
// timers maintained in the Map timer.intervals
timer.setInterval(name, fn, interval);
timer.clearInterval(name);
timer.intervalExists(name);
// timers maintained in the Map timer.immediates
timer.setImmediate(name, fn);
timer.clearImmediate(name);
timer.immediateExists(name);
// timers maintained in the Map timer.animationFrames
timer.requestAnimationFrame(name, fn);
timer.cancelAnimationFrame(name);
timer.animationFrameExists(name);
```
Mostly, using timers is a pain *inside* react-native components, so we present to you
__Contextual Timers__. API:
```js
timer.setTimeout(context, name, fn, interval);
timer.clearTimeout(context, name);
timer.clearTimeout(context) // clears all timeouts for a context
timer.timeoutExists(context, name);
timer.setInterval(context, name, fn, interval);
timer.clearInterval(context, name);
timer.clearInterval(context); // clears all intervals for a context
timer.intervalExists(context, name);
timer.setImmediate(context, name, fn);
timer.clearImmediate(context, name);
timer.clearImmediate(context); // clears all immediates for a context
timer.immediateExists(context, name);
timer.requestAnimationFrame(context, name, fn);
timer.cancelAnimationFrame(context, name);
timer.cancelAnimationFrame(context); // cancels all animation frames for a context
timer.animationFrameExists(context, name);
```
Example Below:
```js
const timer = require('react-native-timer');
class Foo extends React.Component {
state = {
showMsg: false
};
componentWillUnmount() {
timer.clearTimeout(this);
}
showMsg() {
this.setState({showMsg: true}, () => timer.setTimeout(
this, 'hideMsg', () => this.setState({showMsg: false}), 2000
));
}
render() {
return {
<View style={{flex: 1}}>
<TouchableOpacity onPress={() => requestAnimationFrame(() => this.showMsg())}>
<Text>Press Me</Text>
</TouchableOpacity>
{this.state.showMsg ? (
<Text>Hello!!</Text>
) : (
null
)}
</View>
}
}
}
```
PS: Kinda not a best practice, but `const t = require('react-native-timer')` can cut down some typing.
Also, this lib can be used in browsers too, but will focus on them when I am working with them.
================================================
FILE: src/index.js
================================================
class Timer {
timeouts = new Map();
intervals = new Map();
immediates = new Map();
animationFrames = new Map();
executedTimeouts = new Set();
executedImmediates = new Set();
executedAnimationFrames = new Set();
contextTimers = new WeakMap();
contextTimer(ctx) {
if (!this.contextTimers.has(ctx)) {
this.contextTimers.set(ctx, new Timer());
}
return this.contextTimers.get(ctx);
}
setTimeout(...args) {
if ((typeof args[0]) === 'object') {
return this._setTimeoutContext(...args);
} else {
return this._setTimeoutVanilla(...args);
}
}
_setTimeoutContext(ctx, name, fn, interval) {
this.contextTimer(ctx).setTimeout(name, fn, interval);
return this;
}
_setTimeoutVanilla(name, fn, interval) {
this.clearTimeout(name);
this.timeouts.set(name, setTimeout(() => {
this.clearTimeout(name);
this.executedTimeouts.add(name);
fn();
this.executedTimeouts.delete(name);
}, interval));
return this;
}
clearTimeout(...args) {
if ((typeof args[0]) === 'object') {
return this._clearTimeoutContext(...args);
} else {
return this._clearTimeoutVanilla(...args);
}
}
_clearTimeoutContext(ctx, ...args) {
if (!this.contextTimers.has(ctx)) {
return this;
}
if (args.length === 0) {
Array.from(this.contextTimer(ctx).timeouts.keys()).forEach((timeout) => {
this.contextTimer(ctx).clearTimeout(timeout);
});
} else {
const [timeout] = args;
this.contextTimer(ctx).clearTimeout(timeout);
}
return this;
}
_clearTimeoutVanilla(name) {
if (this.timeouts.has(name)) {
clearTimeout(this.timeouts.get(name));
this.timeouts.delete(name);
}
return this;
}
timeoutExists(...args) {
if ((typeof args[0]) === 'object') {
return this._timeoutExistsContext(...args);
} else {
return this._timeoutExistsVanilla(...args);
}
}
_timeoutExistsContext(ctx, name) {
return this.contextTimers.has(ctx) && this.contextTimer(ctx).timeoutExists(name);
}
_timeoutExistsVanilla(name) {
return this.timeouts.has(name) || this.executedTimeouts.has(name);
}
setInterval(...args) {
if ((typeof args[0]) === 'object') {
return this._setIntervalContext(...args);
} else {
return this._setIntervalVanilla(...args);
}
}
_setIntervalContext(ctx, name, fn, interval) {
this.contextTimer(ctx).setInterval(name, fn, interval);
return this;
}
_setIntervalVanilla(name, fn, interval) {
this.clearInterval(name);
this.intervals.set(name, setInterval(fn, interval));
return this;
}
clearInterval(...args) {
if ((typeof args[0]) === 'object') {
return this._clearIntervalContext(...args);
} else {
return this._clearIntervalVanilla(...args);
}
}
_clearIntervalContext(ctx, ...args) {
if (!this.contextTimers.has(ctx)) {
return this;
}
if (args.length === 0) {
Array.from(this.contextTimer(ctx).intervals.keys()).forEach((interval) => {
this.contextTimer(ctx).clearInterval(interval);
});
} else {
const [interval] = args;
this.contextTimer(ctx).clearInterval(interval);
}
return this;
}
_clearIntervalVanilla(name) {
if (this.intervals.has(name)) {
clearInterval(this.intervals.get(name));
this.intervals.delete(name);
}
return this;
}
intervalExists(...args) {
if (typeof args[0] === 'object') {
return this._intervalExistsContext(...args);
} else {
return this._intervalExistsVanilla(...args);
}
}
_intervalExistsContext(ctx, name) {
return this.contextTimers.has(ctx) && this.contextTimer(ctx).intervalExists(name);
}
_intervalExistsVanilla(name) {
return this.intervals.has(name);
}
setImmediate(...args) {
if ((typeof args[0]) === 'object') {
return this._setImmediateContext(...args);
} else {
return this._setImmediateVanilla(...args);
}
}
_setImmediateContext(ctx, name, fn) {
this.contextTimer(ctx).setImmediate(name, fn);
return this;
}
_setImmediateVanilla(name, fn) {
this.clearImmediate(name);
this.immediates.set(name, setImmediate(() => {
this.clearImmediate(name);
this.executedImmediates.add(name);
fn();
this.executedImmediates.delete(name);
}));
return this;
}
clearImmediate(...args) {
if ((typeof args[0]) === 'object') {
return this._clearImmediateContext(...args);
} else {
return this._clearImmediateVanilla(...args);
}
}
_clearImmediateContext(ctx, ...args) {
if (!this.contextTimers.has(ctx)) {
return this;
}
if (args.length === 0) {
Array.from(this.contextTimer(ctx).immediates.keys()).forEach((immediate) => {
this.contextTimer(ctx).clearImmediate(immediate);
});
} else {
const [immediate] = args;
this.contextTimer(ctx).clearImmediate(immediate);
}
return this;
}
_clearImmediateVanilla(name) {
if (this.immediates.has(name)) {
clearImmediate(this.immediates.get(name));
this.immediates.delete(name);
}
return this;
}
immediateExists(...args) {
if (typeof args[0] === 'object') {
return this._immediateExistsContext(...args);
} else {
return this._immediateExistsVanilla(...args);
}
}
_immediateExistsContext(ctx, name) {
return this.contextTimers.has(ctx) && this.contextTimer(ctx).immediateExists(name);
}
_immediateExistsVanilla(name) {
return this.immediates.has(name) || this.executedImmediates.has(name);
}
requestAnimationFrame(...args) {
if ((typeof args[0]) === 'object') {
return this._requestAnimationFrameContext(...args);
} else {
return this._requestAnimationFrameVanilla(...args);
}
}
_requestAnimationFrameContext(ctx, name, fn) {
this.contextTimer(ctx).requestAnimationFrame(name, fn);
return this;
}
_requestAnimationFrameVanilla(name, fn) {
this.cancelAnimationFrame(name);
this.animationFrames.set(name, requestAnimationFrame(() => {
this.cancelAnimationFrame(name);
this.executedAnimationFrames.add(name);
fn();
this.executedAnimationFrames.delete(name);
}));
return this;
}
cancelAnimationFrame(...args) {
if ((typeof args[0]) === 'object') {
return this._cancelAnimationFrameContext(...args);
} else {
return this._cancelAnimationFrameVanilla(...args);
}
}
_cancelAnimationFrameContext(ctx, ...args) {
if (!this.contextTimers.has(ctx)) {
return this;
}
if (args.length === 0) {
Array.from(this.contextTimer(ctx).animationFrames.keys()).forEach((animationFrame) => {
this.contextTimer(ctx).cancelAnimationFrame(animationFrame);
});
} else {
const [animationFrame] = args;
this.contextTimer(ctx).cancelAnimationFrame(animationFrame);
}
return this;
}
_cancelAnimationFrameVanilla(name) {
if (this.animationFrames.has(name)) {
cancelAnimationFrame(this.animationFrames.get(name));
this.animationFrames.delete(name);
}
return this;
}
animationFrameExists(...args) {
if (typeof args[0] === 'object') {
return this._animationFrameExistsContext(...args);
} else {
return this._animationFrameExistsVanilla(...args);
}
}
_animationFrameExistsContext(ctx, name) {
return this.contextTimers.has(ctx) && this.contextTimer(ctx).animationFrameExists(name);
}
_animationFrameExistsVanilla(name) {
return this.animationFrames.has(name) || this.executedAnimationFrames.has(name);
}
}
module.exports = new Timer();
================================================
FILE: test/main.js
================================================
const assert = require('assert');
const timer = require('../lib/index');
(() => {
let done = false;
timer.setTimeout('testSetTimeout', () => { done = true; }, 100);
setTimeout(() => {
assert.ok(done && !timer.timeoutExists('testSetTimeout'), 'setTimeout works');
}, 105);
})();
(() => {
const ctx = {};
let done = false;
timer.setTimeout(ctx, 'testSetTimeoutCtx', () => { done = true; }, 100);
setTimeout(() => {
assert.ok(
done && !timer.contextTimer(ctx).timeoutExists('testSetTimeout'),
'setTimeoutCtx works'
);
}, 105);
})();
(() => {
let done = false;
timer.setTimeout('testClearTimeout', () => { done = true; }, 100);
setTimeout(() => timer.clearTimeout('testClearTimeout'), 50);
setTimeout(() => {
assert.ok(!done, 'clearTimeout works');
}, 105);
})();
(() => {
const ctx = {};
let done = false;
timer.setTimeout(ctx, 'testClearTimeoutCtx', () => { done = true; }, 100);
setTimeout(() => timer.clearTimeout(ctx), 50);
setTimeout(() => {
assert.ok(!done, 'clearTimeoutCtx works');
}, 105);
})();
(() => {
const flags = [];
timer.setInterval('testInterval', () => { flags.push(flags.length); }, 100);
setTimeout(() => {
assert.ok(
flags.length === 3,
'setInterval works, if it doesn\'t, modify the timeout of this timeout'
);
timer.clearInterval('testInterval');
setTimeout(() => {
assert.ok(flags.length === 3 && !timer.intervalExists('testInterval'), 'clearInterval works');
}, 200);
}, 400);
// usually for around 390, this test seems to pass, so setting it greater than that
})();
(() => {
const ctx = {};
const flags = [];
timer.setInterval(ctx, 'testIntervalCtx', () => { flags.push(flags.length); }, 100);
setTimeout(() => {
assert.ok(
flags.length === 3,
'setIntervalCtx works, if it doesn\'t, modify the timeout of this timeout'
);
timer.clearInterval(ctx);
setTimeout(() => {
assert.ok(
flags.length === 3 && !timer.contextTimer(ctx).intervalExists('testIntervalCtx'),
'clearIntervalCtx works'
);
}, 200);
}, 400);
// usually for around 390, this test seems to pass, so setting it greater than that
})();
(() => {
const ctx = {};
const flags = [];
const timerName = 'timerName';
timer.setInterval(ctx, timerName, () => { flags.push(flags.length); }, 100);
setTimeout(() => {
assert.ok(
flags.length === 3,
'setIntervalCtx works with timerName, if it doesn\'t, modify the timeout of this timeout'
);
timer.clearInterval(ctx, timerName);
setTimeout(() => {
assert.ok(
flags.length === 3 && !timer.contextTimer(ctx).intervalExists(timerName),
'clearIntervalCtx works with timerName works'
);
}, 200);
}, 400);
// usually for around 390, this test seems to pass, so setting it greater than that
})();
(() => {
let done = false;
timer.setTimeout('testSetImmediate', () => { done = true; });
setTimeout(() => {
assert.ok(done && !timer.immediateExists('testSetImmediate'), 'setImmediate works');
}, 50);
})();
(() => {
const ctx = {};
let done = false;
timer.setTimeout(ctx, 'testSetImmediateCtx', () => { done = true; });
setTimeout(() => {
assert.ok(
done && !timer.contextTimer(ctx).immediateExists('testSetImmediateCtx'),
'setImmediateCtx works'
);
}, 50);
})();
(() => {
let done = false;
let m = 1;
for (let i=0; i<1000; i=i+1) {
if (i === 0) {
timer.setImmediate('testClearImmediate', () => { done = true; });
}
if (i === 5) {
timer.clearImmediate('testClearImmediate');
}
m = m*i+1;
}
setTimeout(() => {
assert.ok(!done, 'clearImmediate works');
}, 50);
})();
(() => {
const ctx = {};
let done = false;
let m = 1;
for (let i=0; i<1000; i=i+1) {
if (i === 0) {
timer.setImmediate(ctx, 'testClearImmediateCtx', () => { done = true; });
}
if (i === 5) {
timer.clearImmediate(ctx);
}
m = m*i+1;
}
setTimeout(() => {
assert.ok(!done, 'clearImmediateCtx works');
}, 50);
})();
/**
* below two can only be tested in a UI env, but they should work, code is simple,
* so commenting them out for now
*/
/*
(() => {
let done = false;
timer.requestAnimationFrame('testRequestAnimationFrame', () => { done = true; });
setTimeout(() => {
assert.ok(
done &&
!timer.animationFrames.has('testRequestAnimationFrame'), 'requestAnimationFrame works'
);
}, 50);
})();
(() => {
let done = false;
let m = 1;
for (let i=0; i<1000; i=i+1) {
if (i === 0) {
timer.requestAnimationFrame('testCancelRequestAnimationFrame', () => { done = true; });
}
if (i === 5) {
timer.cancelAnimationFrame('testCancelRequestAnimationFrame');
}
m = m*i+1;
}
setTimeout(() => {
assert.ok(!done, 'cancelAnimationFrame works');
}, 50);
})();
*/
gitextract__3rsdz2t/
├── .babelrc
├── .eslintrc
├── .gitignore
├── .npmignore
├── LICENSE
├── lib/
│ └── index.js
├── package.json
├── readme.md
├── src/
│ └── index.js
└── test/
└── main.js
SYMBOL INDEX (41 symbols across 2 files)
FILE: lib/index.js
function defineProperties (line 1) | function defineProperties(target,props){for(var i=0;i<props.length;i++){...
function _classCallCheck (line 1) | function _classCallCheck(instance,Constructor){if(!(instance instanceof ...
function Timer (line 1) | function Timer(){_classCallCheck(this,Timer);this.
FILE: src/index.js
class Timer (line 1) | class Timer {
method contextTimer (line 13) | contextTimer(ctx) {
method setTimeout (line 21) | setTimeout(...args) {
method _setTimeoutContext (line 29) | _setTimeoutContext(ctx, name, fn, interval) {
method _setTimeoutVanilla (line 34) | _setTimeoutVanilla(name, fn, interval) {
method clearTimeout (line 46) | clearTimeout(...args) {
method _clearTimeoutContext (line 54) | _clearTimeoutContext(ctx, ...args) {
method _clearTimeoutVanilla (line 71) | _clearTimeoutVanilla(name) {
method timeoutExists (line 80) | timeoutExists(...args) {
method _timeoutExistsContext (line 88) | _timeoutExistsContext(ctx, name) {
method _timeoutExistsVanilla (line 92) | _timeoutExistsVanilla(name) {
method setInterval (line 96) | setInterval(...args) {
method _setIntervalContext (line 104) | _setIntervalContext(ctx, name, fn, interval) {
method _setIntervalVanilla (line 109) | _setIntervalVanilla(name, fn, interval) {
method clearInterval (line 115) | clearInterval(...args) {
method _clearIntervalContext (line 123) | _clearIntervalContext(ctx, ...args) {
method _clearIntervalVanilla (line 140) | _clearIntervalVanilla(name) {
method intervalExists (line 149) | intervalExists(...args) {
method _intervalExistsContext (line 157) | _intervalExistsContext(ctx, name) {
method _intervalExistsVanilla (line 161) | _intervalExistsVanilla(name) {
method setImmediate (line 165) | setImmediate(...args) {
method _setImmediateContext (line 173) | _setImmediateContext(ctx, name, fn) {
method _setImmediateVanilla (line 178) | _setImmediateVanilla(name, fn) {
method clearImmediate (line 190) | clearImmediate(...args) {
method _clearImmediateContext (line 198) | _clearImmediateContext(ctx, ...args) {
method _clearImmediateVanilla (line 215) | _clearImmediateVanilla(name) {
method immediateExists (line 224) | immediateExists(...args) {
method _immediateExistsContext (line 232) | _immediateExistsContext(ctx, name) {
method _immediateExistsVanilla (line 236) | _immediateExistsVanilla(name) {
method requestAnimationFrame (line 240) | requestAnimationFrame(...args) {
method _requestAnimationFrameContext (line 248) | _requestAnimationFrameContext(ctx, name, fn) {
method _requestAnimationFrameVanilla (line 254) | _requestAnimationFrameVanilla(name, fn) {
method cancelAnimationFrame (line 266) | cancelAnimationFrame(...args) {
method _cancelAnimationFrameContext (line 274) | _cancelAnimationFrameContext(ctx, ...args) {
method _cancelAnimationFrameVanilla (line 291) | _cancelAnimationFrameVanilla(name) {
method animationFrameExists (line 300) | animationFrameExists(...args) {
method _animationFrameExistsContext (line 308) | _animationFrameExistsContext(ctx, name) {
method _animationFrameExistsVanilla (line 312) | _animationFrameExistsVanilla(name) {
Condensed preview — 10 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (45K chars).
[
{
"path": ".babelrc",
"chars": 68,
"preview": "{\n \"presets\": [\"es2015\", \"react-native\", \"react-native-stage-0\"]\n}\n"
},
{
"path": ".eslintrc",
"chars": 14408,
"preview": "{\n \"parser\": \"babel-eslint\",\n\n \"env\": {\n \"browser\": true,\n \"node\": true,\n \"es6\": true\n },\n\n \"plugins\": [\"re"
},
{
"path": ".gitignore",
"chars": 27,
"preview": "node_modules\nnpm-debug.log\n"
},
{
"path": ".npmignore",
"chars": 9,
"preview": ".babelrc\n"
},
{
"path": "LICENSE",
"chars": 747,
"preview": "ISC License (ISC)\nCopyright <2019> <Kapil Verma>\n\nPermission to use, copy, modify, and/or distribute this software for a"
},
{
"path": "lib/index.js",
"chars": 10540,
"preview": "'use strict';var _typeof=typeof Symbol===\"function\"&&typeof(typeof Symbol==='function'?Symbol.iterator:'@@iterator')===\""
},
{
"path": "package.json",
"chars": 998,
"preview": "{\n \"name\": \"react-native-timer\",\n \"version\": \"1.3.6\",\n \"description\": \"something to manage timers in react native\",\n "
},
{
"path": "readme.md",
"chars": 3779,
"preview": "# react-native-timer\n## We follow [breaking].[feature].[fix] versioning\n\n`npm install --save react-native-timer`\n\n#### A"
},
{
"path": "src/index.js",
"chars": 7753,
"preview": "class Timer {\n timeouts = new Map();\n intervals = new Map();\n immediates = new Map();\n animationFrames = new Map();\n"
},
{
"path": "test/main.js",
"chars": 4940,
"preview": "const assert = require('assert');\nconst timer = require('../lib/index');\n\n(() => {\n let done = false;\n timer.setTimeou"
}
]
About this extraction
This page contains the full source code of the fractaltech/react-native-timer GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 10 files (42.3 KB), approximately 11.1k tokens, and a symbol index with 41 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.