UNPKG

@chief-editor/flow

Version:

Old核心数据流

1 lines 7.65 kB
var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(t){for(var e,i=1,r=arguments.length;i<r;i++)for(var a in e=arguments[i])Object.prototype.hasOwnProperty.call(e,a)&&(t[a]=e[a]);return t}).apply(this,arguments)},__spreadArrays=this&&this.__spreadArrays||function(){for(var t=0,e=0,i=arguments.length;e<i;e++)t+=arguments[e].length;var r=Array(t),a=0;for(e=0;e<i;e++)for(var o=arguments[e],n=0,c=o.length;n<c;n++,a++)r[a]=o[n];return r};import{NodeType,extractStaticConfig}from"@chief-editor/base";import{NestWatcher}from"@co-hooks/nest-watcher";import{PriorityQueue}from"@co-hooks/priority-queue";import{guid}from"@co-hooks/util";import{extractValidate,getDefaultValidate}from"../config/validate";import{extractHooks}from"../config/hooks";import{getAbsoluteKey,getWatchableKeys}from"../util/keys";import{extractEvents}from"../config/events";import{wrapFunction,wrapJson}from"../util/wrap";var FlowScheduler=function(){function t(t){this.watcherId=guid(),this.events={},this.hooks={},this.validator=getDefaultValidate(),this.pending=[],this.brickConfig=t.brickConfig,this.model=t.model,this.queue=this.initPriorityQueue()}return t.prototype.initBrickData=function(t,e){Object.assign(this,{brick:t}),this.data=e;var i=e.id,r=void 0===i?guid():i,a=e.type,o=e.info,n=o.field,c=o.label,s=o.lock,u=void 0!==s&&s;return{id:r,creator:guid(),label:c,readonly:!n,brickType:a,field:n,env:"flow",virtual:!1,lock:u}},t.prototype.getBrickData=function(){return this.data},t.prototype.getPartList=function(){var t=this,e=this.data.parts;return e?(this.brickConfig.getPartKeyMapFromBrickData?this.brickConfig.getPartKeyMapFromBrickData(this.brick):Object.keys(e).map((function(t){return{key:t,part:t,repeat:!1,field:void 0}}))).map((function(i){var r=i.key,a=i.part,o=i.field,n=i.bricks,c=i.repeat,s=void 0!==c&&c,u=t.brickConfig.getPartBoardInfoFromBrickData(t.brick,a),l={bricks:n||[]};return e[a]&&(n||(l.bricks=e[a].bricks),l.id=e[a].id),__assign(__assign({key:r,repeat:s,field:o},l),null==u?{type:NodeType.STREAM}:u)})):[]},t.prototype.isPartDynamic=function(){return null!=this.brickConfig.getPartKeyMapFromBrickData},t.prototype.setState=function(t){this.brick.updateBrickInstance({state:__assign(__assign({},this.brick.state),t)})},t.prototype.setValue=function(t,e,i){this.model.setValueByPath(this.brick,t,e,i)},t.prototype.getDatasource=function(t){this.queue.executeAsync("datasource",t)},t.prototype.dispatchEvent=function(t,e){var i="string"==typeof t?t:t.type;this.events[i]&&this.events[i](t,e)},t.prototype.getHook=function(t){return this.model.getOwnerContext().getHook(t)},t.prototype.dispose=function(){this.model.getWatcher().unregister(this.brick.creator)},t.prototype.initStaticBrickData=function(){var t=this.brick;this.extractBrickData();var e=this.data,i=e.config,r=e.layout,a=e.styles;if(this.loader){var o=this.loader.loadStaticDatasource()||this.brickConfig.getInitialDatasource();t.updateBrickInstance({datasource:o,staticDatasource:o},!0)}t.updateBrickInstance({layout:r,config:extractStaticConfig(i),styles:extractStaticConfig(a),state:this.brickConfig.getInitialState()},!0),t.readonly||(this.loader&&this.loader.loadStaticDefaultValue?(t.updateBrickInstance({value:this.loader.loadStaticDefaultValue()},!0),null==this.model.getValueByBrick(t)&&this.setValue(t.value,!0,!0)):(t.updateBrickInstance({value:this.brickConfig.getDefaultValue()},!0),null==this.model.getValueByBrick(t)&&this.setValue(this.brickConfig.getDynamicDefaultValue(t),!0,!0)))},t.prototype.init=function(){this.loadBrickScheduler()},t.prototype.updateBrickData=function(t){this.data=t,t.id=this.brick.id;var e=t.id,i=void 0===e?guid():e,r=t.type,a=t.info,o=a.field,n=a.label;this.brick.updateBrickInstance({id:i,creator:guid(),label:n,readonly:!o,brickType:r,field:o,env:"flow",virtual:!1},!0),this.initStaticBrickData(),this.loadBrickScheduler()},t.prototype.setBrickLayout=function(t){this.brick.updateBrickInstance({layout:Object.assign({},this.brick.layout,t)})},t.prototype.getByExpression=function(){for(var t,e=[],i=0;i<arguments.length;i++)e[i]=arguments[i];return(t=this.brick).getByExpression.apply(t,e)},t.prototype.executeHook=function(t,e){return this.hooks[t]?this.hooks[t](e):Promise.resolve(e)},t.prototype.extractBrickData=function(){var t=this;this.pending=[],this.model.getWatcher().unregister(this.watcherId),this.watcherId=guid();var e=this.brick,i=this.data;this.events=extractEvents(e,i.events),this.hooks=extractHooks(e,this.brickConfig,i.hooks),i.datasource&&(this.loader=this.model.getOwnerContext().getLoader(i.datasource,this),this.loader.watcher&&this.pending.push({watchers:this.loader.watcher,func:function(){return t.queue.execute("datasource",{})}})),this.validator=extractValidate(e,i.validate),this.initConfig()},t.prototype.initConfig=function(){var t=this,e=this.data,i=e.config,r=e.styles,a=wrapJson({config:i,styles:r},this.brick),o=a.watchers,n=a.func,c=a.asyncMap,s=Object.keys(c),u={},l=wrapFunction((function(){try{t.brick.updateBrickInstance(n(u))}catch(e){console.warn(e),console.warn("execute prop expression error: "+e.msg),t.brick.updateBrickInstance({config:extractStaticConfig(i),styles:extractStaticConfig(r)})}}),this.brick);if(s.length){s.forEach((function(e){c[e].watchers&&t.pending.push({func:function(){return c[e].func().then((function(t){return u[e]=t})).catch((function(t){console.warn(t),console.warn("execute prop expression error: "+t.msg)})).then(l)},watchers:c[e].watchers,immediate:!1,lazy:!0})}));this.pending.push({watchers:o,func:function(){return Promise.all(s.map((function(t){return c[t].func().then((function(e){return u[t]=e})).catch((function(t){console.warn(t),console.warn("execute prop expression error")}))}))).then(l)},immediate:!0})}else this.pending.push({func:l,watchers:o,immediate:!0,lazy:!1})},t.prototype.loadBrickScheduler=function(){var t=this,e=this.brick;this.queue.execute("datasource",{}).then((function(){var e=t.pending;t.pending=[];var i=[];return e.forEach((function(e){t.model.getWatcher().register(t.watcherId,e.watchers,e.func,e.lazy),e.immediate&&i.push(e.func)})),Promise.all(i.map((function(t){return t()})))})).then((function(){var i=t.model.getValueByBrick(e,!0);if(!t.brick.readonly){var r=t.brickConfig.getDynamicDefaultValue(e),a=void 0===i?r:i;a!==i&&(i=a,t.model.setValueByPath(t.brick,a,!1,!0)),t.model.getWatcher().register(e.creator,[NestWatcher.combineKeys(__spreadArrays([t.model.name],getWatchableKeys(e.getNode())))],(function(){var i=t.model.getValueByBrick(e,!0);return i!==e.value&&e.updateBrickInstance({value:i}),Promise.resolve()}))}t.brick.updateBrickInstance({state:t.brickConfig.getDynamicInitialState(t.brick),value:i,validateInfo:getDefaultValidateInfo(),validating:!1,ready:!0}),t.model.getWatcher().finishKey(getAbsoluteKey(t.brick))})).catch(console.log)},t.prototype.initPriorityQueue=function(){var t=this;return new PriorityQueue([{key:"datasource",fn:function(e,i){return t.getDatasourceTask(i)}}])},t.prototype.getDatasourceTask=function(t){var e=this,i=this.loader;if(!i)return Promise.resolve();var r=i.loadDynamicDatasource;if(!r)return Promise.resolve();var a=this.brickConfig.getRenderConfig(this.brick).updateWhenLoading;return this.brick.updateBrickInstance({loading:!0},!a),this.executeHook("onBeforeGetDatasource",t).then((function(t){return r(t)})).then((function(t){return e.brick.updateBrickInstance({datasource:t}),e.executeHook("onAfterGetDatasource",t)})).then((function(){return e.brick.updateBrickInstance({loading:!1},!a)}))},t}();export{FlowScheduler};export function getDefaultValidateInfo(){return{catchable:!1,needValidate:!1,partial:!1,valid:!0,error:"",children:[]}}