@radixdlt/atom
Version:
Container for CRUD instructions known as 'Particles' that are sent to the Radix decentralized ledger
164 lines • 8.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpunParticle = exports.isAnySpunParticle = exports.asDownParticle = exports.asUpParticle = exports.asAnyDownParticle = exports.asAnyUpParticle = exports.anyDownParticle = exports.anyUpParticle = exports.spunDownParticle = exports.spunUpParticle = exports.downParticle = exports.upParticle = exports.spunParticle = exports.anySpunParticle = void 0;
const _types_1 = require("./_types");
const spin_1 = require("./meta/spin");
const neverthrow_1 = require("neverthrow");
const data_formats_1 = require("@radixdlt/data-formats");
const _types_2 = require("../_types");
const utils_1 = require("../utils");
const SERIALIZER = 'radix.spun_particle';
const JSONDecoder = data_formats_1.taggedObjectDecoder(SERIALIZER, _types_2.SERIALIZER_KEY)((input) => neverthrow_1.ok(exports.anySpunParticle(input)));
const jsonDecoding = utils_1.JSONDecoding.withDecoders(JSONDecoder).create();
/* eslint-disable max-params */
const anySpunParticlesEquals = (lhs, rhs) => {
return (lhs.spin === rhs.spin &&
lhs.particle.equals(rhs.particle) &&
rhs.particle.equals(lhs.particle));
};
/* eslint-enable max-params */
/**
* Creates an AnySpunParticle (type-erased SpunParticle) with a specified spin.
*
* @param spunParticleBase {SpunParticleBase} A particle of any type of to give a spin.
* @returns {AnySpunParticle} an AnySpunParticle (type-erased SpunParticle) with a specified spin.
*/
const anySpunParticle = (spunParticleBase) => (Object.assign(Object.assign(Object.assign(Object.assign({}, spunParticleBase), data_formats_1.JSONEncoding(SERIALIZER)({
particle: spunParticleBase.particle,
spin: spunParticleBase.spin,
})), data_formats_1.DSONEncoding(SERIALIZER)({
particle: spunParticleBase.particle,
spin: data_formats_1.DSONPrimitive(spunParticleBase.spin),
})), { equals: (other) => anySpunParticlesEquals(spunParticleBase, other), downedAsAny: () => spunParticleBase.spin === _types_1.Spin.UP
? neverthrow_1.ok(exports.anyDownParticle(spunParticleBase.particle))
: neverthrow_1.err(new Error('Cannot down a particle with spin Down')) }));
exports.anySpunParticle = anySpunParticle;
/**
* Creates a typed SpunParticle with a specified spin.
*
* @param particle {P} A particle of typed type `P` to give a spin.
* @param spin {Spin} The spun of the particle
* @returns {SpunParticleT<P>} a typed SpunParticle with a specified spin.
* @template P a specific type of particle, that **is a** `ParticleBase`
*/
const spunParticle = (input) => {
const anySpun = exports.anySpunParticle(input);
return Object.assign(Object.assign({}, anySpun), { particle: input.particle, eraseToAny: () => anySpun, downed: () => input.spin === _types_1.Spin.UP
? neverthrow_1.ok(exports.downParticle(input.particle))
: neverthrow_1.err(new Error('Cannot down a particle with spin Down')) });
};
exports.spunParticle = spunParticle;
/**
* Creates a typed UpParticle, a container for typed particle with the at compile time known Spin.UP.
*
* @param particle {P} A particle of typed type `P` to give the spin UP.
* @returns {UpParticle<P>} a typed UpParticle, a container for typed particle with the at compile time known Spin.UP.
* @template P a specific type of particle, that **is a** `ParticleBase`
*/
const upParticle = (particle) => {
const spin = _types_1.Spin.UP;
const spun = exports.spunParticle({ particle, spin });
return Object.assign(Object.assign({}, spun), { spin, toSpunParticle: () => spun, eraseToAnyUp: () => exports.anyUpParticle(particle) });
};
exports.upParticle = upParticle;
/**
* Creates a typed DownParticle, a container for typed particle with the at compile time known Spin.Down.
*
* @param particle {P} A particle of typed type `P` to give the spin DOWN.
* @returns {DownParticle<P>} a typed DownParticle, a container for typed particle with the at compile time known Spin.Down.
* @template P a specific type of particle, that **is a** `ParticleBase`
*/
const downParticle = (particle) => {
const spin = _types_1.Spin.DOWN;
const spun = exports.spunParticle({ particle, spin });
return Object.assign(Object.assign({}, spun), { spin, toSpunParticle: () => spun, eraseToAnyDown: () => exports.anyDownParticle(particle) });
};
exports.downParticle = downParticle;
/**
* Creates a typed SpunParticle with Spin.UP.
*
* @param particle {P} A particle of typed type `P` to give the spin UP.
* @returns {SpunParticleT<P>} a typed SpunParticle with a spin UP.
* @template P a specific type of particle, that **is a** `ParticleBase`
*/
const spunUpParticle = (particle) => exports.upParticle(particle).toSpunParticle();
exports.spunUpParticle = spunUpParticle;
/**
* Creates a typed SpunParticle with Spin.DOWN.
*
* @param particle {P} A particle of typed type `P` to give the spin DOWN.
* @returns {SpunParticleT<P>} a typed SpunParticle with a spin DOWN.
* @template P a specific type of particle, that **is a** `ParticleBase`
*/
const spunDownParticle = (particle) => exports.downParticle(particle).toSpunParticle();
exports.spunDownParticle = spunDownParticle;
/**
* Creates an AnyUpParticle (type-erased UpParticle) with the at compile time known spin UP.
*
* @param particle {ParticleBase} A particle of any type of to give the spin UP.
* @returns {AnyUpParticle} an AnyUpParticle (type-erased UpParticle) with the at compile time known spin UP.
*/
const anyUpParticle = (particle) => {
const spin = _types_1.Spin.UP;
const anySpun = exports.anySpunParticle({ particle, spin });
return Object.assign(Object.assign({}, anySpun), { spin, toAnySpunParticle: () => anySpun });
};
exports.anyUpParticle = anyUpParticle;
/**
* Creates an AnyDownParticle (type-erased DownParticle) with the at compile time known spin DOWN.
*
* @param particle {ParticleBase} A particle of any type of to give the spin DOWN.
* @returns {AnyDownParticle} an AnyDownParticle (type-erased DownParticle) with the at compile time known spin DOWN.
*/
const anyDownParticle = (particle) => {
const spin = _types_1.Spin.DOWN;
const anySpun = exports.anySpunParticle({ particle, spin });
return Object.assign(Object.assign({}, anySpun), { spin, toAnySpunParticle: () => anySpun });
};
exports.anyDownParticle = anyDownParticle;
const asAnyUpParticle = (spunParticle) => {
if (spunParticle.spin !== _types_1.Spin.UP) {
return neverthrow_1.err(new Error('Particle does not have spin UP.'));
}
return neverthrow_1.ok(exports.anyUpParticle(spunParticle.particle));
};
exports.asAnyUpParticle = asAnyUpParticle;
const asAnyDownParticle = (spunParticle) => {
if (spunParticle.spin !== _types_1.Spin.DOWN) {
return neverthrow_1.err(new Error('Particle does not have spin DOWN.'));
}
return neverthrow_1.ok(exports.anyDownParticle(spunParticle.particle));
};
exports.asAnyDownParticle = asAnyDownParticle;
const asUpParticle = (spunParticle) => {
if (spunParticle.spin !== _types_1.Spin.UP) {
return neverthrow_1.err(new Error('Particle does not have spin UP.'));
}
return neverthrow_1.ok(exports.upParticle(spunParticle.particle));
};
exports.asUpParticle = asUpParticle;
const asDownParticle = (spunParticle) => {
if (spunParticle.spin !== _types_1.Spin.DOWN) {
return neverthrow_1.err(new Error('Particle does not have spin DOWN.'));
}
return neverthrow_1.ok(exports.downParticle(spunParticle.particle));
};
exports.asDownParticle = asDownParticle;
const isParticleBase = (something) => {
const inspection = something;
return inspection.equals !== undefined;
};
// eslint-disable-next-line complexity
const isAnySpunParticle = (something) => {
const inspection = something;
return (inspection.spin !== undefined &&
spin_1.isSpin(inspection.spin) &&
inspection.particle !== undefined &&
isParticleBase(inspection.particle) &&
inspection.equals !== undefined &&
inspection.downedAsAny() !== undefined);
};
exports.isAnySpunParticle = isAnySpunParticle;
exports.SpunParticle = Object.assign(Object.assign({}, jsonDecoding), { JSONDecoder,
SERIALIZER });
//# sourceMappingURL=spunParticle.js.map