Git Product home page Git Product logo

Comments (4)

TomiS avatar TomiS commented on August 11, 2024 1

@alexfedoseev Cool. I'll see if I'll find a way (and time) to make it happen.

from rescript-logger.

alex35mil avatar alex35mil commented on August 11, 2024 1

Looking good. Couple of suggestions.

  1. It makes sense to memoize platform value.
  2. You can do this to get rid of raw:
[@bs.val] external window: 'a = "window";
window->Js.typeof == "undefined"

from rescript-logger.

alex35mil avatar alex35mil commented on August 11, 2024

Hey, I wouldn't mind having UniversalLogger.re that leverages current loggers but I'm not sure about details of environment detection and it'd be far low in my priorities list in the near future to make it happen. I would happily accept PR though!

from rescript-logger.

TomiS avatar TomiS commented on August 11, 2024

Ok, not yet a PR, but I made one kind of working PoC inside my app. It looks like this:

// Let's define a variant because it's pretty
type platform =
  | Browser
  | Node;

// Utilizes 'ugly' raw javascript tester function
// that simply checks if window is available
let getPlatform = () => {
  let windowExists: unit => bool = [%raw
    {|
  function() {
    if (typeof window === 'undefined') { return false; }
    else return true;
  }
|}
  ];
  if (windowExists()) {
    Browser;
  } else {
    Node;
  };
};

// An universal wrapper module
module Log = {
  let error = (loc: string, msg: string) =>
    switch (getPlatform()) {
    | Browser => BrowserLogger.error(loc, msg)
    | Node => NodeLogger.error(loc, msg)
    };

  let info = (loc: string, msg: string) =>
    switch (getPlatform()) {
    | Browser => BrowserLogger.info(loc, msg)
    | Node => NodeLogger.info(loc, msg)
    };

  let warn = (loc: string, msg: string) =>
    switch (getPlatform()) {
    | Browser => BrowserLogger.warn(loc, msg)
    | Node => NodeLogger.warn(loc, msg)
    };
};

from rescript-logger.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.