@blockprotocol/core
Advanced tools
Comparing version 0.0.12 to 0.0.13
@@ -15,20 +15,2 @@ "use strict"; | ||
class CoreHandler { | ||
constructor({ element, sourceType, }) { | ||
/** an object containing registered callbacks for messages, by service and message name */ | ||
this.messageCallbacksByService = {}; | ||
/** a map of promise settlers and expected response names for requests, by requestId */ | ||
this.responseSettlersByRequestIdMap = new Map(); | ||
/** the service handlers which have been registered to receive and send messages */ | ||
this.services = new Map(); | ||
/** when this handler itself sends messages, serviceName identifies it as the core handler */ | ||
this.serviceName = "core"; | ||
this.eventListener = (event) => { | ||
this.processReceivedMessage(event); | ||
}; | ||
this.listeningElement = element; | ||
this.dispatchingElement = element; | ||
this.sourceType = sourceType; | ||
this.constructor.instanceMap.set(element, this); | ||
this.attachEventListeners(); | ||
} | ||
static isBlockProtocolMessage(message) { | ||
@@ -66,2 +48,20 @@ return (typeof message === "object" && | ||
} | ||
constructor({ element, sourceType, }) { | ||
/** an object containing registered callbacks for messages, by service and message name */ | ||
this.messageCallbacksByService = {}; | ||
/** a map of promise settlers and expected response names for requests, by requestId */ | ||
this.responseSettlersByRequestIdMap = new Map(); | ||
/** the service handlers which have been registered to receive and send messages */ | ||
this.services = new Map(); | ||
/** when this handler itself sends messages, serviceName identifies it as the core handler */ | ||
this.serviceName = "core"; | ||
this.eventListener = (event) => { | ||
this.processReceivedMessage(event); | ||
}; | ||
this.listeningElement = element; | ||
this.dispatchingElement = element; | ||
this.sourceType = sourceType; | ||
this.constructor.instanceMap.set(element, this); | ||
this.attachEventListeners(); | ||
} | ||
attachEventListeners() { | ||
@@ -68,0 +68,0 @@ if (!this.listeningElement) { |
import { HtmlBlockDefinition } from "./types"; | ||
declare type ScriptRef = { | ||
type ScriptRef = { | ||
src: string; | ||
}; | ||
declare type BlockIdentifier = string | { | ||
type BlockIdentifier = string | { | ||
blockId: string; | ||
@@ -7,0 +7,0 @@ } | ScriptRef | null | undefined; |
import { CoreHandler } from "./core-handler"; | ||
import { ServiceHandler } from "./service-handler"; | ||
export declare type UnknownRecord = Record<string, unknown>; | ||
export declare type JsonValue = null | boolean | number | string | JsonValue[] | JsonObject; | ||
export declare type JsonObject = { | ||
export type UnknownRecord = Record<string, unknown>; | ||
export type JsonValue = null | boolean | number | string | JsonValue[] | JsonObject; | ||
export type JsonObject = { | ||
[key: string]: JsonValue; | ||
@@ -10,3 +10,3 @@ }; | ||
} | ||
export declare type BlockVariant = { | ||
export type BlockVariant = { | ||
description?: string | null; | ||
@@ -18,7 +18,7 @@ icon?: string | null; | ||
}; | ||
export declare type BlockType = { | ||
export type BlockType = { | ||
entryPoint: "custom-element" | "html" | "react"; | ||
tagName?: string; | ||
}; | ||
export declare type BlockMetadataRepository = { | ||
export type BlockMetadataRepository = { | ||
type: string; | ||
@@ -28,3 +28,3 @@ url: string; | ||
} | string; | ||
export declare type BlockMetadata = { | ||
export type BlockMetadata = { | ||
/** | ||
@@ -103,3 +103,3 @@ * The name of the author of the block | ||
}; | ||
export declare type MessageError<ErrorCode extends string> = { | ||
export type MessageError<ErrorCode extends string> = { | ||
code: ErrorCode; | ||
@@ -109,7 +109,7 @@ message: string; | ||
}; | ||
export declare type MessageData<Data, ErrorCode extends string | null> = { | ||
export type MessageData<Data, ErrorCode extends string | null> = { | ||
data?: Data; | ||
errors?: ErrorCode extends string ? MessageError<ErrorCode>[] : undefined | []; | ||
}; | ||
export declare type MessageContents<Data extends any = any, ErrorCode extends string | null = null> = { | ||
export type MessageContents<Data extends any = any, ErrorCode extends string | null = null> = { | ||
messageName: string; | ||
@@ -124,10 +124,10 @@ } & MessageData<Data, ErrorCode>; | ||
} | ||
export declare type MessageCallback<InputData, InputErrorCode extends string | null, ReturnData extends any | null = null, ReturnErrorCode extends ReturnData extends null ? null : string | null = null> = { | ||
export type MessageCallback<InputData, InputErrorCode extends string | null, ReturnData extends any | null = null, ReturnErrorCode extends ReturnData extends null ? null : string | null = null> = { | ||
(messageData: MessageData<InputData, InputErrorCode>): ReturnData extends null ? void : Promise<MessageData<ReturnData, ReturnErrorCode>>; | ||
}; | ||
export declare type GenericMessageCallback = MessageCallback<any, string> | MessageCallback<any, string, any> | MessageCallback<any, null, any> | MessageCallback<any, null, any, string>; | ||
export declare type MessageCallbacksByService = { | ||
export type GenericMessageCallback = MessageCallback<any, string> | MessageCallback<any, string, any> | MessageCallback<any, null, any> | MessageCallback<any, null, any, string>; | ||
export type MessageCallbacksByService = { | ||
[serviceName: string]: Map<string, GenericMessageCallback> | undefined; | ||
}; | ||
export declare type EmbedderInitMessage = { | ||
export type EmbedderInitMessage = { | ||
[serviceName: string]: { | ||
@@ -137,3 +137,3 @@ [messageName: string]: any; | ||
}; | ||
export declare type SendMessageArgs = { | ||
export type SendMessageArgs = { | ||
partialMessage: MessageContents; | ||
@@ -144,6 +144,6 @@ requestId?: string; | ||
}; | ||
declare type PromiseConstructorFnArgs = Parameters<ConstructorParameters<PromiseConstructorLike>[0]>; | ||
export declare type PromiseResolver = PromiseConstructorFnArgs[0]; | ||
export declare type PromiseRejecter = PromiseConstructorFnArgs[1]; | ||
export declare type ResponseSettlersByRequestIdMap = Map<string, { | ||
type PromiseConstructorFnArgs = Parameters<ConstructorParameters<PromiseConstructorLike>[0]>; | ||
export type PromiseResolver = PromiseConstructorFnArgs[0]; | ||
export type PromiseRejecter = PromiseConstructorFnArgs[1]; | ||
export type ResponseSettlersByRequestIdMap = Map<string, { | ||
expectedResponseName: string; | ||
@@ -153,3 +153,3 @@ resolve: PromiseResolver; | ||
}>; | ||
export declare type ServiceMessageDefinition = { | ||
export type ServiceMessageDefinition = { | ||
messageName: string; | ||
@@ -163,3 +163,3 @@ description: string; | ||
}; | ||
export declare type ServiceDefinition = { | ||
export type ServiceDefinition = { | ||
name: string; | ||
@@ -170,3 +170,3 @@ version: string; | ||
}; | ||
export declare type HtmlBlockDefinition = { | ||
export type HtmlBlockDefinition = { | ||
/** | ||
@@ -173,0 +173,0 @@ * The url to the block's HTML file entry point, e.g. `https://example.com/my-block.html` |
@@ -12,20 +12,2 @@ import { v4 as uuid } from "uuid"; | ||
export class CoreHandler { | ||
constructor({ element, sourceType, }) { | ||
/** an object containing registered callbacks for messages, by service and message name */ | ||
this.messageCallbacksByService = {}; | ||
/** a map of promise settlers and expected response names for requests, by requestId */ | ||
this.responseSettlersByRequestIdMap = new Map(); | ||
/** the service handlers which have been registered to receive and send messages */ | ||
this.services = new Map(); | ||
/** when this handler itself sends messages, serviceName identifies it as the core handler */ | ||
this.serviceName = "core"; | ||
this.eventListener = (event) => { | ||
this.processReceivedMessage(event); | ||
}; | ||
this.listeningElement = element; | ||
this.dispatchingElement = element; | ||
this.sourceType = sourceType; | ||
this.constructor.instanceMap.set(element, this); | ||
this.attachEventListeners(); | ||
} | ||
static isBlockProtocolMessage(message) { | ||
@@ -63,2 +45,20 @@ return (typeof message === "object" && | ||
} | ||
constructor({ element, sourceType, }) { | ||
/** an object containing registered callbacks for messages, by service and message name */ | ||
this.messageCallbacksByService = {}; | ||
/** a map of promise settlers and expected response names for requests, by requestId */ | ||
this.responseSettlersByRequestIdMap = new Map(); | ||
/** the service handlers which have been registered to receive and send messages */ | ||
this.services = new Map(); | ||
/** when this handler itself sends messages, serviceName identifies it as the core handler */ | ||
this.serviceName = "core"; | ||
this.eventListener = (event) => { | ||
this.processReceivedMessage(event); | ||
}; | ||
this.listeningElement = element; | ||
this.dispatchingElement = element; | ||
this.sourceType = sourceType; | ||
this.constructor.instanceMap.set(element, this); | ||
this.attachEventListeners(); | ||
} | ||
attachEventListeners() { | ||
@@ -65,0 +65,0 @@ if (!this.listeningElement) { |
import { HtmlBlockDefinition } from "./types"; | ||
declare type ScriptRef = { | ||
type ScriptRef = { | ||
src: string; | ||
}; | ||
declare type BlockIdentifier = string | { | ||
type BlockIdentifier = string | { | ||
blockId: string; | ||
@@ -7,0 +7,0 @@ } | ScriptRef | null | undefined; |
import { CoreHandler } from "./core-handler"; | ||
import { ServiceHandler } from "./service-handler"; | ||
export declare type UnknownRecord = Record<string, unknown>; | ||
export declare type JsonValue = null | boolean | number | string | JsonValue[] | JsonObject; | ||
export declare type JsonObject = { | ||
export type UnknownRecord = Record<string, unknown>; | ||
export type JsonValue = null | boolean | number | string | JsonValue[] | JsonObject; | ||
export type JsonObject = { | ||
[key: string]: JsonValue; | ||
@@ -10,3 +10,3 @@ }; | ||
} | ||
export declare type BlockVariant = { | ||
export type BlockVariant = { | ||
description?: string | null; | ||
@@ -18,7 +18,7 @@ icon?: string | null; | ||
}; | ||
export declare type BlockType = { | ||
export type BlockType = { | ||
entryPoint: "custom-element" | "html" | "react"; | ||
tagName?: string; | ||
}; | ||
export declare type BlockMetadataRepository = { | ||
export type BlockMetadataRepository = { | ||
type: string; | ||
@@ -28,3 +28,3 @@ url: string; | ||
} | string; | ||
export declare type BlockMetadata = { | ||
export type BlockMetadata = { | ||
/** | ||
@@ -103,3 +103,3 @@ * The name of the author of the block | ||
}; | ||
export declare type MessageError<ErrorCode extends string> = { | ||
export type MessageError<ErrorCode extends string> = { | ||
code: ErrorCode; | ||
@@ -109,7 +109,7 @@ message: string; | ||
}; | ||
export declare type MessageData<Data, ErrorCode extends string | null> = { | ||
export type MessageData<Data, ErrorCode extends string | null> = { | ||
data?: Data; | ||
errors?: ErrorCode extends string ? MessageError<ErrorCode>[] : undefined | []; | ||
}; | ||
export declare type MessageContents<Data extends any = any, ErrorCode extends string | null = null> = { | ||
export type MessageContents<Data extends any = any, ErrorCode extends string | null = null> = { | ||
messageName: string; | ||
@@ -124,10 +124,10 @@ } & MessageData<Data, ErrorCode>; | ||
} | ||
export declare type MessageCallback<InputData, InputErrorCode extends string | null, ReturnData extends any | null = null, ReturnErrorCode extends ReturnData extends null ? null : string | null = null> = { | ||
export type MessageCallback<InputData, InputErrorCode extends string | null, ReturnData extends any | null = null, ReturnErrorCode extends ReturnData extends null ? null : string | null = null> = { | ||
(messageData: MessageData<InputData, InputErrorCode>): ReturnData extends null ? void : Promise<MessageData<ReturnData, ReturnErrorCode>>; | ||
}; | ||
export declare type GenericMessageCallback = MessageCallback<any, string> | MessageCallback<any, string, any> | MessageCallback<any, null, any> | MessageCallback<any, null, any, string>; | ||
export declare type MessageCallbacksByService = { | ||
export type GenericMessageCallback = MessageCallback<any, string> | MessageCallback<any, string, any> | MessageCallback<any, null, any> | MessageCallback<any, null, any, string>; | ||
export type MessageCallbacksByService = { | ||
[serviceName: string]: Map<string, GenericMessageCallback> | undefined; | ||
}; | ||
export declare type EmbedderInitMessage = { | ||
export type EmbedderInitMessage = { | ||
[serviceName: string]: { | ||
@@ -137,3 +137,3 @@ [messageName: string]: any; | ||
}; | ||
export declare type SendMessageArgs = { | ||
export type SendMessageArgs = { | ||
partialMessage: MessageContents; | ||
@@ -144,6 +144,6 @@ requestId?: string; | ||
}; | ||
declare type PromiseConstructorFnArgs = Parameters<ConstructorParameters<PromiseConstructorLike>[0]>; | ||
export declare type PromiseResolver = PromiseConstructorFnArgs[0]; | ||
export declare type PromiseRejecter = PromiseConstructorFnArgs[1]; | ||
export declare type ResponseSettlersByRequestIdMap = Map<string, { | ||
type PromiseConstructorFnArgs = Parameters<ConstructorParameters<PromiseConstructorLike>[0]>; | ||
export type PromiseResolver = PromiseConstructorFnArgs[0]; | ||
export type PromiseRejecter = PromiseConstructorFnArgs[1]; | ||
export type ResponseSettlersByRequestIdMap = Map<string, { | ||
expectedResponseName: string; | ||
@@ -153,3 +153,3 @@ resolve: PromiseResolver; | ||
}>; | ||
export declare type ServiceMessageDefinition = { | ||
export type ServiceMessageDefinition = { | ||
messageName: string; | ||
@@ -163,3 +163,3 @@ description: string; | ||
}; | ||
export declare type ServiceDefinition = { | ||
export type ServiceDefinition = { | ||
name: string; | ||
@@ -170,3 +170,3 @@ version: string; | ||
}; | ||
export declare type HtmlBlockDefinition = { | ||
export type HtmlBlockDefinition = { | ||
/** | ||
@@ -173,0 +173,0 @@ * The url to the block's HTML file entry point, e.g. `https://example.com/my-block.html` |
{ | ||
"name": "@blockprotocol/core", | ||
"version": "0.0.12", | ||
"version": "0.0.13", | ||
"description": "Implementation of the Block Protocol Core specification for blocks and embedding applications", | ||
@@ -17,3 +17,3 @@ "keywords": [ | ||
"url": "git@github.com:blockprotocol/blockprotocol.git", | ||
"directory": "packages/@blockprotocol/core" | ||
"directory": "libs/@blockprotocol/core" | ||
}, | ||
@@ -36,2 +36,4 @@ "license": "MIT", | ||
"clean": "rimraf ./dist/", | ||
"fix:eslint": "eslint --fix .", | ||
"lint:eslint": "eslint --report-unused-disable-directives .", | ||
"lint:tsc": "tsc --noEmit" | ||
@@ -44,6 +46,8 @@ }, | ||
"devDependencies": { | ||
"@local/eslint-config": "0.0.0-private", | ||
"@local/tsconfig": "0.0.0-private", | ||
"eslint": "8.31.0", | ||
"rimraf": "^3.0.2", | ||
"typescript": "4.8.2" | ||
"typescript": "4.9.4" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
226716
47
5