carmi
CARMI - Compiler for Automatic Reactive Modelling of Inference
Last updated 17 hours ago by wix-ci-publisher .
MIT · Original npm · Tarball · package.json
$ cnpm install carmi 
SYNC missed versions from official npm registry.

CARMI

Build Status

Compiler for Automatic Reactive Modelling of Inference

This is a POC of an entirely new approach to modelling inferred state, there are 4 classic methods of handling derivation of state

  1. The naive - compute from scratch every time the state is changed
  2. Handle cache invalidation manually with all the world of hurt that entails.
  3. Using Immutable data and caching computation based on the identity of the inputs
  4. Using Functional Reactive Programming to box fragments of your state with getters&setters, running derivations in a way that logs which fragments were read during the computation, and invalidate when one of their setters is invoked

This project is an attempt at a new approach, a DSL+Compiler which are fed two types of inputs:

  1. The derivation of state you need
  2. The paths in the model you want to write to

The compiler generates JS source code which handles all the reactive cache invalidation automatically

Because the compiler knows in advance all the stuff that can be read/written from/to the model, it can do all sort of cool stuff that is nearly impossible to do automatically using other approaches

  1. Track conditional consumption of parts of the model only if used with zero over head
  2. Hoisting shared sub expressions, so they are only calculated once
  3. Not track dependencies if there are no setters that can cause the expression to invalidate
  4. All computation is incremental
const { compile, root, arg0, setter, splice } = require("carmi");
const todosByIdx = root.keyBy("idx");
const anyTodoNotDone = todosByIdx.anyValues(todo => todo.get("done").not());
const todosDisplayByIdx = todosByIdx.mapValues(todo =>
  todo.get("task").plus(todo.get("done").ternary(" - done", " - not done"))
);
const todosDisplay = root.map(todo => todosDisplayByIdx.get(todo.get("idx")));
const model = {
  todosDisplay,
  anyTodoNotDone,
  setTodoDone: setter(arg0, "done"),
  spliceTodos: splice(),
};

const todosModel = eval(compile(model));
const todos = todosModel([
  { idx: "1", done: false, task: "write a blog post about carmi" },
  { idx: "2", done: true, task: "publish to npm" },
  { idx: "3", done: false, task: "write a demo for carmi" },
]);
console.log(todos.todosDisplay);
/*
[ 'write a demo for carmi - not done',
'write a blog post about carmi - not done',
'publish to npm - done' ]
*/
todos.setTodoDone(2, true); // only todosDisplayByIdx of the demo is recalculated
console.log(todos.todosDisplay);
/*
[ 'write a blog post about carmi - not done',
  'publish to npm - done',
  'write a demo for carmi - done' ]
*/
todos.spliceTodos(0, 3, todos.$model[2], todos.$model[1], todos.$model[0]); // todosDisplayByIdx is not called at all
console.log(todos.todosDisplay);
/*
[ 'write a demo for carmi - done',
  'publish to npm - done',
  'write a blog post about carmi - not done' ]
*/

Usage with Babel Macros

Using a magic comment

// @carmi

require('carmi/macro') // Activate the macro!

const { root } = require('carmi')
module.exports = { first: root.get(0) }

Using string literals

const carmi = require('carmi/macro')

const modelBuilder = carmi`
  const { root } = require('carmi')
  module.exports = { first: root.get(0) }
`

const model = modelBuilder(["first", "second"])
console.log(model.first) // prints "first"!

Usage with Babel (as a plugin)

Compiles Carmi files (xxx.carmi.js) automatically using Babel. It reserves the external modules, required by require, in order to help bundlers (like webpack) understand dependencies across Carmi models (and to help them watch the files).

// @carmi

const { root } = require("carmi");
const { second } = require("./anotherModel.carmi.js");

module.exports = { first: root.get(0), second };

turns to:

require("carmi");
require("./anotherModel.camri.js");

module.exports = CARMI_COMPILATION_RESULT;

Add to your babel configuration

Add this plugin to your .babelrc:

{
  ... babel conf ...,
  "plugins": ["carmi/babel"]
}

Now you're set! :moneybag:

Usage with Webpack

