A delectable demo of Signals by Kristen Hewell Garrett (@pzuraq)
The example app in this demo is for planning meals around
high level macro nutrient goals. It's not an app for tracking food
consumption or calories, but is conceptually related to those types of apps.
const cheese = new Signal.State({
name: 'Cheese!',
servingSize: 1,
servingMeasure: 'cups',
protein: 4,
carbs: 5,
fat: 7,
});
const quesadilla = new Signal.State({
name: 'Quesadilla',
ingredients: [
{
value: cheese,
amount: 0.5
},
{
value: tortilla,
amount: 1,
}
]
});
const quesMacros = new Signal.Computed(() => {
const macros = {
protein: 0,
carbs: 0,
fats: 0,
};
const { ingredients } = quesadilla.get();
for (const { value, amount } of ingredients) {
const {
protein,
carbs,
fat,
} = value.get();
macros.protein += protein * amount;
macros.carbs += carbs * amount;
macros.fats += fat * amount;
}
return macros;
});
const plan = new Signal.State({
name: 'Friday',
people: ['Kristen', 'Liz'],
meals: [
{
name: 'Breakfast',
items: {
Kristen: [
{
value: quesMacros,
amount: 2,
},
{
value: cheese,
amount: 1,
}
],
Liz: [
{
value: quesMacros,
amount: 3,
}
]
}
}
]
});
const planMacros = new Signal.Computed(() => {
const { people, meals } = plan.get();
const macros = {};
// initialize each person
for (const person of people) {
macros[person] = {
protein: 0,
carbs: 0,
fat: 0,
}
}
// loop over each meal to get the items
for (const { items } of meals) {
// loop over people to get their specific
// items for that meal
for (const person of people) {
// loop over the items for that person
for (const { value, amount } of items[person]) {
// ok, finally add the item macros to the totals!
const {
protein,
carbs,
fat,
} = value.get();
macros[person].protein += protein * amount;
macros[person].carbs += carbs * amount;
macros[person].fat += fat * amount;
}
}
}
return macros;
});
@pzuraq | pzuraq.com