Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Protofy-xyz/Protofy
Browse files Browse the repository at this point in the history
  • Loading branch information
ap0k4 committed Apr 28, 2024
2 parents d46eb05 + 13393f8 commit e259b0c
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 5 deletions.
44 changes: 44 additions & 0 deletions packages/protolib/bundles/flow/contextV2/filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { getLogger } from 'protolib/base/logger';
import { forEach } from './forEach';

const logger = getLogger();

export const filter = async (options: {
list?: any[],
mode?: "series" | "manual",
onFilter?: (item, accept, reject) => Promise<any>,
onDone?: (list) => void,
onError?: (err) => void
}) => {
const list = options.list || [];
const mode = options.mode || "series";
const onFilter = options.onFilter || (() => { });
const onDone = options.onDone || (() => { });
const onError = options.onError || ((err) => {
logger.error({ error: err }, "Error in filter");
});

const filteredList = [];
return await forEach({
list: list,
mode: mode,
onEach: async (item, stop, next) => {
let filtered = await onFilter(item, () => {
filteredList.push(item);
next()
}, () => {
next()
})
if(mode == "series") {
if (filtered) {
filteredList.push(item);
}
next()
}
},
onDone: () => {
onDone(filteredList)
},
onError: onError
})
}
4 changes: 2 additions & 2 deletions packages/protolib/bundles/flow/contextV2/forEach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const logger = getLogger();
export const forEach = async (options: {
list?: any[],
mode?: 'parallel' | 'series' | 'manual',
onEach?: (item, done?, next?) => Promise<void>,
onEach?: (item, stop?, next?) => Promise<void>,
onDone?: () => void,
onError?: (err) => void
}) => {
Expand Down Expand Up @@ -46,7 +46,7 @@ export const forEach = async (options: {
const item = list[i];
await onEach(item, () => {
stopLoop = true; // Set the flag to stop the loops
});
}, () => {});
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/protolib/bundles/flow/contextV2/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {flowSwitch} from "./switch";
import {forEach} from "./forEach";
import {filter} from "./filter";
import {map} from "./map";
export default {
"switch": flowSwitch,
forEach
forEach,
filter,
map
}
43 changes: 43 additions & 0 deletions packages/protolib/bundles/flow/contextV2/map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { getLogger } from 'protolib/base/logger';
import { forEach } from './forEach';

const logger = getLogger();

export const map = async (options: {
list?: any[],
mode?: "series" | "manual",
onMap?: (item, next) => Promise<any>,
onDone?: (mappedList) => void,
onError?: (err) => void
}) => {
const list = options.list || [];
const mode = options.mode || "series";
const onMap = options.onMap || (() => { });
const onDone = options.onDone || (() => { });
const onError = options.onError || ((err) => {
logger.error({ error: err }, "Error in map");
});

const mappedList = [];
return await forEach({
list: list,
mode: mode,
onEach: async (item, stop, next) => {
const result = await onMap(item, (val) => {
if(mode === "manual") {
mappedList.push(val);
next();
}
});

if(mode === "series") {
mappedList.push(result);
next();
}
},
onDone: () => {
onDone(mappedList)
},
onError: onError
});
}
56 changes: 56 additions & 0 deletions packages/protolib/bundles/flow/masksV2/Filter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Node, NodeOutput, NodeParams, filterObject, restoreObject } from 'protoflow';
import { useColorFromPalette } from 'protoflow/src/diagram/Theme';
import { Filter } from 'lucide-react';

const FilterNode = ({ node = {}, nodeData = {}, children }: any) => {
const color = useColorFromPalette(12)
return (
<Node icon={Filter} node={node} isPreview={!node.id} title='Filter List' color={color} id={node.id} skipCustom={true}>
<NodeParams id={node.id} params={[
{ label: 'List', field: 'mask-list', type: 'input' },
]} />
<NodeParams id={node.id} params={[{ label: 'Mode', field: 'mask-mode', type: 'select', data: ["series", "manual"] }]} />
<div style={{height: '30px'}} />
<NodeOutput id={node.id} type={'input'} label={'On Filter'} vars={['item', 'accept', 'reject']} handleId={'mask-onFilter'} />
<NodeOutput id={node.id} type={'input'} label={'Done'} vars={['list']} handleId={'mask-onDone'} />
<NodeOutput id={node.id} type={'input'} label={'Error'} vars={['err']} handleId={'mask-onError'} />
</Node>
)
}

