wc-context
Advanced tools
Comparing version 0.14.1 to 1.0.0
@@ -9,2 +9,6 @@ import { | ||
/** | ||
* @typedef { import('./core.js').Context } Context | ||
*/ | ||
function setValue(host, value, instance) { | ||
@@ -19,3 +23,16 @@ instance._value = value | ||
/** | ||
* @callback ContextConsumerCallback | ||
* @param {HTMLElement} host | ||
* @param {*} [value] | ||
* @returns {void} | ||
*/ | ||
class ContextConsumer { | ||
/** | ||
* Creates an instance of ContextProvider. | ||
* @param {HTMLElement} host | ||
* @param {string | Context} context Context identifier | ||
* @param {ContextConsumerCallback} callback | ||
*/ | ||
constructor(host, context, callback) { | ||
@@ -47,2 +64,8 @@ host.addController(this) | ||
class ContextProvider { | ||
/** | ||
* Creates an instance of ContextProvider. | ||
* @param {HTMLElement} host | ||
* @param {string | Context} context Context identifier | ||
* @param {*} initialValue | ||
*/ | ||
constructor(host, context, initialValue) { | ||
@@ -49,0 +72,0 @@ if (typeof host.addController === 'function') { |
50
core.js
@@ -71,6 +71,12 @@ const noContext = Symbol('noContext') | ||
/** | ||
* @typedef Context | ||
* @typedef {Object} Context | ||
*/ | ||
/** | ||
* @typedef {Object} ContextGetter | ||
* @property {Function} getter Function that is called in provider | ||
* @property {any} [payload] Payload passed to getter | ||
*/ | ||
/** | ||
* @param {string} key Identify the context | ||
@@ -92,3 +98,3 @@ * @return {Context} | ||
* @param {Function} options.getter | ||
* @param {*} options.payload | ||
* @param {*} [options.payload] | ||
* @return {*} | ||
@@ -101,2 +107,3 @@ */ | ||
/** | ||
* @description Default context getter implementation. Just returns the payload | ||
* @param {HTMLElement} provider HTMLElement acting as a context provider | ||
@@ -111,4 +118,2 @@ * @param {*} payload Options passed to the callback | ||
/** | ||
* | ||
* | ||
* @param {HTMLElement} provider HTMLElement acting as a context provider | ||
@@ -157,2 +162,8 @@ * @param {string | Context} context Context identifier | ||
/** | ||
* @param {HTMLElement} provider HTMLElement that provides a context | ||
* @param {string | Context} context Context identifier | ||
* @param {string} caller Function caller identifier | ||
* @return {ContextGetter} | ||
*/ | ||
function getProvidedContext(provider, context, caller) { | ||
@@ -169,2 +180,7 @@ const providedContexts = provider.__wcContextProvided | ||
/** | ||
* @param {HTMLElement} provider HTMLElement that provides a context | ||
* @param {string | Context} context Context identifier | ||
* @param {*} [payload=context] Value passed to provider context getter | ||
*/ | ||
function updateContext(provider, context, payload) { | ||
@@ -194,2 +210,3 @@ const observerMap = provider.__wcContextObserverMap | ||
} | ||
function consumerSetter(consumer, value, name) { | ||
@@ -225,4 +242,3 @@ const oldValue = consumer[name] | ||
/** | ||
* | ||
* | ||
* @description Observes a context in a consumer. Optionally define how the context value is set | ||
* @param {HTMLElement} consumer HTMLElement that consumes a context | ||
@@ -257,2 +273,7 @@ * @param {string | Context} context Context identifier | ||
/** | ||
* @description Unobserves a context in a consumer | ||
* @param {HTMLElement} consumer HTMLElement that consumes a context | ||
* @param {string | Context} context Context identifier | ||
*/ | ||
function unobserveContext(consumer, context) { | ||
@@ -271,2 +292,7 @@ const providerMap = consumer.__wcContextProviderMap | ||
/** | ||
* @param {HTMLElement} provider | ||
* @param {string | Context} context Context identifier | ||
* @param {Function} callback | ||
*/ | ||
function onContextObserve(provider, context, callback) { | ||
@@ -283,2 +309,7 @@ const providedContext = getProvidedContext( | ||
/** | ||
* @param {HTMLElement} provider | ||
* @param {string | Context} context Context identifier | ||
* @param {Function} callback | ||
*/ | ||
function onContextUnobserve(provider, context, callback) { | ||
@@ -296,2 +327,9 @@ const providedContext = getProvidedContext( | ||
/** | ||
* | ||
* | ||
* @param {HTMLElement} consumer | ||
* @param {Context | string} context | ||
* @return {*} | ||
*/ | ||
async function getContext(consumer, context) { | ||
@@ -298,0 +336,0 @@ return new Promise((resolve) => { |
55
lit.js
@@ -12,2 +12,7 @@ import { noChange } from 'lit' | ||
/** | ||
* @typedef { import('./core.js').Context } Context | ||
* @typedef { import('lit').ElementPart } ElementPart | ||
**/ | ||
function getFromProperty(provider, property) { | ||
@@ -17,3 +22,9 @@ return provider[property] | ||
function createClass(Base) { | ||
/** | ||
* @template {typeof HTMLElement } BaseClass | ||
* @param {BaseClass} Base - Base element class | ||
* @returns {BaseClass} | ||
*/ | ||
function withContext(Base) { | ||
return class extends Base { | ||
@@ -102,35 +113,20 @@ static getPropertyDescriptor(name, key, options) { | ||
/** | ||
* @typedef {import('@lit/reactive-element/decorators/base.js').ClassDescriptor} ClassDescriptor | ||
*/ | ||
/** | ||
* @template ElementClass | ||
* @param {ElementClass | ClassDescriptor} classOrDescriptor - Base element class or decorator descriptor | ||
* @returns {ElementClass} | ||
*/ | ||
function withContext(classOrDescriptor) { | ||
// current state of decorators sucks. Lets abuse of duck typing | ||
if (typeof classOrDescriptor === 'function') { | ||
// constructor -> typescript decorator | ||
return createClass(classOrDescriptor) | ||
} | ||
if (classOrDescriptor.kind === 'class') { | ||
// descriptor -> spec decorator | ||
const { kind, elements } = classOrDescriptor | ||
return { | ||
kind, | ||
elements, | ||
finisher(ctor) { | ||
return createClass(ctor) | ||
}, | ||
} | ||
} | ||
} | ||
class ContextProviderDirective extends Directive { | ||
/** | ||
* @type {string | Context} | ||
*/ | ||
context | ||
/** | ||
* @type {any} | ||
* @memberof ContextProviderDirective | ||
*/ | ||
value | ||
/** | ||
* @param {ElementPart} part | ||
* @param {[Context | string, *]} [context, value] directive arguments | ||
* @return {*} | ||
* @memberof ContextProviderDirective | ||
*/ | ||
update(part, [context, value]) { | ||
@@ -143,2 +139,3 @@ if (!this.context) { | ||
} | ||
this.value = value | ||
return noChange | ||
@@ -145,0 +142,0 @@ } |
@@ -15,2 +15,8 @@ import { | ||
/** | ||
* @template {typeof HTMLElement } BaseClass | ||
* @param {BaseClass} Base - Base element class | ||
* @returns {BaseClass} | ||
*/ | ||
const withContext = (Base) => { | ||
@@ -17,0 +23,0 @@ return class extends Base { |
{ | ||
"name": "wc-context", | ||
"version": "0.14.1", | ||
"version": "1.0.0", | ||
"description": "Context for HTML custom elements / web components", | ||
@@ -37,25 +37,49 @@ "repository": "blikblum/wc-context", | ||
"devDependencies": { | ||
"@esm-bundle/chai": "4.3.4-fix.0", | ||
"@open-wc/testing": "^3.2.0", | ||
"@types/istanbul-lib-report": "^3.0.0", | ||
"@types/istanbul-reports": "^3.0.1", | ||
"@types/jsdom": "^21.1.1", | ||
"@types/node": "^20.2.5", | ||
"@web/dev-server": "^0.1.35", | ||
"eslint": "^8.32.0", | ||
"eslint-config-prettier": "^8.6.0", | ||
"jest": "^29.3.1", | ||
"jest-environment-jsdom": "^29.3.1", | ||
"lit": "^2.6.1", | ||
"prettier": "^2.5.1", | ||
"typescript": "^5.1.3", | ||
"vitepress": "^1.0.0-alpha.46", | ||
"vue": "^3.2.47" | ||
"@types/node": "^20.4.8", | ||
"@web/dev-server": "^0.3.0", | ||
"@web/test-runner": "^0.17.0", | ||
"@web/test-runner-puppeteer": "^0.14.0", | ||
"eslint": "^8.46.0", | ||
"eslint-config-prettier": "^9.0.0", | ||
"jest": "^29.6.2", | ||
"jest-environment-jsdom": "^29.6.2", | ||
"jsdom": "^22.1.0", | ||
"lit": "^2.8.0", | ||
"prettier": "^2.8.8", | ||
"typescript": "^5.1.6", | ||
"vite": "^4.4.9", | ||
"vitepress": "^1.0.0-beta.7", | ||
"vitest": "^0.34.1", | ||
"vue": "^3.3.4" | ||
}, | ||
"exports": { | ||
".": "./core.js", | ||
"./mixin.js": "./mixin.js", | ||
"./lit.js": "./lit.js", | ||
"./lit": "./lit.js", | ||
"./controllers.js": "./controllers.js", | ||
"./context-consumer.js": "./context-consumer.js", | ||
"./context-provider.js": "./context-provider.js" | ||
".": { | ||
"types": "./types/core.d.ts", | ||
"default": "./core.js" | ||
}, | ||
"./mixin.js": { | ||
"types": "./types/mixin.d.ts", | ||
"default": "./mixin.js" | ||
}, | ||
"./lit.js": { | ||
"types": "./types/lit.d.ts", | ||
"default": "./lit.js" | ||
}, | ||
"./controllers.js": { | ||
"types": "./types/controllers.d.ts", | ||
"default": "./controllers.js" | ||
}, | ||
"./context-consumer.js": { | ||
"types": "./types/context-consumer.d.ts", | ||
"default": "./context-consumer.js" | ||
}, | ||
"./context-provider.js": { | ||
"types": "./types/context-provider.d.ts", | ||
"default": "./context-provider.js" | ||
} | ||
}, | ||
@@ -71,4 +95,6 @@ "scripts": { | ||
"test": "yarn node --experimental-vm-modules $(yarn bin jest)", | ||
"types": "tsc" | ||
"test-example:wtr": "web-test-runner examples/testing/wtr.test.js --node-resolve --puppeteer", | ||
"test-example:vitest": "vitest vitest.test.js --run --environment jsdom", | ||
"types": "tsc --project tsconfig.types.json" | ||
} | ||
} |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
37704
21
848
0
21