UNPKG

@rimbu/graph

Version:

Immutable Graph data structures for TypeScript

58 lines 2.26 kB
import { Reducer } from '@rimbu/stream'; import { isEmptyStreamSourceInstance } from '@rimbu/stream/custom'; import { GraphBuilder, GraphEmpty, GraphNonEmpty } from '@rimbu/graph/custom'; export class GraphContext { constructor(isDirected, typeTag, linkMapContext, linkConnectionsContext) { this.isDirected = isDirected; this.typeTag = typeTag; this.linkMapContext = linkMapContext; this.linkConnectionsContext = linkConnectionsContext; this.empty = () => { return this._empty; }; this.from = (...sources) => { let builder = this.builder(); let i = -1; const length = sources.length; while (++i < length) { const source = sources[i]; if (isEmptyStreamSourceInstance(source)) continue; if (builder.isEmpty && this.isNonEmptyInstance(source) && source.context === this) { if (i === length - 1) return source; builder = source.toBuilder(); continue; } builder.addGraphElements(source); } return builder.build(); }; // prettier-ignore this.of = (...values) => { return this.from(values).assumeNonEmpty(); }; this.builder = () => { return new GraphBuilder(this.isDirected, this); }; this.reducer = (source) => { return Reducer.create(() => undefined === source ? this.builder() : this.from(source).toBuilder(), (builder, entry) => { builder.addGraphElement(entry); return builder; }, (builder) => builder.build()); }; this._empty = Object.freeze(new GraphEmpty(isDirected, this)); } isNonEmptyInstance(source) { return source instanceof GraphNonEmpty; } createBuilder(source) { return new GraphBuilder(this.isDirected, this, source); } createNonEmpty(linkMap, connectionSize) { return new GraphNonEmpty(this.isDirected, this, linkMap, connectionSize); } } //# sourceMappingURL=context.mjs.map