Skip to content

Instantly share code, notes, and snippets.

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 => {
function createLoader (keysTask) {
let keys = []
let nextTaskPromise
return {
load (key) {
const index = keys.push(key) - 1;
if (!nextTaskPromise) {
let resolver
@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)
},
@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 / 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 / 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 / what-is-raj.md
Created May 17, 2018 02:10
A description of Raj and comparison to Redux and Elm

What is Raj

Raj solves two problems: state management and running side-effects. These problems are the hardest problems in building client applications.

State management is two things: storing state and handling state transitions. Raj holds a single variable called state which stores the application state. Raj will update this variable as messages are dispatched. State transitions are synchronous and preferably written in an immutable manner as to be simple to test quickly.

@andrejewski
andrejewski / raj-faq.md
Last active June 18, 2018 20:15
Frequently asked Raj questions

How do Raj and Redux compare performance wise?

Raj is faster theoretically. In Redux you have N reducers that need to be iterated, in Raj you have a single update which can call sub graph updates (log N). Redux also can have multiple subscriptions whereas Raj doesn't have any. Looking at code size and the amount of work each project does and the list-vs-tree distribution of work, Raj should be better.

Also since (logic+view+data) assemble into Raj programs, it's easier to code-split and lazy-load. There's no definite story for how to do at in Redux. In Raj, you use raj-spa where a program can resolve from a promise.

Is there a way to apply Raj to a backend state machine where there is no view?

So view is mostly for apps with UIs. It is a side-effect that receives the whole state as opposed to plain effects which need portions of state explicitly passed. If you don't need a view, you can think of it like a "window" into the current state. For example, you can have something like tapProgram wh

@andrejewski
andrejewski / raj-emitter.js
Created July 15, 2018 19:12
A single value emitter
exports.createEmitter = function createEmitter ({ shouldPrime, initialValue }) {
let currentValue = initialValue
let listeners = []
return {
emit: value => () => {
currentValue = value
listeners.forEach(listener => listener(currentValue))
},
@andrejewski
andrejewski / reconciler.js
Created July 19, 2018 02:01
A data reconciler with subscriptions
function createReconciler ({
mergeEntity,
getEntityKey,
entityStore = {},
subscriptions = new Set(),
}) {
function notify (subscriptions) {
subscriptions.forEach(subscription => {
const entities = []