-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): change from stamps to TypeScript classes (#4095)
Refs #3481 BREAKING CHANGE: IdentityManager from apidom-core package became a class and requires to be instantiated with new operator.
- Loading branch information
Showing
9 changed files
with
57 additions
and
72 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,60 @@ | ||
import { Element, StringElement } from 'minim'; | ||
import stampit from 'stampit'; | ||
import ShortUniqueId from 'short-unique-id'; | ||
|
||
import ElementIdentityError from './errors/ElementIdentityError'; | ||
import { isElement, isStringElement } from '../predicates'; | ||
|
||
export interface IdentityManager<T extends Element = Element> { | ||
length: number; | ||
uuid: ShortUniqueId; | ||
identityMap: WeakMap<T, StringElement>; | ||
export class IdentityManager { | ||
protected readonly uuid: ShortUniqueId; | ||
|
||
identify(this: IdentityManager<T>, element: T): StringElement; | ||
forget(this: IdentityManager<T>, element: T): boolean; | ||
generateId(this: IdentityManager<T>): string; | ||
} | ||
protected readonly identityMap: WeakMap<Element, StringElement>; | ||
|
||
// @TODO(oliwia.rogala@smartbear.com): transforming this stamp to class will break backward compatibility | ||
export const IdentityManager: stampit.Stamp<IdentityManager> = stampit({ | ||
props: { | ||
uuid: null, | ||
length: null, | ||
identityMap: null, | ||
}, | ||
init(this: IdentityManager, { length = 6 } = {}) { | ||
this.length = 6; | ||
constructor({ length = 6 } = {}) { | ||
this.uuid = new ShortUniqueId({ length }); | ||
this.identityMap = new WeakMap(); | ||
}, | ||
methods: { | ||
identify<T extends Element>(this: IdentityManager, element: T): StringElement { | ||
if (!isElement(element)) { | ||
throw new ElementIdentityError( | ||
'Cannot not identify the element. `element` is neither structurally compatible nor a subclass of an Element class.', | ||
{ | ||
value: element, | ||
}, | ||
); | ||
} | ||
|
||
// use already assigned identity | ||
if ( | ||
element.meta.hasKey('id') && | ||
isStringElement(element.meta.get('id')) && | ||
!element.meta.get('id').equals('') | ||
) { | ||
return element.id; | ||
} | ||
|
||
// assign identity in immutable way | ||
if (this.identityMap.has(element)) { | ||
return this.identityMap.get(element)!; | ||
} | ||
|
||
// return element identity | ||
const id = new StringElement(this.generateId()); | ||
this.identityMap.set(element, id); | ||
return id; | ||
}, | ||
|
||
forget<T extends Element>(this: IdentityManager, element: T): boolean { | ||
if (this.identityMap.has(element)) { | ||
this.identityMap.delete(element); | ||
return true; | ||
} | ||
return false; | ||
}, | ||
|
||
generateId(this: IdentityManager): string { | ||
return this.uuid.randomUUID(); | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
identify<T extends Element>(this: IdentityManager, element: T): StringElement { | ||
if (!isElement(element)) { | ||
throw new ElementIdentityError( | ||
'Cannot not identify the element. `element` is neither structurally compatible nor a subclass of an Element class.', | ||
{ | ||
value: element, | ||
}, | ||
); | ||
} | ||
|
||
// use already assigned identity | ||
if ( | ||
element.meta.hasKey('id') && | ||
isStringElement(element.meta.get('id')) && | ||
!element.meta.get('id').equals('') | ||
) { | ||
return element.id; | ||
} | ||
|
||
// assign identity in immutable way | ||
if (this.identityMap.has(element)) { | ||
return this.identityMap.get(element)!; | ||
} | ||
|
||
// return element identity | ||
const id = new StringElement(this.generateId()); | ||
this.identityMap.set(element, id); | ||
return id; | ||
} | ||
|
||
forget<T extends Element>(element: T): boolean { | ||
if (this.identityMap.has(element)) { | ||
this.identityMap.delete(element); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
generateId() { | ||
return this.uuid.randomUUID(); | ||
} | ||
} | ||
|
||
export const defaultIdentityManager = IdentityManager({ length: 6 }); | ||
export const defaultIdentityManager = new IdentityManager(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters