Skip to content

eimfach/frozen-core

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

frozen-core

Coverage Status Code Climate

Experimental lib to create immutable objects and structures in a very opinionated way.

  • Very small (4KB, raw)
  • Object properties are immutable
  • Optional state properties
  • Simple Typesafe inheritance
  • Hierarchic method bubbling
  • Enforces a specifc way to implement your Objects and Methods
var frozenCore = require('frozen-core');
var myObject = frozenCore.extend({
  state: {
    //mutable property values (the object mask)`
  },
  core: {
    // immutable property values here
    //only functions allowed (other types are ommited)
    // 'shares' the state object scope by obtaining a immutable copy (the snapshot) (lexical this refers to that snapshot)
    /* Every method here only can access that copy but no other method within 
    the object except the inherited public api methods (extend, bubble) */
  }
})
// the properties of the resulting object are always immutable, not configurable as the object itself too, one can't add new properties, remove or configure them

API

myObject.extend(/*options object*/);
myObject.bubble(/*method to call (which will be executed on every parent)*/)
myObject.getSnapshot() //returns a reference to the immutable state copy
myObject.parent // reference to the latest origin

Simple implementation example

var cube = frozenCore.extend({
  core: {
    mutate: function(){
      //'this' is safe to use here since it will be immutable - it will refer to a copy of the state object
      /* this will have no effect except throwing an error in strict mode !*/this.width = 1;
      return this.extend({
        state: {
          width: this.width+20,
          height: this.height+20,
          depth: this.depth+20
        }
      });
    }
  },
  state: {
    width: 0,
    height: 0,
    depth: 0
  }
});

var mutatedCube = cube.mutate();

Releases

No releases published

Packages

No packages published