@yeti-cgi/aux-common
Advanced tools
Comparing version 0.3.12 to 0.3.14
import { AuxOp, FileOp, TagOp, InsertOp, ValueOp, DeleteOp } from './AuxOpTypes'; | ||
import { CausalTree } from '../causal-trees/CausalTree'; | ||
import { CausalTree, CausalTreeOptions } from '../causal-trees/CausalTree'; | ||
import { FilesState, FileEvent, PartialFile, File } from '../Files'; | ||
@@ -15,4 +15,5 @@ import { AuxReducerMetadata } from './AuxReducer'; | ||
* @param tree The stored tree that this object should be constructed from. | ||
* @param options The options to use. | ||
*/ | ||
constructor(tree: StoredCausalTree<AuxOp>); | ||
constructor(tree: StoredCausalTree<AuxOp>, options?: CausalTreeOptions); | ||
/** | ||
@@ -19,0 +20,0 @@ * Creates a new root atom and adds it to the tree. |
@@ -16,5 +16,6 @@ import { AuxOpType } from './AuxOpTypes'; | ||
* @param tree The stored tree that this object should be constructed from. | ||
* @param options The options to use. | ||
*/ | ||
constructor(tree) { | ||
super(tree, new AuxReducer()); | ||
constructor(tree, options) { | ||
super(tree, new AuxReducer(), options); | ||
} | ||
@@ -21,0 +22,0 @@ /** |
@@ -8,5 +8,5 @@ import { CausalTreeFactory } from "../causal-trees"; | ||
return new CausalTreeFactory({ | ||
'aux': (tree) => new AuxCausalTree(tree) | ||
'aux': (tree, options) => new AuxCausalTree(tree, options) | ||
}); | ||
} | ||
//# sourceMappingURL=AuxCausalTreeFactory.js.map |
@@ -11,2 +11,12 @@ import { AtomOp, Atom, AtomId } from "./Atom"; | ||
/** | ||
* Defines an interface that contains possible options that can be set on a causal tree. | ||
*/ | ||
export interface CausalTreeOptions { | ||
/** | ||
* Specifies whether the causal tree should try to remove atoms that no longer affect the tree. | ||
* Defaults to false. | ||
*/ | ||
garbageCollect?: boolean; | ||
} | ||
/** | ||
* Defines a class that represents a Causal Tree. | ||
@@ -29,2 +39,3 @@ * That is, a conflict-free replicated data type. (CRDT) | ||
* Gets or sets whether the causal tree should collect garbage. | ||
* Defaults to false. | ||
*/ | ||
@@ -73,4 +84,5 @@ garbageCollect: boolean; | ||
* @param reducer The reducer used to convert a list of operations into a single value. | ||
* @param options The options to use. | ||
*/ | ||
constructor(tree: StoredCausalTree<TOp>, reducer: AtomReducer<TOp, TValue, TMetadata>); | ||
constructor(tree: StoredCausalTree<TOp>, reducer: AtomReducer<TOp, TValue, TMetadata>, options?: CausalTreeOptions); | ||
/** | ||
@@ -153,2 +165,7 @@ * Creates a root element on this tree. | ||
/** | ||
* Triggers a round of garbage collection on the given atoms. | ||
* @param atoms The atoms to garbage collect. | ||
*/ | ||
protected triggerGarbageCollection(atoms: Atom<TOp>[]): Atom<TOp>[]; | ||
/** | ||
* Recalculates the values associated the given references. | ||
@@ -155,0 +172,0 @@ * This can be used as a performance improvement to only recalculate the parts of the |
@@ -14,4 +14,5 @@ import { Weave } from "./Weave"; | ||
* @param reducer The reducer used to convert a list of operations into a single value. | ||
* @param options The options to use. | ||
*/ | ||
constructor(tree, reducer) { | ||
constructor(tree, reducer, options = {}) { | ||
this._site = tree.site; | ||
@@ -30,3 +31,3 @@ this._knownSites = unionBy([ | ||
this._batch = []; | ||
this.garbageCollect = false; | ||
this.garbageCollect = options.garbageCollect || false; | ||
this.import(tree); | ||
@@ -108,8 +109,3 @@ } | ||
const refs = [ref]; | ||
if (this.garbageCollect) { | ||
const removed = this.collectGarbage(refs); | ||
if (removed.length > 0) { | ||
this._atomArchived.next(removed); | ||
} | ||
} | ||
this.triggerGarbageCollection(refs); | ||
[this._value, this._metadata] = this._calculateValue(refs); | ||
@@ -156,8 +152,3 @@ this._atomAdded.next(refs); | ||
if (this._batch.length > 0) { | ||
if (this.garbageCollect) { | ||
const removed = this.collectGarbage(this._batch); | ||
if (removed.length > 0) { | ||
this._atomArchived.next(removed); | ||
} | ||
} | ||
this.triggerGarbageCollection(this._batch); | ||
[this._value, this._metadata] = this._calculateValue(this._batch); | ||
@@ -182,2 +173,3 @@ this._atomAdded.next(this._batch); | ||
} | ||
this.triggerGarbageCollection(newAtoms); | ||
[this._value, this._metadata] = this._calculateValue(newAtoms); | ||
@@ -191,22 +183,24 @@ return newAtoms; | ||
import(tree) { | ||
let added; | ||
if (tree.weave) { | ||
if (tree.formatVersion === 2) { | ||
return this.importWeave(tree.weave); | ||
added = this.importWeave(tree.weave); | ||
} | ||
else if (tree.formatVersion === 3) { | ||
if (tree.ordered) { | ||
return this.importWeave(tree.weave); | ||
added = this.importWeave(tree.weave); | ||
} | ||
else { | ||
return this.addMany(tree.weave); | ||
added = this.addMany(tree.weave); | ||
} | ||
} | ||
else if (typeof tree.formatVersion === 'undefined') { | ||
return this.importWeave(tree.weave.map(ref => ref.atom)); | ||
added = this.importWeave(tree.weave.map(ref => ref.atom)); | ||
} | ||
else { | ||
console.warn("[CausalTree] Don't know how to import tree version:", tree.formatVersion); | ||
return []; | ||
added = []; | ||
} | ||
} | ||
return added; | ||
} | ||
@@ -218,4 +212,5 @@ /** | ||
return { | ||
formatVersion: 2, | ||
formatVersion: 3, | ||
site: this._site, | ||
ordered: true, | ||
knownSites: this.knownSites.slice(), | ||
@@ -296,2 +291,16 @@ weave: this.weave.atoms.slice() | ||
/** | ||
* Triggers a round of garbage collection on the given atoms. | ||
* @param atoms The atoms to garbage collect. | ||
*/ | ||
triggerGarbageCollection(atoms) { | ||
if (this.garbageCollect) { | ||
const removed = this.collectGarbage(atoms); | ||
if (removed.length > 0) { | ||
this._atomArchived.next(removed); | ||
} | ||
return removed; | ||
} | ||
return []; | ||
} | ||
/** | ||
* Recalculates the values associated the given references. | ||
@@ -298,0 +307,0 @@ * This can be used as a performance improvement to only recalculate the parts of the |
import { AtomOp } from "./Atom"; | ||
import { CausalTree } from "./CausalTree"; | ||
import { CausalTree, CausalTreeOptions } from "./CausalTree"; | ||
import { StoredCausalTree } from "./StoredCausalTree"; | ||
export interface CausalTreeFactoryMap { | ||
[type: string]: (tree: StoredCausalTree<AtomOp> | null) => CausalTree<AtomOp, any, any>; | ||
[type: string]: (tree: StoredCausalTree<AtomOp> | null, options: CausalTreeOptions) => CausalTree<AtomOp, any, any>; | ||
} | ||
@@ -18,3 +18,3 @@ /** | ||
*/ | ||
create(type: string, storedTree?: StoredCausalTree<AtomOp>): CausalTree<AtomOp, any, any>; | ||
create(type: string, storedTree?: StoredCausalTree<AtomOp>, options?: CausalTreeOptions): CausalTree<AtomOp, any, any>; | ||
} |
@@ -13,6 +13,6 @@ /** | ||
*/ | ||
create(type, storedTree) { | ||
create(type, storedTree, options) { | ||
const factory = this._map[type]; | ||
if (factory) { | ||
return factory(storedTree); | ||
return factory(storedTree, options); | ||
} | ||
@@ -19,0 +19,0 @@ else { |
import { RealtimeChannel } from "./RealtimeChannel"; | ||
import { AtomOp, Atom } from "./Atom"; | ||
import { CausalTree } from "./CausalTree"; | ||
import { CausalTree, CausalTreeOptions } from "./CausalTree"; | ||
import { CausalTreeStore } from "./CausalTreeStore"; | ||
@@ -21,2 +21,3 @@ import { CausalTreeFactory } from "./CausalTreeFactory"; | ||
private _subs; | ||
private _options; | ||
/** | ||
@@ -52,4 +53,5 @@ * Gets the realtime channel that this tree is using. | ||
* @param channel The channel used to communicate with other devices. | ||
* @param options The options that should be used for the causal tree. | ||
*/ | ||
constructor(factory: CausalTreeFactory, store: CausalTreeStore, channel: RealtimeChannel<Atom<AtomOp>[]>); | ||
constructor(factory: CausalTreeFactory, store: CausalTreeStore, channel: RealtimeChannel<Atom<AtomOp>[]>, options?: CausalTreeOptions); | ||
/** | ||
@@ -56,0 +58,0 @@ * Initializes the realtime causal tree. |
@@ -27,4 +27,5 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
* @param channel The channel used to communicate with other devices. | ||
* @param options The options that should be used for the causal tree. | ||
*/ | ||
constructor(factory, store, channel) { | ||
constructor(factory, store, channel, options) { | ||
this._factory = factory; | ||
@@ -37,2 +38,3 @@ this._store = store; | ||
this._subs = []; | ||
this._options = options; | ||
// TODO: Get the causal tree to store the state | ||
@@ -85,3 +87,3 @@ // without tanking performance. | ||
if (stored) { | ||
this._setTree(this._factory.create(this.type, stored)); | ||
this._setTree(this._factory.create(this.type, stored, this._options)); | ||
if (stored.weave) { | ||
@@ -88,0 +90,0 @@ if (stored.formatVersion === 2) { |
{ | ||
"name": "@yeti-cgi/aux-common", | ||
"version": "0.3.12", | ||
"version": "0.3.14", | ||
"description": "Common library for AUX projects", | ||
@@ -60,3 +60,3 @@ "main": "index.js", | ||
], | ||
"gitHead": "82ca4757e0680ddd43eb3b4d8f46f548a3dd17c8" | ||
"gitHead": "3bf518925f80f29b8dfe87f9afc5132ceaff0fd1" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
418348
8054