Skip to content

Instantly share code, notes, and snippets.

@andrejewski
andrejewski / rdb-bad.js
Created March 29, 2018 13:51
Scratch of how to solve the polymorphic association problem generically
/*
select table_name
from information_schema.tables
where table_type = 'BASE TABLE' and table_schema = 'public';
resources {
id serial primary key,
type text not null
}
@andrejewski
andrejewski / raj-subscription.js
Created March 24, 2018 21:22
Another stab at declarative subscriptions in Raj
/*
Another day spent on declarative subscriptions.
This implementation works like this:
subscription({
program: myProgram,
subscription: {
foo: sub1,
bar: sub2
},
@andrejewski
andrejewski / can--raj-style.js
Last active March 9, 2018 15:59
Sample of raj-can-map
import { program } from 'https://gist.github.com/andrejewski/5455ccdef77122389b1ac8a7b5ff8415'
// Functional | Reductionist
program(DefineMap, () => ({
init: [{
name: '',
nameChangeCount: 0
}],
update: (msg, model) => [{
name: msg,
@andrejewski
andrejewski / can-defer.js
Last active March 14, 2018 20:27
Declaratively defer rendering for large components and lists (Can v2)
import Map from 'can/map/';
import Component from 'can/component/';
const viewModel = Map.extend('CanDeferVM', {
define: {
delayMs: {
value: 0,
set: x => parseInt(x, 10)
},
function createLoader (keysTask) {
let keys = []
let nextTaskPromise
return {
load (key) {
const index = keys.push(key) - 1;
if (!nextTaskPromise) {
let resolver
import React from 'react'
import react from 'raj-react'
import { union } from 'tagmeme'
import { mapEffect } from 'raj-compose'
export function loadModelsForMake (id) {
return function (dispatch) {
window.fetch(`/models?id=${id}`)
.then(res => res.json())
.then(res => {
@andrejewski
andrejewski / a-small-refactor.js
Created February 22, 2018 19:30
A small refactor
// A quick refactor
// 1
function foo (fieldValue) {
//If the fieldValue is a number, use ID
//Otherwise, use codeName
//Postgres gets grouchy about comparing strings to numbers
let fieldName;
try {
const parsed = parseInt(fieldValue);
@andrejewski
andrejewski / can-portal.js
Created February 21, 2018 15:20
Portals for Can.js (V2)
import Map from 'can/map/';
import Component from 'can/component/';
// Range methods from https://github.com/yapplabs/ember-wormhole/blob/f6bdea79ee1d9b3ef6b7f19e378e3bfb103b2e5e/addon/components/ember-wormhole.js
function appendRange (destinationElement, firstNode, lastNode) {
while(firstNode) {
destinationElement.insertBefore(firstNode, null);
firstNode = firstNode !== lastNode ? lastNode.parentNode.firstChild : null;
}
}
@andrejewski
andrejewski / a-refactoring.md
Created February 16, 2018 02:50
A code refactoring with commentary

A refactoring

This is a refactoring where an example task goes through rounds of iteration, improving the design with commentary.

Our goal is to create a service for sending email. We need a set of options that fit into two sets:

  • Service options, that will remain constant for the service's lifetime, like transport configuration.
  • Per-email options, that will be used to send a single email.

We separate the two for ergonomics.

@andrejewski
andrejewski / example.js
Last active January 12, 2018 21:20
A Can Map powered by a Raj runtime
import Component from 'can-component'
import {program} from './raj-can-map'
import Map from 'can-map' // or whatever Map-like thing
import stache from 'can-stache'
export default Component.extend({
tag: 'my-app',
template: stache(`
<p>{{count}}</p>
<button on:click="increment()">Increment</button>