mercredi 19 février 2020

Delete element from a list, with multiple lists to check -- what's a good way to implement?

I created state within App.js for a single page application (kanban board)

Within the state i have three key values with arrays and one other key value:

    this.state = {
      currentItem: { text: '' },
      toDoListItems: [
        { text: 'hello world' }
      ],
      inProgressListItems: [],
      doneListItems: []
    }

Currently my implementation of use cases like delete a task from any board, is implemented by passing the name of the list item.

ie. {() => this.deleteItem(item, "toDoListItems")}

How should i make my code more extensible? I was thinking first, I should place all the lists within an object within state. But is it good/bad practice to manually iterate and which array contains the element that I'm looking for? It feels like if i were to do that, I'm making redundant checks.

Converting single file revealing module pattern javascript to multi-file using import

I am new to JS design patterns and have not used much of require or import. I have a single module which contains multiple functions and private variables, which is packaged into a module. Currently everything is in one file but splitting it into multiple files would be good practice and provide better clarity. The simplified view of the module's pattern looks something like this:

let Module = () => {
  //some private variables
  let private1,
      private2;

  //some public functions
  function addDatatoPrivate1 (data) {
    private1 = processData(data);
  }

  function addDatatoPrivate2 (data) {
    private2 = processData(data);
  }

  //private function processData
  function processData(data) {
    return data.trim();
  }

  return {
    addDatatoPrivate1: addDatatoPrivate1,
    addDatatoPrivate2: addDatatoPrivate2,
  }
}

I would like to split up the functions into multiple files i.e. separate file for addDatatoPrivate1, addDatatoPrivate2 and processData. In addition I would like to have the variables private1 and private2 be available for other methods in the Module privately. How do I go about splitting the code into multiple files and then how to use import to get the different components of the module to package into one.

The eventual aim is to have something which a user can load into their own project and use something like d3js or jQuery. For example, with the code as above anyone can simply assign the module to a variable and use it like so:

  let moduleInstance = Module();
  moduleInstance.addDatatoPrivate1(' some data here ');
  moduleInstance.addDatatoPrivate2(' some data here2 ');

Calling methods conditionally from two different classes

I have two classes ExceptionLog and DebugLog

public class ExceptionLog {
    public static String StackTrace {get; set;}
    public static String ClassName {get; set;}
    public static String MethodName {get; set;}
    public static String LogType {get;set;}
    public static Exception ex {get;set;}

    public Static void Debug(Exception ex)
    {
        logType = 'EXCEPTION'; 
        ex = ex;
        log();
    }

    public Static void log()
    {
        try
        {
            extractException(); 
            writeToObject(); 

        }
        catch(Exception e)
        {
            //new ExceptionLog().Module('LogException').log(e);            
        }    
    }

    public static void extractException()
    {
        // Logic here            
    }

    public static void writeToObject()
    {        
        // data save to object logic here       
    }    
}

and

public class DebugLog {
    public static String LogType {get;set;}
    public static String DebugMessage {get;set;} 

    public Static void Debug(String message)
    {
        Debug(null, message);
    }

    public Static void Debug(LoggingLevel level, String message)
    {
        if(level != null )
        {
            LogType = String.valueOf(level);             
        }        

        DebugMessage = message;

        log();
    }

    public Static void log()
    {
        // Log logic here   
    }        

}

What I want to achieve is, write a controller class that will make decision of which debug method needs to be called

public class Log {
    public void Debug(String message)
    {
        DebugLog.Debug(message);
    }
    public void Debug(loggingLevel loggingLevel, String message)
    {
        DebugLog.Debug(loggingLevel, message);    
    }
    public void Debug(Exception ex)
    {
        ExceptionLog.Debug(ex);
    }
}

That is, if I pass Exception in the debug method, it will call the ExceptionLog.Debug(ex) else it will call the debug method from DebugLog class.

I know the the object oriented approach is not good here. How can I design the classes more elegantly or any design pattern fit here?

Why use a (EIP) Normalizer instead of keeping a separate queue for each data format?

Within the context of enterprise integration patterns (EIP) there is the concept of a Normalizer -- which is composed of a queue, a content based message router, and a message translator to translate the different data formats to a uniform one.

I have always kept one queue for each kind of data. So when is this pattern necessary? It seems better to have a separate queue for each data format and route them directly to the appropriate translator -- and not have to rely upon (probably brittle) message identification.

Am I thinking about this wrong?

Design patterns for swing devlopment

I have start a new position at Cadence design system Indago team as GUI developer using java swing, as you know when design patterns used for such big tools, i want to ask what design patterns should i know to start understand such complex code and architecture.

Best Design to Sync Video Stream Across Clients in Asp.net Application?

We are developing an Asp.net core application with a video streaming feature. The project contains an action on a controller that returns video stream. For an upcoming demo we need to mock a video stream using a single video file played in a loop for a demo. Business requirements expect the stream to appear the same on all clients, e.g. if the controller just loads the video file from the beginning when the web request starts each client will be viewing the video at different time points and it would not be synced like a live stream.

What is the best design/architecture to sync all of the clients video feeds based on a single video file stored on the webserver?

useFactory depending on current component

I want to implement an Abstract Factory pattern. Here is a factory:

   export abstract class MyFactory {
      abstract getAdapter(): DataAdapter;
    }

Lets say we have 2 components AComp and BComp which belong to the same module AModule. Both of them have the factory dependency:

  constructor(
    ...
    private factory: MyFactory
  ) {}

I wan to hide concrete factory creation logic. So the solution I came up with is to put the creation logic into AModule:

@NgModule({
      declarations: [AComp, BComp],
      providers: [
        {
          provide: MyFactory,
          deps: [MY_FACTORY_TOKEN],
          useFactory: (token: string) => {
            // return concrete factory depending on the token
          }
        }
      ]
})
export class AModule { }

And each component has its own token provider:

@Component({
  ...
  providers: [ {provide: MY_FACTORY_TOKEN, useValue: 'string val' } ]
})

But it says No provider for InjectionToken myfactorytoken!. Apparently I can't get the component provider on the module level.

Basically, my question is how to return concrete factory depending on current component? E.g. if it is AComp then return AConcreteFactory, if it is BComp then return BConcreteFactory.