Browse Source

custom nodes factory refactoring

sanex3339 8 years ago
parent
commit
8d390178a3
2 changed files with 63 additions and 60 deletions
  1. 25 21
      dist/index.js
  2. 38 39
      src/container/modules/custom-nodes/CustomNodesModule.ts

+ 25 - 21
dist/index.js

@@ -2621,6 +2621,12 @@ exports.InversifyContainerFacade = InversifyContainerFacade;
 "use strict";
 "use strict";
 
+var _map = __webpack_require__(13);
+
+var _map2 = _interopRequireDefault(_map);
+
+function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
+
 var inversify_1 = __webpack_require__(2);
 var ServiceIdentifiers_1 = __webpack_require__(4);
 var CustomNodes_1 = __webpack_require__(20);
@@ -2644,39 +2650,37 @@ var StringArrayCallsWrapper_1 = __webpack_require__(78);
 var StringArrayNode_1 = __webpack_require__(79);
 var StringArrayRotateFunctionNode_1 = __webpack_require__(80);
 exports.customNodesModule = new inversify_1.ContainerModule(function (bind) {
-    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).to(BinaryExpressionFunctionNode_1.BinaryExpressionFunctionNode).whenTargetNamed(CustomNodes_1.CustomNodes.BinaryExpressionFunctionNode);
-    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).to(ControlFlowStorageCallNode_1.ControlFlowStorageCallNode).whenTargetNamed(CustomNodes_1.CustomNodes.ControlFlowStorageCallNode);
-    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).to(ControlFlowStorageNode_1.ControlFlowStorageNode).whenTargetNamed(CustomNodes_1.CustomNodes.ControlFlowStorageNode);
-    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).to(ConsoleOutputDisableExpressionNode_1.ConsoleOutputDisableExpressionNode).whenTargetNamed(CustomNodes_1.CustomNodes.ConsoleOutputDisableExpressionNode);
-    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).to(DebugProtectionFunctionCallNode_1.DebugProtectionFunctionCallNode).whenTargetNamed(CustomNodes_1.CustomNodes.DebugProtectionFunctionCallNode);
-    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).to(DebugProtectionFunctionIntervalNode_1.DebugProtectionFunctionIntervalNode).whenTargetNamed(CustomNodes_1.CustomNodes.DebugProtectionFunctionIntervalNode);
-    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).to(DebugProtectionFunctionNode_1.DebugProtectionFunctionNode).whenTargetNamed(CustomNodes_1.CustomNodes.DebugProtectionFunctionNode);
-    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).to(DomainLockNode_1.DomainLockNode).whenTargetNamed(CustomNodes_1.CustomNodes.DomainLockNode);
-    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).to(NodeCallsControllerFunctionNode_1.NodeCallsControllerFunctionNode).whenTargetNamed(CustomNodes_1.CustomNodes.NodeCallsControllerFunctionNode);
-    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).to(SelfDefendingUnicodeNode_1.SelfDefendingUnicodeNode).whenTargetNamed(CustomNodes_1.CustomNodes.SelfDefendingUnicodeNode);
-    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).to(StringArrayCallsWrapper_1.StringArrayCallsWrapper).whenTargetNamed(CustomNodes_1.CustomNodes.StringArrayCallsWrapper);
-    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).to(StringArrayNode_1.StringArrayNode).whenTargetNamed(CustomNodes_1.CustomNodes.StringArrayNode);
-    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).to(StringArrayRotateFunctionNode_1.StringArrayRotateFunctionNode).whenTargetNamed(CustomNodes_1.CustomNodes.StringArrayRotateFunctionNode);
+    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).toConstructor(BinaryExpressionFunctionNode_1.BinaryExpressionFunctionNode).whenTargetNamed(CustomNodes_1.CustomNodes.BinaryExpressionFunctionNode);
+    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).toConstructor(ControlFlowStorageCallNode_1.ControlFlowStorageCallNode).whenTargetNamed(CustomNodes_1.CustomNodes.ControlFlowStorageCallNode);
+    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).toConstructor(ControlFlowStorageNode_1.ControlFlowStorageNode).whenTargetNamed(CustomNodes_1.CustomNodes.ControlFlowStorageNode);
+    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).toConstructor(ConsoleOutputDisableExpressionNode_1.ConsoleOutputDisableExpressionNode).whenTargetNamed(CustomNodes_1.CustomNodes.ConsoleOutputDisableExpressionNode);
+    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).toConstructor(DebugProtectionFunctionCallNode_1.DebugProtectionFunctionCallNode).whenTargetNamed(CustomNodes_1.CustomNodes.DebugProtectionFunctionCallNode);
+    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).toConstructor(DebugProtectionFunctionIntervalNode_1.DebugProtectionFunctionIntervalNode).whenTargetNamed(CustomNodes_1.CustomNodes.DebugProtectionFunctionIntervalNode);
+    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).toConstructor(DebugProtectionFunctionNode_1.DebugProtectionFunctionNode).whenTargetNamed(CustomNodes_1.CustomNodes.DebugProtectionFunctionNode);
+    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).toConstructor(DomainLockNode_1.DomainLockNode).whenTargetNamed(CustomNodes_1.CustomNodes.DomainLockNode);
+    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).toConstructor(NodeCallsControllerFunctionNode_1.NodeCallsControllerFunctionNode).whenTargetNamed(CustomNodes_1.CustomNodes.NodeCallsControllerFunctionNode);
+    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).toConstructor(SelfDefendingUnicodeNode_1.SelfDefendingUnicodeNode).whenTargetNamed(CustomNodes_1.CustomNodes.SelfDefendingUnicodeNode);
+    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).toConstructor(StringArrayCallsWrapper_1.StringArrayCallsWrapper).whenTargetNamed(CustomNodes_1.CustomNodes.StringArrayCallsWrapper);
+    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).toConstructor(StringArrayNode_1.StringArrayNode).whenTargetNamed(CustomNodes_1.CustomNodes.StringArrayNode);
+    bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode).toConstructor(StringArrayRotateFunctionNode_1.StringArrayRotateFunctionNode).whenTargetNamed(CustomNodes_1.CustomNodes.StringArrayRotateFunctionNode);
     bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNodeGroup).to(ConsoleOutputCustomNodeGroup_1.ConsoleOutputCustomNodeGroup).whenTargetNamed(CustomNodeGroups_1.CustomNodeGroups.ConsoleOutputCustomNodeGroup);
     bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNodeGroup).to(DebugProtectionCustomNodeGroup_1.DebugProtectionCustomNodeGroup).whenTargetNamed(CustomNodeGroups_1.CustomNodeGroups.DebugProtectionCustomNodeGroup);
     bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNodeGroup).to(DomainLockCustomNodeGroup_1.DomainLockCustomNodeGroup).whenTargetNamed(CustomNodeGroups_1.CustomNodeGroups.DomainLockCustomNodeGroup);
     bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNodeGroup).to(SelfDefendingCustomNodeGroup_1.SelfDefendingCustomNodeGroup).whenTargetNamed(CustomNodeGroups_1.CustomNodeGroups.SelfDefendingCustomNodeGroup);
     bind(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNodeGroup).to(StringArrayCustomNodeGroup_1.StringArrayCustomNodeGroup).whenTargetNamed(CustomNodeGroups_1.CustomNodeGroups.StringArrayCustomNodeGroup);
     bind(ServiceIdentifiers_1.ServiceIdentifiers['Factory<ICustomNode>']).toFactory(function (context) {
+        var cache = new _map2.default();
         var cachedOptions = void 0;
         return function (customNodeName) {
             if (!cachedOptions) {
                 cachedOptions = context.container.get(ServiceIdentifiers_1.ServiceIdentifiers.IOptions);
             }
-            switch (customNodeName) {
-                case CustomNodes_1.CustomNodes.BinaryExpressionFunctionNode:
-                    return new BinaryExpressionFunctionNode_1.BinaryExpressionFunctionNode(cachedOptions);
-                case CustomNodes_1.CustomNodes.ControlFlowStorageCallNode:
-                    return new ControlFlowStorageCallNode_1.ControlFlowStorageCallNode(cachedOptions);
-                case CustomNodes_1.CustomNodes.ControlFlowStorageNode:
-                    return new ControlFlowStorageNode_1.ControlFlowStorageNode(cachedOptions);
+            if (cache.has(customNodeName)) {
+                return new (cache.get(customNodeName))();
             }
-            return context.container.getNamed(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode, customNodeName);
+            var constructor = context.container.getNamed(ServiceIdentifiers_1.ServiceIdentifiers.ICustomNode, customNodeName);
+            cache.set(customNodeName, constructor);
+            return new constructor(cachedOptions);
         };
     });
     bind(ServiceIdentifiers_1.ServiceIdentifiers['Factory<ICustomNodeGroup>']).toFactory(function (context) {

+ 38 - 39
src/container/modules/custom-nodes/CustomNodesModule.ts

@@ -30,56 +30,56 @@ import { StringArrayRotateFunctionNode } from '../../../custom-nodes/string-arra
 
 export const customNodesModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => {
     // custom nodes
-    bind<ICustomNode>(ServiceIdentifiers.ICustomNode)
-        .to(BinaryExpressionFunctionNode)
+    bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.ICustomNode)
+        .toConstructor(BinaryExpressionFunctionNode)
         .whenTargetNamed(CustomNodes.BinaryExpressionFunctionNode);
 
-    bind<ICustomNode>(ServiceIdentifiers.ICustomNode)
-        .to(ControlFlowStorageCallNode)
+    bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.ICustomNode)
+        .toConstructor(ControlFlowStorageCallNode)
         .whenTargetNamed(CustomNodes.ControlFlowStorageCallNode);
 
-    bind<ICustomNode>(ServiceIdentifiers.ICustomNode)
-        .to(ControlFlowStorageNode)
+    bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.ICustomNode)
+        .toConstructor(ControlFlowStorageNode)
         .whenTargetNamed(CustomNodes.ControlFlowStorageNode);
 
-    bind<ICustomNode>(ServiceIdentifiers.ICustomNode)
-        .to(ConsoleOutputDisableExpressionNode)
+    bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.ICustomNode)
+        .toConstructor(ConsoleOutputDisableExpressionNode)
         .whenTargetNamed(CustomNodes.ConsoleOutputDisableExpressionNode);
 
