This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/src/DoricContext.ts

35 lines
937 B
TypeScript
Raw Normal View History

2019-12-19 13:07:33 +08:00
import { jsObtainContext } from 'doric/src/runtime/sandbox'
import { Panel } from 'doric'
2019-12-19 11:52:15 +08:00
import { DoricPlugin } from "./DoricPlugin"
2019-12-19 13:07:33 +08:00
import { createContext } from "./DoricDriver"
2019-12-19 11:52:15 +08:00
const doricContexts: Map<string, DoricContext> = new Map
2019-12-19 13:07:33 +08:00
let __contextId__ = 0
function getContextId() {
return `context_${__contextId__++}`
}
2019-12-19 11:52:15 +08:00
export function getDoricContext(contextId: string) {
return doricContexts.get(contextId)
}
export class DoricContext {
2019-12-19 13:07:33 +08:00
contextId = getContextId()
2019-12-19 11:52:15 +08:00
pluginInstances: Map<string, DoricPlugin> = new Map
2019-12-19 13:07:33 +08:00
constructor(content: string) {
createContext(this.contextId, content)
doricContexts.set(this.contextId, this)
2019-12-19 11:52:15 +08:00
}
2019-12-19 13:07:33 +08:00
get context() {
return jsObtainContext(this.contextId)
}
get panel() {
return this.context?.entity as Panel
}
getEntityMethod(method: string) {
return Reflect.get(this.panel, method, this.panel) as Function
}
2019-12-19 11:52:15 +08:00
}