avocado-old/packages/core/index.js

124 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-03-17 23:45:48 -05:00
/**
* Composes single-argument functions from right to left. The rightmost
* function can take multiple arguments as it provides the signature for
* the resulting composite function.
*
* @param {...Function} funcs The functions to compose.
* @returns {Function} A function obtained by composing the argument functions
* from right to left. For example, compose(f, g, h) is identical to doing
* (...args) => f(g(h(...args))).
*/
2019-03-22 14:46:01 -05:00
export function arrayUnique(array) {
2019-04-16 23:58:44 -05:00
return Array.from((new Set(array)).values());
2019-03-22 14:46:01 -05:00
}
2019-03-17 23:45:48 -05:00
export function compose(...funcs) {
if (funcs.length === 0) {
return arg => arg
}
if (funcs.length === 1) {
return funcs[0]
}
return funcs.reduce((a, b) => (...args) => a(b(...args)))
}
2019-04-07 14:34:22 -05:00
export function flatten(array) {
return array.reduce((flattened, elm) => flattened.concat(elm), []);
}
2019-03-17 23:45:48 -05:00
export function virtualize(fields) {
return (Superclass) => {
class Virtualized extends Superclass {}
fields.forEach((field) => {
Virtualized.prototype[field] = function() {
const prototype = Object.getPrototypeOf(this);
const className = prototype.constructor.name;
throw new ReferenceError(
`"${className}" has undefined pure virtual method "${field}"`
);
}
});
return Virtualized;
}
}
2019-03-24 01:16:04 -05:00
export function virtualizeStatic(fields) {
return (Superclass) => {
class Virtualized extends Superclass {}
fields.forEach((field) => {
Virtualized[field] = function() {
const prototype = Virtualized.prototype;
const className = prototype.constructor.name;
throw new ReferenceError(
`"${className}" has undefined pure virtual static method "${field}"`
);
}
});
return Virtualized;
}
}
2019-03-17 23:45:48 -05:00
export class TickingPromise extends Promise {
constructor(executor, ticker) {
let _reject, _resolve;
super((resolve, reject) => {
_reject = reject;
_resolve = resolve;
if (executor) {
executor(resolve, reject);
}
});
this.executor = executor;
this.reject = _reject;
this.resolve = _resolve;
this.ticker = ticker;
2019-03-17 23:45:48 -05:00
}
2019-09-08 07:39:31 -05:00
static all(promises) {
const tickingPromises = [];
for (let i = 0; i < promises.length; i++) {
const promise = promises[i];
if (promise instanceof TickingPromise) {
tickingPromises.push(promise);
// After resolution, stop ticking the promise.
promise.then(() => {
tickingPromises.splice(tickingPromises.indexOf(promise), 1);
})
2019-09-08 07:39:31 -05:00
}
}
if (0 === tickingPromises.length) {
return super.all(promises);
}
return new TickingPromise(
(resolve) => {
resolve(Promise.all(results));
},
(elapsed) => {
for (let i = 0; i < tickingPromises.length; i++) {
tickingPromises[i].tick(elapsed);
}
},
);
}
tick(elapsed) {
this.ticker(elapsed, this.resolve, this.reject);
}
2019-03-17 23:45:48 -05:00
}
2019-04-28 23:45:03 -05:00
export {EventEmitterMixin as EventEmitter} from './event-emitter';
2019-05-17 04:31:37 -05:00
export {merge} from './merge';
export {
mergeDiff,
mergeDiffArray,
mergeDiffObject,
mergeDiffPrimitive,
} from './merge-diff';
2019-05-02 20:55:14 -05:00
export {fastApply} from './fast-apply';
2019-04-28 23:45:03 -05:00
export {PropertyMixin as Property} from './property';