@pipex/core
What is @pipex/core?
process data like a pipeline
Design
Usage
const value = {
name: '@pipex/core',
loading: false
};
const customStart = {
getName(value) {
return `custom returned ${value.name}`;
},
getLoading(value) {
return value.loading;
},
getValue(value) {
return value;
}
}
simple example
const pipeCore = createPipeCore(value, customStart);
await pipeCore
.getName(name => {
// name === 'custom returned @pipex/core'
return '123';
})
.pipe<string>(name => {
// name === '123'
})
.getName(name => {
// name === 'custom returned @pipex/core'
})
.pipeEnd();
use piecePipe
const pipeCore = createPipeCore(value, customStart);
await pipeCore
.getName((name, piecePipe) => {
// name === 'custom returned @pipex/core'
piecePipe
.getLoading((loading, _, set) => {
// loading === false
// update loading value to true
set({ loading: true });
})
return '123';
})
.pipe<string>((name, piecePipe) => {
// name === '123'
piecePipe
.getLoading((loading, _, set) => {
// loading === true
})
})
.pipeEnd();
use instance()
const pipeCore = createPipeCore(value, customStart);
const testInstance = async () => {
await pipeCore
.instance()
.getValue(async (_, __, set) => {
// update loading to true
set({ loading: true });
await testNewInstance();
})
.pipeEnd();
}
const testNewInstance = async () => {
await pipeCore
.instance(true)
.getValue((_, __, set) => {
// update name
set({ name: 'instance @pipex/core' });
})
.pipeEnd();
};
await pipeCore
.getName((name, piecePipe) => {
// name === 'custom returned @pipex/core'
piecePipe
.getLoading(async (loading, _piecePipe, set) => {
// loading === false
// update loading value to true
await testInstance();
_piecePipe
.getName(_name => {
// name === 'custom returned @pipex/core',instance(true) not reference value to source PipeCore
})
.getLoading(loading => {
// loading === true
// instance always called before _piecePipe, so loading is true
})
})
})
.pipeEnd();
API
createPipeCore(value: Record<string, string>, config: Record<string, (value: Value) => any>): PipeCore
createPipeCore
will create one PipeCore.value
means the origin Record value source.config
means the pipeline start function.
instance(createOneFreshValue?: boolean): PipeCore
instance
will create one new pipeline to call.createOneFreshValue
means need create one independent PipeCore.
piecePipe: PiecePipeCore
piecePipe
means if you do sth in line A, you could usepiecePipe
to do sth in other line includes line A
pipeEnd(): void;
pipeEnd
means call all functions, only called in PipeCore, not in PiecePipeCore
Annotation
- Both
instance()
andpiecePipe
could do sth in other line, theinstance()
would call beforepiecePipe
in everyPipeCore
function.
TODO
support for React state