Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

waitasecond

Package Overview
Dependencies
Maintainers
1
Versions
124
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

waitasecond - npm Package Compare versions

Comparing version 1.8.1 to 1.8.2

26

dist/functions/forAllImagesInElement.js

@@ -0,1 +1,2 @@

"use strict";
/*

@@ -10,9 +11,11 @@ TODO: !!! Rename forImagesReady, forMediaReady

*/
exports.__esModule = true;
exports.forAllImagesInElement = void 0;
// tslint:disable:no-console
export function forAllImagesInElement(element) {
return Promise.all(Array.from(element.querySelectorAll('img')).map((imgElement, i) => {
return new Promise((resolve, reject) => {
function forAllImagesInElement(element) {
return Promise.all(Array.from(element.querySelectorAll('img')).map(function (imgElement, i) {
return new Promise(function (resolve, reject) {
if (imgElement.complete) {
if (imgElement.naturalHeight === 0) {
console.info(`Image ${i} rejected due to 0 naturalHeight`);
console.info("Image " + i + " rejected due to 0 naturalHeight");
// TODO: !!! Check this also in load

@@ -22,19 +25,20 @@ reject(imgElement);

else {
console.info(`Image ${i} is already completed`);
console.info("Image " + i + " is already completed");
resolve();
}
}
imgElement.addEventListener('load', () => {
console.info(`Image ${i} resolved`);
imgElement.addEventListener('load', function () {
console.info("Image " + i + " resolved");
resolve();
});
imgElement.addEventListener('error', () => {
console.info(`Image ${i} rejected`);
imgElement.addEventListener('error', function () {
console.info("Image " + i + " rejected");
// TODO: !!! imgElement into ImageError
reject(new Error(`Some images can\`t be loaded. If you want to supress this error keep rejectWhenNotLoaded=false (default value).`));
reject(new Error("Some images can`t be loaded. If you want to supress this error keep rejectWhenNotLoaded=false (default value)."));
});
});
})).then(() => {
})).then(function () {
/* Note: Returning void */
});
}
exports.forAllImagesInElement = forAllImagesInElement;

@@ -1,4 +0,7 @@

export function forAnimationFrame() {
return new Promise((resolve) => {
requestAnimationFrame((now) => {
"use strict";
exports.__esModule = true;
exports.forAnimationFrame = void 0;
function forAnimationFrame() {
return new Promise(function (resolve) {
requestAnimationFrame(function (now) {
// TODO: !!! What exactly is now?

@@ -9,1 +12,2 @@ resolve(now);

}
exports.forAnimationFrame = forAnimationFrame;

@@ -1,4 +0,7 @@

export function forDocumentLoad() {
return new Promise((resolve) => {
window.addEventListener('load', () => {
"use strict";
exports.__esModule = true;
exports.forDocumentLoad = void 0;
function forDocumentLoad() {
return new Promise(function (resolve) {
window.addEventListener('load', function () {
resolve();

@@ -8,1 +11,2 @@ });

}
exports.forDocumentLoad = forDocumentLoad;

@@ -1,4 +0,7 @@

export function forDOMContentLoaded() {
return new Promise((resolve) => {
document.addEventListener('DOMContentLoaded', () => {
"use strict";
exports.__esModule = true;
exports.forDOMContentLoaded = void 0;
function forDOMContentLoaded() {
return new Promise(function (resolve) {
document.addEventListener('DOMContentLoaded', function () {
resolve();

@@ -8,1 +11,2 @@ });

}
exports.forDOMContentLoaded = forDOMContentLoaded;

@@ -1,5 +0,9 @@

export function forEver() {
return new Promise(() => {
"use strict";
exports.__esModule = true;
exports.forEver = void 0;
function forEver() {
return new Promise(function () {
// Note: Never ever resolved
});
}
exports.forEver = forEver;

@@ -1,6 +0,10 @@

import { forTime } from './forTime.js';
export function forImmediate() {
"use strict";
exports.__esModule = true;
exports.forImmediate = void 0;
var forTime_js_1 = require("./forTime.js");
function forImmediate() {
// Note: Not using setImmediate because it is non-standard feature only in browser window
// @see https://developer.mozilla.org/en-US/docs/Web/API/Window/setImmediate
return forTime(0);
return (0, forTime_js_1.forTime)(0);
}
exports.forImmediate = forImmediate;

@@ -1,4 +0,7 @@

export function forTime(miliseconds) {
return new Promise((resolve) => {
setTimeout(() => {
"use strict";
exports.__esModule = true;
exports.forTime = void 0;
function forTime(miliseconds) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve();

@@ -8,1 +11,2 @@ }, miliseconds);

}
exports.forTime = forTime;

@@ -1,11 +0,65 @@

import { forTime } from './forTime.js';
export async function forTimeout(timeout, value) {
return await Promise.race([
value,
(async () => {
await forTime(timeout);
throw new Error(`Timeout ${timeout}ms is up.`);
})(),
]);
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
exports.__esModule = true;
exports.forTimeout = void 0;
var forTime_js_1 = require("./forTime.js");
function forTimeout(timeout, value) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Promise.race([
value,
(function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, forTime_js_1.forTime)(timeout)];
case 1:
_a.sent();
throw new Error("Timeout " + timeout + "ms is up.");
}
});
}); })(),
])];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
}
exports.forTimeout = forTimeout;
/*

@@ -12,0 +66,0 @@ Note: Possible alternative way:

@@ -1,2 +0,41 @@

import { forTime } from './forTime.js';
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
exports.__esModule = true;
exports.forTimeSynced = void 0;
var forTime_js_1 = require("./forTime.js");
/**

@@ -7,7 +46,20 @@ * This waiter function will sync some action to some precise time

*/
export async function forTimeSynced(period, shift = 0) {
const currentTime = new Date().getTime();
const wait = period - ((currentTime - shift) % period);
await forTime(wait);
function forTimeSynced(period, shift) {
if (shift === void 0) { shift = 0; }
return __awaiter(this, void 0, void 0, function () {
var currentTime, wait;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
currentTime = new Date().getTime();
wait = period - ((currentTime - shift) % period);
return [4 /*yield*/, (0, forTime_js_1.forTime)(wait)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
exports.forTimeSynced = forTimeSynced;
// TODO: !!! Human readable forTimeSynced (it is somewhere already implemented)

@@ -1,14 +0,82 @@

import { forTime } from './forTime.js';
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
exports.__esModule = true;
exports.forValueDefined = void 0;
var forTime_js_1 = require("./forTime.js");
// TODO: !!! Options style in func params everywhere
export async function forValueDefined(getValue, waiter = async () => {
await forTime(10);
}, limit = 1000) {
for (let i = 0; i < limit; i++) {
const value = await getValue();
if (value !== undefined && value !== null) {
return value;
}
await waiter();
}
throw new Error(`Limit ${limit} overflowed in waitForDefined.`);
function forValueDefined(getValue, waiter, limit) {
var _this = this;
if (waiter === void 0) { waiter = function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (0, forTime_js_1.forTime)(10)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); }; }
if (limit === void 0) { limit = 1000; }
return __awaiter(this, void 0, void 0, function () {
var i, value;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
i = 0;
_a.label = 1;
case 1:
if (!(i < limit)) return [3 /*break*/, 5];
return [4 /*yield*/, getValue()];
case 2:
value = _a.sent();
if (value !== undefined && value !== null) {
return [2 /*return*/, value];
}
return [4 /*yield*/, waiter()];
case 3:
_a.sent();
_a.label = 4;
case 4:
i++;
return [3 /*break*/, 1];
case 5: throw new Error("Limit " + limit + " overflowed in waitForDefined.");
}
});
});
}
exports.forValueDefined = forValueDefined;

@@ -0,13 +1,25 @@

"use strict";
// GENERATED WITH generate-main-exports
// Warning: Do not edit by hand, all changes will be lost on next execution!
import { forAllImagesInElement } from './functions/forAllImagesInElement.js';
import { forAnimationFrame } from './functions/forAnimationFrame.js';
import { forDocumentLoad } from './functions/forDocumentLoad.js';
import { forDOMContentLoaded } from './functions/forDOMContentLoaded.js';
import { forEver } from './functions/forEver.js';
import { forImmediate } from './functions/forImmediate.js';
import { forTime } from './functions/forTime.js';
import { forTimeout } from './functions/forTimeout.js';
import { forTimeSynced } from './functions/forTimeSynced.js';
import { forValueDefined } from './functions/forValueDefined.js';
export { forTime, forEver, forTimeout, forImmediate, forTimeSynced, forValueDefined, forDocumentLoad, forAnimationFrame, forDOMContentLoaded, forAllImagesInElement, };
exports.__esModule = true;
exports.forAllImagesInElement = exports.forDOMContentLoaded = exports.forAnimationFrame = exports.forDocumentLoad = exports.forValueDefined = exports.forTimeSynced = exports.forImmediate = exports.forTimeout = exports.forEver = exports.forTime = void 0;
var forAllImagesInElement_js_1 = require("./functions/forAllImagesInElement.js");
exports.forAllImagesInElement = forAllImagesInElement_js_1.forAllImagesInElement;
var forAnimationFrame_js_1 = require("./functions/forAnimationFrame.js");
exports.forAnimationFrame = forAnimationFrame_js_1.forAnimationFrame;
var forDocumentLoad_js_1 = require("./functions/forDocumentLoad.js");
exports.forDocumentLoad = forDocumentLoad_js_1.forDocumentLoad;
var forDOMContentLoaded_js_1 = require("./functions/forDOMContentLoaded.js");
exports.forDOMContentLoaded = forDOMContentLoaded_js_1.forDOMContentLoaded;
var forEver_js_1 = require("./functions/forEver.js");
exports.forEver = forEver_js_1.forEver;
var forImmediate_js_1 = require("./functions/forImmediate.js");
exports.forImmediate = forImmediate_js_1.forImmediate;
var forTime_js_1 = require("./functions/forTime.js");
exports.forTime = forTime_js_1.forTime;
var forTimeout_js_1 = require("./functions/forTimeout.js");
exports.forTimeout = forTimeout_js_1.forTimeout;
var forTimeSynced_js_1 = require("./functions/forTimeSynced.js");
exports.forTimeSynced = forTimeSynced_js_1.forTimeSynced;
var forValueDefined_js_1 = require("./functions/forValueDefined.js");
exports.forValueDefined = forValueDefined_js_1.forValueDefined;
{
"name": "waitasecond",
"version": "1.8.1",
"version": "1.8.2",
"description": "Simple tool library for the waiting using Promises.",
"type": "module",
"main": "dist/main.js",

@@ -12,4 +11,4 @@ "typings": "dist/main.d.ts",

],
"directions": {
"functions": "dist/functions"
"exports": {
"functions": "./dist/functions/"
},

@@ -16,0 +15,0 @@ "scripts": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc