-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:Protofy-xyz/Protofy
- Loading branch information
Showing
8 changed files
with
211 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters