Skip to content

Commit 593c2da

Browse files
authored
Merge pull request #1 from GeekyAnts/feat/basic-implementation
Basic implementation done
2 parents 8a226d0 + 6f499a6 commit 593c2da

File tree

12 files changed

+5330
-13
lines changed

12 files changed

+5330
-13
lines changed

example/Plugins/ClickMePlugin.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { IPlugin } from '../../.';
2+
3+
class ClickMePlugin implements IPlugin {
4+
private pluginStore;
5+
6+
init(pluginStore) {
7+
console.log('Inside init');
8+
this.pluginStore = pluginStore;
9+
}
10+
activate() {
11+
console.log('Inside activate', this.pluginStore);
12+
13+
this.pluginStore.addFunction('sendAlert', () => {
14+
alert('Testing');
15+
});
16+
}
17+
deactivate() {
18+
//
19+
}
20+
}
21+
22+
export default ClickMePlugin;

example/components/Test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as React from 'react';
2+
import { usePluginStore } from '../../.';
3+
4+
const Test = (props: any) => {
5+
const pluginStore: any = usePluginStore();
6+
console.log(pluginStore);
7+
pluginStore.executeFunction('test', 1, 2);
8+
return (
9+
<>
10+
<h1>Working</h1>{' '}
11+
<button
12+
onClick={() => {
13+
pluginStore.executeFunction('sendAlert');
14+
}}
15+
>
16+
Send Alert
17+
</button>
18+
</>
19+
);
20+
};
21+
22+
export default Test;

example/index.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import 'react-app-polyfill/ie11';
22
import * as React from 'react';
33
import * as ReactDOM from 'react-dom';
4-
import { Thing } from '../.';
4+
import { createPluginStore, PluginProvider } from '../.';
5+
import ClickMePlugin from './Plugins/ClickMePlugin';
6+
import Test from './components/Test';
57

68
const App = () => {
9+
const pluginStore = createPluginStore();
10+
pluginStore.install(new ClickMePlugin());
11+
12+
pluginStore.addFunction('test', (a, b) => {
13+
console.log('working', a, b);
14+
});
715
return (
8-
<div>
9-
<Thing />
10-
</div>
16+
<PluginProvider pluginStore={pluginStore}>
17+
<Test></Test>
18+
</PluginProvider>
1119
);
1220
};
1321

example/yarn.lock

Lines changed: 5207 additions & 0 deletions
Large diffs are not rendered by default.

src/PluginProvider.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
import { PluginStore } from './PluginStore';
3+
import PluginStoreContext from './PluginStoreContext';
4+
5+
export const PluginProvider: React.SFC<{
6+
pluginStore: PluginStore;
7+
children: any;
8+
}> = ({ pluginStore, children }) => {
9+
const PluginStore = PluginStoreContext;
10+
return (
11+
<PluginStore.Provider value={pluginStore}>{children}</PluginStore.Provider>
12+
);
13+
};

src/PluginStore.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { IPlugin } from './interfaces/IPlugin';
2+
3+
export class PluginStore {
4+
private functionArray: Map<string, any>;
5+
private pluginMap: Array<IPlugin>;
6+
7+
constructor() {
8+
this.functionArray = new Map<string, any>();
9+
this.pluginMap = [];
10+
}
11+
12+
install(plugin: IPlugin) {
13+
this.pluginMap.push(plugin);
14+
plugin.init(this);
15+
plugin.activate();
16+
}
17+
18+
addFunction(key: string, fn: any) {
19+
this.functionArray.set(key, fn);
20+
}
21+
22+
executeFunction(key: string, ...args: any) {
23+
let fn = this.functionArray.get(key);
24+
fn(...args);
25+
}
26+
}

src/PluginStoreContext.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import React from 'react';
2+
import { PluginStore } from './PluginStore';
3+
4+
const PluginStoreContext = React.createContext(new PluginStore());
5+
6+
export default PluginStoreContext;

src/hooks/createPluginStore.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { PluginStore } from '../PluginStore';
2+
3+
export function createPluginStore() {
4+
return new PluginStore();
5+
}

src/hooks/usePluginStore.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { useContext } from 'react';
2+
import PluginStoreContext from '../PluginStoreContext';
3+
4+
export function usePluginStore() {
5+
return useContext(PluginStoreContext);
6+
}

src/index.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import * as React from 'react';
2-
3-
// Delete me
4-
export const Thing = () => {
5-
return <div>the snozzberries taste like snozzberries</div>;
6-
};
1+
export { IPlugin } from './interfaces/IPlugin';
2+
export { createPluginStore } from './hooks/createPluginStore';
3+
export { PluginProvider } from './PluginProvider';
4+
export { usePluginStore } from './hooks/usePluginStore';

0 commit comments

Comments
 (0)