|
1 | 1 | var trie = require("./trie.js");
|
2 | 2 |
|
| 3 | +var NotImplemented = function (name) { |
| 4 | + return { |
| 5 | + name: "NotImplementedException", |
| 6 | + message: "method '" + name + "' not implemented" |
| 7 | + }; |
| 8 | +}; |
| 9 | + |
| 10 | +var BaseStore = Object.create(Object.prototype, { |
| 11 | + // "abstract" methods |
| 12 | + get: { value: function (path) { throw NotImplemented("get"); } }, |
| 13 | + getTarget: { value: function (path) { throw NotImplemented("getTarget"); } }, |
| 14 | + getAll: { value: function (path) { throw NotImplemented("getAll"); } }, |
| 15 | + add: { value: function (path) { throw NotImplemented("add"); } }, |
| 16 | + update: { value: function (path) { throw NotImplemented("update"); } }, |
| 17 | + remove: { value: function (path) { throw NotImplemented("remove"); } }, |
| 18 | + hasRoute: { value: function (path) { throw NotImplemented("hasRoute"); } }, |
| 19 | + |
| 20 | + cleanPath: { |
| 21 | + value: function (path) { |
| 22 | + return trie.trim_prefix(path); |
| 23 | + } |
| 24 | + } |
| 25 | +}); |
| 26 | + |
3 | 27 | function MemoryStore () {
|
4 | 28 | var routes = {};
|
5 | 29 | var urls = new trie.URLTrie()
|
6 | 30 |
|
7 |
| - return { |
8 |
| - get: function (path) { return routes[path]; }, |
9 |
| - getTarget: function(path) { return urls.get(path); }, |
10 |
| - getAll: function () { return routes; }, |
11 |
| - cleanPath: function(path) { return trie.trim_prefix(path); }, |
12 |
| - add: function (path, data) { |
13 |
| - routes[path] = data; |
14 |
| - urls.add(path, data); |
| 31 | + return Object.create(BaseStore, { |
| 32 | + get: { |
| 33 | + value: function (path) { |
| 34 | + return routes[path]; |
| 35 | + } |
15 | 36 | },
|
16 |
| - update: function(path, data) { Object.assign(routes[path], data) }, |
17 |
| - remove: function (path) { |
18 |
| - delete routes[path]; |
19 |
| - urls.remove(path); |
| 37 | + getTarget: { |
| 38 | + value: function (path) { |
| 39 | + return urls.get(path); |
| 40 | + } |
20 | 41 | },
|
21 |
| - hasRoute: function (path) { return routes.hasOwnProperty(path); } |
22 |
| - }; |
| 42 | + getAll: { |
| 43 | + value: function () { |
| 44 | + return routes; |
| 45 | + } |
| 46 | + }, |
| 47 | + add: { |
| 48 | + value: function (path, data) { |
| 49 | + routes[path] = data; |
| 50 | + urls.add(path, data); |
| 51 | + } |
| 52 | + }, |
| 53 | + update: { |
| 54 | + value: function (path, data) { |
| 55 | + Object.assign(routes[path], data); |
| 56 | + } |
| 57 | + }, |
| 58 | + remove: { |
| 59 | + value: function (path) { |
| 60 | + delete routes[path]; |
| 61 | + urls.remove(path); |
| 62 | + } |
| 63 | + }, |
| 64 | + hasRoute: { |
| 65 | + value: function (path) { |
| 66 | + return routes.hasOwnProperty(path); |
| 67 | + } |
| 68 | + } |
| 69 | + }); |
23 | 70 | }
|
24 | 71 |
|
25 | 72 | exports.MemoryStore = MemoryStore;
|
0 commit comments