Skip to content

Instantly share code, notes, and snippets.

View alizbazar's full-sized avatar

Albert Nazander alizbazar

View GitHub Profile
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@alizbazar
alizbazar / check-for-legacy-headers.js
Last active January 29, 2021 03:51
Gradually rolling out eslint rules resulting in many errors in a legacy project
/*
This script is meant to be used with lint-staged receiving changed files as arguments.
It checks if file has "rules-disabled-on" header present with date in the past.
If yes, it reports files that need to be reformatted.
*/
const _ = require('lodash')
const fs = require('fs')
const moment = require('moment')
@alizbazar
alizbazar / machine.js
Last active September 3, 2020 21:47
Generated by XState Viz: https://xstate.js.org/viz
const assessmentMachine = Machine(
{
id: "assessment",
initial: "phq9_1",
context: {
phq9: 0,
gad7: 0,
mhq: false,
answers: []
},
@alizbazar
alizbazar / machine.js
Last active August 20, 2020 09:35
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@alizbazar
alizbazar / firebase.ts
Created September 19, 2019 00:16
Firebase real-time database mock: great to use with offline tests etc.
import _, { Dictionary, isNil, pickBy, mapValues } from 'lodash'
const { database: firebase } = require('firebase-admin')
export const FIREBASE_TIMESTAMP = firebase.ServerValue.TIMESTAMP
let cache = {}
const convertEmptyToNull = (obj: any): any => {
if (isNil(obj)) {
return null
@alizbazar
alizbazar / machine.js
Created August 9, 2019 12:26
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@alizbazar
alizbazar / healthCheck.js
Created June 14, 2019 15:42
Scheduled health check for a server
/*
The idea is that the machine is used by a single cloud function / endpoint, which is called either periodically or based on
- Health check using periodic process, such as Google Cloud Scheduler or cron
- On failure, schedule additional checks, for ex. using Google Cloud Tasks
- If failures continue, restart the system
- Go to normal mode of operation if all good
*/
// To visualize statechart, copy paste code to https://statecharts.github.io/xstate-viz/
@alizbazar
alizbazar / healthCheck.js
Created June 14, 2019 15:23
Scheduled health check for the server.
// To visualize statechart, copy paste code to https://statecharts.github.io/xstate-viz/
// Available variables:
// Machine (machine factory function)
// assign (action)
// XState (all XState exports)
const healthCheck = Machine({
id: 'healthCheck',
context: { attempts: 0 },
initial: 'normal',
@alizbazar
alizbazar / global-events-for-local-transitions.js
Created May 21, 2019 17:32
Using global events for local transitions causes services be re-invoked
let i = 0
Machine({
on: {
ERROR: 'restarting',
},
invoke: {
src: 'subscribeToStatus',
},
initial: 'running',
const XState = require('xstate')
const { Machine, interpret } = XState
const { raise } = XState.actions
/*
run from cli using:
node xstate-onentry-timing-bug.js [sync|async|raise]
*/