Skip to content

Calling createTSClient stops console.log functioning #101

Open
@mcintyre94

Description

Describe the bug

When a page calls createTSClient from @run-wasm/ts its console.log function is silently broken. This is because that functions calls the TSClient constructor:

  public constructor(protected ts: any) {
    // Overriding the console.log method so that we can store the logs
    console.oldLog = console.log
    console.log = (value) => {
      // For some reason, the first 'incoming' log is always an instance of TSClient. Ignoring it
      if (!(value instanceof TSClient)) {
        this.logs.push(value)
      }
    }
  }

This is not sandboxed in any way by the browser, so calling it overrides console.log with a function that no longer logs messages

To Reproduce

I've created a replit showing this: https://ConsoleLogOverride.mcintyre94.repl.co

Replit seems to have an issue loading ATM so you may need to use the edit link: https://replit.com/@mcintyre94/ConsoleLogOverride#pages/index.js (can run the app from here)

  • Open the browser console
  • Click the top button "Log something!" a few times
  • It logs a unique message each click
  • Click the second button "Load TS Client", this calls createTSClient
  • The log message "About to call createTSClient!" appears
  • The log message "Now called createTSClient!" does not appear
  • Click the top button "Log something!" again
  • It will no longer log messages because console.log has been overwritten

Expected behavior

Calling createTSClient should not affect the caller's ability to log.

I'm not 100% sure but maybe we should have the overwritten console.log also call console.oldLog? This would mean that logs in the editor are printed to the browser console as well as captured to be displayed as output - probably not a bad thing? That'd be a one line fix and we wouldn't need to try to figure out a way to isolate the console change, which I suspect we can't do in the browser context anyway.

Using console.oldLog(...) in the caller works correctly as a workaround for now.

Screenshots

Screenshot 2021-10-20 at 23 01 37

Desktop (please complete the following information):

  • OS: macOS
  • Browser: Vivaldi
  • Version: 4.3.2439.44

Additional context

The page in my replit has the following code:

import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'
import { createTSClient } from '@run-wasm/ts'
import { useEffect } from 'react'

export default function Home() {
  const logMessage = () => {
    console.log(`You have clicked a button ${Math.random()}`)
  }

  const loadTSClient = () => {
    const ts = {}
    console.log('About to call createTSClient!')
    createTSClient(ts)
    console.log('Now called createTSClient!')
  }

  return (
    <>
    <button onMouseDown={logMessage}>Log something!</button>
    <br />
    <button onMouseDown={loadTSClient}>Load TS Client</button>
    </>
  )
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions