Skip to content

Instantly share code, notes, and snippets.

Avatar

Shopify Engineering Communications ShopifyEng

View GitHub Profile
View pricing-calculation-final-implementation-using-sorbet.rb
# ----------------------------- Gem -----------------------------
module PricingEngine
module PricingRepositoryInterface
extend T::Sig
extend T::Helpers
interface!
sig { abstract.params(ids: T::Array[Integer]).returns(T::Array[Schema::Variant]) }
def variants_by_ids(ids);end
View pricing-calculation-final-implementation.rb
# ----------------------------- Gem -----------------------------
module PricingEngine
class PricingRepositoryInterface
def variants_by_ids(ids)
raise NotImplemented
end
def variants_by_titles(titles)
raise NotImplemented
end
View pricing-calculation-gem-using-repository.rb
module PricingEngine
class Engine
# @param repository [PricingEngine::PricingRepositoryInterface] environment specific functions to
# access db and cache data.
#
# @return [void]
def initialize(repository)
@repository = repository
end
View pricing-calculation-gem-consumer-2.rb
# Consumer 2 Logic
require 'pricing_engine'
module StorefrontRenderer
class PricingRepository < PricingEngine::PricingRepositoryInterface
def variants_by_ids(ids)
variants = DataStoreConnection.execute("SELECT title, price FROM variants WHERE id in (?)", ids).limit(50)
variants.map { |variant| PricingEngine::Schema::Variant.new(title: variant.title, price: variant.price) }
View pricing-calculation-gem-consumer-1.rb
# Consumer 1 Logic
require 'pricing_engine'
module ShopifyCore
class PricingRepository < PricingEngine::PricingRepositoryInterface
def variants_by_ids(ids)
variants = ProductVariant.where(id: ids).select(:title, :price).limit(50)
variants.map { |variant| PricingEngine::Schema::Variant.new(title: variant.title, price: variant.price) }
end
View pricing-calculation-gem-schema-definition.rb
module PricingEngine
class PricingRepositoryInterface
def variants_by_ids(ids)
raise NotImplemented
end
def variants_by_titles(titles)
raise NotImplemented
end
end
View example-pricing-gem.rb
# Gem
module PricingEngine
class Engine
#...
# Calculates the price for variants given the buyer context.
#
# @param context [Schema::BuyerContext] the buyer context
#
# @return [Array<PricingEngine::Schema::Price>] An array of prices for the context passed.
def calculate_prices_for_variants(context)
View Capturing Every Change From Shopify’s Sharded Monolith
{
"payload": {
"op": "c",
"ts_ms": 1465491411815,
"before": null,
"after": {
"id": 1,
"first_name": "Peyton",
"last_name": "Manning",
"address1": "18 Willow St",
@ShopifyEng
ShopifyEng / Confetti.tsx
Created Apr 6, 2020
Building Arrive's Confetti in React Native with Reanimated - Confetti Final
View Confetti.tsx
import React, {useMemo} from 'react'
import Animated from 'react-native-reanimated'
import {View, Dimensions, StyleSheet} from 'react-native'
import FastImage from 'react-native-fast-image'
import ConfettiImage from 'assets/images/confetti.png'
const NUM_CONFETTI = 100
const COLORS = ['#00e4b2', '#09aec5', '#107ed5']
const CONFETTI_SIZE = 16
@ShopifyEng
ShopifyEng / profile.rb
Created Mar 30, 2020
Optimizing Ruby Lazy Initialization in TruffleRuby with Deoptimization - Code ||= Lazy Initialization Profiling
View profile.rb
# When run on the following 20 repositories, it found 2082 ||= and it was used as lazy init 64.3% of the time
# Those numbers are fuzzy, as there is not certain way to prove if something was used as a lazy init
# This script only marks a usage as a lazy init if the variable is being set to a constant,
# in which case if it is not a lazy init then the developers have done something weird.
# It is also marked as a lazy init if the variable is an instance variable or a class variable (either @var or @@var)
# AND the lazy init is used in the top level of a function, AND the function name is contained by the variable or vice versa.
# These are almost certain to be lazy initialized because the assumed behaviour is to always call the method instead of the variable,
# and other usages would be both weird usage of instance/class variables and/or weird usage of the method naming
# This of course, misses a variety of cases but likely does not get any cases that do not lazy initialize.
# A better way to profile could be