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

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3*/
4
5"use strict";
6
7/**
8 * Defines the serializer middleware type used by this module.
9 * @template T, K, C
10 * @typedef {import("./SerializerMiddleware")<T, K, C>} SerializerMiddleware
11 */
12
13/**
14 * Represents Serializer.
15 * @template DeserializedValue
16 * @template SerializedValue
17 * @template Context
18 */
19class Serializer {
20 /**
21 * Creates an instance of Serializer.
22 * @param {SerializerMiddleware<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>[]} middlewares serializer middlewares
23 * @param {Context=} context context
24 */
25 constructor(middlewares, context) {
26 this.serializeMiddlewares = [...middlewares];
27 this.deserializeMiddlewares = [...middlewares].reverse();
28 this.context = context;
29 }
30
31 /**
32 * Serializes this instance into the provided serializer context.
33 * @template ExtendedContext
34 * @param {DeserializedValue | Promise<DeserializedValue>} obj object
35 * @param {Context & ExtendedContext} context context object
36 * @returns {Promise<SerializedValue>} result
37 */
38 serialize(obj, context) {
39 const ctx = { ...context, ...this.context };
40 let current = obj;
41 for (const middleware of this.serializeMiddlewares) {
42 if (
43 current &&
44 typeof (/** @type {Promise<DeserializedValue>} */ (current).then) ===
45 "function"
46 ) {
47 current =
48 /** @type {Promise<DeserializedValue>} */
49 (current).then((data) => data && middleware.serialize(data, ctx));
50 } else if (current) {
51 try {
52 current = middleware.serialize(current, ctx);
53 } catch (err) {
54 current = Promise.reject(err);
55 }
56 } else {
57 break;
58 }
59 }
60 return /** @type {Promise<SerializedValue>} */ (current);
61 }
62
63 /**
64 * Restores this instance from the provided deserializer context.
65 * @template ExtendedContext
66 * @param {SerializedValue | Promise<SerializedValue>} value value
67 * @param {Context & ExtendedContext} context object
68 * @returns {Promise<DeserializedValue>} result
69 */
70 deserialize(value, context) {
71 const ctx = { ...context, ...this.context };
72 let current = value;
73 for (const middleware of this.deserializeMiddlewares) {
74 current =
75 current &&
76 typeof (/** @type {Promise<SerializedValue>} */ (current).then) ===
77 "function"
78 ? /** @type {Promise<SerializedValue>} */ (current).then((data) =>
79 middleware.deserialize(data, ctx)
80 )
81 : middleware.deserialize(current, ctx);
82 }
83 return /** @type {Promise<DeserializedValue>} */ (current);
84 }
85}
86
87module.exports = Serializer;
Note: See TracBrowser for help on using the repository browser.