forked from steemit/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemoize.js
More file actions
170 lines (138 loc) · 3.35 KB
/
memoize.js
File metadata and controls
170 lines (138 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import Map from 'es6-map'
import IS_DEV from '../constants/is-dev'
/**
* This module serves to memoize methods on immutable instances.
*/
// Global: True if memoization should is enabled. Only effective in DEV mode
let ENABLED = true
// Global: Changing this cache key will clear all previous cached
// results. Only effective in DEV mode
let CACHE_KEY = 0
/**
* The leaf node of a cache tree. Used to support variable argument length.
*
* A unique object, so that native Maps will key it by reference.
*
* @type {Object}
*/
const LEAF = {}
/**
* A value to represent a memoized undefined value. Allows efficient
* value retrieval using Map.get only.
*
* @type {Object}
*/
const UNDEFINED = {}
/**
* Default value for unset keys in native Maps
*/
const UNSET = undefined
/**
* Memoize all of the `properties` on a `object`.
*
* @param {Object} object
* @param {Array} properties
* @return {Record}
*/
function memoize(object, properties) {
for (const property of properties) {
const original = object[property]
if (!original) {
throw new Error(`Object does not have a property named "${property}".`)
}
object[property] = function (...args) {
if (IS_DEV) {
if (!ENABLED) {
// Memoization disabled
return original.apply(this, args)
} else if (CACHE_KEY !== this.__cache_key) {
// Previous caches must be cleared
this.__cache_key = CACHE_KEY
this.__cache = new Map()
}
}
const keys = [property, ...args]
this.__cache = this.__cache || new Map()
const cachedValue = getIn(this.__cache, keys)
if (cachedValue !== UNSET) {
return cachedValue === UNDEFINED ? undefined : cachedValue
}
const value = original.apply(this, args)
this.__cache = setIn(this.__cache, keys, value)
return value
}
}
}
/**
* Set a value at a key path in a tree of Map, creating Maps on the go.
*
* @param{Map} map
* @param{Array} keys
* @param{Any} value
* @return {Map}
*/
function setIn(map, keys, value) {
value = value === undefined ? UNDEFINED : value
let parentMap = map
let childMap
for (const key of keys) {
childMap = parentMap.get(key)
if (childMap === UNSET) {
// This path was not created yet
childMap = new Map()
parentMap.set(key, childMap)
}
parentMap = childMap
}
// The whole map path was created
// Set the value to the bottom most map
childMap.set(LEAF, value)
return map
}
/**
* Get a value at a key path in a tree of Map.
* If not set, returns UNSET.
* If the set value is undefined, returns UNDEFINED
*
* @param{Map} map
* @param{Array} keys
* @return {Any | UNSET | UNDEFINED}
*/
function getIn(map, keys) {
let childMap
for (const key of keys) {
childMap = map.get(key)
if (childMap === UNSET) {
// Not found
return UNSET
}
map = childMap
}
return childMap.get(LEAF)
}
/**
* In DEV mode, clears the previously memoized values, globally.
* @return {Void}
*/
function __clear() {
CACHE_KEY++
if (CACHE_KEY >= Number.MAX_SAFE_INTEGER) {
CACHE_KEY = 0
}
}
/**
* In DEV mode, enable or disable the use of memoize values, globally.
* @param {Boolean} enabled
* @return {Void}
*/
function __enable(enabled) {
ENABLED = enabled
}
/**
* Export.
*/
export {
memoize as default,
__clear,
__enable
}