Compiles Carmi files on the fly with a fly webpack loader.

Require using webpack configurations

Add this to your webpack configurations:

module: {
  rules: [
    {
      test: /\.carmi\.js$/,
      exclude: /(node_modules|bower_components)/,
      use: {
        loader: 'carmi/loader',
        options: {
          // Carmi options...
        }
      }
    }
  ]
}

Available carmi options:

debug: Add debug function to the generated code.
type-check: Add static type checking to the generated code. no-cache: Cache will be ignored. (It could noticeably affect the build time) cache-scenario (mtime (default) | git-hash): Specify which cache scenario you want to use. Each of scenario is collection all dependencies of the entry file and checking if at least one of it was updated. Then cache will be ignored and carmi will compile file from the scratch. The difference is how carmi checks which dependency was updated. mtime option will check last modified date of the file. But it won't work for cases when you are going to git clone project before each build since git doesn't contain a created/modified file metadata. For this cases git-hash option could be useful. It collects state based on git hash of each dependency and invalidate file if some of hash was updated (usually it happens after each git tree modification.) (Will be ignored if no-cache option is enabled) ast: Add AST to the output for debug purposes.

Then you can just require('./model.carmi.js') like a boss

Require with explicit loader

const modelBuilder = require("carmi/loader!./model.carmi.js");
// modelBuilder is the carmi builder function!

Current Tags

