File size: 2,320 Bytes
31e8457
 
 
 
051bada
 
 
 
 
 
 
 
 
31e8457
 
 
 
051bada
 
 
 
 
 
 
 
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
051bada
 
 
 
31e8457
 
 
 
 
051bada
31e8457
 
 
 
 
051bada
 
 
 
31e8457
 
 
 
 
 
 
051bada
 
 
 
31e8457
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import * as PrimaryKey from "../../PrimaryKey.js";
import * as Request from "../../Request.js";
import * as Schema from "../../Schema.js";
/**
 * Defines the property key used to attach success and error schemas to persistable
 * requests.
 *
 * **When to use**
 *
 * Use to implement persistable request values by attaching success and error
 * schemas at this property key.
 *
 * @category symbols
 * @since 4.0.0
 */
export const symbol = "~effect/persistence/Persistable";
/**
 * Creates request classes that implement `Persistable` and `Request.Request`.
 *
 * **Details**
 *
 * The generated class stores the supplied tag, derives its primary key from
 * the payload, and carries schemas for persisted success and error exits.
 *
 * @category constructors
 * @since 4.0.0
 */
export const Class = () => (tag, options) => {
  function Persistable(props) {
    this._tag = tag;
    if (props) {
      Object.assign(this, props);
    }
  }
  Persistable.prototype = {
    ...Request.RequestPrototype,
    [PrimaryKey.symbol]() {
      return options.primaryKey(this);
    },
    [symbol]: {
      success: options.success ?? Schema.Void,
      error: options.error ?? Schema.Never
    }
  };
  return Persistable;
};
/**
 * Returns the cached `Exit` schema for a persistable request's success and
 * error schemas.
 *
 * @category accessors
 * @since 4.0.0
 */
export const exitSchema = self => {
  let schema = exitSchemaCache.get(self);
  if (schema) return schema;
  schema = Schema.Exit(self[symbol].success, self[symbol].error, Schema.Defect());
  exitSchemaCache.set(self, schema);
  return schema;
};
const exitSchemaCache = /*#__PURE__*/new WeakMap();
/**
 * Encodes an `Exit` for a persistable request using its success and error
 * schemas.
 *
 * @category serialization
 * @since 4.0.0
 */
export const serializeExit = (self, exit) => {
  const schema = Schema.toCodecJson(exitSchema(self));
  return Schema.encodeEffect(schema)(exit);
};
/**
 * Decodes a persisted value into an `Exit` for a persistable request using its
 * success and error schemas.
 *
 * @category serialization
 * @since 4.0.0
 */
export const deserializeExit = (self, encoded) => {
  const schema = Schema.toCodecJson(exitSchema(self));
  return Schema.decodeUnknownEffect(schema)(encoded);
};
//# sourceMappingURL=Persistable.js.map