Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
maierson authored and maierson committed Jun 24, 2017
1 parent a5e9e04 commit b5ccef3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 32 deletions.
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

[![Npm Status](https://badge.fury.io/js/one.svg)](https://npmjs.com/package/one-typescript) [![Build Status](https://travis-ci.org/maierson/one.svg)](https://travis-ci.org/maierson/one) [![Coverage Status](https://coveralls.io/repos/github/maierson/one/badge.svg?branch=master)](https://coveralls.io/github/maierson/one?branch=master)

Small ~5kb gzipped.

Each entity tracked for uniqueness must have a unique id. There is precisely ONE distinct entity in the cache
for each unique id. Entities that do not have a unique id are still cached but not tracked for uniqueness.

Expand All @@ -24,23 +26,23 @@ for each unique id. Entities that do not have a unique id are still cached but n
3. Remove the need for ```babel-polyfill```
4. __Breaking api changes:__ Simplified minimal api. You really only need the commands in the table below. There are a couple of other options mostly for debugging (see the Api section for the development api list).

| Command | Action |
| ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| __getCache__ | Create or access a specific version of the cache. There can be multiple concurrent versions of the cache in case distinct versions of the same entity are needed (for example if display and edit data need to be different until a user commits changes). ```One.getCache('edit') ``` would create a cache dedicated to edit operations. |
| __put__ | Add an entity to the cache and make it immutable. |
| __get__ | Retrieve an entity from the cache. This is a fast hash map locator. |
| __getEdit__ | Get a shallow editable version of the entity from the cache. Inner nested entities are still immutable. This is in order to make efficient use of trie structures. |
| __evict__ | Remove an entity from the cache. Evicts it from all parents as well. |
| Command | Action |
| ------------ | -----------------------|
| __getCache__ | Create or access a specific version of the cache. There can be multiple concurrent versions of the cache in case distinct versions of the same entity are needed (for example if display and edit data need to be different until a user commits changes). `One.getCache('edit')` would create a cache dedicated to edit operations. You don't need this if you only use the default version of the cache `One.put(item)` |
| __put__ | Add an entity to the cache and make it immutable.|
| __get__ | Retrieve an entity from the cache. This is a fast hash map locator. |
| __getEdit__ | Get a shallow editable version of the entity from the cache. Inner nested entities are still immutable. This is in order to make efficient use of trie structures. |
| __evict__ | Remove an entity from the cache. Evicts it from all parents as well.|


### __Api__
In addition to the 5 production api commands there are 4 options intended for development:

| Command | Action |
| ---------- | :--------------------------------------------------------------------------------------------------------------------------------------------- |
| __reset__ | Resets the cache to empty. Useful for testing. |
| __length__ | Number of nodes in the current cache. Each node contains one atomic change to the cache so moving between nodes gives you time travelling. |
| __size__ | Number of entities cached on the current node (the size of the node). |
| Command | Action |
| ---------- | :--------------------- |
| __reset__ | Resets the cache to empty. Useful for testing.|
| __length__ | Number of nodes in the current cache. Each node contains one atomic change to the cache so moving between nodes gives you time travelling.|
| __size__ | Number of entities cached on the current node (the size of the |
| __print__ | Provides a printable representation of the entire cache that can be passed on to a logger. Slow. For debugging only. Do not use in production. |


Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "one",
"version": "6.0.0",
"version": "6.0.1",
"description": "Browser application cache. It guarantees entity uniqueness across the entire cache.",
"main": "lib/index.js",
"typings": "lib/index",
Expand Down Expand Up @@ -76,4 +76,4 @@
"webpack-dev-middleware": "^1.8.2",
"webpack-dev-server": "^1.16.1"
}
}
}
9 changes: 3 additions & 6 deletions src/CacheMap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* Hash map of a collection of paths.
*
Expand All @@ -11,7 +10,7 @@ export default class CacheMap<T> {
length = 0

set(key: string | number, value: T): boolean {
if (typeof this.paths[key] === "undefined") {
if (typeof this.paths[key] === 'undefined') {
this.length++
this.paths[key] = value
return true
Expand All @@ -20,9 +19,7 @@ export default class CacheMap<T> {
return false
}

get = (key): T => {
return this.paths[key]
}
get = (key): T => this.paths[key]

delete = (key): boolean => {
if (typeof this.paths[key] !== 'undefined' && this.length > 0) {
Expand All @@ -45,7 +42,7 @@ export default class CacheMap<T> {

clone = (): CacheMap<T> => {
let clone: CacheMap<T> = new CacheMap<T>()
clone.paths = { ...this.paths, }
clone.paths = { ...this.paths }
clone.length = this.length
return clone
}
Expand Down
21 changes: 9 additions & 12 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const toString: any = Object.prototype.toString
var _hasOwnProperty = Object.prototype.hasOwnProperty

export function isNumber(value) {
return typeof value === 'number' || toString(value) === "[object Number]"
return typeof value === 'number' || toString(value) === '[object Number]'
}

export function isString(obj) {
return typeof obj === 'string' || toString(obj) === "[object String]"
return typeof obj === 'string' || toString(obj) === '[object String]'
}

/**
Expand All @@ -38,7 +38,6 @@ export function isFunction(item) {
* checks if argument is an array
*/
export function isArray(value) {

if (!value || value === null) {
return false
}
Expand Down Expand Up @@ -107,7 +106,7 @@ export function hasUid(obj) {
if (!isObject(obj)) {
return false
}
if (typeof obj[config.uidName] === "undefined") {
if (typeof obj[config.uidName] === 'undefined') {
return false
}
let uid = obj[config.uidName]
Expand Down Expand Up @@ -201,21 +200,19 @@ export function deepClone(obj, uidReference?, freeze = true) {
}
} else {
// do nothing here - keep the uid reference - not editable
//result[propName] = deepClone(value)
// result[propName] = deepClone(value)
}
} else {
result[propName] = deepClone(value, uidReference, freeze)
}
}
else if (isFunction(value)) {
} else if (isFunction(value)) {
// object is already constructed - no need to clone the constructor
// also cloning fails with 'unexpected token this' error for objects
// constructed from classes inheriting from other classes
if (propName !== 'constructor') {
result[propName] = value.clone(result)
}
}
else {
} else {
// primitives
result[propName] = value
}
Expand Down Expand Up @@ -255,6 +252,6 @@ export const cacheSize = (instance: ICacheInstance): number => {
return cacheNode ? cacheNode.items.size() : 0
}

export const cacheLength = (instance: ICacheInstance): number => {
return instance.thread.nodes.length
}
export const cacheLength = (instance: ICacheInstance): number => (
instance.thread.nodes.length
)

0 comments on commit b5ccef3

Please sign in to comment.