Skip to content

Instantly share code, notes, and snippets.

View chrisza4's full-sized avatar

Chakrit Likitkhajorn chrisza4

  • ThoughtWorks
  • Thailand
View GitHub Profile
export interface IWaitAnimatingMatter {
matter: IMatter
expected_animated_time: Date | null
}
export interface IDeQueueWaitingMattersResult {
newQueue: IWaitAnimatingMatter[]
matterToAnimated?: IMatter
}
// Fetching logic
var matters = Api.getUserMatters(userId);
foreach (var matter of matters) {
GameEngine.getInstance().Matters.add(matter)
sleep(300)
}
class GameEngine { // Singleton
public IEnumerable<Matter> Matters;
public void ExecuteGameLoop () {
@chrisza4
chrisza4 / t.ex
Created November 18, 2019 16:38
So many nil check
# Assuming get(id) is database hit, so we don't want to hit database for everything up-front
def some_func(id1, id2, id3) do
if EntityA.get(id1) == nil do
{:error, :not_found}
else
if EntityB.get(id2) == nil do
{:error, :not_found}
else
if EntityC.get(id3) == nil do
@chrisza4
chrisza4 / hack-navigation.jsx
Created October 26, 2019 14:42
Hack navigation to alert
function getActiveRouteName(navigationState) {
if (!navigationState) {
return null
}
const route = navigationState.routes[navigationState.index]
// dive into nested navigators
if (route.routes) {
return getActiveRouteName(route)
}
return route.routeName
// Assuming <WeekRow />, <DateRow /> and <DateText /> contain styling that we want
const getDateSet = (start: moment.Moment) => {
const result = []
const numberOfDatesNeeded = 35 // 5 rows, 7 days for each row. Simply ignore 28-days February with 1st Feb on Monday.
for (let i = 0; i < numberOfDatesNeeded; i++) {
result.push(moment(start).add(i, 'day'))
}
return result
}
@chrisza4
chrisza4 / x.ex
Last active August 31, 2019 02:50
# ปรับนิดหน่อย
defp do_reset(conn, %{"email" => email, "reset_password_url" => reset_password_url} = attrs) do
with {:ok, reset_password_url} <- validate_url(reset_password_url, "reset_password_url"),
{:ok, forward_url} <- validate_url(attrs["forward_url"], "forward_url"),
{:ok, request} <- ResetPasswordGate.request(email)
|> send_request_email(request, reset_password_url, forward_url)
|> Pipe.ok do
render(conn, :empty, %{success: true})
else
# Prevents attackers from gaining knowledge about a user's email.
; This is a constructor for class with two method "increment" and "decrement"
(defn object-contructor "This is a factory" []
(let [mystate (atom {:x 1})]
(fn [dispatch]
(case dispatch
"increment" (swap! mystate update :x inc)
"decrement" (swap! mystate update :x dec)
mystate))))
; This is an object
class Engine
{
private int power = 0;
public int boost ()
{
this.power += 3;
}
}
class Car: Engine
// Inheritance
namespace Test
{
class AutoMobile
{
}
class Car: AutoMobile
{
@chrisza4
chrisza4 / Connector.jsx
Created May 19, 2019 06:50
ApiConnector
class ApiConnector extends React.Component {
componentDidMount = async () => {
this.setState({
loadingState: 'loading',
})
try {
const data = await this.props.loadData()
this.setState({
loadingState: 'loaded',
data,