Skip to content

Commit

Permalink
v1.2.0, static typing of Plugins' params
Browse files Browse the repository at this point in the history
  • Loading branch information
Ni55aN committed Apr 4, 2019
1 parent 8feb2e2 commit bdaaedf
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rete",
"version": "1.2.0-rc.10",
"version": "1.2.0",
"description": "JavaScript framework",
"main": "build/rete.common.js",
"module": "build/rete.esm.js",
Expand Down
6 changes: 3 additions & 3 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Emitter } from './emitter'
import { Validator } from './validator'
import { Events } from './events';
import { Component } from '../engine/component';
import { Plugin } from './plugin';
import { Plugin, PluginParams } from './plugin';
import { EventsTypes as DefaultEvents } from './events';

export class Context<EventsTypes> extends Emitter<EventsTypes & DefaultEvents> {

id: string;
plugins: Map<string, object>;
plugins: Map<string, any>;
components: Map<string, Component>;

constructor(id: string, events: Events) {
Expand All @@ -22,7 +22,7 @@ export class Context<EventsTypes> extends Emitter<EventsTypes & DefaultEvents> {
this.components = new Map();
}

use(plugin: Plugin, options = {}) {
use<T extends Plugin, O extends PluginParams<T>>(plugin: T, options?: O) {
if (plugin.name && this.plugins.has(plugin.name)) throw new Error(`Plugin ${plugin.name} already in use`)

plugin.install(this, options);
Expand Down
6 changes: 4 additions & 2 deletions src/core/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface Plugin {
name: string;
install: Function;
}
install: (context: any, options: any) => void;
}

export type PluginParams<T extends Plugin> = T['install'] extends (arg1: any, arg2: infer U) => any ? U : void;

0 comments on commit bdaaedf

Please sign in to comment.