Real-time collaboration within space (#411)
This commit is contained in:
+1
-1
@@ -6,5 +6,5 @@ export { mime } from "https://deno.land/x/mimetypes@v1.0.0/mod.ts";
|
||||
export { default as cacheDir } from "https://deno.land/x/cache_dir@0.2.0/mod.ts";
|
||||
export * as flags from "https://deno.land/std@0.165.0/flags/mod.ts";
|
||||
export * as esbuild from "https://deno.land/x/esbuild@v0.17.18/mod.js";
|
||||
export { denoPlugins } from "https://deno.land/x/esbuild_deno_loader@0.7.0/mod.ts";
|
||||
export { denoPlugins } from "https://deno.land/x/esbuild_deno_loader@0.8.1/mod.ts";
|
||||
export * as YAML from "https://deno.land/std@0.184.0/yaml/mod.ts";
|
||||
|
||||
@@ -10,9 +10,9 @@ export type EventHookT = {
|
||||
|
||||
export class EventHook implements Hook<EventHookT> {
|
||||
private system?: System<EventHookT>;
|
||||
public localListeners: Map<string, ((data: any) => any)[]> = new Map();
|
||||
public localListeners: Map<string, ((...args: any[]) => any)[]> = new Map();
|
||||
|
||||
addLocalListener(eventName: string, callback: (data: any) => any) {
|
||||
addLocalListener(eventName: string, callback: (...args: any[]) => any) {
|
||||
if (!this.localListeners.has(eventName)) {
|
||||
this.localListeners.set(eventName, []);
|
||||
}
|
||||
@@ -41,13 +41,13 @@ export class EventHook implements Hook<EventHookT> {
|
||||
return [...eventNames];
|
||||
}
|
||||
|
||||
async dispatchEvent(eventName: string, data?: any): Promise<any[]> {
|
||||
async dispatchEvent(eventName: string, ...args: any[]): Promise<any[]> {
|
||||
if (!this.system) {
|
||||
throw new Error("Event hook is not initialized");
|
||||
}
|
||||
const responses: any[] = [];
|
||||
for (const plug of this.system.loadedPlugs.values()) {
|
||||
const manifest = await plug.manifest;
|
||||
const manifest = plug.manifest;
|
||||
for (
|
||||
const [name, functionDef] of Object.entries(
|
||||
manifest!.functions,
|
||||
@@ -56,7 +56,7 @@ export class EventHook implements Hook<EventHookT> {
|
||||
if (functionDef.events && functionDef.events.includes(eventName)) {
|
||||
// Only dispatch functions that can run in this environment
|
||||
if (await plug.canInvoke(name)) {
|
||||
const result = await plug.invoke(name, [data]);
|
||||
const result = await plug.invoke(name, args);
|
||||
if (result !== undefined) {
|
||||
responses.push(result);
|
||||
}
|
||||
@@ -67,7 +67,7 @@ export class EventHook implements Hook<EventHookT> {
|
||||
const localListeners = this.localListeners.get(eventName);
|
||||
if (localListeners) {
|
||||
for (const localListener of localListeners) {
|
||||
const result = await Promise.resolve(localListener(data));
|
||||
const result = await Promise.resolve(localListener(...args));
|
||||
if (result) {
|
||||
responses.push(result);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
import { KV, KVStore } from "./kv_store.ts";
|
||||
|
||||
export class JSONKVStore implements KVStore {
|
||||
private data: { [key: string]: any } = {};
|
||||
|
||||
async load(path: string) {
|
||||
this.loadString(await Deno.readTextFile(path));
|
||||
}
|
||||
|
||||
loadString(jsonString: string) {
|
||||
this.data = JSON.parse(jsonString);
|
||||
}
|
||||
|
||||
async save(path: string) {
|
||||
await Deno.writeTextFile(path, JSON.stringify(this.data));
|
||||
}
|
||||
|
||||
del(key: string): Promise<void> {
|
||||
delete this.data[key];
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
deletePrefix(prefix: string): Promise<void> {
|
||||
for (const key in this.data) {
|
||||
if (key.startsWith(prefix)) {
|
||||
delete this.data[key];
|
||||
}
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
deleteAll(): Promise<void> {
|
||||
this.data = {};
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
set(key: string, value: any): Promise<void> {
|
||||
this.data[key] = value;
|
||||
return Promise.resolve();
|
||||
}
|
||||
batchSet(kvs: KV[]): Promise<void> {
|
||||
for (const kv of kvs) {
|
||||
this.data[kv.key] = kv.value;
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
batchDelete(keys: string[]): Promise<void> {
|
||||
for (const key of keys) {
|
||||
delete this.data[key];
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
batchGet(keys: string[]): Promise<any[]> {
|
||||
return Promise.resolve(keys.map((key) => this.data[key]));
|
||||
}
|
||||
get(key: string): Promise<any> {
|
||||
return Promise.resolve(this.data[key]);
|
||||
}
|
||||
has(key: string): Promise<boolean> {
|
||||
return Promise.resolve(key in this.data);
|
||||
}
|
||||
queryPrefix(keyPrefix: string): Promise<{ key: string; value: any }[]> {
|
||||
const results: { key: string; value: any }[] = [];
|
||||
for (const key in this.data) {
|
||||
if (key.startsWith(keyPrefix)) {
|
||||
results.push({ key, value: this.data[key] });
|
||||
}
|
||||
}
|
||||
return Promise.resolve(results);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user