@ceramicnetwork/common
Version:
Ceramic common types and utilities
68 lines • 2.26 kB
JavaScript
import cloneDeep from 'lodash.clonedeep';
import { StreamID } from '@ceramicnetwork/streamid';
import { CommitID } from '@ceramicnetwork/streamid';
import { Observable } from 'rxjs';
import { SyncOptions } from './streamopts.js';
export { AnchorStatus } from './anchor-service.js';
export var SignatureStatus;
(function (SignatureStatus) {
SignatureStatus[SignatureStatus["GENESIS"] = 0] = "GENESIS";
SignatureStatus[SignatureStatus["PARTIAL"] = 1] = "PARTIAL";
SignatureStatus[SignatureStatus["SIGNED"] = 2] = "SIGNED";
})(SignatureStatus || (SignatureStatus = {}));
export var EventType;
(function (EventType) {
EventType[EventType["INIT"] = 0] = "INIT";
EventType[EventType["DATA"] = 1] = "DATA";
EventType[EventType["TIME"] = 2] = "TIME";
})(EventType || (EventType = {}));
export class Stream extends Observable {
constructor(state$, api) {
super((subscriber) => {
state$.subscribe(subscriber);
});
this.state$ = state$;
this._api = api;
}
get id() {
return new StreamID(this.state$.value.type, this.state$.value.log[0].cid);
}
get api() {
return this._api;
}
get content() {
const { next, content } = this.state$.value;
return cloneDeep(next?.content ?? content);
}
get tip() {
return this.state$.value.log[this.state$.value.log.length - 1].cid;
}
get commitId() {
return CommitID.make(this.id, this.tip);
}
get allCommitIds() {
return this.state$.value.log.map(({ cid }) => CommitID.make(this.id, cid));
}
get anchorCommitIds() {
return this.state$.value.log
.filter(({ type }) => type === EventType.TIME)
.map(({ cid }) => CommitID.make(this.id, cid));
}
get state() {
return cloneDeep(this.state$.value);
}
async sync(opts = {}) {
opts = { sync: SyncOptions.PREFER_CACHE, ...opts };
const stream = await this.api.loadStream(this.id, opts);
this.state$.next(stream.state);
}
async requestAnchor() {
return this.api.requestAnchor(this.id);
}
}
export function StreamStatic() {
return (constructor) => {
constructor;
};
}
//# sourceMappingURL=stream.js.map