namespace lib_mvc { /** */ export type type_event = { type : string; data : type_data; }; /** */ export type type_model = { listeners : Array<((event : type_event) => Promise)>; state : type_state; }; /** */ export type type_view = { setup : ((model : type_model) => Promise); update : ((model : type_model, event : type_event) => Promise); }; /** */ export type type_control = { setup : ((model : type_model) => Promise); }; /** */ export type type_complex = { model : type_model; views : Array>; controls : Array>; }; /** */ export function model_make ( state : type_state ) : type_model { return { "listeners": [], "state": state, }; } /** */ export function model_listen ( model : type_model, action : ((event : type_event) => Promise) ) : void { model.listeners.push(action); } /** */ export async function model_notify ( model : type_model, event : type_event ) : Promise { for await (const action of model.listeners) { action(event); } } /** */ export async function view_setup ( view : type_view, model : type_model, options : { blocking ?: boolean; } = {} ) : Promise { options = Object.assign ( { "blocking": true, }, options ); await view.setup(model); model_listen ( model, (info) => { if (options.blocking) { return view.update(model, info); } else { view.update(model, info); return Promise.resolve(undefined); } } ); } /** */ export async function control_setup ( control : type_control, model : type_model, options : { } = {} ) : Promise { options = Object.assign ( { }, options ); await control.setup(model); } /** */ export async function complex_setup ( complex : type_complex ) : Promise { for await (const view of complex.views) { await view_setup(view, complex.model); } for await (const control of complex.controls) { await control_setup(control, complex.model); } } }