reactronic
Version:
Reactronic - Transactional Reactive State Management
110 lines (109 loc) • 4.13 kB
JavaScript
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());
});
};
export class Utils {
static freezeSet(obj) {
if (obj instanceof Set) {
const pd = { configurable: false, enumerable: false, get: UNDEF, set: UNDEF };
Object.defineProperty(obj, "add", pd);
Object.defineProperty(obj, "delete", pd);
Object.defineProperty(obj, "clear", pd);
Object.freeze(obj);
}
return obj;
}
static freezeMap(obj) {
if (obj instanceof Map) {
const pd = { configurable: false, enumerable: false, get: UNDEF, set: UNDEF };
Object.defineProperty(obj, "set", pd);
Object.defineProperty(obj, "delete", pd);
Object.defineProperty(obj, "clear", pd);
Object.freeze(obj);
}
return obj;
}
static copyAllMembers(source, target) {
for (const m of Object.getOwnPropertyNames(source))
target[m] = source[m];
for (const m of Object.getOwnPropertySymbols(source))
target[m] = source[m];
return target;
}
}
export function UNDEF(...args) {
throw new Error("this method should never be called");
}
export function all(promises) {
return __awaiter(this, void 0, void 0, function* () {
let error;
const result = yield Promise.all(promises.map(x => x.catch(e => { error = error || e; return e; })));
if (error)
throw error;
return result;
});
}
export function pause(timeout) {
return new Promise(function (resolve) {
setTimeout(resolve.bind(null, () => resolve), timeout);
});
}
export function proceedSyncOrAsync(result, success, failure) {
let r;
if (result instanceof Promise)
r = result.then(v => success(v), e => failure(e));
else
r = success(result);
return r;
}
export function emitLetters(n) {
if (n < 0)
throw new Error(`emitLetters: argument (${n}) should not be negative or zero`);
let result = "";
while (n >= 0) {
const r = n % 26;
n = Math.floor(n / 26) - 1;
result = String.fromCharCode(65 + r) + result;
}
return result;
}
export function objectHasMember(obj, member) {
return obj === Object(obj) && !Array.isArray(obj) && member in obj;
}
export function getCallerInfo(prefix) {
const restore = Error.stackTraceLimit = 20;
const error = new Error();
const stack = error.stack || "";
Error.stackTraceLimit = restore;
const lines = stack.split("\n");
let i = lines.findIndex(x => x.indexOf(".declare") >= 0);
i = i >= 0 ? i + 2 : 5;
let caller = extractFunctionAndLocation(lines[i]);
let location = caller;
if (caller.func.endsWith(".script")) {
i = i - 1;
caller = extractFunctionAndLocation(lines[i]);
location = extractFunctionAndLocation(lines[i + 1]);
}
else {
while (!caller.func && i > 0) {
i = i - 1;
caller = extractFunctionAndLocation(lines[i]);
}
location = extractFunctionAndLocation(lines[i + 1]);
}
const result = `${prefix}·${caller.func}@${location.file}`;
return result;
}
function extractFunctionAndLocation(s) {
const match = s.match(/(?:\s*at\s+)?(?:(\S+)\s\()?(?:.*?)([^\/\(\)]+)(?:(:|\d)*\)?)$/);
return {
func: (match === null || match === void 0 ? void 0 : match[1]) || "",
file: (match === null || match === void 0 ? void 0 : match[2]) || "",
};
}