Make cleanup and add some docs.

This commit is contained in:
Irakli Gozalishvili 2012-10-22 00:08:39 -07:00
Родитель c9ebc235cd
Коммит 34a5452a62
3 изменённых файлов: 91 добавлений и 41 удалений

Просмотреть файл

@ -1,47 +1,58 @@
var method = require("method")
/* vim:set ts=2 sw=2 sts=2 expandtab */
/*jshint asi: true undef: true es5: true node: true browser: true devel: true
forin: true latedef: false globalstrict: true*/
"use strict";
var diff = require("./state/diff")
var patch = require("./state/patch")
var extend = require("xtend")
var diff = method()
var patch = method()
state.diff = diff
state.patch = patch
var make = Object.create || (function() {
function Type() {}
return function make(prototype) {
Type.prototype = prototype
return new Type()
}
})()
// Generated unique name is used to store `delta` on the state object
// which is object containing changes from previous state to current.
var delta = "delta@" + module.id
// State is a type used for representing application states. Primarily
// reason to have a type is to have an ability implement polymorphic
// methods for it.
function State() {}
// Returns diff that has being applied to a previous state to get to a
// current one.
diff.define(State, function diff(state) {
return state[delta]
})
// Patches given `state` with a given `diff` creating a new state that is
// returned back.
patch.define(State, function patch(state, diff) {
var value = new State()
// Store `diff` is stored so that it can be retrieved without calculations.
value[delta] = diff
// Make a shallow copy into object that is type of `State` and contains
// reference to an applied diff in it's prototype. That way `diff` is
// stored but remains invisible for `Object.keys` and `JSON.stringify`.
return extend(make(value), state, diff)
})
function state() {
/**
Creates an object representing a state snapshot.
**/
var value = new State()
// this function always creates initila
value[delta] = value
return value
}
state.type = State
module.exports = state
function state() { return new State() }
function State() {
}
var diffName = "diff@" + module.id
State.prototype[diffName] = null
diff.define(State, function (state) {
return state[diffName]
})
/*
patch(state, {
itemId: {
completed: false
}
, otherItemId: null
})
patch(state, {
id: otherItemId
, __deleted__: true
})
*/
patch.define(State, function (oldState, changes) {
var base = new State()
base[diffName] = changes
return extend(Object.create(base), oldState, changes)
})

18
state/diff.js Normal file
Просмотреть файл

@ -0,0 +1,18 @@
/* vim:set ts=2 sw=2 sts=2 expandtab */
/*jshint asi: true undef: true es5: true node: true browser: true devel: true
forin: true latedef: false globalstrict: true*/
"use strict";
var method = require("method")
// Method is designed to work with data structures representing application
// state. Calling it with a state should return object representing `delta`
// that has being applied to a previous state to get to a current state.
//
// Example
//
// diff(state) // => { "item-id-1": { title: "some title" } "item-id-2": null }
var diff = method()
module.exports = diff

21
state/patch.js Normal file
Просмотреть файл

@ -0,0 +1,21 @@
/* vim:set ts=2 sw=2 sts=2 expandtab */
/*jshint asi: true undef: true es5: true node: true browser: true devel: true
forin: true latedef: false globalstrict: true*/
"use strict";
var method = require("method")
// Method is designed to work with data structures representing application
// state. Calling it with a state and delta should return object representing
// new state, with changes in `delta` being applied to previous.
//
// ## Example
//
// patch(state, {
// "item-id-1": { completed: false }, // update
// "item-id-2": null // delete
// })
var patch = method()
module.exports = patch