-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
135 additions
and
87 deletions.
There are no files selected for viewing
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import assert from './assert' | ||
|
||
// start at one to make the falsy semantics easier | ||
let uuidGenerator = 1 | ||
|
||
interface ElementKey { | ||
_uuid?: string | ||
} | ||
|
||
export default class ElementMap { | ||
_map: { | ||
[key: string]: unknown | ||
} = {} | ||
|
||
set(key: ElementKey, value: unknown) { | ||
let uuid = key._uuid | ||
if (!uuid) { | ||
key._uuid = uuid = '' + uuidGenerator++ | ||
} | ||
this._map[uuid] = value | ||
} | ||
|
||
get(key: ElementKey) { | ||
if (key._uuid) { | ||
return this._map[key._uuid] | ||
} | ||
return null | ||
} | ||
|
||
remove(key: ElementKey) { | ||
assertHasUuid(key) | ||
delete this._map[key._uuid] | ||
} | ||
} | ||
|
||
function assertHasUuid(key: ElementKey): asserts key is { _uuid: string } { | ||
assert('tried to fetch a value for an element not seen before', !!key._uuid) | ||
} |
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