Skip to content

Instantly share code, notes, and snippets.

View machine.js
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
View machine.js
const states = {
IDLE: 'IDLE',
SEARCHING: 'SEARCHING',
};
const searchNotEmptyTarget = {
target: states.SEARCHING,
cond: (_ctx, evt) => evt.search !== '', // && evt.search.length > 3,
actions: assign({ search: (_ctx, evt) => evt.search }),
};
@drmikecrowe
drmikecrowe / index.d.ts
Created May 31, 2020
cache-manager index.d.ts types
View index.d.ts
declare module 'cache-manager/caching' {
/**
* Generic caching interface that wraps any caching library with a compatible interface.
* @param args.store - The store must at least have `set` and a `get` functions.
* @param [args.isCacheableValue] - A callback function which is called
* with every value returned from cache or from a wrapped function. This lets you specify
* which values should and should not be cached. If the function returns true, it will be
* stored in cache. By default it caches everything except undefined.
*/
function caching(args: {
@drmikecrowe
drmikecrowe / index.d.ts
Created May 30, 2020
index.d.ts for cache-manager
View index.d.ts
/**
* Instead of manually managing the cache like this:
* function getCachedUserManually(id, cb) {
* memoryCache.get(id, function(err, result) {
* if (err) { return cb(err); }
*
* if (result) {
* return cb(null, result);
* }
*
View machine.js
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@drmikecrowe
drmikecrowe / results.md
Last active Jan 20, 2020
Time various loop structures
View results.md

Base code/idea comes from this blog post

The output:

Testing vanilla:      4.1ms
Testing lodash:      25.4ms -- 518% slower
Testing es6-for-of:   7.3ms --  78% slower
Testing forEach:      6.1ms --  48% slower
Testing map:          9.2ms -- 125% slower
@drmikecrowe
drmikecrowe / README.md
Last active Jan 10, 2020
Using debug with xstate
View README.md

For anyone else coming across this and wondering how this works, here's a summary.

NOTE: I'm still learning xstate, so there may be better ways to do this!

Step #1, install debug

debug is a small debugging library that can be used in the console or browser. In the screenshot above, each of the different colors comes from a different debug instance. For example, I instatiate the following in my console application:

const dbgM: debug.Debugger = require('debug')("fsm:master");
@drmikecrowe
drmikecrowe / test1-mutation-observer.js
Last active Jan 6, 2020
Watch elements as page loads
View test1-mutation-observer.js
const targets = ["#top-header", "#main-header"];
const config = {
attributes: true,
attributeOldValue: true,
subtree: true,
childList: true,
};
function logAllEvents(target, myElement) {
@drmikecrowe
drmikecrowe / searchImdb.py
Created Jan 3, 2020
Rename ripped directory from ARM based on search
View searchImdb.py
#!/home/mcrowe/Programming/Personal/imdb/.direnv/python-3.7.5/bin/python3
import sys
import imdb
import click
from os import listdir, system, rename
from os.path import isfile, join, getctime, basename
from lxml.builder import E
View machine.js
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
You can’t perform that action at this time.