export default {
id: 'os2.filter',
type: 'CallExpression',
category: "Flow",
keywords: ['filter', 'array', 'functional', 'flow', 'loop'],
check: (node, nodeData) => {
return node.type == "CallExpression" && nodeData.to?.startsWith('context.flow2.filter')
},
getComponent: (node, nodeData, children) => <FilterNode node={node} nodeData={nodeData} children={children} />,
filterChildren: filterObject({keys: {
list: 'input',
onFilter: 'output',
onDone: 'output',
onError: 'output'
}}),
restoreChildren: restoreObject({keys: {
list: 'input',
onFilter: { params: {'param-item': { key: "item"}, 'param-accept': { key: "accept"}, 'param-reject': { key: "reject"}}},
onDone: { params: {'param-list': { key: "list"}}},
onError: { params: { 'param-err': { key: "err"}}}
}}),
getInitialData: () => {
return {
await: true,
to: 'context.flow2.filter',
"mask-list": {
value: "",
kind: "Identifier"
},
"mask-mode": {
value: "series",
kind: "StringLiteral"
}
}
}
}
56 changes: 56 additions & 0 deletions packages/protolib/bundles/flow/masksV2/Map.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Node, NodeOutput, NodeParams, filterObject, restoreObject } from 'protoflow';
import { useColorFromPalette } from 'protoflow/src/diagram/Theme';
import { MapPin } from 'lucide-react';

const MapNode = ({ node = {}, nodeData = {}, children }: any) => {
const color = useColorFromPalette(12)
return (
<Node icon={MapPin} node={node} isPreview={!node.id} title='Map List' color={color} id={node.id} skipCustom={true}>
<NodeParams id={node.id} params={[
{ label: 'List', field: 'mask-list', type: 'input' },
]} />
<NodeParams id={node.id} params={[{ label: 'Mode', field: 'mask-mode', type: 'select', data: ["series", "manual"] }]} />
<div style={{height: '30px'}} />
<NodeOutput id={node.id} type={'input'} label={'On Map'} vars={['item', 'next']} handleId={'mask-onMap'} />
<NodeOutput id={node.id} type={'input'} label={'Done'} vars={['list']} handleId={'mask-onDone'} />
<NodeOutput id={node.id} type={'input'} label={'Error'} vars={['err']} handleId={'mask-onError'} />
</Node>
)
}

export default {
id: 'os2.map',
type: 'CallExpression',
category: "Flow",
keywords: ['map', 'array', 'functional', 'flow', 'loop'],
check: (node, nodeData) => {
return node.type == "CallExpression" && nodeData.to?.startsWith('context.flow2.map')
},
getComponent: (node, nodeData, children) => <MapNode node={node} nodeData={nodeData} children={children} />,
filterChildren: filterObject({keys: {
list: 'input',
onMap: 'output',
onDone: 'output',
onError: 'output'
}}),
restoreChildren: restoreObject({keys: {
list: 'input',
onMap: { params: {'param-item': { key: "item"}, 'param-next': { key: "next"}}},
onDone: { params: {'param-list': { key: "list"}}},
onError: { params: { 'param-err': { key: "err"}}}
}}),
getInitialData: () => {
return {
await: true,
to: 'context.flow2.map',
"mask-list": {
value: "",
kind: "Identifier"
},
"mask-mode": {
value: "series",
kind: "StringLiteral"
}
}
}
}
5 changes: 4 additions & 1 deletion packages/protolib/bundles/flow/masksV2/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import FlowSwitch from "./FlowSwitch";
import ForEach from "./ForEach";

import Filter from "./Filter";
import Map from "./Map";
export default [
FlowSwitch,
ForEach,
Filter,
Map
]
2 changes: 1 addition & 1 deletion packages/protolib/bundles/os/masks2/ListDir.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default {
await: true,
to: 'context.os2.listDir',
"mask-path": {
value: "",
value: "/",
kind: "StringLiteral"
}
}
Expand Down

0 comments on commit e259b0c

Please sign in to comment.