Skip to content

Instantly share code, notes, and snippets.

View cevr's full-sized avatar
🏠
Working from home

Cristian Velasquez Ramos cevr

🏠
Working from home
View GitHub Profile
@cevr
cevr / child-actor.tsx
Last active September 27, 2023 15:22
Accessing Shared Child of Child Actor using xstate & @xstate/react (v5 beta)
import type { ActorRefFrom, AnyActorRef, AnyStateMachine, SnapshotFrom } from 'xstate';
import { useMachine, useSelector } from '@xstate/react';
type MachineStateFrom<T extends AnyStateMachine | AnyActorRef> = [
state: SnapshotFrom<T>,
send: ActorRefFrom<T>['send'],
actor: ActorRefFrom<T>,
];
export function useActorFromRef<T extends AnyActorRef>(ref: T): MachineStateFrom<T> {
@cevr
cevr / esm-imports.cjs
Last active August 23, 2023 15:20
ts-morph codefix for esm imports
const path = require('path');
const fs = require('fs');
const { Project, SyntaxKind } = require('ts-morph');
async function main() {
const project = new Project();
const glob = path.join(process.cwd(), './**/(src|test)/**/*.{ts,tsx,js}');
project.addSourceFilesAtPaths(glob);
@cevr
cevr / client.tsx
Last active November 24, 2021 18:35
Cache update after mutation with writeFragment
createCaseFromEvent(result, variables, cache) {
const readEvent = () =>
cache.readFragment(
gql`
fragment _ on CaseManagementEvent {
id
cases {
id
status
}
@cevr
cevr / 3060_ti_ca.yaml
Created March 2, 2021 22:32
inventory hunter-config
---
refresh_interval: 30 # seconds
max_price: 700 # dollars
urls:
- https://www.canadacomputers.com/product_info.php?cPath=43_557_559&item_id=184760
- https://www.canadacomputers.com/product_info.php?cPath=43_557_559&item_id=185988
- https://www.canadacomputers.com/product_info.php?cPath=43_557_559&item_id=184759
- https://www.canadacomputers.com/product_info.php?cPath=43_557_559&item_id=184431
- https://www.canadacomputers.com/product_info.php?cPath=43_557_559&item_id=185087
- https://www.canadacomputers.com/product_info.php?cPath=43_557_559&item_id=185751
@cevr
cevr / editableTextMachine.ts
Last active November 14, 2021 15:25
xstate fp utils POC
import * as x from './xstate-fp';
export enum EditableTextState {
idle = 'idle',
editing = 'editing',
}
export enum EditableTextEvent {
mouseenter = 'mouseenter',
mouseleave = 'mouseleave',
@cevr
cevr / machine.js
Created December 16, 2019 23:41
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
const connect = (mapState, mapActions) => Component => {
const store = useStore(mapState);
const actions = useActions(mapActions);
return function Wrapped(props) {
return <Component {...props} {...store} {...actions} />;
};
};
function AddTodo() {
const [text, setText] = useState('');
const save = useActions(actions => actions.add);
const handleAddClick = async () => {
await save(text);
setText('');
};
const handleTextChange = e => setText(e.target.value);
return (
<div>
export default function App() {
const fetchTodos = useActions(actions => actions.fetchTodos);
useEffect(() => {
fetchTodos();
}, [fetchTodos]);
return (
<div>
<h1 style={{ margin: 0 }}>Todo</h1>
<Todos />
<AddTodo />
import { action, thunk } from 'easy-peasy';
import mockService from './mock-service';
export default {
todos: {},
// actions
setTodos: action((state, todos) => {
state.todos = todos.reduce((acc, todo) => {
acc[todo.id] = todo;
return acc;