GrimoireGL / GrimoireJS

A WebGL framework for Web development.
https://grimoire.gl
MIT License
350 stars 24 forks source link

Promise attributes? #521

Open kyasbal opened 7 years ago

kyasbal commented 7 years ago

Asynchronous attributes typically hard to operate them. We need feature to handle asynchronous attributes easily.

Change behaviour of fetching attribute if the attribute converter returned Promise.

If an converter returned a Promise instance, Attribute#value should be resolved value. For example, material is asynchronous attribute.

function convertMaterial(){
    return new Promise((resolve,reject)=>{
    // SOMETHING
    resolve(new Material(....));
    });
}

Before this feature

NodeInterface#getAttribute("material") => Promise<Material>;

After this feature

NodeInterface#getAttribute("material") => Material;

Several API changes

For changing this feature, we need define several APIs.

watch API

watch API also should be fired when the attribute was resolved.

watch("material",(mat:Material)=>{ // mat should be Material instance. not Promise.

});

if immediate flag is set, the watch function is fired after resolving current Promise.

isPending API

isPending provides information that specified attribute is waiting for Promise or not.

Component#isPending(attributeName:string):boolean;

NodeInterface#setAttribute, Component#setAttribute

These method should return Promise instance that is currently waiting for.

Before this feature

NodeInterface#setAttribute("material",something)=>void;

After this feature

NodeInterface#setAttribute("material",something)=>Promise<Material>;