1+ /* eslint-disable no-await-in-loop */
2+ import React from 'react'
3+ import { isFunction , isBoolean , isString } from 'utils/is'
4+ import config from '../config'
5+ import api from '../backendAPI'
6+ import { stepFactory } from '../utils'
7+ import i18n from 'utils/createI18n'
8+ import { loadPackagesByType , mountPackagesByType } from '../components/Plugins/actions'
9+ import CodingSDK from '../CodingSDK'
10+ import state from './state'
11+ import { persistTask } from '../mobxStore'
12+
13+ function closestTo ( arr , key , isPrev ) {
14+ const offsetIndex = isPrev ? - 1 : 1
15+ const current = arr . indexOf ( key )
16+ return arr [ current + offsetIndex ]
17+ }
18+
19+ function checkEnable ( enable ) {
20+ if ( enable === undefined ) {
21+ return true
22+ }
23+ if ( isFunction ( enable ) ) {
24+ return enable ( config )
25+ }
26+ return Boolean ( enable )
27+ }
28+
29+ async function initialize ( ) {
30+ const step = stepFactory ( )
31+ let stepNum = 2
32+ await step ( '[0] prepare data' , async ( ) => {
33+ window . CodingSDK = CodingSDK
34+ window . React = React
35+ window . i18n = i18n
36+ window . extension = f => null
37+ window . refs = { }
38+ window . config = config
39+ return true
40+ } )
41+
42+ // await step('[1] load required package', async() => {
43+ // try {
44+ // await loadPackagesByType('Required', state, true)
45+ // } catch (err) {
46+ // return true
47+ // }
48+ // return true
49+ // })
50+
51+ await step ( '=== Run steps in stepCache ===' , async ( ) => {
52+ /*async function goto (key, hasNext = true) {
53+ if (!hasNext) {
54+ return true
55+ }
56+ const nextKey = await step(`[${stepNum++}] ${state.get(key).desc}`, state.get(key).func)
57+ if (nextKey === undefined || isBoolean(nextKey)) {
58+ const next = closestTo(state.keys(), key)
59+ return nextKey && goto(next, !!next)
60+ }
61+ if (isString(nextKey)) {
62+ return goto(nextKey)
63+ }
64+ }
65+ return goto(state.keys()[0])*/
66+ for ( const value of state . values ( ) ) {
67+ console . log ( '== value ==' , value . desc )
68+ if ( checkEnable ( value . enable ) ) {
69+ await step ( `[${ stepNum ++ } ] ${ value . desc } ` , value . func )
70+ }
71+ }
72+ console . log ( '=== End running stepCache ===' )
73+ return true
74+ } )
75+
76+
77+ await step ( `[${ stepNum ++ } ] mount required package` , ( ) => {
78+ mountPackagesByType ( 'Required' )
79+ return true
80+ } )
81+
82+ await step ( `[${ stepNum ++ } ] persist Store` , ( ) => {
83+ persistTask ( )
84+ return true
85+ } )
86+
87+ if ( config . packageDev ) {
88+ await step ( `[${ stepNum ++ } ] enable package server hotreload` ,
89+ ( ) => {
90+ const ports = __PACKAGE_PORTS__
91+ if ( ports && ports . length ) {
92+ ports . forEach ( ( port ) => {
93+ const url = `http://ide.test:${ port } `
94+ api . enablePackageHotReload ( url )
95+ } )
96+ } else {
97+ api . enablePackageHotReload ( )
98+ }
99+ return true
100+ } )
101+ }
102+
103+ return step
104+ }
105+
106+ export default initialize
0 commit comments