1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const memoize = require("../util/memoize");
|
8 |
|
9 | const LAZY_TARGET = Symbol("lazy serialization target");
|
10 | const LAZY_SERIALIZED_VALUE = Symbol("lazy serialization data");
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | class SerializerMiddleware {
|
17 |
|
18 | |
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | serialize(data, context) {
|
25 | const AbstractMethodError = require("../AbstractMethodError");
|
26 | throw new AbstractMethodError();
|
27 | }
|
28 |
|
29 |
|
30 | |
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | deserialize(data, context) {
|
37 | const AbstractMethodError = require("../AbstractMethodError");
|
38 | throw new AbstractMethodError();
|
39 | }
|
40 |
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | static createLazy(value, target, options = {}, serializedValue) {
|
49 | if (SerializerMiddleware.isLazy(value, target)) return value;
|
50 | const fn = typeof value === "function" ? value : () => value;
|
51 | fn[LAZY_TARGET] = target;
|
52 | (fn).options = options;
|
53 | fn[LAZY_SERIALIZED_VALUE] = serializedValue;
|
54 | return fn;
|
55 | }
|
56 |
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 | static isLazy(fn, target) {
|
63 | if (typeof fn !== "function") return false;
|
64 | const t = fn[LAZY_TARGET];
|
65 | return target ? t === target : !!t;
|
66 | }
|
67 |
|
68 | |
69 |
|
70 |
|
71 |
|
72 | static getLazyOptions(fn) {
|
73 | if (typeof fn !== "function") return undefined;
|
74 | return (fn).options;
|
75 | }
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 | static getLazySerializedValue(fn) {
|
82 | if (typeof fn !== "function") return undefined;
|
83 | return fn[LAZY_SERIALIZED_VALUE];
|
84 | }
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 | static setLazySerializedValue(fn, value) {
|
92 | fn[LAZY_SERIALIZED_VALUE] = value;
|
93 | }
|
94 |
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 | static serializeLazy(lazy, serialize) {
|
101 | const fn = memoize(() => {
|
102 | const r = lazy();
|
103 | if (r && typeof r.then === "function") {
|
104 | return r.then(data => data && serialize(data));
|
105 | }
|
106 | return serialize(r);
|
107 | });
|
108 | fn[LAZY_TARGET] = lazy[LAZY_TARGET];
|
109 | (fn).options = (lazy).options;
|
110 | lazy[LAZY_SERIALIZED_VALUE] = fn;
|
111 | return fn;
|
112 | }
|
113 |
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 | static deserializeLazy(lazy, deserialize) {
|
120 | const fn = memoize(() => {
|
121 | const r = lazy();
|
122 | if (r && typeof r.then === "function") {
|
123 | return r.then(data => deserialize(data));
|
124 | }
|
125 | return deserialize(r);
|
126 | });
|
127 | fn[LAZY_TARGET] = lazy[LAZY_TARGET];
|
128 | (fn).options = (lazy).options;
|
129 | fn[LAZY_SERIALIZED_VALUE] = lazy;
|
130 | return fn;
|
131 | }
|
132 |
|
133 | |
134 |
|
135 |
|
136 |
|
137 | static unMemoizeLazy(lazy) {
|
138 | if (!SerializerMiddleware.isLazy(lazy)) return lazy;
|
139 | const fn = () => {
|
140 | throw new Error(
|
141 | "A lazy value that has been unmemorized can't be called again"
|
142 | );
|
143 | };
|
144 | fn[LAZY_SERIALIZED_VALUE] = SerializerMiddleware.unMemoizeLazy(
|
145 | lazy[LAZY_SERIALIZED_VALUE]
|
146 | );
|
147 | fn[LAZY_TARGET] = lazy[LAZY_TARGET];
|
148 | fn.options = (lazy).options;
|
149 | return fn;
|
150 | }
|
151 | }
|
152 |
|
153 | module.exports = SerializerMiddleware;
|