Skip to content

Commit

Permalink
fix: Pass options down through utils
Browse files Browse the repository at this point in the history
This fixes an issue where global util unintentionally shared state
between simultaneous async calls.
State is now kept only on one specific instance of window, and is passed
throughout the app.
  • Loading branch information
julianpoy committed Feb 20, 2021
1 parent 0fb03e7 commit 62da395
Show file tree
Hide file tree
Showing 12 changed files with 378 additions and 313 deletions.
24 changes: 0 additions & 24 deletions src/global.js

This file was deleted.

28 changes: 12 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import global from './global';
import { generateConfig } from './utils/config';
import * as ClipUtils from './utils/clip';

export const clipRecipe = async (options) => {
global.options = {
...global.options,
...(options || {}),
};
global.window = global.options.window;
const config = generateConfig(options);

return {
imageURL: ClipUtils.clipImageURL(),
title: ClipUtils.clipTitle(),
description: ClipUtils.clipDescription(),
source: ClipUtils.clipSource(),
yield: ClipUtils.clipYield(),
activeTime: ClipUtils.clipActiveTime(),
totalTime: ClipUtils.clipTotalTime(),
ingredients: await ClipUtils.clipIngredients(),
instructions: await ClipUtils.clipInstructions(),
notes: ClipUtils.clipNotes(),
imageURL: ClipUtils.clipImageURL(config),
title: ClipUtils.clipTitle(config),
description: ClipUtils.clipDescription(config),
source: ClipUtils.clipSource(config),
yield: ClipUtils.clipYield(config),
activeTime: ClipUtils.clipActiveTime(config),
totalTime: ClipUtils.clipTotalTime(config),
ingredients: await ClipUtils.clipIngredients(config),
instructions: await ClipUtils.clipInstructions(config),
notes: ClipUtils.clipNotes(config),
};
};
62 changes: 31 additions & 31 deletions src/utils/clip.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import global from '../global';
import { format } from './text';
import {
getSrcFromImage,
Expand Down Expand Up @@ -26,53 +25,54 @@ import {
getInstructionsFromSchema,
} from './schema';

export const clipImageURL = () => format.imageURL(
getImageSrcFromSchema()
|| getSrcFromImage(grabLargestImage()),
export const clipImageURL = (config) => format.imageURL(
getImageSrcFromSchema(config.window)
|| getSrcFromImage(grabLargestImage(config.window)),
);

export const clipTitle = () => format.title(
getTitleFromSchema()
|| grabLongestMatchByClasses(...ClassMatchers.title) || grabRecipeTitleFromDocumentTitle(),
export const clipTitle = (config) => format.title(
getTitleFromSchema(config.window)
|| grabLongestMatchByClasses(config.window, ...ClassMatchers.title)
|| grabRecipeTitleFromDocumentTitle(),
);

export const clipDescription = () => format.description(
getDescriptionFromSchema()
|| grabLongestMatchByClasses(...ClassMatchers.description),
export const clipDescription = (config) => format.description(
getDescriptionFromSchema(config.window)
|| grabLongestMatchByClasses(config.window, ...ClassMatchers.description),
);

export const clipSource = () => format.source(
grabSourceFromDocumentTitle() || global.window.location.hostname,
export const clipSource = (config) => format.source(
grabSourceFromDocumentTitle(config.window) || config.window.location.hostname,
);

export const clipYield = () => format.yield(
getYieldFromSchema()
|| grabLongestMatchByClasses(...ClassMatchers.yield)
|| closestToRegExp(matchYield).replace('\n', ''),
export const clipYield = (config) => format.yield(
getYieldFromSchema(config.window)
|| grabLongestMatchByClasses(config.window, ...ClassMatchers.yield)
|| closestToRegExp(config.window, matchYield).replace('\n', ''),
);

export const clipActiveTime = () => format.activeTime(
grabLongestMatchByClasses(...ClassMatchers.activeTime)
|| closestToRegExp(matchActiveTime).replace('\n', ''),
export const clipActiveTime = (config) => format.activeTime(
grabLongestMatchByClasses(config.window, ...ClassMatchers.activeTime)
|| closestToRegExp(config.window, matchActiveTime).replace('\n', ''),
);

export const clipTotalTime = () => format.totalTime(
grabLongestMatchByClasses(...ClassMatchers.totalTime)
|| closestToRegExp(matchTotalTime).replace('\n', ''),
export const clipTotalTime = (config) => format.totalTime(
grabLongestMatchByClasses(config.window, ...ClassMatchers.totalTime)
|| closestToRegExp(config.window, matchTotalTime).replace('\n', ''),
);

export const clipIngredients = async () => format.ingredients(
getIngredientsFromSchema()
export const clipIngredients = async (config) => format.ingredients(
getIngredientsFromSchema(config.window)
|| grabLongestMatchByClasses(...ClassMatchers.ingredients),
)
|| format.ingredients(await grabByMl(1));
|| format.ingredients(await grabByMl(config, 1));

export const clipInstructions = async () => format.instructions(
getInstructionsFromSchema()
|| grabLongestMatchByClasses(...ClassMatchers.instructions),
export const clipInstructions = async (config) => format.instructions(
getInstructionsFromSchema(config.window)
|| grabLongestMatchByClasses(config.window, ...ClassMatchers.instructions),
)
|| format.instructions(await grabByMl(2));
|| format.instructions(await grabByMl(config, 2));

export const clipNotes = () => format.notes(
grabLongestMatchByClasses(...ClassMatchers.notes),
export const clipNotes = (config) => format.notes(
grabLongestMatchByClasses(config.window, ...ClassMatchers.notes),
);
Loading

0 comments on commit 62da395

Please sign in to comment.