-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #519 from Green-Software-Foundation/plugin-storage
Plugin storage
- Loading branch information
Showing
9 changed files
with
84 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {pluginStorage} from '../util/plugin-storage'; | ||
import {PluginInterface} from './interface'; | ||
|
||
export type PluginStorage = { | ||
[key: string]: PluginInterface; | ||
}; | ||
|
||
export type PluginStorageInterface = ReturnType<typeof pluginStorage>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {LeveledLogMethod} from 'winston'; | ||
|
||
/** | ||
* Keeps in memory logged messages. If called with redundant message, skips logging. | ||
*/ | ||
const memoizedLogger = () => { | ||
const memory: string[] = []; | ||
|
||
return (logger: LeveledLogMethod, message: string) => { | ||
if (memory.includes(message)) { | ||
return; | ||
} | ||
|
||
memory.push(message); | ||
logger(message); | ||
}; | ||
}; | ||
|
||
/** Singleton pattern. */ | ||
export const memoizedLog = memoizedLogger(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {ERRORS} from '../util/errors'; | ||
|
||
import {PluginInterface} from '../types/interface'; | ||
import {PluginStorage} from '../types/plugin-storage'; | ||
|
||
const {PluginInitalizationError} = ERRORS; | ||
|
||
/** | ||
* Storage for maintaining plugins. | ||
*/ | ||
export const pluginStorage = () => { | ||
const storage: PluginStorage = {}; | ||
|
||
return { | ||
/** | ||
* Gets plugin by given `name`. If it's missing then throws error. | ||
*/ | ||
get: (name: string) => { | ||
const plugin = storage[name]; | ||
|
||
if (!plugin) { | ||
throw new PluginInitalizationError( | ||
`Not initalized plugin: ${name}. Check if ${name} is in 'manifest.initalize.plugins'.` | ||
); | ||
} | ||
|
||
return plugin; | ||
}, | ||
/** | ||
* Saves given `plugin` with given `name`. | ||
*/ | ||
set: function (name: string, plugin: PluginInterface) { | ||
storage[name] = plugin; | ||
|
||
return this; | ||
}, | ||
}; | ||
}; |