Open WendaoLee opened 1 year ago
也许不能直接通过thunk创建一个新thunk
draft
// getter get a thunk which represent the return type
// new always get ThunkProxy,not thunk itself represented.
// every code with thunk do nothing in the level of coder.Writing thunk just declare caculation.And all caculation will do implicitly as orders declared.
// if user want to get thunk's result,use util function thunkRef(),and use util function isThunk() to check whether thunk has been evaluated correctly.
import {TMap} from 'Base'
let map = TMap.Map("domid") //a proxy instance of thunks.Use this as the `TMap.Map` in TencentMap.Operations on it will create Thunks.
let mapT = TMap.Map("domid").thunk() // get MapThunk
let center = map.getCenter() //get LatLngThunk,because getCenter return LatLng
let lat = center.getLat() //get NumberThunk,with LatLngThunk supporting create new thunk represented number.
mapT = thunkRef(mapT) // make mapT point to a MapThunk,which will auto becomes MapThunk's result `map` when MapThunk evaluated.
也许不能直接通过thunk创建一个新thunk
事实上是可以的。但是问题在于thunk是否应该引入一个默认的hook来保证thunk的按序执行。按序执行还有一个问题,是否应该在错误发生时中断?
draft.Thunk With Promise.
// for some situation e.g wait for data from server
import {TMap,LatLng} from 'Base'
let map = TMap.Map("domid")
axios.get('https://example').then((res)=>{
data = res.data // e.g {lat:1,lng:1}
map.setCenter(new TMap.LatLng(data.lat,data.lng).thunk()) // passing thunk into thunk.
})
let defaultCenter = new TMap.LatLng(data.lat,data.lng).thunk()
map.setCenter(defaultCenter)
//above will do as expected.As `(res)=>{}` creating thunk after `defaultCenter`
draft.Thunk Suck
seems need thunk suck for sleeping condition.
draft.ThunkProxy And ThunkSymbol
Every constructor function in TencentMap which can use with new
operator is a ThunkProxy
in cuteTencentMap.
ThunkProxy
is a proxy of thunk.You can use it to create new thunks represented the operation on TencentMap's related method.
e.g
import {TMap} from 'Base'
let map = new TMap.Map("domid") //it creare ThunkProxy of TMap.Map
map.getCenter() //ThunkProxy ensure the methods of TMap.Map you created invoke correctly when it being evaluated.That means the center you get is from the `let map = TMap.Map("domid")` 's map instance.Not others.
ThunkProxy
not Thunk
itself.If you want to pass arguments to a method which need type from TencentMap,you should use .thunk()
to get the thunk.
e.g. TMap.Map.setCenter(latlng)
need latlng
to be type of TMap.LatLng
import {TMap} from 'Base'
let map = new TMap.Map("domid")
let center = new TMap.LatLng(12,3)
map.setCenter(center.thunk()) // need `.thunk()` because `new` create `ThunkProxy`,not `Thunk`
class ThunkProxy{
target
__type__ = "ThunkProxy"
}
class ThunkSymbol{
__type__ = "ThunkSymbol"
}
some method has object options.Should it be a thunk?And how to deal with these thunks?
new TMap.Map(domId, options);
//optins.center :: TMap.LatLng
note: it should not be a thunk.Just write special case of evaluate function.
Goal.Example