@ceramicnetwork/core
Version:
Typescript implementation of the Ceramic protocol
32 lines • 1.65 kB
JavaScript
import { StreamUtils } from '@ceramicnetwork/common';
import { applyTipToState } from './apply-tip-helper.js';
export class StreamUpdater {
constructor(logger, commitStorer, logSyncer, anchorTimestampExtractor, stateManipulator) {
this.logger = logger;
this.commitStorer = commitStorer;
this.logSyncer = logSyncer;
this.anchorTimestampExtractor = anchorTimestampExtractor;
this.stateManipulator = stateManipulator;
}
async applyTipFromNetwork(state, tip) {
return applyTipToState(this.logSyncer, this.anchorTimestampExtractor, this.stateManipulator, state, tip, {
throwOnInvalidCommit: false,
throwIfStale: false,
throwOnConflict: false,
});
}
async applyCommitFromUser(state, commit) {
const streamId = StreamUtils.streamIdFromState(state);
const commitCid = await this.commitStorer.storeEvent(commit, StreamUtils.streamIdFromState(state));
this.logger.verbose(`StreamUpdater stored commit for stream ${streamId.toString()}, CID: ${commitCid.toString()}`);
const updatedState = await applyTipToState(this.logSyncer, this.anchorTimestampExtractor, this.stateManipulator, state, commitCid, {
throwOnInvalidCommit: true,
throwIfStale: true,
throwOnConflict: true,
});
const newTip = StreamUtils.tipFromState(updatedState);
this.logger.verbose(`StreamUpdater applied commit ${commitCid.toString()} to stream ${streamId.toString()}. New tip: ${newTip.toString()} `);
return updatedState;
}
}
//# sourceMappingURL=stream-updater.js.map