-    bind<ICustomNode>(ServiceIdentifiers.ICustomNode)
-        .to(DebugProtectionFunctionCallNode)
+    bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.ICustomNode)
+        .toConstructor(DebugProtectionFunctionCallNode)
         .whenTargetNamed(CustomNodes.DebugProtectionFunctionCallNode);
 
-    bind<ICustomNode>(ServiceIdentifiers.ICustomNode)
-        .to(DebugProtectionFunctionIntervalNode)
+    bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.ICustomNode)
+        .toConstructor(DebugProtectionFunctionIntervalNode)
         .whenTargetNamed(CustomNodes.DebugProtectionFunctionIntervalNode);
 
-    bind<ICustomNode>(ServiceIdentifiers.ICustomNode)
-        .to(DebugProtectionFunctionNode)
+    bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.ICustomNode)
+        .toConstructor(DebugProtectionFunctionNode)
         .whenTargetNamed(CustomNodes.DebugProtectionFunctionNode);
 
-    bind<ICustomNode>(ServiceIdentifiers.ICustomNode)
-        .to(DomainLockNode)
+    bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.ICustomNode)
+        .toConstructor(DomainLockNode)
         .whenTargetNamed(CustomNodes.DomainLockNode);
 
-    bind<ICustomNode>(ServiceIdentifiers.ICustomNode)
-        .to(NodeCallsControllerFunctionNode)
+    bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.ICustomNode)
+        .toConstructor(NodeCallsControllerFunctionNode)
         .whenTargetNamed(CustomNodes.NodeCallsControllerFunctionNode);
 
