@randajan/jet-base
Advanced tools
Comparing version 2.0.4 to 2.1.0
@@ -1,470 +0,2 @@ | ||
// src/Base.js | ||
import jet4 from "@randajan/jet-core"; | ||
// src/defs.js | ||
var LIST = /* @__PURE__ */ new Map(); | ||
var states = ["error", "pending", "initializing", "ready"]; | ||
var use = (base) => LIST.get(base); | ||
var register = (base) => { | ||
const priv = { | ||
paths: /* @__PURE__ */ new Set(), | ||
flat: {}, | ||
fits: {}, | ||
watches: {}, | ||
stateCode: 1, | ||
debug: false | ||
}; | ||
Object.defineProperty(priv, "state", { get: (_) => states[priv.stateCode] }); | ||
LIST.set(base, priv); | ||
return priv; | ||
}; | ||
var config = (base, options) => { | ||
const _p = use(base); | ||
if (_p.stateCode !== 1) { | ||
base.throw("config", `state must be '${states[1]}' insted of ${_p.state}`); | ||
} | ||
if (_p.optionsSet) { | ||
base.throw("config", "can be called just once"); | ||
} | ||
_p.optionsSet = true; | ||
_p.options = options; | ||
return base; | ||
}; | ||
var init = (base, options) => { | ||
const _p = use(base); | ||
if (_p.stateCode !== 1) { | ||
base.throw("init", `state must be '${states[1]}' insted of '${_p.state}'`); | ||
} | ||
if (_p.optionsSet && options) { | ||
base.throw("init", "options was predefined with config()"); | ||
} | ||
_p.stateCode = 2; | ||
const closure = (isReady, error) => { | ||
if (isReady) { | ||
_p.stateCode = 3; | ||
} else { | ||
_p.stateCode = 0; | ||
_p.error = error || new Error("Unknown error"); | ||
base.throw("init", _p.error); | ||
} | ||
return base; | ||
}; | ||
return _p.initialization = _p.onInit(_p.options = Object.jet.to(options || _p.options), closure); | ||
}; | ||
var autoInit = (base) => { | ||
if (base.stateCode === 3) { | ||
return; | ||
} | ||
if (base.stateCode === 0) { | ||
base.throw("", use(base).error); | ||
} | ||
if (base.stateCode === 2) { | ||
return use(base).initialization; | ||
} | ||
if (base.stateCode === 1) { | ||
return init(base); | ||
} | ||
}; | ||
// src/duty.js | ||
import jet3 from "@randajan/jet-core"; | ||
// src/vals.js | ||
import jet2 from "@randajan/jet-core"; | ||
var spread = (path) => { | ||
const x = path.lastIndexOf("."); | ||
if (x < 0) { | ||
return [path, "", path]; | ||
} | ||
return [path, path.slice(0, x), path.slice(x + 1)]; | ||
}; | ||
var forEach = (value, callback, path) => { | ||
const put2 = (v, p) => callback(v, p, false); | ||
const deep = (v, p) => { | ||
jet2.forEach(v, put2, deep, p); | ||
return callback(v, p, true); | ||
}; | ||
return jet2.forEach(value, put2, deep, path); | ||
}; | ||
var deflate = (paths, path, value) => { | ||
const flat = { [path]: value }; | ||
let p = path; | ||
do { | ||
if (paths.has(p)) { | ||
break; | ||
} | ||
paths.add(p); | ||
const x = p.lastIndexOf("."); | ||
if (x <= 0) { | ||
break; | ||
} | ||
p = p.slice(0, x); | ||
} while (true); | ||
forEach(value, (v, p2) => { | ||
paths.add(p2); | ||
flat[p2] = v; | ||
}, path); | ||
return flat; | ||
}; | ||
var prepareParent = (key, parent, img, copy = false) => { | ||
if (parent) { | ||
return parent; | ||
} | ||
if (!img || !jet2.isMapable(img)) { | ||
return String.jet.isNumeric(key) ? [] : {}; | ||
} | ||
return copy ? jet2.copy(img, true, false) : jet2.create(jet2(img, false)); | ||
}; | ||
var put = (path, value, parents, parentImgs, imgCopy = false) => { | ||
const [p, d, k] = spread(path); | ||
parents[d] = prepareParent(k, parents[d], parentImgs[d], imgCopy); | ||
if (p) { | ||
jet2.set(parents[d], k, value); | ||
} | ||
parents[p] = value; | ||
}; | ||
var fit = (fits, path, to, from) => fits[path] && fits[path].fit ? fits[path].fit(to[path], from[path]) : to[path]; | ||
var set = (base, path, value) => { | ||
const _p = use(base), { input, flat, fits, paths, watches } = _p; | ||
path = String.jet.to(path, "."); | ||
const flatIn = deflate(paths, path, value); | ||
const rawPaths = [...paths].sort(); | ||
for (let i = rawPaths.length - 1; i >= 0; i--) { | ||
const p = rawPaths[i]; | ||
if (path === p) { | ||
put(p, fit(fits, p, flatIn, flat), flatIn, flat, true); | ||
} else if (path.startsWith(p)) { | ||
put(p, fit(fits, p, flatIn, flat), flatIn, flat, true); | ||
} else if (p.startsWith(path)) { | ||
put(p, fit(fits, p, flatIn, flat), flatIn, flatIn); | ||
} | ||
} | ||
_p.input = path ? jet2.digIn(input, path, value, true) : value; | ||
const output = _p.output = flatIn[""]; | ||
const pathsOut = _p.paths = /* @__PURE__ */ new Set(); | ||
const flatOut = _p.flat = { "": output }; | ||
const cngs = _p.changes = []; | ||
pathsOut.add(""); | ||
forEach(output, (v, p, isMapable) => { | ||
if (!isMapable && v !== flat[p]) { | ||
cngs.push(p); | ||
} | ||
pathsOut.add(p); | ||
flatOut[p] = v; | ||
delete flat[p]; | ||
}); | ||
for (const p in flat) { | ||
if (!jet2.isMapable(flat[p]) && flatOut[p] != flat[p]) { | ||
cngs.push(p); | ||
} | ||
} | ||
if (cngs.length) { | ||
const c = " ^" + cngs.join(" ^"); | ||
for (const p in watches) { | ||
if (c.includes(" ^" + p)) { | ||
watches[p].run(cngs); | ||
} | ||
} | ||
} | ||
return cngs; | ||
}; | ||
var get = (base, path) => use(base).flat[String.jet.to(path, ".")]; | ||
// src/duty.js | ||
var formatDutyArgs = (path, fce) => { | ||
if (fce === void 0 && jet3.isRunnable(path)) { | ||
fce = path; | ||
path = ""; | ||
} else { | ||
path = String.jet.to(path, "."); | ||
} | ||
return [path, fce]; | ||
}; | ||
var addDuty = (priv, kind, path, fce) => (priv[kind][path] = priv[kind][path] || jet3.create.RunPool()).add(fce); | ||
var filterChanges = (path, changes) => { | ||
if (!path) { | ||
return [...changes]; | ||
} | ||
const result = []; | ||
for (const c of changes) { | ||
if (c.startsWith(path)) { | ||
result.push(c.slice(path.length + 1)); | ||
} | ||
} | ||
return result; | ||
}; | ||
var addWatch = (base, path, fce, initRun = false) => { | ||
[path, fce] = formatDutyArgs(path, fce); | ||
const get2 = (p) => base.get([path, p]); | ||
if (initRun) { | ||
setTimeout((_) => fce(get2, (_2) => [])); | ||
} | ||
return addDuty(use(base), "watches", path, (cngs) => { | ||
fce(get2, (_) => filterChanges(path, cngs)); | ||
}); | ||
}; | ||
var addFit = (base, path, fce) => { | ||
[path, fce] = formatDutyArgs(path, fce); | ||
const _p = use(base); | ||
const rem = addDuty(_p, "fits", path, fce); | ||
const refit = path ? (_) => set(base, path, jet3.digOut(_p.input, path)) : (_) => set(base, path, _p.input); | ||
refit(); | ||
return (_) => { | ||
rem(); | ||
refit(); | ||
}; | ||
}; | ||
// src/Base.js | ||
var Base = class { | ||
constructor(onInit) { | ||
const _p = register(this); | ||
const props = { | ||
error: { enumerable: true, get: (_) => _p.error }, | ||
state: { enumerable: true, get: (_) => _p.state }, | ||
stateCode: { enumerable: true, get: (_) => _p.stateCode }, | ||
debug: { enumerable: true, get: (_) => _p.debug, set: (v) => _p.debug = Boolean.jet.to(debug) } | ||
}; | ||
Object.defineProperties(this, props); | ||
_p.onInit = (options, closure) => { | ||
const image = Object.defineProperties({}, props); | ||
for (const i in Object.getOwnPropertyDescriptors(Base.prototype)) { | ||
if (i !== "constructor") { | ||
image[i] = Base.prototype[i].bind(this); | ||
} | ||
} | ||
return onInit(image, options, closure); | ||
}; | ||
} | ||
log(...msg) { | ||
if (this.debug) { | ||
console.log("jet-base log", ...msg); | ||
} | ||
} | ||
throw(method, msg, path) { | ||
throw `jet-base${method ? ` ${method}(...)` : ""}${path ? ` path '${path}'` : ""} ${msg}`; | ||
} | ||
config(options) { | ||
return config(this, options); | ||
} | ||
init(options) { | ||
return init(this, options); | ||
} | ||
is(path, value) { | ||
return get(this, path) === value; | ||
} | ||
isType(path, type, strict = true) { | ||
return jet4.is(type, get(this, path), strict); | ||
} | ||
isFull(path) { | ||
return jet4.isFull(get(this, path)); | ||
} | ||
get(path, def2) { | ||
const v = get(this, path); | ||
return v != null ? jet4.copy(v, true) : def2; | ||
} | ||
getType(path, strict = true) { | ||
return jet4(get(this, path), strict); | ||
} | ||
set(path, value) { | ||
if (value === void 0 && typeof path === "object" && !Array.isArray(path)) { | ||
value = path; | ||
path = ""; | ||
} | ||
const ms = new Date(); | ||
const changes = set(this, path, value); | ||
this.log("changes", new Date() - ms + "ms", changes); | ||
return changes; | ||
} | ||
remove(path) { | ||
return set(this, path); | ||
} | ||
pull(path) { | ||
const v = get(this, path); | ||
set(this, path); | ||
return v != null ? jet4.copy(v, true) : def; | ||
} | ||
watch(path, fce, initRun = false) { | ||
return addWatch(this, path, fce, initRun); | ||
} | ||
fit(path, fce) { | ||
return addFit(this, path, fce); | ||
} | ||
fitTo(path, type, ...args) { | ||
return addFit(this, path, (next, v, f) => jet4.to(type, next(v, f), ...args)); | ||
} | ||
fitType(path, type, strict = true) { | ||
return addFit(this, path, (next, v, f) => { | ||
v = next(v, f); | ||
return jet4.is(type, v, strict) ? v : jet4.create(type); | ||
}); | ||
} | ||
setDefault(path, value, fullDetect = true) { | ||
const isFull = fullDetect ? jet4.isFull : (v) => v != null; | ||
return addFit(this, path, (next, v, f) => isFull(next(v, f)) ? v : value); | ||
} | ||
setLock(path, value) { | ||
if (value === void 0) { | ||
value = get(this, path); | ||
} | ||
return addFit(this, path, (_) => value); | ||
} | ||
}; | ||
var Base_default = Base; | ||
// src/BaseSync.js | ||
var BaseSync = class extends Base_default { | ||
constructor(onInit) { | ||
super((base, options, closure) => { | ||
if (!jet.isRunnable(onInit)) { | ||
return closure(true); | ||
} | ||
try { | ||
onInit(base, options); | ||
} catch (e) { | ||
return closure(false, e); | ||
} | ||
return closure(true); | ||
}); | ||
} | ||
is(path, value) { | ||
autoInit(this); | ||
return super.is(path, value); | ||
} | ||
isType(path, type, strict = true) { | ||
autoInit(this); | ||
return super.isType(path, type, strict); | ||
} | ||
isFull(path) { | ||
autoInit(this); | ||
return super.isFull(path); | ||
} | ||
get(path, def2) { | ||
autoInit(this, true); | ||
return super.get(path, def2); | ||
} | ||
getType(path, strict = true) { | ||
autoInit(this); | ||
return super.getType(path, strict); | ||
} | ||
set(path, value) { | ||
autoInit(this); | ||
return super.set(path, value); | ||
} | ||
remove(path) { | ||
autoInit(this); | ||
return super.remove(path); | ||
} | ||
pull(path) { | ||
autoInit(this); | ||
return super.pull(path); | ||
} | ||
watch(path, fce, initRun = false) { | ||
autoInit(this, true); | ||
return super.watch(path, fce, initRun); | ||
} | ||
fit(path, fce) { | ||
autoInit(this); | ||
return super.fit(path, fce); | ||
} | ||
fitTo(path, type, ...args) { | ||
autoInit(this); | ||
return super.fitTo(path, type, ...args); | ||
} | ||
fitType(path, type, strict = true) { | ||
autoInit(this); | ||
return super.fitType(path, type, strict); | ||
} | ||
setDefault(path, value, fullDetect = true) { | ||
autoInit(this); | ||
return super.setDefault(path, value, fullDetect); | ||
} | ||
setLock(path, value) { | ||
autoInit(this); | ||
return super.setLock(path, value); | ||
} | ||
}; | ||
var BaseSync_default = BaseSync; | ||
// src/BaseAsync.js | ||
var BaseAsync = class extends Base_default { | ||
constructor(onInit) { | ||
super(async (image, options, closure) => { | ||
if (!jet.isRunnable(onInit)) { | ||
return closure(true); | ||
} | ||
try { | ||
await onInit(image, options); | ||
} catch (e) { | ||
return closure(false, e); | ||
} | ||
return closure(true); | ||
}); | ||
} | ||
async is(path, value) { | ||
await autoInit(this); | ||
return super.is(path, value); | ||
} | ||
async isType(path, type, strict = true) { | ||
await autoInit(this, true); | ||
return super.isType(path, type, strict); | ||
} | ||
async isFull(path) { | ||
await autoInit(this, true); | ||
return super.isFull(path); | ||
} | ||
async get(path, def2) { | ||
await autoInit(this, true); | ||
return super.get(path, def2); | ||
} | ||
async getType(path, strict = true) { | ||
await autoInit(this, true); | ||
return super.getType(path, strict); | ||
} | ||
async set(path, value) { | ||
await autoInit(this); | ||
return super.set(path, value); | ||
} | ||
async remove(path) { | ||
await autoInit(this); | ||
return super.remove(path); | ||
} | ||
async pull(path) { | ||
await autoInit(this); | ||
return super.pull(path); | ||
} | ||
async watch(path, fce, initRun = false) { | ||
await autoInit(this, true); | ||
return super.watch(path, fce, initRun); | ||
} | ||
async fit(path, fce) { | ||
await autoInit(this); | ||
return super.fit(path, fce); | ||
} | ||
async fitTo(path, type, ...args) { | ||
await autoInit(this); | ||
return super.fitTo(path, type, ...args); | ||
} | ||
async fitType(path, type, strict = true) { | ||
await autoInit(this); | ||
return super.fitType(path, type, strict); | ||
} | ||
async setDefault(path, value, fullDetect = true) { | ||
await autoInit(this); | ||
return super.setDefault(path, value, fullDetect); | ||
} | ||
async setLock(path, value) { | ||
await autoInit(this); | ||
return super.setLock(path, value); | ||
} | ||
}; | ||
var BaseAsync_default = BaseAsync; | ||
// src/index.js | ||
var src_default = { BaseSync: BaseSync_default, BaseAsync: BaseAsync_default }; | ||
export { | ||
BaseAsync_default as BaseAsync, | ||
BaseSync_default as BaseSync, | ||
src_default as default | ||
}; | ||
import{a as r}from"./chunk-ZROAN64L.js";import{a as o}from"./chunk-V6RJHOKA.js";import"./chunk-S7HT22IM.js";var m={BaseSync:o,BaseAsync:r};export{r as BaseAsync,o as BaseSync,m as default}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@randajan/jet-base", | ||
"version": "2.0.4", | ||
"version": "2.1.0", | ||
"description": "Ecosystem of types and related usefull tools.", | ||
@@ -10,3 +10,5 @@ "repository": "randajan/jet-base", | ||
"exports": { | ||
".": "./dist/index.js" | ||
".": "./dist/index.js", | ||
"./sync": "./dist/symc.js", | ||
"./async": "./dist/async.js" | ||
}, | ||
@@ -19,6 +21,6 @@ "scripts": { | ||
"devDependencies": { | ||
"@randajan/simple-lib": "^1.2.1" | ||
"@randajan/simple-lib": "^2.0.0" | ||
}, | ||
"peerDependencies": { | ||
"@randajan/jet-core": "^3.0.8" | ||
"@randajan/jet-core": "^3.0.11" | ||
}, | ||
@@ -25,0 +27,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15
36976
34
1