2021-06-06 10:50:01 +00:00
|
|
|
import { BaseSession } from './sessions'
|
|
|
|
|
2021-06-23 12:46:16 +00:00
|
|
|
/**
|
|
|
|
* # Session Manager
|
|
|
|
*
|
|
|
|
* Sessions are instances that manage data mutations for activities such as
|
|
|
|
* dragging, transforming, drawing, etc. The session manager singleton provides
|
|
|
|
* an API with which the state's actions can begin a session and interact it.
|
|
|
|
*
|
|
|
|
* A session has a lifecycle:
|
|
|
|
*
|
|
|
|
* - it `begin`s when the session instance is created
|
|
|
|
* - it receives `update`s during the session
|
|
|
|
* - it ends either when it is `complete`d or `cancel`led
|
|
|
|
*
|
|
|
|
* Each session may produce different effects during these life cycles, according
|
|
|
|
* to its implementation. Most sessions call a command when completed.
|
|
|
|
*
|
|
|
|
* It's intended that only a single session occurs at once. This pattern helps
|
|
|
|
* ensure that we don't accidentally begin a new session before the current one
|
|
|
|
* is cancelled or completes.
|
|
|
|
*/
|
|
|
|
|
2021-06-06 10:50:01 +00:00
|
|
|
class SessionManager {
|
2021-06-23 12:46:16 +00:00
|
|
|
#current?: BaseSession
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-06-23 12:46:16 +00:00
|
|
|
/**
|
|
|
|
* Begin a new session.
|
|
|
|
* @param session A Session instance.
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* session.begin(new Sessions.EditSession(data))
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
begin(session: BaseSession) {
|
|
|
|
if (this.#current) {
|
|
|
|
throw Error(
|
|
|
|
'Cannot begin a session until the current session is complete. This error indicates a problem with the state chart.'
|
|
|
|
)
|
|
|
|
}
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-06-23 12:46:16 +00:00
|
|
|
this.#current = session
|
2021-06-06 10:50:01 +00:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2021-06-23 12:46:16 +00:00
|
|
|
/**
|
|
|
|
* Update the current session. Include the session type as a generic in order to properly type the arguments.
|
|
|
|
* @param args The arguments of the current session's `update` method.
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* session.update<Sessions.EditSession>(data, payload.change)
|
|
|
|
* ```
|
|
|
|
*/
|
2021-06-23 08:45:34 +00:00
|
|
|
update<T extends BaseSession>(...args: Parameters<T['update']>) {
|
2021-06-23 12:46:16 +00:00
|
|
|
const session = this.#current
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-06-23 12:46:16 +00:00
|
|
|
if (session === undefined) {
|
|
|
|
throw Error('No current session.')
|
|
|
|
}
|
|
|
|
|
|
|
|
session.update.call(this.#current, ...args)
|
2021-06-23 08:45:34 +00:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2021-06-23 12:46:16 +00:00
|
|
|
/**
|
|
|
|
* Complete the current session. Include the session type as a generic in order to properly type the arguments.
|
|
|
|
* @param args The arguments of the current session's `complete` method.
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* session.update<Sessions.EditSession>(data, payload.change)
|
|
|
|
* ```
|
|
|
|
*/
|
2021-06-23 08:50:51 +00:00
|
|
|
complete<T extends BaseSession>(...args: Parameters<T['complete']>) {
|
2021-06-23 15:51:22 +00:00
|
|
|
this.#current?.complete.call(this.#current, ...args)
|
2021-06-23 12:46:16 +00:00
|
|
|
this.#current = undefined
|
2021-06-23 08:45:34 +00:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2021-06-23 12:46:16 +00:00
|
|
|
/**
|
|
|
|
* Cancel the current session.
|
|
|
|
* @param args The arguments of the current session's `cancel` method.
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* session.cancel(data)
|
|
|
|
* ```
|
|
|
|
*/
|
2021-06-23 08:45:34 +00:00
|
|
|
cancel<T extends BaseSession>(...args: Parameters<T['cancel']>) {
|
2021-06-23 15:51:22 +00:00
|
|
|
this.#current?.cancel.call(this.#current, ...args)
|
2021-06-23 12:46:16 +00:00
|
|
|
this.#current = undefined
|
2021-06-23 08:45:34 +00:00
|
|
|
return this
|
|
|
|
}
|
2021-06-23 18:34:37 +00:00
|
|
|
|
|
|
|
get isInSession() {
|
|
|
|
return this.#current !== undefined
|
|
|
|
}
|
2021-06-06 10:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const session = new SessionManager()
|
|
|
|
|
|
|
|
export default session
|