Page MenuHomeSealhub

map-reduce.ts
No OneTemporary

map-reduce.ts

import {
App,
Collection,
CalculatedField,
Item,
SubjectPathEquiv,
ActionName,
Context,
} from "../../../main";
type ParamGetter<T> = (context: Context, item: Item) => Promise<T> | T;
type ParamOrGetter<T> = T | ParamGetter<T>;
type MapReduceParams<ReturnType, IntermediateValue = any> = {
source: {
subject_path: ParamOrGetter<SubjectPathEquiv>;
action_name: ParamOrGetter<ActionName>;
params: ParamOrGetter<any>;
};
map: (items: Item[]) => IntermediateValue[];
reduce: [(intermediate: IntermediateValue[]) => ReturnType, any];
};
export default class MapReduce<
ReturnType,
IntermediateValue
> extends CalculatedField<ReturnType> {
name = "map-reduce";
params: MapReduceParams<ReturnType, IntermediateValue>;
constructor(
app: App,
collection: Collection,
params: MapReduceParams<ReturnType, IntermediateValue>
) {
super(app, collection);
this.params = params;
}
async calculate(context: Context, item: Item) {
const action_arguments = [
context,
this.params.source.subject_path,
this.params.source.action_name,
this.params.source.params,
].map(function (element) {
if (element instanceof Function) {
return element(context, item);
} else {
return element;
}
});
const fulfilled_params = Promise.all(action_arguments);
return (await this.app.runAction.apply(App, fulfilled_params))
.map(this.params.map)
.reduce(this.params.reduce[0], this.params.reduce[1]);
}
}

File Metadata

Mime Type
text/plain
Expires
Tue, Dec 24, 14:05 (12 h, 39 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557248
Default Alt Text
map-reduce.ts (1 KB)

Event Timeline