preserves/implementations/javascript/packages/core/src/pointer.ts

118 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-04-22 18:51:48 +00:00
import { Encoder, EncoderState } from "./encoder";
2021-04-24 19:59:52 +00:00
import { Decoder, DecoderState } from "./decoder";
2021-03-17 09:21:48 +00:00
import type { Value } from "./values";
import { strip } from "./strip";
2021-04-24 19:59:52 +00:00
export type PointerType<T> = {
decode(s: DecoderState): T;
encode(s: EncoderState, v: T): void;
fromValue(v: Value<GenericPointer>): T;
toValue(v: T): Value<GenericPointer>;
2021-04-24 19:59:52 +00:00
}
2021-03-17 09:21:48 +00:00
2021-04-24 19:59:52 +00:00
export class Pointer<T> {
embeddedValue: T;
constructor(embeddedValue: T) {
this.embeddedValue = embeddedValue;
2021-03-17 09:21:48 +00:00
}
equals(other: any, is: (a: any, b: any) => boolean) {
2021-04-24 19:59:52 +00:00
return isPointer<T>(other) && is(this.embeddedValue, other.embeddedValue);
2021-03-17 09:21:48 +00:00
}
asPreservesText(): string {
2021-04-24 19:59:52 +00:00
return '#!' + (this.embeddedValue as any).asPreservesText();
2021-03-17 09:21:48 +00:00
}
}
2021-04-24 19:59:52 +00:00
export function embed<T>(embeddedValue: T): Pointer<T> {
return new Pointer(embeddedValue);
}
export function isPointer<T>(v: Value<T>): v is Pointer<T> {
return typeof v === 'object' && 'embeddedValue' in v;
2021-03-17 09:21:48 +00:00
}
export class GenericPointer {
2021-04-24 19:59:52 +00:00
generic: Value;
constructor(generic: Value) {
this.generic = generic;
}
equals(other: any, is: (a: any, b: any) => boolean) {
return typeof other === 'object' && 'generic' in other && is(this.generic, other.generic);
}
asPreservesText(): string {
return this.generic.asPreservesText();
}
2021-03-17 09:21:48 +00:00
}
export const genericPointerType: PointerType<GenericPointer> = {
decode(s: DecoderState): GenericPointer {
return new GenericPointer(new Decoder(s, this).next());
2021-04-24 19:59:52 +00:00
},
encode(s: EncoderState, v: GenericPointer): void {
2021-04-24 19:59:52 +00:00
new Encoder(s, this).push(v.generic);
},
fromValue(v: Value<GenericPointer>): GenericPointer {
return new GenericPointer(strip(v));
2021-04-24 19:59:52 +00:00
},
toValue(v: GenericPointer): Value<GenericPointer> {
2021-04-24 19:59:52 +00:00
return v.generic;
}
};
export const neverPointerType: PointerType<never> = {
decode(_s: DecoderState): never {
throw new Error("Pointers not permitted at this point in Preserves document");
},
encode(_s: EncoderState, _v: never): void {
throw new Error("Pointers not permitted encoding Preserves document");
},
fromValue(_v: Value<GenericPointer>): never {
2021-04-24 19:59:52 +00:00
throw new Error("Pointers not permitted at this point in Preserves document");
},
toValue(_v: never): Value<GenericPointer> {
2021-04-24 19:59:52 +00:00
throw new Error("Pointers not permitted encoding Preserves document");
}
};
let _nextId = 0;
const _registry = new WeakMap<object, number>();
export function pointerId(v: any): number {
let id = _registry.get(v);
if (id === void 0) {
id = _nextId++;
_registry.set(v, id);
}
return id;
2021-03-17 09:21:48 +00:00
}
2021-04-24 19:59:52 +00:00
export const identityPointerType: PointerType<any> = {
decode(_s: DecoderState): any {
throw new Error("Cannot decode identityPointerType");
},
encode(s: EncoderState, v: any): void {
new Encoder(s, this).push(pointerId(v));
},
fromValue(_v: Value<GenericPointer>): any {
2021-04-24 19:59:52 +00:00
throw new Error("Cannot decode identityPointerType");
},
toValue(v: any): Value<GenericPointer> {
2021-04-24 19:59:52 +00:00
return pointerId(v);
}
};