Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@encodable/registry

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@encodable/registry - npm Package Compare versions

Comparing version 0.1.2 to 0.2.0

CHANGELOG.md

8

esm/models/createRegistryStore.js

@@ -10,4 +10,9 @@ "use strict";

/**
* Create a registry store from the given config
* @param config
*/
function createRegistryStore({
name = '',
globalId,
name,
defaultKey,

@@ -18,2 +23,3 @@ setFirstItemAsDefault = false,

return {
globalId,
name,

@@ -20,0 +26,0 @@ defaultKey,

@@ -21,6 +21,6 @@ "use strict";

* @type V Type of value
* @type W Type of value returned from loader function when using registerLoader().
* W can be either V, Promise<V> or V | Promise<V>
* Set W=V when does not support asynchronous loader.
* By default W is set to V | Promise<V> to support
* @type L Type of value returned from loader function when using `registerLoader()`.
* `L` can be either `V`, `Promise<V>` or `V | Promise<V>`
* Set `L=V` when does not support asynchronous loader.
* By default `L` is set to `V | Promise<V>` to support
* both synchronous and asynchronous loaders.

@@ -32,9 +32,14 @@ */

if (config.isGlobal) {
if (typeof config.globalId === 'undefined') {
this.store = (0, _createRegistryStore.default)(config);
} else {
this.store = (0, _globalBox.getStore)().getOrCreate(config.globalId, () => (0, _createRegistryStore.default)(config));
} else {
this.store = (0, _createRegistryStore.default)(config);
}
}
/**
* Clear all item in the registry.
* Reset default key to initial default key (if any)
*/
clear() {

@@ -46,3 +51,8 @@ this.store.items = {};

}
/**
* Check if item with the given key exists
* @param key the key to look for
*/
has(key) {

@@ -52,3 +62,9 @@ const item = this.store.items[key];

}
/**
* Register key with a value
* @param key
* @param value
*/
registerValue(key, value) {

@@ -80,3 +96,9 @@ const item = this.store.items[key];

}
/**
* Register key with a loader, a function that returns a value.
* @param key
* @param loader
*/
registerLoader(key, loader) {

@@ -108,3 +130,9 @@ const item = this.store.items[key];

}
/**
* Get value from the specified key.
* If the item contains a loader, invoke the loader and return its output.
* @param key
*/
get(key) {

@@ -125,3 +153,9 @@ const targetKey = key != null ? key : this.store.defaultKey;

}
/**
* Similar to `.get()` but wrap results in a `Promise`.
* This is useful when some items are async loaders to provide uniform output.
* @param key
*/
getAsPromise(key) {

@@ -144,7 +178,18 @@ const promise = this.store.promises[key];

}
/**
* Return the current default key.
* Default key is a fallback key to use if `.get()` was called without a key.
*/
getDefaultKey() {
return this.store.defaultKey;
}
/**
* Set default key to the specified key
* Default key is a fallback key to use if `.get()` was called without a key.
* @param key
*/
setDefaultKey(key) {

@@ -154,3 +199,8 @@ this.store.defaultKey = key;

}
/**
* Remove default key.
* Default key is a fallback key to use if `.get()` was called without a key.
*/
clearDefaultKey() {

@@ -160,3 +210,7 @@ this.store.defaultKey = undefined;

}
/**
* Return a map of all key-values in this registry.
*/
getMap() {

@@ -169,3 +223,7 @@ return this.keys().reduce((prev, key) => {

}
/**
* Same with `.getMap()` but return a `Promise` that resolves when all values are resolved.
*/
getMapAsPromise() {

@@ -179,15 +237,32 @@ const keys = this.keys();

}
/**
* Return all keys in this registry.
*/
keys() {
return Object.keys(this.store.items);
}
/**
* Return all values in this registry.
* For loaders, they are invoked and their outputs are returned.
*/
values() {
return this.keys().map(key => this.get(key));
}
/**
* Same with `.values()` but return a `Promise` that resolves when all values are resolved.
*/
valuesAsPromise() {
return Promise.all(this.keys().map(key => this.getAsPromise(key)));
}
/**
* Return all key-value entries in this registry.
*/
entries() {

@@ -199,3 +274,7 @@ return this.keys().map(key => ({

}
/**
* Same with `.entries()` but return a `Promise` that resolves when all values are resolved.
*/
entriesAsPromise() {

@@ -208,3 +287,9 @@ const keys = this.keys();

}
/**
* Remove the item with the specified key.
* Do nothing if an item with the given key does not exist.
* @param key
*/
remove(key) {

@@ -211,0 +296,0 @@ delete this.store.items[key];

@@ -10,4 +10,7 @@ "use strict";

/**
* Synchronous registry
*/
class SyncRegistry extends _Registry.default {}
exports.default = SyncRegistry;

@@ -6,2 +6,6 @@ "use strict";

/**
* Helper function for creating a singleton
* @param create factory function
*/
function makeSingleton(create) {

@@ -8,0 +12,0 @@ let singleton;

8

lib/models/createRegistryStore.d.ts

@@ -1,3 +0,7 @@

import { RegistryStoreConfig, RegistryStore } from '../types';
export default function createRegistryStore<V, W extends V | Promise<V>>({ name, defaultKey, setFirstItemAsDefault, overwritePolicy, }: RegistryStoreConfig): RegistryStore<V, W>;
import { RegistryConfig, RegistryStore } from '../types';
/**
* Create a registry store from the given config
* @param config
*/
export default function createRegistryStore<V, W extends V | Promise<V>>({ globalId, name, defaultKey, setFirstItemAsDefault, overwritePolicy, }: RegistryConfig): RegistryStore<V, W>;
//# sourceMappingURL=createRegistryStore.d.ts.map

@@ -10,4 +10,9 @@ "use strict";

/**
* Create a registry store from the given config
* @param config
*/
function createRegistryStore({
name = '',
globalId,
name,
defaultKey,

@@ -18,2 +23,3 @@ setFirstItemAsDefault = false,

return {
globalId,
name,

@@ -20,0 +26,0 @@ defaultKey,

@@ -7,33 +7,96 @@ import { RegistryStore, RegistryConfig } from '../types';

* @type V Type of value
* @type W Type of value returned from loader function when using registerLoader().
* W can be either V, Promise<V> or V | Promise<V>
* Set W=V when does not support asynchronous loader.
* By default W is set to V | Promise<V> to support
* @type L Type of value returned from loader function when using `registerLoader()`.
* `L` can be either `V`, `Promise<V>` or `V | Promise<V>`
* Set `L=V` when does not support asynchronous loader.
* By default `L` is set to `V | Promise<V>` to support
* both synchronous and asynchronous loaders.
*/
export default class Registry<V, W extends V | Promise<V> = V | Promise<V>> {
store: RegistryStore<V, W>;
export default class Registry<V, L extends V | Promise<V> = V | Promise<V>> {
readonly store: RegistryStore<V, L>;
constructor(config?: RegistryConfig);
/**
* Clear all item in the registry.
* Reset default key to initial default key (if any)
*/
clear(): this;
/**
* Check if item with the given key exists
* @param key the key to look for
*/
has(key: string): boolean;
/**
* Register key with a value
* @param key
* @param value
*/
registerValue(key: string, value: V): this;
registerLoader(key: string, loader: () => W): this;
get(key?: string): V | W | undefined;
/**
* Register key with a loader, a function that returns a value.
* @param key
* @param loader
*/
registerLoader(key: string, loader: () => L): this;
/**
* Get value from the specified key.
* If the item contains a loader, invoke the loader and return its output.
* @param key
*/
get(key?: string): V | L | undefined;
/**
* Similar to `.get()` but wrap results in a `Promise`.
* This is useful when some items are async loaders to provide uniform output.
* @param key
*/
getAsPromise(key: string): Promise<V>;
/**
* Return the current default key.
* Default key is a fallback key to use if `.get()` was called without a key.
*/
getDefaultKey(): string | undefined;
/**
* Set default key to the specified key
* Default key is a fallback key to use if `.get()` was called without a key.
* @param key
*/
setDefaultKey(key: string): this;
/**
* Remove default key.
* Default key is a fallback key to use if `.get()` was called without a key.
*/
clearDefaultKey(): this;
/**
* Return a map of all key-values in this registry.
*/
getMap(): {
[key: string]: V | W | undefined;
[key: string]: V | L | undefined;
};
/**
* Same with `.getMap()` but return a `Promise` that resolves when all values are resolved.
*/
getMapAsPromise(): Promise<{
[key: string]: V;
}>;
/**
* Return all keys in this registry.
*/
keys(): string[];
values(): (V | W | undefined)[];
/**
* Return all values in this registry.
* For loaders, they are invoked and their outputs are returned.
*/
values(): (V | L | undefined)[];
/**
* Same with `.values()` but return a `Promise` that resolves when all values are resolved.
*/
valuesAsPromise(): Promise<V[]>;
/**
* Return all key-value entries in this registry.
*/
entries(): {
key: string;
value: V | W | undefined;
value: V | L | undefined;
}[];
/**
* Same with `.entries()` but return a `Promise` that resolves when all values are resolved.
*/
entriesAsPromise(): Promise<{

@@ -43,4 +106,9 @@ key: string;

}[]>;
/**
* Remove the item with the specified key.
* Do nothing if an item with the given key does not exist.
* @param key
*/
remove(key: string): this;
}
//# sourceMappingURL=Registry.d.ts.map

@@ -21,6 +21,6 @@ "use strict";

* @type V Type of value
* @type W Type of value returned from loader function when using registerLoader().
* W can be either V, Promise<V> or V | Promise<V>
* Set W=V when does not support asynchronous loader.
* By default W is set to V | Promise<V> to support
* @type L Type of value returned from loader function when using `registerLoader()`.
* `L` can be either `V`, `Promise<V>` or `V | Promise<V>`
* Set `L=V` when does not support asynchronous loader.
* By default `L` is set to `V | Promise<V>` to support
* both synchronous and asynchronous loaders.

@@ -32,9 +32,14 @@ */

if (config.isGlobal) {
if (typeof config.globalId === 'undefined') {
this.store = (0, _createRegistryStore.default)(config);
} else {
this.store = (0, _globalBox.getStore)().getOrCreate(config.globalId, () => (0, _createRegistryStore.default)(config));
} else {
this.store = (0, _createRegistryStore.default)(config);
}
}
/**
* Clear all item in the registry.
* Reset default key to initial default key (if any)
*/
clear() {

@@ -46,3 +51,8 @@ this.store.items = {};

}
/**
* Check if item with the given key exists
* @param key the key to look for
*/
has(key) {

@@ -52,3 +62,9 @@ const item = this.store.items[key];

}
/**
* Register key with a value
* @param key
* @param value
*/
registerValue(key, value) {

@@ -80,3 +96,9 @@ const item = this.store.items[key];

}
/**
* Register key with a loader, a function that returns a value.
* @param key
* @param loader
*/
registerLoader(key, loader) {

@@ -108,3 +130,9 @@ const item = this.store.items[key];

}
/**
* Get value from the specified key.
* If the item contains a loader, invoke the loader and return its output.
* @param key
*/
get(key) {

@@ -125,3 +153,9 @@ const targetKey = key != null ? key : this.store.defaultKey;

}
/**
* Similar to `.get()` but wrap results in a `Promise`.
* This is useful when some items are async loaders to provide uniform output.
* @param key
*/
getAsPromise(key) {

@@ -144,7 +178,18 @@ const promise = this.store.promises[key];

}
/**
* Return the current default key.
* Default key is a fallback key to use if `.get()` was called without a key.
*/
getDefaultKey() {
return this.store.defaultKey;
}
/**
* Set default key to the specified key
* Default key is a fallback key to use if `.get()` was called without a key.
* @param key
*/
setDefaultKey(key) {

@@ -154,3 +199,8 @@ this.store.defaultKey = key;

}
/**
* Remove default key.
* Default key is a fallback key to use if `.get()` was called without a key.
*/
clearDefaultKey() {

@@ -160,3 +210,7 @@ this.store.defaultKey = undefined;

}
/**
* Return a map of all key-values in this registry.
*/
getMap() {

@@ -169,3 +223,7 @@ return this.keys().reduce((prev, key) => {

}
/**
* Same with `.getMap()` but return a `Promise` that resolves when all values are resolved.
*/
getMapAsPromise() {

@@ -179,15 +237,32 @@ const keys = this.keys();

}
/**
* Return all keys in this registry.
*/
keys() {
return Object.keys(this.store.items);
}
/**
* Return all values in this registry.
* For loaders, they are invoked and their outputs are returned.
*/
values() {
return this.keys().map(key => this.get(key));
}
/**
* Same with `.values()` but return a `Promise` that resolves when all values are resolved.
*/
valuesAsPromise() {
return Promise.all(this.keys().map(key => this.getAsPromise(key)));
}
/**
* Return all key-value entries in this registry.
*/
entries() {

@@ -199,3 +274,7 @@ return this.keys().map(key => ({

}
/**
* Same with `.entries()` but return a `Promise` that resolves when all values are resolved.
*/
entriesAsPromise() {

@@ -208,3 +287,9 @@ const keys = this.keys();

}
/**
* Remove the item with the specified key.
* Do nothing if an item with the given key does not exist.
* @param key
*/
remove(key) {

@@ -211,0 +296,0 @@ delete this.store.items[key];

import Registry from './Registry';
/**
* Synchronous registry
*/
export default class SyncRegistry<V> extends Registry<V, V> {
}
//# sourceMappingURL=SyncRegistry.d.ts.map

@@ -10,4 +10,7 @@ "use strict";

/**
* Synchronous registry
*/
class SyncRegistry extends _Registry.default {}
exports.default = SyncRegistry;
import OverwritePolicy from '../models/OverwritePolicy';
interface ItemWithValue<T> {
value: T;
interface ItemWithValue<V> {
/** stored value */
value: V;
}
interface ItemWithLoader<T> {
loader: () => T;
interface ItemWithLoader<L> {
/** function that returns value */
loader: () => L;
}
export interface RegistryStore<V, W extends V | Promise<V>> {
name: string;
export interface RegistryStore<V, L extends V | Promise<V>> {
/**
* If this is a global registry, it will be defined.
*/
globalId?: string;
/** name of this registry */
name?: string;
/**
* fallback key to use if `.get()` was called without a key
* This was the initial value when the registry was created.
*/
initialDefaultKey?: string;
/**
* fallback key to use if `.get()` was called without a key
* This is the current default key.
*/
defaultKey?: string;
/** set the first item registered as the default */
setFirstItemAsDefault: boolean;
/** define if registering with an existing key is allowed, prohibited or warned */
overwritePolicy: OverwritePolicy;
/** map to lookup items by key */
items: {
[key: string]: ItemWithValue<V> | ItemWithLoader<W>;
[key: string]: ItemWithValue<V> | ItemWithLoader<L>;
};
/** map to lookup promises by key */
promises: {

@@ -21,15 +40,18 @@ [key: string]: Promise<V>;

}
export interface RegistryStoreConfig {
export interface RegistryConfig {
/**
* Set this value to define a global registry.
* This will make it a true singleton and accessible via this `globalId` from any package.
*/
globalId?: string;
/** name of this registry */
name?: string;
/** fallback key to use if `.get()` was called without a key */
defaultKey?: string;
/** set the first item registered as the default */
setFirstItemAsDefault?: boolean;
/** define if registering with an existing key is allowed, prohibited or warned */
overwritePolicy?: OverwritePolicy;
}
export declare type RegistryConfig = RegistryStoreConfig & ({
isGlobal?: false;
} | {
isGlobal: true;
globalId: string;
});
export {};
//# sourceMappingURL=index.d.ts.map

@@ -0,2 +1,6 @@

/**
* Helper function for creating a singleton
* @param create factory function
*/
export default function makeSingleton<T>(create: () => T): () => T;
//# sourceMappingURL=makeSingleton.d.ts.map

@@ -6,2 +6,6 @@ "use strict";

/**
* Helper function for creating a singleton
* @param create factory function
*/
function makeSingleton(create) {

@@ -8,0 +12,0 @@ let singleton;

{
"name": "@encodable/registry",
"version": "0.1.2",
"version": "0.2.0",
"description": "Reusable registry models",

@@ -36,3 +36,3 @@ "sideEffects": false,

},
"gitHead": "e20b6d37d5e432c5050c7ba0730d7b51ef3f8b56"
"gitHead": "5eff11e2bddd7cb3ba0c713d3d4eeefd5aed10e4"
}

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

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc