Debian Codegraph @

memoizee/lib/configure-map.js

raw
1 /* eslint no-eq-null: 0, eqeqeq: 0, no-unused-vars: 0 */
2
3 "use strict";
4
5 var customError = require("es5-ext/error/custom")
6 , defineLength = require("es5-ext/function/_define-length")
7 , d = require("d")
8 , ee = require("event-emitter").methods
9 , resolveResolve = require("./resolve-resolve")
10 , resolveNormalize = require("./resolve-normalize");
11
12 var apply = Function.prototype.apply
13 , call = Function.prototype.call
14 , create = Object.create
15 , defineProperties = Object.defineProperties
16 , on = ee.on
17 , emit = ee.emit;
18
19 module.exports = function (original, length, options) {
20 var cache = create(null)
21 , conf
22 , memLength
23 , get
24 , set
25 , del
26 , clear
27 , extDel
28 , extGet
29 , extHas
30 , normalizer
31 , getListeners
32 , setListeners
33 , deleteListeners
34 , memoized
35 , resolve;
36 if (length !== false) memLength = length;
37 else if (isNaN(original.length)) memLength = 1;
38 else memLength = original.length;
39
40 if (options.normalizer) {
41 normalizer = resolveNormalize(options.normalizer);
42 get = normalizer.get;
43 set = normalizer.set;
44 del = normalizer.delete;
45 clear = normalizer.clear;
46 }
47 if (options.resolvers != null) resolve = resolveResolve(options.resolvers);
48
49 if (get) {
50 memoized = defineLength(function (arg) {
51 var id, result, args = arguments;
52 if (resolve) args = resolve(args);
53 id = get(args);
54 if (id !== null) {
55 if (hasOwnProperty.call(cache, id)) {
56 if (getListeners) conf.emit("get", id, args, this);
57 return cache[id];
58 }
59 }
60 if (args.length === 1) result = call.call(original, this, args[0]);
61 else result = apply.call(original, this, args);
62 if (id === null) {
63 id = get(args);
64 if (id !== null) throw customError("Circular invocation", "CIRCULAR_INVOCATION");
65 id = set(args);
66 } else if (hasOwnProperty.call(cache, id)) {
67 throw customError("Circular invocation", "CIRCULAR_INVOCATION");
68 }
69 cache[id] = result;
70 if (setListeners) conf.emit("set", id, null, result);
71 return result;
72 }, memLength);
73 } else if (length === 0) {
74 memoized = function () {
75 var result;
76 if (hasOwnProperty.call(cache, "data")) {
77 if (getListeners) conf.emit("get", "data", arguments, this);
78 return cache.data;
79 }
80 if (arguments.length) result = apply.call(original, this, arguments);
81 else result = call.call(original, this);
82 if (hasOwnProperty.call(cache, "data")) {
83 throw customError("Circular invocation", "CIRCULAR_INVOCATION");
84 }
85 cache.data = result;
86 if (setListeners) conf.emit("set", "data", null, result);
87 return result;
88 };
89 } else {
90 memoized = function (arg) {
91 var result, args = arguments, id;
92 if (resolve) args = resolve(arguments);
93 id = String(args[0]);
94 if (hasOwnProperty.call(cache, id)) {
95 if (getListeners) conf.emit("get", id, args, this);
96 return cache[id];
97 }
98 if (args.length === 1) result = call.call(original, this, args[0]);
99 else result = apply.call(original, this, args);
100 if (hasOwnProperty.call(cache, id)) {
101 throw customError("Circular invocation", "CIRCULAR_INVOCATION");
102 }
103 cache[id] = result;
104 if (setListeners) conf.emit("set", id, null, result);
105 return result;
106 };
107 }
108 conf = {
109 original: original,
110 memoized: memoized,
111 profileName: options.profileName,
112 get: function (args) {
113 if (resolve) args = resolve(args);
114 if (get) return get(args);
115 return String(args[0]);
116 },
117 has: function (id) { return hasOwnProperty.call(cache, id); },
118 delete: function (id) {
119 var result;
120 if (!hasOwnProperty.call(cache, id)) return;
121 if (del) del(id);
122 result = cache[id];
123 delete cache[id];
124 if (deleteListeners) conf.emit("delete", id, result);
125 },
126 clear: function () {
127 var oldCache = cache;
128 if (clear) clear();
129 cache = create(null);
130 conf.emit("clear", oldCache);
131 },
132 on: function (type, listener) {
133 if (type === "get") getListeners = true;
134 else if (type === "set") setListeners = true;
135 else if (type === "delete") deleteListeners = true;
136 return on.call(this, type, listener);
137 },
138 emit: emit,
139 updateEnv: function () { original = conf.original; },
140 };
141 if (get) {
142 extDel = defineLength(function (arg) {
143 var id, args = arguments;
144 if (resolve) args = resolve(args);
145 id = get(args);
146 if (id === null) return;
147 conf.delete(id);
148 }, memLength);
149 } else if (length === 0) {
150 extDel = function () { return conf.delete("data"); };
151 } else {
152 extDel = function (arg) {
153 if (resolve) arg = resolve(arguments)[0];
154 return conf.delete(arg);
155 };
156 }
157 extGet = defineLength(function () {
158 var id, args = arguments;
159 if (length === 0) return cache.data;
160 if (resolve) args = resolve(args);
161 if (get) id = get(args);
162 else id = String(args[0]);
163 return cache[id];
164 });
165 extHas = defineLength(function () {
166 var id, args = arguments;
167 if (length === 0) return conf.has("data");
168 if (resolve) args = resolve(args);
169 if (get) id = get(args);
170 else id = String(args[0]);
171 if (id === null) return false;
172 return conf.has(id);
173 });
174 defineProperties(memoized, {
175 __memoized__: d(true),
176 delete: d(extDel),
177 clear: d(conf.clear),
178 _get: d(extGet),
179 _has: d(extHas),
180 });
181 return conf;
182 };