parent
b9f2f844f3
commit
878de025d8
@ -1,4 +1,5 @@
|
|||||||
import Instantiable from './Instantiable.ts'
|
import Instantiable from './Instantiable.ts'
|
||||||
|
import {StaticClass} from './StaticClass.ts'
|
||||||
const DEPENDENCY_KEYS_METADATA_KEY = 'daton:di:dependencyKeys.ts'
|
const DEPENDENCY_KEYS_METADATA_KEY = 'daton:di:dependencyKeys.ts'
|
||||||
type DependencyKey = Instantiable<any> | string
|
type DependencyKey = Instantiable<any> | StaticClass<any> | string
|
||||||
export { DependencyKey, DEPENDENCY_KEYS_METADATA_KEY }
|
export { DependencyKey, DEPENDENCY_KEYS_METADATA_KEY }
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
export type StaticClass<T> = Function & {prototype: T}
|
||||||
|
|
||||||
|
export function isStaticClass<T>(something: any): something is StaticClass<T> {
|
||||||
|
return typeof something === 'function' && typeof something.prototype !== 'undefined'
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
import Session, { SessionData } from './Session.ts'
|
||||||
|
|
||||||
|
export default class MemorySession extends Session {
|
||||||
|
private _key!: string
|
||||||
|
private _data: SessionData = {}
|
||||||
|
|
||||||
|
public get_key(): string {
|
||||||
|
return this._key
|
||||||
|
}
|
||||||
|
|
||||||
|
public set_key(key: string) {
|
||||||
|
this._key = key
|
||||||
|
}
|
||||||
|
|
||||||
|
public get_data(): SessionData {
|
||||||
|
return this._data
|
||||||
|
}
|
||||||
|
|
||||||
|
public set_data(data: SessionData) {
|
||||||
|
this._data = data
|
||||||
|
}
|
||||||
|
|
||||||
|
public get_attribute(key: string): any {
|
||||||
|
return this._data[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
public set_attribute(key: string, value: any) {
|
||||||
|
this._data[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
public async persist() {}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
import { Service } from '../../../../di/src/decorator/Service.ts'
|
||||||
|
import {Collection} from '../../collection/Collection.ts'
|
||||||
|
import Session from './Session.ts'
|
||||||
|
import SessionManager, {InvalidSessionKeyError} from './SessionManager.ts'
|
||||||
|
import Utility from '../../service/utility/Utility.ts'
|
||||||
|
|
||||||
|
export type SessionRegistrant = { key: string, session: Session }
|
||||||
|
|
||||||
|
@Service()
|
||||||
|
export default class MemorySessionManager extends SessionManager {
|
||||||
|
private _sessions: Collection<SessionRegistrant> = new Collection<SessionRegistrant>()
|
||||||
|
|
||||||
|
public async has_session(key: string): Promise<boolean> {
|
||||||
|
return !!this._sessions.firstWhere('key', '=', key)
|
||||||
|
}
|
||||||
|
|
||||||
|
public async get_session(key?: string): Promise<Session> {
|
||||||
|
if ( !key ) {
|
||||||
|
const utility: Utility = this.make(Utility)
|
||||||
|
const session_key: string = key || utility.uuid()
|
||||||
|
const session = this.make(Session)
|
||||||
|
|
||||||
|
session.set_key(session_key)
|
||||||
|
await session.persist()
|
||||||
|
this._sessions.push({ key: session_key, session })
|
||||||
|
|
||||||
|
return session
|
||||||
|
}
|
||||||
|
|
||||||
|
const session = this._sessions.firstWhere('key', '=', key)
|
||||||
|
if ( !session ) throw new InvalidSessionKeyError(key)
|
||||||
|
return session.session
|
||||||
|
}
|
||||||
|
|
||||||
|
public async purge(key?: string) {
|
||||||
|
if ( key ) {
|
||||||
|
this._sessions = this._sessions.filter(session => session.key !== key)
|
||||||
|
} else {
|
||||||
|
this._sessions = new Collection<SessionRegistrant>()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
import AppClass from '../../lifecycle/AppClass.ts'
|
||||||
|
|
||||||
|
export type SessionData = { [key: string]: any }
|
||||||
|
|
||||||
|
export default abstract class Session extends AppClass {
|
||||||
|
public abstract get_key(): string
|
||||||
|
public abstract set_key(key: string): void
|
||||||
|
public abstract async persist(): Promise<void>
|
||||||
|
public abstract get_data(): SessionData
|
||||||
|
public abstract set_data(data: SessionData): void
|
||||||
|
public abstract get_attribute(key: string): any
|
||||||
|
public abstract set_attribute(key: string, value: any): void
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
import AbstractFactory from '../../../../di/src/factory/AbstractFactory.ts'
|
||||||
|
import MemorySession from './MemorySession.ts'
|
||||||
|
import Session from './Session.ts'
|
||||||
|
import {DependencyRequirement} from '../../../../di/src/type/DependencyRequirement.ts'
|
||||||
|
import {Collection} from '../../collection/Collection.ts'
|
||||||
|
|
||||||
|
// TODO support configurable session backends
|
||||||
|
|
||||||
|
export default class SessionFactory extends AbstractFactory {
|
||||||
|
constructor() {
|
||||||
|
super({})
|
||||||
|
}
|
||||||
|
|
||||||
|
produce(dependencies: any[], parameters: any[]): any {
|
||||||
|
return new MemorySession()
|
||||||
|
}
|
||||||
|
|
||||||
|
match(something: any) {
|
||||||
|
return something === Session
|
||||||
|
}
|
||||||
|
|
||||||
|
get_dependency_keys(): Collection<DependencyRequirement> {
|
||||||
|
return new Collection<DependencyRequirement>()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
import AppClass from '../../lifecycle/AppClass.ts'
|
||||||
|
import Session from './Session.ts'
|
||||||
|
|
||||||
|
export class InvalidSessionKeyError extends Error {
|
||||||
|
constructor(key: any) {
|
||||||
|
super(`Invalid session key: ${key}. No session exists.`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default abstract class SessionManager extends AppClass {
|
||||||
|
|
||||||
|
public abstract async get_session(key?: string): Promise<Session>
|
||||||
|
public abstract async has_session(key: string): Promise<boolean>
|
||||||
|
public abstract async purge(key?: string): Promise<void>
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
import AbstractFactory from '../../../../di/src/factory/AbstractFactory.ts'
|
||||||
|
import {DependencyRequirement} from '../../../../di/src/type/DependencyRequirement.ts'
|
||||||
|
import {Collection} from '../../collection/Collection.ts'
|
||||||
|
import MemorySessionManager from './MemorySessionManager.ts'
|
||||||
|
import SessionManager from './SessionManager.ts'
|
||||||
|
|
||||||
|
// TODO support configurable session backends
|
||||||
|
|
||||||
|
export default class SessionManagerFactory extends AbstractFactory {
|
||||||
|
constructor() {
|
||||||
|
super({})
|
||||||
|
}
|
||||||
|
|
||||||
|
produce(dependencies: any[], parameters: any[]): any {
|
||||||
|
return new MemorySessionManager()
|
||||||
|
}
|
||||||
|
|
||||||
|
match(something: any) {
|
||||||
|
return something === SessionManager
|
||||||
|
}
|
||||||
|
|
||||||
|
get_dependency_keys(): Collection<DependencyRequirement> {
|
||||||
|
return new Collection<DependencyRequirement>()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue