Skip to content

Instantly share code, notes, and snippets.

@ryzed
Created November 11, 2013 17:12
Show Gist options
  • Save ryzed/7416721 to your computer and use it in GitHub Desktop.
Save ryzed/7416721 to your computer and use it in GitHub Desktop.
public function addComponent<T:GOC>(v:T):Void
{
var fid = v.fid;
// check for prev and remove if need
var prev = comps.get(fid);
if (prev != null)
{
// remove prev
removeComponent(prev);
}
// 1. Save V to components-map
// 2. Save V to obj property (if needed)
// 3. Set props of V (self link allowed too)
// 4. V.onAdded()
// 5. Save V to props of each other component AND call onAddedOther
// save to comps (1)
comps.set(fid, v);
// save to obj prop (2)
var bindPropName = v.bindProp;
var hasBindProp = (bindPropName != null);
if (hasBindProp)
{
Reflect.setProperty(this, bindPropName, v);
}
// fill props of v (3)
for (c in comps)
{
if (c.bindProp != null)
{
Reflect.setProperty(v, c.bindProp, c);
}
}
// notify this component (4)
v.onAdded();
// TODO optimise here
// if need, save to prop (5)
if (hasBindProp)
{
// to comps
for (c in comps)
{
if (c != v)
{
Reflect.setProperty(c, bindPropName, v);
c.onAddedOther(v);
}
}
}
else
{
// not "core", just notify
for (c in comps)
{
if (c != v)
{
c.onAddedOther(v);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment