Skip to content

Instantly share code, notes, and snippets.

View pavankataria's full-sized avatar

Pavan Kataria pavankataria

View GitHub Profile
@pavankataria
pavankataria / machine.js
Last active January 3, 2021 04:41
Generated by XState Viz: https://xstate.js.org/viz
const promiseMachine = Machine({
id: 'promise',
initial: 'pending',
states: {
pending: {
on: {
// state transition (shorthand)
// this is equivalent to { target: 'resolved' }
RESOLVE: 'resolved',
@pavankataria
pavankataria / machine.js
Last active December 28, 2021 19:01
Generated by XState Viz: https://xstate.js.org/viz
const lightMachine = Machine({
key: 'light',
initial: 'green',
states: {
green: {
on: { NEXT: "yellow" }
},
yellow: { on: { NEXT: "red" }},
red: {
initial: 'green',
@pavankataria
pavankataria / machine.js
Last active January 2, 2021 12:57
Generated by XState Viz: https://xstate.js.org/viz
const allData = new Array(25).fill(0).map((_valu, i) => i + 1);
const perPage = 10;
const dataMachine = new Machine({
id: 'dataMachine',
initial: 'loading',
context: {
data: []
},
states: {
@pavankataria
pavankataria / machine.js
Last active January 2, 2021 03:47
Generated by XState Viz: https://xstate.js.org/viz
// A very cool visualisation machine
// Allows you to see what the state machine does
// And how it transitions from one state to the next
const toggleMachine = new Machine({
id: 'toggleMachine',
initial: 'inactive',
states: {
inactive: {
// Tell it which events the state should listen for.
// And you do that by defining an on property.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
/// 1. Capture the scene
guard let windowScene = (scene as? UIWindowScene) else { return }
/// 2. Create a new UIWindow using the windowScene constructor which takes in a window scene.
let window = UIWindow(windowScene: windowScene)
/// 3. Create a view hierarchy programmatically
let viewController = ArticleListViewController()
let balanceToConvert: String?
...
if let inputEntry = balance, inputEntry.sign == .negative {
balanceToConvert = Int(inputEntry.input) // Can return nil
.flatMap(Int.init) // Convert to Int if not nil
.flatMap { $0 * -1 } // flip sign if not nil
.flatMap(String.init) // convert back to String if not nil
}
...
// In the flatmap method it would look something like this
switch self {
case .some(let unwrapped):
return transform(unwrappedValue)
case .none:
return nil
}
func processInput(_ stringNumber: String?) -> Int? {
return stringNumber.flatMap { Int($0) }
}
func processInput(_ stringNumber: String?) -> Int? {
guard let unwrappedStringNumber = stringNumber {
return nil
}
return Int(unwrappedStringNumber)
}
@pavankataria
pavankataria / SortButton.swift
Created November 29, 2017 12:51
A UIButton subclass that renders a sort button.
fileprivate extension UIControlState {
static let unspecified = UIControlState(rawValue: 1 << 16)
static let ascending = UIControlState(rawValue: 1 << 17)
static let descending = UIControlState(rawValue: 1 << 18)
}
class SortButton: UIButton {
private var customState: UIControlState = .unspecified {
didSet {
setNeedsLayout()