@thi.ng/api
Advanced tools
Comparing version 8.9.11 to 8.9.12
32
api.js
@@ -1,17 +0,15 @@ | ||
export const DEFAULT_EPS = 1e-6; | ||
/** | ||
* Internal use only. **Do NOT use in user land code!** | ||
* | ||
* @internal | ||
*/ | ||
export const SEMAPHORE = Symbol(); | ||
/** | ||
* No-effect placeholder function. | ||
*/ | ||
export const NO_OP = () => { }; | ||
/** | ||
* Catch-all event ID | ||
*/ | ||
export const EVENT_ALL = "*"; | ||
export const EVENT_ENABLE = "enable"; | ||
export const EVENT_DISABLE = "disable"; | ||
const DEFAULT_EPS = 1e-6; | ||
const SEMAPHORE = Symbol(); | ||
const NO_OP = () => { | ||
}; | ||
const EVENT_ALL = "*"; | ||
const EVENT_ENABLE = "enable"; | ||
const EVENT_DISABLE = "disable"; | ||
export { | ||
DEFAULT_EPS, | ||
EVENT_ALL, | ||
EVENT_DISABLE, | ||
EVENT_ENABLE, | ||
NO_OP, | ||
SEMAPHORE | ||
}; |
# Change Log | ||
- **Last updated**: 2023-12-09T19:12:03Z | ||
- **Last updated**: 2023-12-11T10:07:09Z | ||
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub) | ||
@@ -5,0 +5,0 @@ |
@@ -1,9 +0,6 @@ | ||
/** | ||
* Property decorator factory. Sets `configurable` flag of PropertyDescriptor | ||
* to given state. | ||
* | ||
* @param state - true, if propoerty is configurable | ||
*/ | ||
export const configurable = (state) => function (_, __, descriptor) { | ||
descriptor.configurable = state; | ||
const configurable = (state) => function(_, __, descriptor) { | ||
descriptor.configurable = state; | ||
}; | ||
export { | ||
configurable | ||
}; |
@@ -1,20 +0,15 @@ | ||
/** | ||
* Method property decorator factory. Augments original method with | ||
* deprecation message (via console), shown when method is invoked. | ||
* Accepts optional message arg. Throws error if assigned property | ||
* is not a function. | ||
* | ||
* @param msg - deprecation message | ||
*/ | ||
export const deprecated = (msg, log = console.log) => function (target, prop, descriptor) { | ||
const signature = `${target.constructor.name}#${prop.toString()}`; | ||
const fn = descriptor.value; | ||
if (typeof fn !== "function") { | ||
throw new Error(`${signature} is not a function`); | ||
} | ||
descriptor.value = function () { | ||
log(`DEPRECATED ${signature}: ${msg || "will be removed soon"}`); | ||
return fn.apply(this, arguments); | ||
}; | ||
return descriptor; | ||
const deprecated = (msg, log = console.log) => function(target, prop, descriptor) { | ||
const signature = `${target.constructor.name}#${prop.toString()}`; | ||
const fn = descriptor.value; | ||
if (typeof fn !== "function") { | ||
throw new Error(`${signature} is not a function`); | ||
} | ||
descriptor.value = function() { | ||
log(`DEPRECATED ${signature}: ${msg || "will be removed soon"}`); | ||
return fn.apply(this, arguments); | ||
}; | ||
return descriptor; | ||
}; | ||
export { | ||
deprecated | ||
}; |
@@ -1,10 +0,6 @@ | ||
/** | ||
* Method property decorator. Sets `configurable` flag of | ||
* PropertyDescriptor to `false` (same as `@configurable(false)`). | ||
* Intended to be used in combination with mixin decorators to enable | ||
* partial implementations of mixed-in behaviors in target class and | ||
* avoid them being overidden by mixed-in behaviour. | ||
*/ | ||
export const nomixin = (_, __, descriptor) => { | ||
descriptor.configurable = false; | ||
const nomixin = (_, __, descriptor) => { | ||
descriptor.configurable = false; | ||
}; | ||
export { | ||
nomixin | ||
}; |
@@ -1,9 +0,7 @@ | ||
/** | ||
* Class decorator. Seals both constructor and prototype. | ||
* | ||
* @param constructor - class ctor to seal | ||
*/ | ||
export const sealed = (constructor) => { | ||
Object.seal(constructor); | ||
Object.seal(constructor.prototype); | ||
const sealed = (constructor) => { | ||
Object.seal(constructor); | ||
Object.seal(constructor.prototype); | ||
}; | ||
export { | ||
sealed | ||
}; |
19
deref.js
@@ -1,13 +0,6 @@ | ||
/** | ||
* Returns true iff `x` implements {@link IDeref}. | ||
* | ||
* @param x - | ||
*/ | ||
export const isDeref = (x) => x != null && typeof x["deref"] === "function"; | ||
/** | ||
* If `x` implements {@link IDeref}, returns its wrapped value, else | ||
* returns `x` itself. | ||
* | ||
* @param x - | ||
*/ | ||
export const deref = (x) => (isDeref(x) ? x.deref() : x); | ||
const isDeref = (x) => x != null && typeof x["deref"] === "function"; | ||
const deref = (x) => isDeref(x) ? x.deref() : x; | ||
export { | ||
deref, | ||
isDeref | ||
}; |
22
fn.js
@@ -1,14 +0,8 @@ | ||
/** | ||
* Identity function: `(x) => x` | ||
* | ||
* @param x | ||
*/ | ||
export const identity = (x) => x; | ||
/** | ||
* Zero-arg function always returning true. | ||
*/ | ||
export const always = () => true; | ||
/** | ||
* Zero-arg function always returning false. | ||
*/ | ||
export const never = () => false; | ||
const identity = (x) => x; | ||
const always = () => true; | ||
const never = () => false; | ||
export { | ||
always, | ||
identity, | ||
never | ||
}; |
72
mixin.js
@@ -1,45 +0,35 @@ | ||
/** | ||
* Class behavior mixin based on: | ||
* - http://raganwald.com/2015/06/26/decorators-in-es7.html | ||
* | ||
* Additionally only injects/overwrites properties in target, which are NOT | ||
* marked with | ||
* [`@nomixin`](https://docs.thi.ng/umbrella/api/functions/nomixin.html) (i.e. | ||
* those which haven't set their `configurable` property descriptor flag to | ||
* `false`) | ||
* | ||
* @param behaviour - to mixin | ||
* @param sharedBehaviour - | ||
* @returns decorator function | ||
*/ | ||
export const mixin = (behaviour, sharedBehaviour = {}) => { | ||
const instanceKeys = Reflect.ownKeys(behaviour); | ||
const sharedKeys = Reflect.ownKeys(sharedBehaviour); | ||
const typeTag = Symbol("isa"); | ||
function _mixin(clazz) { | ||
for (let key of instanceKeys) { | ||
const existing = Object.getOwnPropertyDescriptor(clazz.prototype, key); | ||
if (!existing || existing.configurable) { | ||
Object.defineProperty(clazz.prototype, key, { | ||
value: behaviour[key], | ||
writable: true, | ||
}); | ||
} | ||
else { | ||
// console.log(`not patching: ${clazz.name}.${key.toString()}`); | ||
} | ||
} | ||
Object.defineProperty(clazz.prototype, typeTag, { value: true }); | ||
return clazz; | ||
} | ||
for (let key of sharedKeys) { | ||
Object.defineProperty(_mixin, key, { | ||
value: sharedBehaviour[key], | ||
enumerable: sharedBehaviour.propertyIsEnumerable(key), | ||
const mixin = (behaviour, sharedBehaviour = {}) => { | ||
const instanceKeys = Reflect.ownKeys(behaviour); | ||
const sharedKeys = Reflect.ownKeys(sharedBehaviour); | ||
const typeTag = Symbol("isa"); | ||
function _mixin(clazz) { | ||
for (let key of instanceKeys) { | ||
const existing = Object.getOwnPropertyDescriptor( | ||
clazz.prototype, | ||
key | ||
); | ||
if (!existing || existing.configurable) { | ||
Object.defineProperty(clazz.prototype, key, { | ||
value: behaviour[key], | ||
writable: true | ||
}); | ||
} else { | ||
} | ||
} | ||
Object.defineProperty(_mixin, Symbol.hasInstance, { | ||
value: (x) => !!x[typeTag], | ||
Object.defineProperty(clazz.prototype, typeTag, { value: true }); | ||
return clazz; | ||
} | ||
for (let key of sharedKeys) { | ||
Object.defineProperty(_mixin, key, { | ||
value: sharedBehaviour[key], | ||
enumerable: sharedBehaviour.propertyIsEnumerable(key) | ||
}); | ||
return _mixin; | ||
} | ||
Object.defineProperty(_mixin, Symbol.hasInstance, { | ||
value: (x) => !!x[typeTag] | ||
}); | ||
return _mixin; | ||
}; | ||
export { | ||
mixin | ||
}; |
import { EVENT_DISABLE, EVENT_ENABLE } from "../api.js"; | ||
import { mixin } from "../mixin.js"; | ||
/** | ||
* Mixin class decorator, injects IEnable default implementation, incl. a | ||
* `_enabled` property. If the target also implements the {@link INotify} | ||
* interface, {@link IEnable.enable} and {@link IEnable.disable} will | ||
* automatically emit the respective events. | ||
*/ | ||
export const IEnableMixin = mixin({ | ||
_enabled: true, | ||
isEnabled() { | ||
return this._enabled; | ||
}, | ||
enable() { | ||
$enable(this, true, EVENT_ENABLE); | ||
}, | ||
disable() { | ||
$enable(this, false, EVENT_DISABLE); | ||
}, | ||
toggle() { | ||
this._enabled ? this.disable() : this.enable(); | ||
return this._enabled; | ||
}, | ||
const IEnableMixin = mixin({ | ||
_enabled: true, | ||
isEnabled() { | ||
return this._enabled; | ||
}, | ||
enable() { | ||
$enable(this, true, EVENT_ENABLE); | ||
}, | ||
disable() { | ||
$enable(this, false, EVENT_DISABLE); | ||
}, | ||
toggle() { | ||
this._enabled ? this.disable() : this.enable(); | ||
return this._enabled; | ||
} | ||
}); | ||
const $enable = (target, state, id) => { | ||
target._enabled = state; | ||
if (target.notify) { | ||
target.notify({ id, target }); | ||
} | ||
target._enabled = state; | ||
if (target.notify) { | ||
target.notify({ id, target }); | ||
} | ||
}; | ||
export { | ||
IEnableMixin | ||
}; |
import { mixin } from "../mixin.js"; | ||
/** | ||
* Default implementation for {@link IGrid1D} methods. | ||
*/ | ||
export const IGrid1DMixin = mixin({ | ||
order() { | ||
return [0]; | ||
}, | ||
includes(x) { | ||
return x >= 0 && x < this.size[0]; | ||
}, | ||
indexAt(x) { | ||
return this.includes(x) ? this.indexAtUnsafe(x) : -1; | ||
}, | ||
indexAtUnsafe(x) { | ||
return this.offset + (x | 0) * this.stride[0]; | ||
}, | ||
getAt(x) { | ||
return this.includes(x) ? this.data[this.indexAtUnsafe(x)] : 0; | ||
}, | ||
getAtUnsafe(x) { | ||
return this.data[this.indexAtUnsafe(x)]; | ||
}, | ||
setAt(x, val) { | ||
return this.includes(x) | ||
? ((this.data[this.indexAtUnsafe(x)] = val), true) | ||
: false; | ||
}, | ||
setAtUnsafe(x, val) { | ||
this.data[this.indexAtUnsafe(x)] = val; | ||
return true; | ||
}, | ||
const IGrid1DMixin = mixin({ | ||
order() { | ||
return [0]; | ||
}, | ||
includes(x) { | ||
return x >= 0 && x < this.size[0]; | ||
}, | ||
indexAt(x) { | ||
return this.includes(x) ? this.indexAtUnsafe(x) : -1; | ||
}, | ||
indexAtUnsafe(x) { | ||
return this.offset + (x | 0) * this.stride[0]; | ||
}, | ||
getAt(x) { | ||
return this.includes(x) ? this.data[this.indexAtUnsafe(x)] : 0; | ||
}, | ||
getAtUnsafe(x) { | ||
return this.data[this.indexAtUnsafe(x)]; | ||
}, | ||
setAt(x, val) { | ||
return this.includes(x) ? (this.data[this.indexAtUnsafe(x)] = val, true) : false; | ||
}, | ||
setAtUnsafe(x, val) { | ||
this.data[this.indexAtUnsafe(x)] = val; | ||
return true; | ||
} | ||
}); | ||
/** | ||
* Default implementation for {@link IGrid2D} methods. | ||
*/ | ||
export const IGrid2DMixin = mixin({ | ||
order() { | ||
return Math.abs(this.stride[1]) > Math.abs(this.stride[0]) | ||
? [1, 0] | ||
: [0, 1]; | ||
}, | ||
includes(x, y) { | ||
const size = this.size; | ||
return x >= 0 && x < size[0] && y >= 0 && y < size[1]; | ||
}, | ||
indexAt(x, y) { | ||
return this.includes(x, y) ? this.indexAtUnsafe(x, y) : -1; | ||
}, | ||
indexAtUnsafe(x, y) { | ||
return (this.offset + (x | 0) * this.stride[0] + (y | 0) * this.stride[1]); | ||
}, | ||
getAt(x, y) { | ||
return this.includes(x, y) ? this.data[this.indexAtUnsafe(x, y)] : 0; | ||
}, | ||
getAtUnsafe(x, y) { | ||
return this.data[this.indexAtUnsafe(x, y)]; | ||
}, | ||
setAt(x, y, val) { | ||
return this.includes(x, y) | ||
? ((this.data[this.indexAtUnsafe(x, y)] = val), true) | ||
: false; | ||
}, | ||
setAtUnsafe(x, y, val) { | ||
this.data[this.indexAtUnsafe(x, y)] = val; | ||
return true; | ||
}, | ||
const IGrid2DMixin = mixin({ | ||
order() { | ||
return Math.abs(this.stride[1]) > Math.abs(this.stride[0]) ? [1, 0] : [0, 1]; | ||
}, | ||
includes(x, y) { | ||
const size = this.size; | ||
return x >= 0 && x < size[0] && y >= 0 && y < size[1]; | ||
}, | ||
indexAt(x, y) { | ||
return this.includes(x, y) ? this.indexAtUnsafe(x, y) : -1; | ||
}, | ||
indexAtUnsafe(x, y) { | ||
return this.offset + (x | 0) * this.stride[0] + (y | 0) * this.stride[1]; | ||
}, | ||
getAt(x, y) { | ||
return this.includes(x, y) ? this.data[this.indexAtUnsafe(x, y)] : 0; | ||
}, | ||
getAtUnsafe(x, y) { | ||
return this.data[this.indexAtUnsafe(x, y)]; | ||
}, | ||
setAt(x, y, val) { | ||
return this.includes(x, y) ? (this.data[this.indexAtUnsafe(x, y)] = val, true) : false; | ||
}, | ||
setAtUnsafe(x, y, val) { | ||
this.data[this.indexAtUnsafe(x, y)] = val; | ||
return true; | ||
} | ||
}); | ||
/** | ||
* Default implementation for {@link IGrid3D} methods. | ||
*/ | ||
export const IGrid3DMixin = mixin({ | ||
order() { | ||
return strideOrder(this.stride); | ||
}, | ||
includes(x, y, z) { | ||
const size = this.size; | ||
return (x >= 0 && | ||
x < size[0] && | ||
y >= 0 && | ||
y < size[1] && | ||
z >= 0 && | ||
z < size[2]); | ||
}, | ||
indexAt(x, y, z) { | ||
return this.includes(x, y, z) ? this.indexAtUnsafe(x, y, z) : -1; | ||
}, | ||
indexAtUnsafe(x, y, z) { | ||
const stride = this.stride; | ||
return (this.offset + | ||
(x | 0) * stride[0] + | ||
(y | 0) * stride[1] + | ||
(z | 0) * stride[2]); | ||
}, | ||
getAt(x, y, z) { | ||
return this.includes(x, y, z) | ||
? this.data[this.indexAtUnsafe(x, y, z)] | ||
: 0; | ||
}, | ||
getAtUnsafe(x, y, z) { | ||
return this.data[this.indexAtUnsafe(x, y, z)]; | ||
}, | ||
setAt(x, y, z, val) { | ||
return this.includes(x, y, z) | ||
? ((this.data[this.indexAtUnsafe(x, y, z)] = val), true) | ||
: false; | ||
}, | ||
setAtUnsafe(x, y, z, val) { | ||
this.data[this.indexAtUnsafe(x, y, z)] = val; | ||
return true; | ||
}, | ||
const IGrid3DMixin = mixin({ | ||
order() { | ||
return strideOrder(this.stride); | ||
}, | ||
includes(x, y, z) { | ||
const size = this.size; | ||
return x >= 0 && x < size[0] && y >= 0 && y < size[1] && z >= 0 && z < size[2]; | ||
}, | ||
indexAt(x, y, z) { | ||
return this.includes(x, y, z) ? this.indexAtUnsafe(x, y, z) : -1; | ||
}, | ||
indexAtUnsafe(x, y, z) { | ||
const stride = this.stride; | ||
return this.offset + (x | 0) * stride[0] + (y | 0) * stride[1] + (z | 0) * stride[2]; | ||
}, | ||
getAt(x, y, z) { | ||
return this.includes(x, y, z) ? this.data[this.indexAtUnsafe(x, y, z)] : 0; | ||
}, | ||
getAtUnsafe(x, y, z) { | ||
return this.data[this.indexAtUnsafe(x, y, z)]; | ||
}, | ||
setAt(x, y, z, val) { | ||
return this.includes(x, y, z) ? (this.data[this.indexAtUnsafe(x, y, z)] = val, true) : false; | ||
}, | ||
setAtUnsafe(x, y, z, val) { | ||
this.data[this.indexAtUnsafe(x, y, z)] = val; | ||
return true; | ||
} | ||
}); | ||
/** | ||
* Default implementation for {@link IGrid4D} methods. | ||
*/ | ||
export const IGrid4DMixin = mixin({ | ||
order() { | ||
return strideOrder(this.stride); | ||
}, | ||
includes(x, y, z, w) { | ||
const size = this.size; | ||
return (x >= 0 && | ||
x < size[0] && | ||
y >= 0 && | ||
y < size[1] && | ||
z >= 0 && | ||
z < size[2] && | ||
w >= 0 && | ||
w < size[3]); | ||
}, | ||
indexAt(x, y, z, w) { | ||
return this.includes(x, y, z, w) ? this.indexAtUnsafe(x, y, z, w) : -1; | ||
}, | ||
indexAtUnsafe(x, y, z, w) { | ||
const stride = this.stride; | ||
return (this.offset + | ||
(x | 0) * stride[0] + | ||
(y | 0) * stride[1] + | ||
(z | 0) * stride[2] + | ||
(w | 0) * stride[3]); | ||
}, | ||
getAt(x, y, z, w) { | ||
return this.includes(x, y, z, w) | ||
? this.data[this.indexAtUnsafe(x, y, z, w)] | ||
: 0; | ||
}, | ||
getAtUnsafe(x, y, z, w) { | ||
return this.data[this.indexAtUnsafe(x, y, z, w)]; | ||
}, | ||
setAt(x, y, z, w, val) { | ||
return this.includes(x, y, z, w) | ||
? ((this.data[this.indexAtUnsafe(x, y, z, w)] = val), true) | ||
: false; | ||
}, | ||
setAtUnsafe(x, y, z, w, val) { | ||
this.data[this.indexAtUnsafe(x, y, z, w)] = val; | ||
return true; | ||
}, | ||
const IGrid4DMixin = mixin({ | ||
order() { | ||
return strideOrder(this.stride); | ||
}, | ||
includes(x, y, z, w) { | ||
const size = this.size; | ||
return x >= 0 && x < size[0] && y >= 0 && y < size[1] && z >= 0 && z < size[2] && w >= 0 && w < size[3]; | ||
}, | ||
indexAt(x, y, z, w) { | ||
return this.includes(x, y, z, w) ? this.indexAtUnsafe(x, y, z, w) : -1; | ||
}, | ||
indexAtUnsafe(x, y, z, w) { | ||
const stride = this.stride; | ||
return this.offset + (x | 0) * stride[0] + (y | 0) * stride[1] + (z | 0) * stride[2] + (w | 0) * stride[3]; | ||
}, | ||
getAt(x, y, z, w) { | ||
return this.includes(x, y, z, w) ? this.data[this.indexAtUnsafe(x, y, z, w)] : 0; | ||
}, | ||
getAtUnsafe(x, y, z, w) { | ||
return this.data[this.indexAtUnsafe(x, y, z, w)]; | ||
}, | ||
setAt(x, y, z, w, val) { | ||
return this.includes(x, y, z, w) ? (this.data[this.indexAtUnsafe(x, y, z, w)] = val, true) : false; | ||
}, | ||
setAtUnsafe(x, y, z, w, val) { | ||
this.data[this.indexAtUnsafe(x, y, z, w)] = val; | ||
return true; | ||
} | ||
}); | ||
const strideOrder = (strides) => [...strides] | ||
.map((x, i) => [x, i]) | ||
.sort((a, b) => Math.abs(b[0]) - Math.abs(a[0])) | ||
.map((x) => x[1]); | ||
const strideOrder = (strides) => [...strides].map((x, i) => [x, i]).sort((a, b) => Math.abs(b[0]) - Math.abs(a[0])).map((x) => x[1]); | ||
export { | ||
IGrid1DMixin, | ||
IGrid2DMixin, | ||
IGrid3DMixin, | ||
IGrid4DMixin | ||
}; |
import { EVENT_ALL } from "../api.js"; | ||
import { mixin } from "../mixin.js"; | ||
export const inotify_dispatch = (listeners, e) => { | ||
if (!listeners) | ||
return false; | ||
for (let i = 0, n = listeners.length, l; i < n; i++) { | ||
l = listeners[i]; | ||
l[0].call(l[1], e); | ||
if (e.canceled) { | ||
return false; | ||
} | ||
const inotify_dispatch = (listeners, e) => { | ||
if (!listeners) | ||
return false; | ||
for (let i = 0, n = listeners.length, l; i < n; i++) { | ||
l = listeners[i]; | ||
l[0].call(l[1], e); | ||
if (e.canceled) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
return true; | ||
}; | ||
/** | ||
* Mixin class decorator, injects INotify default implementation, incl. | ||
* a lazily instantiated `_listeners` property object, storing | ||
* registered listeners. | ||
*/ | ||
export const INotifyMixin = mixin({ | ||
addListener(id, fn, scope) { | ||
let l = (this._listeners = this._listeners || {})[id]; | ||
!l && (l = this._listeners[id] = []); | ||
if (this.__listener(l, fn, scope) === -1) { | ||
l.push([fn, scope]); | ||
return true; | ||
} | ||
return false; | ||
}, | ||
removeListener(id, fn, scope) { | ||
let listeners; | ||
if (!(listeners = this._listeners)) | ||
return false; | ||
const l = listeners[id]; | ||
if (l) { | ||
const idx = this.__listener(l, fn, scope); | ||
if (idx !== -1) { | ||
l.splice(idx, 1); | ||
!l.length && delete listeners[id]; | ||
return true; | ||
} | ||
} | ||
return false; | ||
}, | ||
notify(e) { | ||
let listeners; | ||
if (!(listeners = this._listeners)) | ||
return false; | ||
e.target === undefined && (e.target = this); | ||
const res = inotify_dispatch(listeners[e.id], e); | ||
return inotify_dispatch(listeners[EVENT_ALL], e) || res; | ||
}, | ||
__listener(listeners, f, scope) { | ||
let i = listeners.length; | ||
while (i-- > 0) { | ||
const l = listeners[i]; | ||
if (l[0] === f && l[1] === scope) { | ||
break; | ||
} | ||
} | ||
return i; | ||
}, | ||
const INotifyMixin = mixin({ | ||
addListener(id, fn, scope) { | ||
let l = (this._listeners = this._listeners || {})[id]; | ||
!l && (l = this._listeners[id] = []); | ||
if (this.__listener(l, fn, scope) === -1) { | ||
l.push([fn, scope]); | ||
return true; | ||
} | ||
return false; | ||
}, | ||
removeListener(id, fn, scope) { | ||
let listeners; | ||
if (!(listeners = this._listeners)) | ||
return false; | ||
const l = listeners[id]; | ||
if (l) { | ||
const idx = this.__listener(l, fn, scope); | ||
if (idx !== -1) { | ||
l.splice(idx, 1); | ||
!l.length && delete listeners[id]; | ||
return true; | ||
} | ||
} | ||
return false; | ||
}, | ||
notify(e) { | ||
let listeners; | ||
if (!(listeners = this._listeners)) | ||
return false; | ||
e.target === void 0 && (e.target = this); | ||
const res = inotify_dispatch(listeners[e.id], e); | ||
return inotify_dispatch(listeners[EVENT_ALL], e) || res; | ||
}, | ||
__listener(listeners, f, scope) { | ||
let i = listeners.length; | ||
while (i-- > 0) { | ||
const l = listeners[i]; | ||
if (l[0] === f && l[1] === scope) { | ||
break; | ||
} | ||
} | ||
return i; | ||
} | ||
}); | ||
export { | ||
INotifyMixin, | ||
inotify_dispatch | ||
}; |
import { mixin } from "../mixin.js"; | ||
export const iterable = (prop) => mixin({ | ||
*[Symbol.iterator]() { | ||
yield* this[prop]; | ||
}, | ||
const iterable = (prop) => mixin({ | ||
*[Symbol.iterator]() { | ||
yield* this[prop]; | ||
} | ||
}); | ||
export { | ||
iterable | ||
}; |
import { mixin } from "../mixin.js"; | ||
export const IWatchMixin = mixin({ | ||
addWatch(id, fn) { | ||
this._watches = this._watches || {}; | ||
if (this._watches[id]) { | ||
return false; | ||
} | ||
this._watches[id] = fn; | ||
return true; | ||
}, | ||
removeWatch(id) { | ||
if (!this._watches) | ||
return; | ||
if (this._watches[id]) { | ||
delete this._watches[id]; | ||
return true; | ||
} | ||
return false; | ||
}, | ||
notifyWatches(oldState, newState) { | ||
if (!this._watches) | ||
return; | ||
const w = this._watches; | ||
for (let id in w) { | ||
w[id](id, oldState, newState); | ||
} | ||
}, | ||
const IWatchMixin = mixin({ | ||
addWatch(id, fn) { | ||
this._watches = this._watches || {}; | ||
if (this._watches[id]) { | ||
return false; | ||
} | ||
this._watches[id] = fn; | ||
return true; | ||
}, | ||
removeWatch(id) { | ||
if (!this._watches) | ||
return; | ||
if (this._watches[id]) { | ||
delete this._watches[id]; | ||
return true; | ||
} | ||
return false; | ||
}, | ||
notifyWatches(oldState, newState) { | ||
if (!this._watches) | ||
return; | ||
const w = this._watches; | ||
for (let id in w) { | ||
w[id](id, oldState, newState); | ||
} | ||
} | ||
}); | ||
export { | ||
IWatchMixin | ||
}; |
{ | ||
"name": "@thi.ng/api", | ||
"version": "8.9.11", | ||
"version": "8.9.12", | ||
"description": "Common, generic types, interfaces & mixins", | ||
@@ -30,3 +30,5 @@ "type": "module", | ||
"scripts": { | ||
"build": "yarn clean && tsc --declaration", | ||
"build": "yarn build:esbuild && yarn build:decl", | ||
"build:decl": "tsc --declaration --emitDeclarationOnly", | ||
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts", | ||
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc api decorators mixins", | ||
@@ -41,2 +43,3 @@ "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts", | ||
"@microsoft/api-extractor": "^7.38.3", | ||
"esbuild": "^0.19.8", | ||
"rimraf": "^5.0.5", | ||
@@ -228,3 +231,3 @@ "tools": "^0.0.1", | ||
}, | ||
"gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n" | ||
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n" | ||
} |
@@ -59,3 +59,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
Package sizes (brotli'd, pre-treeshake): ESM: 2.39 KB | ||
Package sizes (brotli'd, pre-treeshake): ESM: 2.37 KB | ||
@@ -62,0 +62,0 @@ ## Dependencies |
@@ -1,276 +0,157 @@ | ||
/** | ||
* WebGL numeric type constants. Use {@link GL2TYPE} to convert, if needed. | ||
* | ||
* {@link Type} | ||
* {@link GL2TYPE} | ||
* {@link TYPE2GL} | ||
*/ | ||
export var GLType; | ||
(function (GLType) { | ||
GLType[GLType["I8"] = 5120] = "I8"; | ||
GLType[GLType["U8"] = 5121] = "U8"; | ||
GLType[GLType["I16"] = 5122] = "I16"; | ||
GLType[GLType["U16"] = 5123] = "U16"; | ||
GLType[GLType["I32"] = 5124] = "I32"; | ||
GLType[GLType["U32"] = 5125] = "U32"; | ||
GLType[GLType["F32"] = 5126] = "F32"; | ||
})(GLType || (GLType = {})); | ||
/** | ||
* Conversion from {@link GLType} to {@link Type} enums. | ||
*/ | ||
export const GL2TYPE = { | ||
[GLType.I8]: "i8", | ||
[GLType.U8]: "u8", | ||
[GLType.I16]: "i16", | ||
[GLType.U16]: "u16", | ||
[GLType.I32]: "i32", | ||
[GLType.U32]: "u32", | ||
[GLType.F32]: "f32", | ||
var GLType = /* @__PURE__ */ ((GLType2) => { | ||
GLType2[GLType2["I8"] = 5120] = "I8"; | ||
GLType2[GLType2["U8"] = 5121] = "U8"; | ||
GLType2[GLType2["I16"] = 5122] = "I16"; | ||
GLType2[GLType2["U16"] = 5123] = "U16"; | ||
GLType2[GLType2["I32"] = 5124] = "I32"; | ||
GLType2[GLType2["U32"] = 5125] = "U32"; | ||
GLType2[GLType2["F32"] = 5126] = "F32"; | ||
return GLType2; | ||
})(GLType || {}); | ||
const GL2TYPE = { | ||
[5120 /* I8 */]: "i8", | ||
[5121 /* U8 */]: "u8", | ||
[5122 /* I16 */]: "i16", | ||
[5123 /* U16 */]: "u16", | ||
[5124 /* I32 */]: "i32", | ||
[5125 /* U32 */]: "u32", | ||
[5126 /* F32 */]: "f32" | ||
}; | ||
/** | ||
* Potentially lossy conversion from {@link Type} to {@link GLType} enums. | ||
* | ||
* Not all enums are mappable: | ||
* | ||
* - `F64` maps to `undefined`, since unsupported by WebGL | ||
* - `U8C` maps to "u8" | ||
*/ | ||
export const TYPE2GL = { | ||
i8: GLType.I8, | ||
u8: GLType.U8, | ||
u8c: GLType.U8, | ||
i16: GLType.I16, | ||
u16: GLType.U16, | ||
i32: GLType.I32, | ||
u32: GLType.U32, | ||
f32: GLType.F32, | ||
f64: undefined, | ||
const TYPE2GL = { | ||
i8: 5120 /* I8 */, | ||
u8: 5121 /* U8 */, | ||
u8c: 5121 /* U8 */, | ||
i16: 5122 /* I16 */, | ||
u16: 5123 /* U16 */, | ||
i32: 5124 /* I32 */, | ||
u32: 5125 /* U32 */, | ||
f32: 5126 /* F32 */, | ||
f64: void 0 | ||
}; | ||
/** | ||
* Size information (in bytes) for {@link Type} and {@link BigType}. Also see | ||
* {@link sizeOf}. | ||
*/ | ||
export const SIZEOF = { | ||
u8: 1, | ||
u8c: 1, | ||
i8: 1, | ||
u16: 2, | ||
i16: 2, | ||
u32: 4, | ||
i32: 4, | ||
i64: 8, | ||
u64: 8, | ||
f32: 4, | ||
f64: 8, | ||
const SIZEOF = { | ||
u8: 1, | ||
u8c: 1, | ||
i8: 1, | ||
u16: 2, | ||
i16: 2, | ||
u32: 4, | ||
i32: 4, | ||
i64: 8, | ||
u64: 8, | ||
f32: 4, | ||
f64: 8 | ||
}; | ||
/** | ||
* Bit shift values to convert byte addresses into array indices for all | ||
* {@link Type}s and {@link BigType}s. | ||
*/ | ||
export const BIT_SHIFTS = { | ||
i8: 0, | ||
u8: 0, | ||
u8c: 0, | ||
i16: 1, | ||
u16: 1, | ||
i32: 2, | ||
u32: 2, | ||
i64: 3, | ||
u64: 3, | ||
f32: 2, | ||
f64: 3, | ||
const BIT_SHIFTS = { | ||
i8: 0, | ||
u8: 0, | ||
u8c: 0, | ||
i16: 1, | ||
u16: 1, | ||
i32: 2, | ||
u32: 2, | ||
i64: 3, | ||
u64: 3, | ||
f32: 2, | ||
f64: 3 | ||
}; | ||
export const FLOAT_ARRAY_CTORS = { | ||
f32: Float32Array, | ||
f64: Float64Array, | ||
const FLOAT_ARRAY_CTORS = { | ||
f32: Float32Array, | ||
f64: Float64Array | ||
}; | ||
export const INT_ARRAY_CTORS = { | ||
i8: Int8Array, | ||
i16: Int16Array, | ||
i32: Int32Array, | ||
const INT_ARRAY_CTORS = { | ||
i8: Int8Array, | ||
i16: Int16Array, | ||
i32: Int32Array | ||
}; | ||
export const UINT_ARRAY_CTORS = { | ||
u8: Uint8Array, | ||
u8c: Uint8ClampedArray, | ||
u16: Uint16Array, | ||
u32: Uint32Array, | ||
const UINT_ARRAY_CTORS = { | ||
u8: Uint8Array, | ||
u8c: Uint8ClampedArray, | ||
u16: Uint16Array, | ||
u32: Uint32Array | ||
}; | ||
export const BIGINT_ARRAY_CTORS = { | ||
i64: BigInt64Array, | ||
u64: BigUint64Array, | ||
const BIGINT_ARRAY_CTORS = { | ||
i64: BigInt64Array, | ||
u64: BigUint64Array | ||
}; | ||
export const TYPEDARRAY_CTORS = { | ||
...FLOAT_ARRAY_CTORS, | ||
...INT_ARRAY_CTORS, | ||
...UINT_ARRAY_CTORS, | ||
const TYPEDARRAY_CTORS = { | ||
...FLOAT_ARRAY_CTORS, | ||
...INT_ARRAY_CTORS, | ||
...UINT_ARRAY_CTORS | ||
}; | ||
/** | ||
* Returns canonical {@link Type} value of `type` by first | ||
* attempting to resolve it as {@link GLType} enum. | ||
* | ||
* @example | ||
* ```ts | ||
* asNativeType(GLType.F32) => "f32" | ||
* asNativeType("f32") => "f32" | ||
* ``` | ||
* | ||
* @param type - | ||
*/ | ||
export const asNativeType = (type) => { | ||
const t = GL2TYPE[type]; | ||
return t !== undefined ? t : type; | ||
const asNativeType = (type) => { | ||
const t = GL2TYPE[type]; | ||
return t !== void 0 ? t : type; | ||
}; | ||
/** | ||
* Returns suitable {@link GLType} enum of `type`. | ||
* | ||
* @example | ||
* ```ts | ||
* asGLType("f32") => GLType.F32 | ||
* asGLType(GLType.F32) => GLType.F32 | ||
* ``` | ||
* | ||
* @param type - | ||
*/ | ||
export const asGLType = (type) => { | ||
const t = TYPE2GL[type]; | ||
return t !== undefined ? t : type; | ||
const asGLType = (type) => { | ||
const t = TYPE2GL[type]; | ||
return t !== void 0 ? t : type; | ||
}; | ||
/** | ||
* Coerces given numeric args to integer values. | ||
*/ | ||
export const asInt = (...args) => args.map((x) => x | 0); | ||
/** | ||
* Returns byte size for given {@link Type} ID or {@link GLType} enum. | ||
* | ||
* @param type - | ||
*/ | ||
export const sizeOf = (type) => SIZEOF[type] || SIZEOF[asNativeType(type)]; | ||
export function typedArray(type, ...xs) { | ||
const ctor = BIGINT_ARRAY_CTORS[type]; | ||
return new (ctor || TYPEDARRAY_CTORS[asNativeType(type)])(...xs); | ||
const asInt = (...args) => args.map((x) => x | 0); | ||
const sizeOf = (type) => SIZEOF[type] || SIZEOF[asNativeType(type)]; | ||
function typedArray(type, ...xs) { | ||
const ctor = BIGINT_ARRAY_CTORS[type]; | ||
return new (ctor || TYPEDARRAY_CTORS[asNativeType(type)])(...xs); | ||
} | ||
export function typedArrayOfVec(type, data, stride) { | ||
const $data = Array.isArray(data) ? data : [...data]; | ||
if (stride === undefined) | ||
stride = $data[0].length; | ||
const num = $data.length; | ||
const res = typedArray(type, num * stride); | ||
for (let i = 0, j = 0; i < num; i++, j += stride) { | ||
res.set($data[i], j); | ||
} | ||
return res; | ||
function typedArrayOfVec(type, data, stride) { | ||
const $data = Array.isArray(data) ? data : [...data]; | ||
if (stride === void 0) | ||
stride = $data[0].length; | ||
const num = $data.length; | ||
const res = typedArray(type, num * stride); | ||
for (let i = 0, j = 0; i < num; i++, j += stride) { | ||
res.set($data[i], j); | ||
} | ||
return res; | ||
} | ||
/** | ||
* Takes an {@link NumericArray} and returns its corresponding {@link Type} ID. | ||
* Standard JS arrays will default to {@link "f64"}. | ||
* | ||
* @param x - | ||
*/ | ||
export const typedArrayType = (x) => { | ||
if (Array.isArray(x)) | ||
return "f64"; | ||
for (let id in TYPEDARRAY_CTORS) { | ||
if (x instanceof TYPEDARRAY_CTORS[id]) | ||
return id; | ||
} | ||
const typedArrayType = (x) => { | ||
if (Array.isArray(x)) | ||
return "f64"; | ||
for (let id in TYPEDARRAY_CTORS) { | ||
if (x instanceof TYPEDARRAY_CTORS[id]) | ||
return id; | ||
} | ||
return "f64"; | ||
}; | ||
/** | ||
* Returns the smallest possible *unsigned* int type enum for given `x`. | ||
* E.g. if `x <= 256`, the function returns `"u8"`. | ||
* | ||
* @param x - value to classify | ||
*/ | ||
export const uintTypeForSize = (x) => x <= 0x100 ? "u8" : x <= 0x10000 ? "u16" : "u32"; | ||
/** | ||
* Returns the smallest possible *signed* int type enum for given `x`. | ||
* E.g. if `x >= -128 && x < 128`, the function returns `"i8"`. | ||
* | ||
* @param x - value to classify | ||
*/ | ||
export const intTypeForSize = (x) => x >= -0x80 && x < 0x80 ? "i8" : x >= -0x8000 && x < 0x8000 ? "i16" : "i32"; | ||
/** | ||
* Returns suitable {@link UintType} for given bit size (`[0,32]` range) | ||
* | ||
* @param x - | ||
*/ | ||
export const uintTypeForBits = (x) => x > 16 ? "u32" : x > 8 ? "u16" : "u8"; | ||
/** | ||
* Returns suitable {@link IntType} for given bit size (`[0,32]` range) | ||
* | ||
* @param x - | ||
*/ | ||
export const intTypeForBits = (x) => x > 16 ? "i32" : x > 8 ? "i16" : "i8"; | ||
/** | ||
* Returns the next smaller {@link IntType} for given type (or the same type if | ||
* already the narrowest). | ||
* | ||
* @param t | ||
*/ | ||
export const narrowInt = (t) => t === "i64" ? "i32" : t === "i32" ? "i16" : t === "i16" ? "i8" : "i8"; | ||
/** | ||
* Returns the next larger {@link IntType} for given type (or the same type if | ||
* already the widest). | ||
* | ||
* @param t | ||
*/ | ||
export const widenInt = (t) => t === "i8" ? "i16" : t === "i16" ? "i32" : t === "i32" ? "i64" : "i64"; | ||
/** | ||
* Returns the next smaller {@link UintType} for given type (or the same type if | ||
* already the narrowest). | ||
* | ||
* @remarks | ||
* If type is `u8c`, returns `u8`. | ||
* | ||
* @param t | ||
*/ | ||
export const narrowUint = (t) => t === "u64" ? "u32" : t === "u32" ? "u16" : t === "u16" ? "u8" : "u8"; | ||
/** | ||
* Returns the next larger {@link UintType} for given type (or the same type if | ||
* already the widest). | ||
* | ||
* @param t | ||
*/ | ||
export const widenUint = (t) => t === "u8" || t === "u8c" | ||
? "u16" | ||
: t === "u16" | ||
? "u32" | ||
: t === "u32" | ||
? "u64" | ||
: "u64"; | ||
/** | ||
* Returns the next smaller {@link FloatType} for given type (or the same type | ||
* if already the narrowest). | ||
* | ||
* @param t | ||
*/ | ||
export const narrowFloat = (t) => (t === "f64" ? "f32" : "f32"); | ||
/** | ||
* Returns the next larger {@link FloatType} for given type (or the same type if | ||
* already the widest). | ||
* | ||
* @param t | ||
*/ | ||
export const widenFloat = (t) => (t === "f32" ? "f64" : "f64"); | ||
/** | ||
* Returns the next smaller type (i.e. {@link IntType}, {@link UintType} or | ||
* {@link FloatType}) for given type (or the same type if already the smallest). | ||
* | ||
* @param t | ||
*/ | ||
export const narrowType = (t) => t[0] === "i" | ||
? narrowInt(t) | ||
: t[0] === "u" | ||
? narrowUint(t) | ||
: narrowFloat(t); | ||
/** | ||
* Returns the next larger type (i.e. {@link IntType}, {@link UintType} or | ||
* {@link FloatType}) for given type (or the same type if already the widest). | ||
* | ||
* @param t | ||
*/ | ||
export const widenType = (t) => t[0] === "i" | ||
? widenInt(t) | ||
: t[0] === "u" | ||
? widenUint(t) | ||
: widenFloat(t); | ||
const uintTypeForSize = (x) => x <= 256 ? "u8" : x <= 65536 ? "u16" : "u32"; | ||
const intTypeForSize = (x) => x >= -128 && x < 128 ? "i8" : x >= -32768 && x < 32768 ? "i16" : "i32"; | ||
const uintTypeForBits = (x) => x > 16 ? "u32" : x > 8 ? "u16" : "u8"; | ||
const intTypeForBits = (x) => x > 16 ? "i32" : x > 8 ? "i16" : "i8"; | ||
const narrowInt = (t) => t === "i64" ? "i32" : t === "i32" ? "i16" : t === "i16" ? "i8" : "i8"; | ||
const widenInt = (t) => t === "i8" ? "i16" : t === "i16" ? "i32" : t === "i32" ? "i64" : "i64"; | ||
const narrowUint = (t) => t === "u64" ? "u32" : t === "u32" ? "u16" : t === "u16" ? "u8" : "u8"; | ||
const widenUint = (t) => t === "u8" || t === "u8c" ? "u16" : t === "u16" ? "u32" : t === "u32" ? "u64" : "u64"; | ||
const narrowFloat = (t) => t === "f64" ? "f32" : "f32"; | ||
const widenFloat = (t) => t === "f32" ? "f64" : "f64"; | ||
const narrowType = (t) => t[0] === "i" ? narrowInt(t) : t[0] === "u" ? narrowUint(t) : narrowFloat(t); | ||
const widenType = (t) => t[0] === "i" ? widenInt(t) : t[0] === "u" ? widenUint(t) : widenFloat(t); | ||
export { | ||
BIGINT_ARRAY_CTORS, | ||
BIT_SHIFTS, | ||
FLOAT_ARRAY_CTORS, | ||
GL2TYPE, | ||
GLType, | ||
INT_ARRAY_CTORS, | ||
SIZEOF, | ||
TYPE2GL, | ||
TYPEDARRAY_CTORS, | ||
UINT_ARRAY_CTORS, | ||
asGLType, | ||
asInt, | ||
asNativeType, | ||
intTypeForBits, | ||
intTypeForSize, | ||
narrowFloat, | ||
narrowInt, | ||
narrowType, | ||
narrowUint, | ||
sizeOf, | ||
typedArray, | ||
typedArrayOfVec, | ||
typedArrayType, | ||
uintTypeForBits, | ||
uintTypeForSize, | ||
widenFloat, | ||
widenInt, | ||
widenType, | ||
widenUint | ||
}; |
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
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
120458
6
2384