docdef/source/outputs/base.ts

78 lines
1 KiB
TypeScript
Raw Permalink Normal View History

2024-02-08 10:41:17 +01:00
/**
*/
interface type_output<type_result> extends lib_plankton.call.type_coproduct
2024-02-07 15:14:30 +01:00
{
2024-02-08 10:41:17 +01:00
/**
*/
2024-02-08 18:10:46 +01:00
render_object(
object : type_object
) : type_result
;
/**
*/
render_document(
document : type_document
2024-02-08 10:41:17 +01:00
) : type_result
;
2024-02-07 15:14:30 +01:00
}
2024-02-08 11:10:07 +01:00
/**
*/
let output_kind_pool : Record<
string,
(
(
data : any,
make : ((raw : any) => type_output<any>)
)
=>
type_output<any>
)
> = {};
/**
*/
function output_kind_register<type_result>(
name : string,
factory : (
(data : any, make : ((raw : any) => type_output<type_result>))
=>
type_output<type_result>
)
) : void
{
if (name in output_kind_pool)
{
throw (new Error("kind '" + name + "' already registered"));
}
else
{
output_kind_pool[name] = factory;
}
}
/**
*/
function output_make<type_result>(
raw : any
) : type_output<type_result>
{
if (! (raw["kind"] in output_kind_pool))
{
throw (new Error("kind '" + raw["kind"] + "' not registered"));
}
else
{
return output_kind_pool[raw["kind"]](raw["data"], output_make<type_result>);
}
}