source: imaps-frontend/node_modules/webpack/lib/serialization/Serializer.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 1.6 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3*/
4
5"use strict";
6
7/**
8 * @template T, K
9 * @typedef {import("./SerializerMiddleware")<T, K>} SerializerMiddleware
10 */
11
12class Serializer {
13 /**
14 * @param {SerializerMiddleware<any, any>[]} middlewares serializer middlewares
15 * @param {TODO=} context context
16 */
17 constructor(middlewares, context) {
18 this.serializeMiddlewares = middlewares.slice();
19 this.deserializeMiddlewares = middlewares.slice().reverse();
20 this.context = context;
21 }
22
23 /**
24 * @param {any} obj object
25 * @param {TODO} context content
26 * @returns {Promise<any>} result
27 */
28 serialize(obj, context) {
29 const ctx = { ...context, ...this.context };
30 let current = obj;
31 for (const middleware of this.serializeMiddlewares) {
32 if (current && typeof current.then === "function") {
33 current = current.then(data => data && middleware.serialize(data, ctx));
34 } else if (current) {
35 try {
36 current = middleware.serialize(current, ctx);
37 } catch (err) {
38 current = Promise.reject(err);
39 }
40 } else break;
41 }
42 return current;
43 }
44
45 /**
46 * @param {any} value value
47 * @param {TODO} context context
48 * @returns {Promise<any>} result
49 */
50 deserialize(value, context) {
51 const ctx = { ...context, ...this.context };
52 /** @type {any} */
53 let current = value;
54 for (const middleware of this.deserializeMiddlewares) {
55 current =
56 current && typeof current.then === "function"
57 ? current.then(data => middleware.deserialize(data, ctx))
58 : middleware.deserialize(current, ctx);
59 }
60 return current;
61 }
62}
63
64module.exports = Serializer;
Note: See TracBrowser for help on using the repository browser.