2023-04-25 11:01:25 +00:00
|
|
|
/** @internal */
|
|
|
|
export function hasOwnProperty(obj: object, key: string): boolean {
|
|
|
|
return Object.prototype.hasOwnProperty.call(obj, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
export function getOwnProperty<K extends string, V>(
|
|
|
|
obj: Partial<Record<K, V>>,
|
|
|
|
key: K
|
|
|
|
): V | undefined
|
|
|
|
/** @internal */
|
|
|
|
export function getOwnProperty(obj: object, key: string): unknown
|
|
|
|
/** @internal */
|
|
|
|
export function getOwnProperty(obj: object, key: string): unknown {
|
|
|
|
if (!hasOwnProperty(obj, key)) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
// @ts-expect-error we know the property exists
|
|
|
|
return obj[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An alias for `Object.keys` that treats the object as a map and so preserves the type of the keys.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
2024-02-07 16:02:22 +00:00
|
|
|
export function objectMapKeys<Key extends string>(object: {
|
|
|
|
readonly [K in Key]: unknown
|
|
|
|
}): Array<Key> {
|
2023-04-25 11:01:25 +00:00
|
|
|
return Object.keys(object) as Key[]
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An alias for `Object.values` that treats the object as a map and so preserves the type of the
|
|
|
|
* keys.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
2024-02-07 16:02:22 +00:00
|
|
|
export function objectMapValues<Key extends string, Value>(object: {
|
|
|
|
[K in Key]: Value
|
|
|
|
}): Array<Value> {
|
2023-04-25 11:01:25 +00:00
|
|
|
return Object.values(object) as Value[]
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An alias for `Object.entries` that treats the object as a map and so preserves the type of the
|
|
|
|
* keys.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
2024-02-07 16:02:22 +00:00
|
|
|
export function objectMapEntries<Key extends string, Value>(object: {
|
|
|
|
[K in Key]: Value
|
|
|
|
}): Array<[Key, Value]> {
|
2023-04-25 11:01:25 +00:00
|
|
|
return Object.entries(object) as [Key, Value][]
|
|
|
|
}
|
derived presence state (#1204)
This PR adds
- A new `TLInstancePresence` record type, to collect info about the
presence state in a particular instance of the editor. This will
eventually be used to sync presence data instead of sending
instance-only state across the wire.
- **Record Scopes**
`RecordType` now has a `scope` property which can be one of three
things:
- `document`: the record belongs to the document and should be synced
and persisted freely. Currently: `TLDocument`, `TLPage`, `TLShape`, and
`TLAsset`
- `instance`: the record belongs to a single instance of the store and
should not be synced at all. It should not be persisted directly in most
cases, but rather compiled into a kind of 'instance configuration' to
store alongside the local document data so that when reopening the
associated document it can remember some of the previous instance state.
Currently: `TLInstance`, `TLInstancePageState`, `TLCamera`, `TLUser`,
`TLUserDocument`, `TLUserPresence`
- `presence`: the record belongs to a single instance of the store and
should not be persisted, but may be synced using the special presence
sync protocol. Currently just `TLInstancePresence`
This sets us up for the following changes, which are gonna be pretty
high-impact in terms of integrating tldraw into existing systems:
- Removing `instanceId` as a config option. Each instance gets a
randomly generated ID.
- We'd replace it with an `instanceConfig` option that has stuff like
selectedIds, camera positions, and so on. Then it's up to library users
to get and reinstate the instance config at persistence boundaries.
- Removing `userId` as config option, and removing the `TLUser` type
altogether.
- We might need to revisit when doing auth-enabled features like locking
shapes, but I suspect that will be separate.
2023-04-27 18:03:19 +00:00
|
|
|
|
2023-05-24 11:25:41 +00:00
|
|
|
/**
|
|
|
|
* An alias for `Object.fromEntries` that treats the object as a map and so preserves the type of the
|
|
|
|
* keys.
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export function objectMapFromEntries<Key extends string, Value>(
|
|
|
|
entries: ReadonlyArray<readonly [Key, Value]>
|
|
|
|
): { [K in Key]: Value } {
|
|
|
|
return Object.fromEntries(entries) as { [K in Key]: Value }
|
|
|
|
}
|
|
|
|
|
derived presence state (#1204)
This PR adds
- A new `TLInstancePresence` record type, to collect info about the
presence state in a particular instance of the editor. This will
eventually be used to sync presence data instead of sending
instance-only state across the wire.
- **Record Scopes**
`RecordType` now has a `scope` property which can be one of three
things:
- `document`: the record belongs to the document and should be synced
and persisted freely. Currently: `TLDocument`, `TLPage`, `TLShape`, and
`TLAsset`
- `instance`: the record belongs to a single instance of the store and
should not be synced at all. It should not be persisted directly in most
cases, but rather compiled into a kind of 'instance configuration' to
store alongside the local document data so that when reopening the
associated document it can remember some of the previous instance state.
Currently: `TLInstance`, `TLInstancePageState`, `TLCamera`, `TLUser`,
`TLUserDocument`, `TLUserPresence`
- `presence`: the record belongs to a single instance of the store and
should not be persisted, but may be synced using the special presence
sync protocol. Currently just `TLInstancePresence`
This sets us up for the following changes, which are gonna be pretty
high-impact in terms of integrating tldraw into existing systems:
- Removing `instanceId` as a config option. Each instance gets a
randomly generated ID.
- We'd replace it with an `instanceConfig` option that has stuff like
selectedIds, camera positions, and so on. Then it's up to library users
to get and reinstate the instance config at persistence boundaries.
- Removing `userId` as config option, and removing the `TLUser` type
altogether.
- We might need to revisit when doing auth-enabled features like locking
shapes, but I suspect that will be separate.
2023-04-27 18:03:19 +00:00
|
|
|
/**
|
|
|
|
* Filters an object using a predicate function.
|
|
|
|
* @returns a new object with only the entries that pass the predicate
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export function filterEntries<Key extends string, Value>(
|
|
|
|
object: { [K in Key]: Value },
|
|
|
|
predicate: (key: Key, value: Value) => boolean
|
|
|
|
): { [K in Key]: Value } {
|
|
|
|
const result: { [K in Key]?: Value } = {}
|
|
|
|
let didChange = false
|
|
|
|
for (const [key, value] of objectMapEntries(object)) {
|
|
|
|
if (predicate(key, value)) {
|
|
|
|
result[key] = value
|
|
|
|
} else {
|
|
|
|
didChange = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return didChange ? (result as { [K in Key]: Value }) : object
|
|
|
|
}
|
2023-06-12 14:04:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps the values of one object map to another.
|
|
|
|
* @returns a new object with the entries mapped
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export function mapObjectMapValues<Key extends string, ValueBefore, ValueAfter>(
|
|
|
|
object: { readonly [K in Key]: ValueBefore },
|
|
|
|
mapper: (key: Key, value: ValueBefore) => ValueAfter
|
|
|
|
): { [K in Key]: ValueAfter } {
|
|
|
|
const result = {} as { [K in Key]: ValueAfter }
|
|
|
|
for (const [key, value] of objectMapEntries(object)) {
|
|
|
|
const newValue = mapper(key, value)
|
|
|
|
result[key] = newValue
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2023-12-01 16:48:30 +00:00
|
|
|
|
|
|
|
/** @internal */
|
|
|
|
export function areObjectsShallowEqual<T extends Record<string, unknown>>(
|
|
|
|
obj1: T,
|
|
|
|
obj2: T
|
|
|
|
): boolean {
|
|
|
|
if (obj1 === obj2) return true
|
|
|
|
const keys1 = new Set(Object.keys(obj1))
|
|
|
|
const keys2 = new Set(Object.keys(obj2))
|
|
|
|
if (keys1.size !== keys2.size) return false
|
|
|
|
for (const key of keys1) {
|
|
|
|
if (!keys2.has(key)) return false
|
|
|
|
if (!Object.is(obj1[key], obj2[key])) return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|