feat(extensions): implementation

This commit is contained in:
Wroud
2020-04-26 20:01:00 +03:00
parent ee3ca9de12
commit 38fe42fa10
5 changed files with 65 additions and 0 deletions
@@ -0,0 +1,8 @@
{
"name": "@dbeaver/core/extensions",
"private": true,
"sideEffects": false,
"main": "../dist/extensions",
"module": "../dist/extensions",
"types": "../dist/types/extensions"
}
@@ -0,0 +1,15 @@
/*
* cloudbeaver - Cloud Database Manager
* Copyright (C) 2020 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { EXTENSION_SYMBOL, EXTENSION_TYPE_SYMBOL } from './constants';
export interface IExtension<T> {
[EXTENSION_SYMBOL]: symbol;
[EXTENSION_TYPE_SYMBOL]?: T;
}
@@ -0,0 +1,10 @@
/*
* cloudbeaver - Cloud Database Manager
* Copyright (C) 2020 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
export const EXTENSION_SYMBOL = Symbol('@extension');
export const EXTENSION_TYPE_SYMBOL = Symbol('@extension/type');
@@ -0,0 +1,30 @@
/*
* cloudbeaver - Cloud Database Manager
* Copyright (C) 2020 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0.
* you may not use this file except in compliance with the License.
*/
import { EXTENSION_SYMBOL } from './constants';
import { IExtension } from './IExtension';
export function createExtension<T>(
extension: object,
key: symbol
): IExtension<T> {
return Object.assign(extension, { [EXTENSION_SYMBOL]: key });
}
export function isExtension<T>(
obj: any,
key?: symbol,
): obj is IExtension<T> {
if (!(EXTENSION_SYMBOL in obj)) {
return false;
}
if (key && obj[EXTENSION_SYMBOL] !== key) {
return false;
}
return true;
}
@@ -0,0 +1,2 @@
export * from './IExtension';
export * from './createExtension';