How to use the @loki/core.withRetries function in @loki/core

To help you get started, we’ve selected a few @loki/core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github oblador / loki / packages / target-native-core / src / create-websocket-target.js View on Github external
// TODO: remove other listeners
        resolve(ws);
        ws.on('message', onMessage);
      };

      const onClose = () => {
        debug('Connection closed');
        clearTimeout(timeout);
      };

      ws.on('open', onOpen);
      ws.on('close', onClose);
      ws.on('error', onError);
    });

  const prepare = withRetries(5)(async () => {
    sendLokiCommand('prepare');
    await waitForLokiMessage('didPrepare');
  });

  async function start() {
    try {
      socket = await connect(socketUri);
    } catch (err) {
      throw new Error(
        'Failed connecting to storybook server. Start it with `yarn storybook` and review --react-native-port and --host arguments.'
      );
    }
    try {
      await prepare();
    } catch (err) {
      throw new Error(
github oblador / loki / packages / target-native-ios-simulator / src / create-ios-simulator-target.js View on Github external
const fs = require('fs-extra');
const osnap = require('osnap/src/ios');
const { withRetries } = require('@loki/core');
const { createWebsocketTarget } = require('@loki/target-native-core');

const saveScreenshotToFile = withRetries(3)(async filename => {
  await osnap.saveToFile({ filename });
  const { size } = await fs.stat(filename);
  if (size === 0) {
    throw new Error('Screenshot failed ');
  }
});

const createIOSSimulatorTarget = socketUri =>
  createWebsocketTarget(socketUri, 'ios', saveScreenshotToFile);

module.exports = createIOSSimulatorTarget;
github oblador / loki / packages / target-chrome-core / src / create-chrome-target.js View on Github external
};

    const getPositionInViewport = async selector => {
      try {
        return await executeFunctionWithWindow(getSelectorBoxSize, selector);
      } catch (error) {
        if (error.message === 'No visible elements found') {
          throw new Error(
            `Unable to get position of selector "${selector}". Review the \`chromeSelector\` option and make sure your story doesn't crash.`
          );
        }
        throw error;
      }
    };

    client.captureScreenshot = withRetries(options.chromeRetries)(
      withTimeout(CAPTURING_SCREENSHOT_TIMEOUT, 'captureScreenshot')(
        async (selector = 'body') => {
          debug(`Getting viewport position of "${selector}"`);
          const position = await getPositionInViewport(selector);

          if (position.width === 0 || position.height === 0) {
            throw new Error(
              `Selector "${selector} has zero width or height. Can't capture screenshot.`
            );
          }

          const clip = {
            scale: 1,
            x: Math.floor(position.x),
            y: Math.floor(position.y),
            width: Math.ceil(position.width),
github oblador / loki / packages / target-chrome-aws-lambda / src / create-chrome-aws-lambda-target.js View on Github external
function createChromeAWSLambdaTarget({
  baseUrl = 'http://localhost:6006',
  chromeAwsLambdaFunctionName,
  chromeAwsLambdaRetries = 0,
}) {
  const invoke = withRetries(chromeAwsLambdaRetries, 1000)(async payload => {
    const lambda = new AWS.Lambda();

    const params = {
      FunctionName: chromeAwsLambdaFunctionName,
      Payload: JSON.stringify(payload),
    };

    debug(`Invoking ${params.FunctionName} with ${params.Payload}`);
    const data = await lambda.invoke(params).promise();
    const response = JSON.parse(data.Payload);
    if (response.errorMessage) {
      throw parseError(response.errorMessage);
    }
    return response;
  });
github oblador / loki / packages / target-chrome-core / src / create-chrome-target.js View on Github external
return buffer;
        }
      )
    );

    return client;
  }

  const getStoryUrl = (kind, story) =>
    `${baseUrl}/iframe.html?selectedKind=${encodeURIComponent(
      kind
    )}&selectedStory=${encodeURIComponent(story)}`;

  const launchStoriesTab = withTimeout(LOADING_STORIES_TIMEOUT)(
    withRetries(2)(async url => {
      const tab = await launchNewTab({
        width: 100,
        height: 100,
        chromeEnableAnimations: true,
        clearBrowserCookies: false,
      });
      await tab.loadUrl(url);
      return tab;
    })
  );

  async function getStorybook() {
    const url = `${baseUrl}/iframe.html`;
    try {
      const tab = await launchStoriesTab(url);
      return tab.executeFunctionWithWindow(getStories);