This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from vim-denops/singleton
Make denops singleton and optimize APIs
- Loading branch information
Showing
5 changed files
with
99 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Context object which is exposed in l: namespace | ||
*/ | ||
export interface Context { | ||
[key: string]: unknown; | ||
} | ||
|
||
/** | ||
* API of denops core | ||
*/ | ||
export interface Api { | ||
/** | ||
* Call {func} with given {args} and return the result | ||
*/ | ||
call(func: string, ...args: unknown[]): Promise<unknown>; | ||
|
||
/** | ||
* Execute {cmd} under the {context} | ||
*/ | ||
cmd(cmd: string, context: Context): Promise<void>; | ||
|
||
/** | ||
* Evaluate {expr} under the {context} and return result | ||
*/ | ||
eval(expr: string, context: Context): Promise<unknown>; | ||
} | ||
|
||
/** | ||
* Return if a given value is Context object or not | ||
*/ | ||
export function isContext(value: unknown): value is Context { | ||
const t = typeof value; | ||
return t === "function" || (t === "object" && value !== null); | ||
} |
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,29 @@ | ||
// deno-lint-ignore no-explicit-any | ||
const that = globalThis as any; | ||
|
||
// Thread-local cache | ||
that.denopsCache = {}; | ||
|
||
/** | ||
* Get a value from the thread-local context | ||
*/ | ||
export function getCache<T = unknown>(name: string): T | undefined { | ||
return that.denopsCache[name]; | ||
} | ||
|
||
/** | ||
* Set a value to the thread-local cache | ||
*/ | ||
export function setCache<T>(name: string, value: T): void { | ||
that.denopsCache[name] = value; | ||
} | ||
|
||
/** | ||
* Get a value from the thread-local cache or set if no value exist | ||
*/ | ||
export function getCacheOrElse<T>(name: string, factory: () => T): T { | ||
if (!that.denopsCache[name]) { | ||
that.denopsCache[name] = factory(); | ||
} | ||
return that.denopsCache[name]; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export { Queue } from "https://deno.land/x/[email protected]/mod.ts"; | ||
export { Session } from "https://deno.land/x/msgpack_rpc@v2.4/mod.ts"; | ||
export type { Dispatcher } from "https://deno.land/x/msgpack_rpc@v2.4/mod.ts"; | ||
export { Session } from "https://deno.land/x/msgpack_rpc@v2.6/mod.ts"; | ||
export type { Dispatcher } from "https://deno.land/x/msgpack_rpc@v2.6/mod.ts"; | ||
export { copy as copyBytes } from "https://deno.land/[email protected]/bytes/mod.ts"; |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from "./api.ts"; | ||
export * from "./cache.ts"; | ||
export * from "./denops.ts"; | ||
export * from "./worker.ts"; |