-    bind<ICustomNode>(ServiceIdentifiers.ICustomNode)
-        .to(SelfDefendingUnicodeNode)
+    bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.ICustomNode)
+        .toConstructor(SelfDefendingUnicodeNode)
         .whenTargetNamed(CustomNodes.SelfDefendingUnicodeNode);
 
-    bind<ICustomNode>(ServiceIdentifiers.ICustomNode)
-        .to(StringArrayCallsWrapper)
+    bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.ICustomNode)
+        .toConstructor(StringArrayCallsWrapper)
         .whenTargetNamed(CustomNodes.StringArrayCallsWrapper);
 
-    bind<ICustomNode>(ServiceIdentifiers.ICustomNode)
-        .to(StringArrayNode)
+    bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.ICustomNode)
+        .toConstructor(StringArrayNode)
         .whenTargetNamed(CustomNodes.StringArrayNode);
 
-    bind<ICustomNode>(ServiceIdentifiers.ICustomNode)
-        .to(StringArrayRotateFunctionNode)
+    bind<interfaces.Newable<ICustomNode>>(ServiceIdentifiers.ICustomNode)
+        .toConstructor(StringArrayRotateFunctionNode)
         .whenTargetNamed(CustomNodes.StringArrayRotateFunctionNode);
 
     // node groups
@@ -106,6 +106,8 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
     // customNode factory
     bind<ICustomNode>(ServiceIdentifiers['Factory<ICustomNode>'])
         .toFactory<ICustomNode>((context: interfaces.Context) => {
+            const cache: Map <CustomNodes, interfaces.Newable<ICustomNode>> = new Map();
+
             let cachedOptions: IOptions;
 
             return (customNodeName: CustomNodes) => {
@@ -113,22 +115,19 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
                     cachedOptions = context.container.get<IOptions>(ServiceIdentifiers.IOptions);
                 }
 
-                // we should avoid automatic resolve for custom nodes which will resolve during traverse over AST tree
-                switch (customNodeName) {
-                    case CustomNodes.BinaryExpressionFunctionNode:
-                        return new BinaryExpressionFunctionNode(cachedOptions);
+                if (cache.has(customNodeName)) {
+                    return new (<interfaces.Newable<ICustomNode>>cache.get(customNodeName));
+                }
 
-                    case CustomNodes.ControlFlowStorageCallNode:
-                        return new ControlFlowStorageCallNode(cachedOptions);
+                const constructor: interfaces.Newable<ICustomNode> = context.container
+                    .getNamed<interfaces.Newable<ICustomNode>>(
+                        ServiceIdentifiers.ICustomNode,
+                        customNodeName
+                    );
 
-                    case CustomNodes.ControlFlowStorageNode:
-                        return new ControlFlowStorageNode(cachedOptions);
-                }
+                cache.set(customNodeName, constructor);
 
-                return context.container.getNamed<ICustomNode>(
-                    ServiceIdentifiers.ICustomNode,
-                    customNodeName
-                );
+                return new constructor(cachedOptions);
             };
         });