384 Versions

  • 1.0.242                                ...           17 hours ago
  • 1.0.241                                ...           2 days ago
  • 1.0.240                                ...           4 days ago
  • 1.0.239                                ...           5 days ago
  • 1.0.238                                ...           6 days ago
  • 1.0.237                                ...           7 days ago
  • 1.0.236                                ...           8 days ago
  • 1.0.235                                ...           9 days ago
  • 1.0.234                                ...           10 days ago
  • 1.0.233                                ...           11 days ago
  • 1.0.232                                ...           12 days ago
  • 0.0.0-f35f6f45dc879a289ee7071c6e8318c1                                ...           a month ago
  • 0.0.0-7cefc4d3c321ead580529c321fec88ac                                ...           a month ago
  • 0.0.0-8d2f8892cac4358e1a1adfd39acef212                                ...           a month ago
  • 0.0.0-a39ccc34451bb30c7af50f188a329739                                ...           a month ago
  • 1.0.231                                ...           a month ago
  • 0.0.0-49e995acc0fabe4858ed7990b5bf7ed0                                ...           2 months ago
  • 0.0.0-98abb291c1171a2782b16aa0408bf93f                                ...           2 months ago
  • 0.0.0-8ae39c08383867b78f396438a9f33514                                ...           2 months ago
  • 1.0.230                                ...           2 months ago
  • 0.0.0-1a69c9bd3cf4617af027bbce95b04a65                                ...           2 months ago
  • 0.0.0-c3d5cc936f1e17f04c800f79384ea18f                                ...           2 months ago
  • 1.0.229                                ...           2 months ago
  • 1.0.228                                ...           2 months ago
  • 0.0.0-c6edffa9721fd2dd3429e5e1d9541d8b                                ...           2 months ago
  • 0.0.0-c609126ab1fa20bb59e08f2b484e5609                                ...           2 months ago
  • 0.0.0-c3f303a3c35d5084f944a2a1e0c0d3f9                                ...           3 months ago
  • 0.0.0-1eec4eeca5a2ec57feb64e0aa092ef2c                                ...           3 months ago
  • 0.0.0-2a435e64f5f21db5fd7c358ec2c5e537                                ...           3 months ago
  • 1.0.227                                ...           3 months ago
  • 0.0.0-60bea30b85a5e16d5882bf730bf41c90                                ...           3 months ago
  • 1.0.226                                ...           3 months ago
  • 0.0.0-5eeb85cb679d8e304cf18828cc126672                                ...           3 months ago
  • 0.0.0-00f9a04a70dc158ccb7bb349348e6e72                                ...           3 months ago
  • 0.0.0-5da6363fff5d807827b26cc024d369be                                ...           3 months ago
  • 1.0.225                                ...           3 months ago
  • 0.0.0-e0cb18929a2ae5aaeee5b0ba74ec6d7a                                ...           3 months ago
  • 1.0.224                                ...           3 months ago
  • 1.0.223                                ...           3 months ago
  • 0.0.0-b4577aad1524bf77986c8ee16d5fdbdb                                ...           4 months ago
  • 1.0.222                                ...           4 months ago
  • 1.0.221                                ...           4 months ago
  • 1.0.220                                ...           5 months ago
  • 1.0.219                                ...           5 months ago
  • 1.0.218                                ...           6 months ago
  • 1.0.217                                ...           6 months ago
  • 1.0.216                                ...           6 months ago
  • 1.0.215                                ...           6 months ago
  • 1.0.214                                ...           6 months ago
  • 1.0.213                                ...           6 months ago
  • 1.0.212                                ...           6 months ago
  • 1.0.211                                ...           7 months ago
  • 1.0.210                                ...           7 months ago
  • 1.0.209                                ...           7 months ago
  • 1.0.208                                ...           7 months ago
  • 1.0.207                                ...           7 months ago
  • 1.0.206                                ...           7 months ago
  • 1.0.205                                ...           7 months ago
  • 1.0.204                                ...           8 months ago
  • 1.0.203                                ...           8 months ago
  • 1.0.202                                ...           8 months ago
  • 1.0.201                                ...           8 months ago
  • 1.0.200                                ...           8 months ago
  • 1.0.199                                ...           8 months ago
  • 1.0.198                                ...           8 months ago
  • 1.0.197                                ...           8 months ago
  • 1.0.196                                ...           8 months ago
  • 1.0.195                                ...           8 months ago
  • 1.0.194                                ...           8 months ago
  • 1.0.193                                ...           8 months ago
  • 1.0.192                                ...           8 months ago
  • 1.0.191                                ...           8 months ago
  • 1.0.190                                ...           8 months ago
  • 1.0.189                                ...           8 months ago
  • 1.0.188                                ...           8 months ago
  • 1.0.187                                ...           8 months ago
  • 1.0.186                                ...           8 months ago
  • 1.0.185                                ...           8 months ago
  • 1.0.184                                ...           9 months ago
  • 1.0.183                                ...           9 months ago
  • 1.0.182                                ...           9 months ago
  • 1.0.181                                ...           9 months ago
  • 1.0.180                                ...           9 months ago
  • 1.0.179                                ...           9 months ago
  • 1.0.178                                ...           9 months ago
  • 1.0.177                                ...           9 months ago
  • 1.0.176                                ...           9 months ago
  • 1.0.175                                ...           9 months ago
  • 1.0.174                                ...           9 months ago
  • 1.0.173                                ...           9 months ago
  • 1.0.172                                ...           9 months ago
  • 1.0.171                                ...           9 months ago
  • 1.0.170                                ...           9 months ago
  • 1.0.169                                ...           9 months ago
  • 1.0.168                                ...           9 months ago
  • 1.0.167                                ...           9 months ago
  • 1.0.166                                ...           9 months ago
  • 1.0.165                                ...           9 months ago
  • 1.0.164                                ...           9 months ago
  • 1.0.163                                ...           9 months ago
  • 1.0.162                                ...           9 months ago
  • 1.0.161                                ...           9 months ago
  • 1.0.160                                ...           9 months ago
  • 1.0.159                                ...           9 months ago
  • 1.0.158                                ...           9 months ago
  • 1.0.157                                ...           9 months ago
  • 1.0.156                                ...           9 months ago
  • 1.0.155                                ...           9 months ago
  • 1.0.154                                ...           9 months ago
  • 1.0.153                                ...           9 months ago
  • 1.0.152                                ...           9 months ago
  • 1.0.151                                ...           9 months ago
  • 1.0.150                                ...           9 months ago
  • 1.0.149                                ...           9 months ago
  • 1.0.148                                ...           9 months ago
  • 1.0.147                                ...           9 months ago
  • 1.0.146                                ...           9 months ago
  • 1.0.145                                ...           9 months ago
  • 1.0.144                                ...           9 months ago
  • 1.0.143                                ...           10 months ago
  • 1.0.142                                ...           10 months ago
  • 1.0.141                                ...           10 months ago
  • 1.0.140                                ...           10 months ago
  • 1.0.139                                ...           10 months ago
  • 1.0.138                                ...           10 months ago
  • 1.0.137                                ...           10 months ago
  • 1.0.136                                ...           10 months ago
  • 1.0.135                                ...           10 months ago
  • 1.0.134                                ...           10 months ago
  • 1.0.133                                ...           10 months ago
  • 1.0.132                                ...           10 months ago
  • 1.0.131                                ...           10 months ago
  • 1.0.130                                ...           10 months ago
  • 1.0.129                                ...           10 months ago
  • 1.0.128                                ...           10 months ago
  • 1.0.127                                ...           10 months ago
  • 1.0.126                                ...           10 months ago
  • 1.0.125                                ...           10 months ago
  • 1.0.124                                ...           10 months ago
  • 1.0.123                                ...           10 months ago
  • 1.0.122                                ...           10 months ago
  • 1.0.121                                ...           10 months ago
  • 1.0.120                                ...           10 months ago
  • 1.0.119                                ...           10 months ago
  • 1.0.118                                ...           10 months ago
  • 1.0.117                                ...           10 months ago
  • 1.0.116                                ...           10 months ago
  • 1.0.115                                ...           10 months ago
  • 1.0.114                                ...           10 months ago
  • 1.0.113                                ...           10 months ago
  • 1.0.112                                ...           10 months ago
  • 1.0.111                                ...           10 months ago
  • 1.0.110                                ...           10 months ago
  • 1.0.109                                ...           10 months ago
  • 1.0.108                                ...           10 months ago
  • 1.0.107                                ...           10 months ago
  • 1.0.106                                ...           10 months ago
  • 1.0.105                                ...           10 months ago
  • 1.0.104                                ...           10 months ago
  • 1.0.103                                ...           10 months ago
  • 1.0.102                                ...           10 months ago
  • 1.0.101                                ...           10 months ago
  • 1.0.100                                ...           10 months ago
  • 1.0.99                                ...           10 months ago
  • 1.0.98                                ...           10 months ago
  • 1.0.97                                ...           10 months ago
  • 1.0.96                                ...           10 months ago
  • 1.0.95                                ...           10 months ago
  • 1.0.94                                ...           10 months ago
  • 1.0.93                                ...           10 months ago
  • 1.0.92                                ...           10 months ago
  • 1.0.91                                ...           10 months ago
  • 1.0.90                                ...           10 months ago
  • 1.0.89                                ...           10 months ago
  • 1.0.88                                ...           10 months ago
  • 1.0.87                                ...           10 months ago
  • 1.0.86                                ...           10 months ago
  • 1.0.85                                ...           10 months ago
  • 1.0.84                                ...           10 months ago
  • 1.0.83                                ...           10 months ago
  • 1.0.82                                ...           10 months ago
  • 1.0.81                                ...           10 months ago
  • 1.0.80                                ...           10 months ago
  • 1.0.79                                ...           10 months ago
  • 1.0.78                                ...           10 months ago
  • 1.0.77                                ...           10 months ago
  • 1.0.76                                ...           10 months ago
  • 1.0.75                                ...           a year ago
  • 1.0.74                                ...           a year ago
  • 1.0.73                                ...           a year ago
  • 1.0.72                                ...           a year ago
  • 1.0.71                                ...           a year ago
  • 1.0.70                                ...           a year ago
  • 1.0.69                                ...           a year ago
  • 1.0.68                                ...           a year ago
  • 1.0.67                                ...           a year ago
  • 1.0.66                                ...           a year ago
  • 1.0.65                                ...           a year ago
  • 1.0.64                                ...           a year ago
  • 1.0.63                                ...           a year ago
  • 1.0.62                                ...           a year ago
  • 1.0.61                                ...           a year ago
  • 1.0.60                                ...           a year ago
  • 1.0.59                                ...           a year ago
  • 1.0.58                                ...           a year ago
  • 1.0.57                                ...           a year ago
  • 1.0.56                                ...           a year ago
  • 1.0.55                                ...           a year ago
  • 1.0.54                                ...           a year ago
  • 1.0.53                                ...           a year ago
  • 1.0.52                                ...           a year ago
  • 1.0.51                                ...           a year ago
  • 1.0.50                                ...           a year ago
  • 1.0.49                                ...           a year ago
  • 1.0.48                                ...           a year ago
  • 1.0.47                                ...           a year ago
  • 1.0.46                                ...           a year ago
  • 1.0.45                                ...           a year ago
  • 1.0.44                                ...           a year ago
  • 1.0.43                                ...           a year ago
  • 1.0.42                                ...           a year ago
  • 1.0.41                                ...           a year ago
  • 1.0.40                                ...           a year ago
  • 1.0.39                                ...           a year ago
  • 1.0.38                                ...           a year ago
  • 1.0.37                                ...           a year ago
  • 1.0.36                                ...           a year ago
  • 1.0.35                                ...           a year ago
  • 1.0.34                                ...           a year ago
  • 1.0.33                                ...           a year ago
  • 1.0.32                                ...           a year ago
  • 1.0.31                                ...           a year ago
  • 1.0.30                                ...           a year ago
  • 1.0.29                                ...           a year ago
  • 1.0.28                                ...           a year ago
  • 1.0.27                                ...           a year ago
  • 1.0.26                                ...           a year ago
  • 1.0.25                                ...           a year ago
  • 1.0.24                                ...           a year ago
  • 1.0.23                                ...           a year ago
  • 1.0.22                                ...           a year ago
  • 1.0.21                                ...           a year ago
  • 1.0.20                                ...           a year ago
  • 1.0.19                                ...           a year ago
  • 1.0.18                                ...           a year ago
  • 1.0.17                                ...           a year ago
  • 1.0.16                                ...           a year ago
  • 1.0.15                                ...           a year ago
  • 1.0.14                                ...           a year ago
  • 1.0.13                                ...           a year ago
  • 1.0.12                                ...           a year ago
  • 1.0.11                                ...           a year ago
  • 1.0.10                                ...           a year ago
  • 1.0.9                                ...           a year ago
  • 1.0.8                                ...           a year ago
  • 1.0.7                                ...           a year ago
  • 1.0.6                                ...           a year ago
  • 1.0.5                                ...           a year ago
  • 1.0.4                                ...           a year ago
  • 1.0.3                                ...           a year ago
  • 1.0.2                                ...           a year ago
  • 1.0.1                                ...           a year ago
  • 1.0.0                                ...           a year ago
  • 0.2.17                                ...           a year ago
  • 0.2.16                                ...           a year ago
  • 0.2.15                                ...           a year ago
  • 0.2.14                                ...           a year ago
  • 0.2.13                                ...           a year ago
  • 0.2.12                                ...           a year ago
  • 0.2.11                                ...           a year ago
  • 0.2.10                                ...           a year ago
  • 0.2.9                                ...           a year ago
  • 0.2.8                                ...           a year ago
  • 0.2.7                                ...           a year ago
  • 0.2.6                                ...           a year ago
  • 0.2.5                                ...           a year ago
  • 0.2.4                                ...           a year ago
  • 0.2.3                                ...           a year ago
  • 0.2.2                                ...           a year ago
  • 0.2.1                                ...           a year ago
  • 0.2.0                                ...           a year ago
  • 0.1.68                                ...           a year ago
  • 0.1.67                                ...           a year ago
  • 0.1.66                                ...           a year ago
  • 0.1.65                                ...           a year ago
  • 0.1.64                                ...           a year ago
  • 0.1.63                                ...           a year ago
  • 0.1.62                                ...           a year ago
  • 0.1.61                                ...           a year ago
  • 0.1.60                                ...           a year ago
  • 0.1.59                                ...           a year ago
  • 0.1.58                                ...           a year ago
  • 0.1.57                                ...           a year ago
  • 0.1.56                                ...           a year ago
  • 0.1.55                                ...           a year ago
  • 0.1.54                                ...           a year ago
  • 0.1.53                                ...           a year ago
  • 0.1.52                                ...           a year ago
  • 0.1.51                                ...           a year ago
  • 0.1.50                                ...           a year ago
  • 0.1.49                                ...           a year ago
  • 0.1.48                                ...           a year ago
  • 0.1.47                                ...           a year ago
  • 0.1.46                                ...           a year ago
  • 0.1.45                                ...           a year ago
  • 0.1.44                                ...           a year ago
  • 0.1.43                                ...           a year ago
  • 0.1.42                                ...           a year ago
  • 0.1.41                                ...           a year ago
  • 0.1.40                                ...           a year ago
  • 0.1.39                                ...           a year ago
  • 0.1.38                                ...           a year ago
  • 0.1.37                                ...           a year ago
  • 0.1.36                                ...           a year ago
  • 0.1.35                                ...           a year ago
  • 0.1.34                                ...           a year ago
  • 0.1.33                                ...           a year ago
  • 0.1.32                                ...           a year ago
  • 0.1.31                                ...           a year ago
  • 0.1.30                                ...           a year ago
  • 0.1.29                                ...           a year ago
  • 0.1.28                                ...           a year ago
  • 0.1.27                                ...           a year ago
  • 0.1.26                                ...           a year ago
  • 0.1.25                                ...           a year ago
  • 0.1.24                                ...           a year ago
  • 0.1.23                                ...           a year ago
  • 0.1.22                                ...           a year ago
  • 0.1.21                                ...           a year ago
  • 0.1.20                                ...           a year ago
  • 0.1.19                                ...           a year ago
  • 0.1.18                                ...           a year ago
  • 0.1.17                                ...           a year ago
  • 0.1.16                                ...           a year ago
  • 0.1.15                                ...           a year ago
  • 0.1.14                                ...           a year ago
  • 0.1.13                                ...           a year ago
  • 0.1.12                                ...           a year ago
  • 0.1.11                                ...           a year ago
  • 0.1.10                                ...           a year ago
  • 0.1.9                                ...           a year ago
  • 0.1.8                                ...           a year ago
  • 0.1.7                                ...           a year ago
  • 0.1.6                                ...           a year ago
  • 0.1.5                                ...           a year ago
  • 0.1.4                                ...           a year ago
  • 0.1.3                                ...           a year ago
  • 0.1.2                                ...           a year ago
  • 0.1.1                                ...           a year ago
  • 0.1.0                                ...           a year ago
  • 0.0.34                                ...           a year ago
  • 0.0.33                                ...           a year ago
  • 0.0.32                                ...           a year ago
  • 0.0.31                                ...           a year ago
  • 0.0.30                                ...           a year ago
  • 0.0.29                                ...           a year ago
  • 0.0.28                                ...           a year ago
  • 0.0.27                                ...           a year ago
  • 0.0.26                                ...           a year ago
  • 0.0.25                                ...           a year ago
  • 0.0.24                                ...           a year ago
  • 0.0.23                                ...           a year ago
  • 0.0.22                                ...           a year ago
  • 0.0.21                                ...           a year ago
  • 0.0.20                                ...           a year ago
  • 0.0.19                                ...           a year ago
  • 0.0.18                                ...           a year ago
  • 0.0.17                                ...           2 years ago
  • 0.0.16                                ...           2 years ago
  • 0.0.15                                ...           2 years ago
  • 0.0.14                                ...           2 years ago
  • 0.0.13                                ...           2 years ago
  • 0.0.12                                ...           2 years ago
  • 0.0.11                                ...           2 years ago
  • 0.0.10                                ...           2 years ago
  • 0.0.9                                ...           2 years ago
  • 0.0.8                                ...           2 years ago
  • 0.0.7                                ...           2 years ago
  • 0.0.6                                ...           2 years ago
  • 0.0.5                                ...           2 years ago
  • 0.0.4                                ...           2 years ago
  • 0.0.3                                ...           2 years ago
  • 0.0.2                                ...           2 years ago
  • 0.0.1                                ...           2 years ago
Downloads
Today 41
This Week 713
This Month 3,399
Last Day 276
Last Week 1,907
Last Month 2,078
Dependencies (29)
Dev Dependencies (10)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |