Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Commit

Permalink
[breaking] pass exceptions with component binds
Browse files Browse the repository at this point in the history
  • Loading branch information
WebFreak001 committed Oct 20, 2018
1 parent b370f3c commit 24b89b3
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions source/workspaced/api.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ alias ImportPathProvider = string[]delegate();
///
alias BroadcastCallback = void delegate(WorkspaceD, WorkspaceD.Instance, JSONValue);
///
alias ComponentBindFailCallback = void delegate(WorkspaceD.Instance, ComponentFactory);
alias ComponentBindFailCallback = void delegate(WorkspaceD.Instance, ComponentFactory, Exception);

/// Will never call this function
enum ignoredFunc;
Expand Down Expand Up @@ -260,7 +260,7 @@ interface ComponentWrapper

interface ComponentFactory
{
ComponentWrapper create(WorkspaceD workspaced, WorkspaceD.Instance instance);
ComponentWrapper create(WorkspaceD workspaced, WorkspaceD.Instance instance, out Exception error);
ComponentInfo info() @property;
}

Expand Down Expand Up @@ -562,17 +562,18 @@ class WorkspaceD
factory = new DefaultComponentFactory!T;
components ~= ComponentFactoryInstance(factory, autoRegister);
auto info = factory.info;
auto glob = factory.create(this, null);
Exception error;
auto glob = factory.create(this, null, error);
if (glob)
globalComponents ~= ComponentWrapperInstance(glob, info);
if (autoRegister)
foreach (ref instance; instances)
{
auto inst = factory.create(this, instance);
auto inst = factory.create(this, instance, error);
if (inst)
instance.instanceComponents ~= ComponentWrapperInstance(inst, info);
else if (onBindFail)
onBindFail(instance, factory);
onBindFail(instance, factory, error);
}
static if (__traits(compiles, T.registered(this)))
T.registered(this);
Expand Down Expand Up @@ -600,11 +601,12 @@ class WorkspaceD
{
if (!factory.autoRegister && factory.info.name == name)
{
auto wrap = factory.create(this, inst);
Exception error;
auto wrap = factory.create(this, inst, error);
if (wrap)
inst.instanceComponents ~= ComponentWrapperInstance(wrap, factory.info);
else if (onBindFail)
onBindFail(inst, factory);
onBindFail(inst, factory, error);
break;
}
}
Expand All @@ -613,11 +615,12 @@ class WorkspaceD
{
if (factory.autoRegister)
{
auto wrap = factory.create(this, inst);
Exception error;
auto wrap = factory.create(this, inst, error);
if (wrap)
inst.instanceComponents ~= ComponentWrapperInstance(wrap, factory.info);
else if (onBindFail)
onBindFail(inst, factory);
onBindFail(inst, factory, error);
}
}
return inst;
Expand All @@ -638,13 +641,25 @@ class WorkspaceD
return false;
}

bool attach(Instance instance, string component)
deprecated("Use overload taking an out Exception error or attachSilent instead") bool attach(
Instance instance, string component)
{
return attachSilent(instance, component);
}

bool attachSilent(Instance instance, string component)
{
Exception error;
return attach(instance, component, error);
}

bool attach(Instance instance, string component, out Exception error)
{
foreach (factory; components)
{
if (factory.info.name == component)
{
auto wrap = factory.create(this, instance);
auto wrap = factory.create(this, instance, error);
if (wrap)
{
instance.instanceComponents ~= ComponentWrapperInstance(wrap, factory.info);
Expand All @@ -660,7 +675,7 @@ class WorkspaceD

class DefaultComponentFactory(T : ComponentWrapper) : ComponentFactory
{
ComponentWrapper create(WorkspaceD workspaced, WorkspaceD.Instance instance)
ComponentWrapper create(WorkspaceD workspaced, WorkspaceD.Instance instance, out Exception error)
{
auto wrapper = new T();
try
Expand All @@ -670,6 +685,7 @@ class DefaultComponentFactory(T : ComponentWrapper) : ComponentFactory
}
catch (Exception e)
{
error = e;
return null;
}
}
Expand Down

0 comments on commit 24b89b3

Please sign in to comment.