react-native-drawing
Version:
A React Native library that provides a canvas to perform drawing actions
52 lines (51 loc) • 1.35 kB
JavaScript
import { DoubleMap } from './DoubleMap';
import { IdAllocator } from './IdAllocator';
/**
* An object to "transform" symbols to numeric ids
*/
export class SymbolParser {
constructor() {
Object.defineProperty(this, "index", {
enumerable: true,
configurable: true,
writable: true,
value: new DoubleMap()
});
Object.defineProperty(this, "idAllocator", {
enumerable: true,
configurable: true,
writable: true,
value: new IdAllocator((id) => {
return this.index.get(id) === undefined;
})
});
}
/**
* Gets a numeric id to represent the symbol (the id is created if it doesn't exist)
*/
toId(sym) {
let id = this.index.get(sym);
if (id === undefined) {
id = this.idAllocator.getFree();
this.index.set(sym, id);
}
return id;
}
/**
* Gets a symbol that represents the provided id
*/
toSymbol(id) {
let sym = this.index.get(id);
if (sym === undefined) {
sym = Symbol();
this.index.set(sym, id);
}
return sym;
}
/**
* Delete the references between the symbol and its id
*/
unRef(sym) {
this.index.delete(sym);
}
}