Ink: Multiple ink apps

Created on 16 May 2019  路  5Comments  路  Source: vadimdemedes/ink

Hi,

I am trying to run one ink app, wait until it exits and then start another ink app. This doesn't seem to work. Here is an example code:

import {Color, AppContext, render} from 'ink';
import React, {Component} from 'react'

class Elem extends Component{
  constructor(){
    super()
    setTimeout(() => {
      this.props.exit()
    }, 2000);
  }

  render(){
    return (<Color bold>{this.props.text}</Color>)
  }
}

const ElemApp = ({text})=>(
  <AppContext.Consumer>
    {({ exit }) => (
      <Elem text={text} exit={exit}/>
    )}
  </AppContext.Consumer>
)

async function start(){
  const app = render(<ElemApp text="App 1" />);
  await app.waitUntilExit()
  const app2 = render(<ElemApp text="App 2" />);
  await app2.waitUntilExit()
  console.log("finished")
}

start()

App 1 renders fine but App 2 never renders and directly jumps to finished line.
Am I doing it wrong?
Thanks

bug

All 5 comments

I'll take a look and reply back here, thanks!

I'm having a similar issue, except instead of skipping the 2nd render, it just hangs. It doesn't render the 2nd Ink app.

export function startDevServer(cwd: string) {
  const stores = {
    components: observable.array([])
  };

  const { waitUntilExit } = render(
    <Provider {...stores}>
      <DevServerComponent cwd={cwd} />
    </Provider>
  );

  return waitUntilExit();
}
const getNameAndChoice = (defaultName: string | null): Promise<any> => {
  return new Promise(resolve => {
    const { unmount } = render(
      <BoilerplatePicker
        defaultName={defaultName || null}
        onSelect={(choice, name) => {
          resolve([choice, name]);
          unmount();
        }}
      />
    );
  });
};

export async function newCommand(
  defaultName: string | null,
  destination: string | null,
  autoOpen: boolean
) {
  const user = await requireLogin();

  const _destination = destination || __dirname;

  const [choice, name] = await getNameAndChoice(defaultName);

  // ... unrelated code that prints some logs and writes some files

  await startDevServer(_destination);
}

Edit: nevermind. It's the same behavior. I put the log in the wrong place. 馃う鈥嶁檪

I worked around it by forking the process :joy:

return require("child_process").fork(cmdName, ["dev"], {
    cwd: _destination,
    env: process.env,
    detached: false
  });

Thanks for a detailed bug report, guys! Issue should be fixed via https://github.com/vadimdemedes/ink/commit/6889f47e5c67210e780c357f0fbb51297a25f5e2.

@Jarred-Sumner @asadm would you be able to pull Ink's master (npm install vadimdemedes/ink) and verify that it solved your problem? I will do a new release afterwards.

Fixed in 2.3.0.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cometkim picture cometkim  路  3Comments

JakeDawkins picture JakeDawkins  路  4Comments

HMR
konsumer picture konsumer  路  3Comments

danikaze picture danikaze  路  5Comments

nmehta6 picture nmehta6  路  3Comments