Socket
Socket
Sign inDemoInstall

@lit-labs/context

Package Overview
Dependencies
Maintainers
11
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lit-labs/context - npm Package Compare versions

Comparing version 0.3.2-pre.0 to 0.3.2

2

development/lib/context-request-event.d.ts

@@ -20,3 +20,3 @@ /**

*/
export type ContextCallback<ValueType> = (value: ValueType, unsubscribe?: () => void) => void;
export declare type ContextCallback<ValueType> = (value: ValueType, unsubscribe?: () => void) => void;
/**

@@ -23,0 +23,0 @@ * Interface definition for a ContextRequest

@@ -21,2 +21,3 @@ /**

constructor(host, contextOrOptions, callback, subscribe) {
var _a;
this.subscribe = false;

@@ -62,3 +63,3 @@ this.provided = false;

this.callback = options.callback;
this.subscribe = options.subscribe ?? false;
this.subscribe = (_a = options.subscribe) !== null && _a !== void 0 ? _a : false;
}

@@ -68,3 +69,3 @@ else {

this.callback = callback;
this.subscribe = subscribe ?? false;
this.subscribe = subscribe !== null && subscribe !== void 0 ? subscribe : false;
}

@@ -71,0 +72,0 @@ this.host.addController(this);

@@ -15,5 +15,2 @@ /**

this.context = context;
// Work around TypeScript compiler bug
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, new.target.prototype);
}

@@ -20,0 +17,0 @@ }

@@ -9,3 +9,3 @@ /**

*/
export type Context<KeyType, ValueType> = KeyType & {
export declare type Context<KeyType, ValueType> = KeyType & {
__context__: ValueType;

@@ -16,7 +16,7 @@ };

*/
export type ContextKey<KeyType, ValueType> = Context<KeyType, ValueType>;
export declare type ContextKey<KeyType, ValueType> = Context<KeyType, ValueType>;
/**
* A helper type which can extract a Context value type from a Context type
*/
export type ContextType<Key extends Context<unknown, unknown>> = Key extends Context<unknown, infer ValueType> ? ValueType : never;
export declare type ContextType<Key extends Context<unknown, unknown>> = Key extends Context<unknown, infer ValueType> ? ValueType : never;
/**

@@ -23,0 +23,0 @@ * Creates a typed Context.

@@ -33,6 +33,24 @@ /**

*/
export declare function consume<ValueType>({ context: context, subscribe, }: {
export declare function consume<ValueType>({ context, subscribe, }: {
context: Context<unknown, ValueType>;
subscribe?: boolean;
}): <K extends PropertyKey>(protoOrDescriptor: ReactiveElement & Partial<Record<K, ValueType>>, name?: K) => void | any;
}): ConsumerDecorator<ValueType>;
declare type ConsumerDecorator<ValueType> = {
<K extends PropertyKey, Proto extends ReactiveElement>(protoOrDescriptor: Proto, name?: K): FieldMustMatchProvidedType<Proto, K, ValueType>;
};
declare type DecoratorReturn = void | any;
declare type FieldMustMatchProvidedType<Obj, Key extends PropertyKey, ProvidedType> = Obj extends Record<Key, infer ConsumingType> ? [
ProvidedType
] extends [ConsumingType] ? DecoratorReturn : {
message: 'provided type not assignable to consuming field';
provided: ProvidedType;
consuming: ConsumingType;
} : Obj extends Partial<Record<Key, infer ConsumingType>> ? [
ProvidedType
] extends [ConsumingType | undefined] ? DecoratorReturn : {
message: 'provided type not assignable to consuming field';
provided: ProvidedType;
consuming: ConsumingType | undefined;
} : DecoratorReturn;
export {};
//# sourceMappingURL=consume.d.ts.map

@@ -39,3 +39,3 @@ /**

*/
export function consume({ context: context, subscribe, }) {
export function consume({ context, subscribe, }) {
return decorateProperty({

@@ -42,0 +42,0 @@ finisher: (ctor, name) => {

@@ -37,3 +37,21 @@ /**

context: Context<unknown, ValueType>;
}): <K extends PropertyKey>(protoOrDescriptor: ReactiveElement & Record<K, ValueType>, name?: K) => void | any;
}): ProvideDecorator<ValueType>;
declare type ProvideDecorator<ContextType> = {
<K extends PropertyKey, Proto extends ReactiveElement>(protoOrDescriptor: Proto, name?: K): FieldMustMatchContextType<Proto, K, ContextType>;
};
declare type DecoratorReturn = void | any;
declare type FieldMustMatchContextType<Obj, Key extends PropertyKey, ContextType> = Obj extends Record<Key, infer ProvidingType> ? [
ProvidingType
] extends [ContextType] ? DecoratorReturn : {
message: 'providing field not assignable to context';
context: ContextType;
provided: ProvidingType;
} : Obj extends Partial<Record<Key, infer Providing>> ? [
Providing | undefined
] extends [ContextType] ? DecoratorReturn : {
message: 'providing field not assignable to context';
context: ContextType;
consuming: Providing | undefined;
} : DecoratorReturn;
export {};
//# sourceMappingURL=provide.d.ts.map

@@ -51,7 +51,8 @@ /**

const descriptor = Object.getOwnPropertyDescriptor(ctor.prototype, name);
const oldSetter = descriptor?.set;
const oldSetter = descriptor === null || descriptor === void 0 ? void 0 : descriptor.set;
const newDescriptor = {
...descriptor,
set: function (value) {
controllerMap.get(this)?.setValue(value);
var _a;
(_a = controllerMap.get(this)) === null || _a === void 0 ? void 0 : _a.setValue(value);
if (oldSetter) {

@@ -58,0 +59,0 @@ oldSetter.call(this, value);

@@ -15,2 +15,13 @@ /**

export class ValueNotifier {
constructor(defaultValue) {
this.callbacks = new Map();
this.updateObservers = () => {
for (const [callback, disposer] of this.callbacks) {
callback(this._value, disposer);
}
};
if (defaultValue !== undefined) {
this.value = defaultValue;
}
}
get value() {

@@ -29,13 +40,2 @@ return this._value;

}
constructor(defaultValue) {
this.callbacks = new Map();
this.updateObservers = () => {
for (const [callback, disposer] of this.callbacks) {
callback(this._value, disposer);
}
};
if (defaultValue !== undefined) {
this.value = defaultValue;
}
}
addCallback(callback, subscribe) {

@@ -42,0 +42,0 @@ if (subscribe) {

@@ -20,3 +20,3 @@ /**

*/
export type ContextCallback<ValueType> = (value: ValueType, unsubscribe?: () => void) => void;
export declare type ContextCallback<ValueType> = (value: ValueType, unsubscribe?: () => void) => void;
/**

@@ -23,0 +23,0 @@ * Interface definition for a ContextRequest

@@ -6,3 +6,3 @@ import{ContextRequestEvent as t}from"../context-request-event.js";

* SPDX-License-Identifier: BSD-3-Clause
*/class s{constructor(t,s,i,h){if(this.subscribe=!1,this.provided=!1,this.value=void 0,this.t=(t,s)=>{this.unsubscribe&&(this.unsubscribe!==s&&(this.provided=!1,this.unsubscribe()),this.subscribe||this.unsubscribe()),this.value=t,this.host.requestUpdate(),this.provided&&!this.subscribe||(this.provided=!0,this.callback&&this.callback(t,s)),this.unsubscribe=s},this.host=t,void 0!==s.context){const t=s;this.context=t.context,this.callback=t.callback,this.subscribe=t.subscribe??!1}else this.context=s,this.callback=i,this.subscribe=h??!1;this.host.addController(this)}hostConnected(){this.dispatchRequest()}hostDisconnected(){this.unsubscribe&&(this.unsubscribe(),this.unsubscribe=void 0)}dispatchRequest(){this.host.dispatchEvent(new t(this.context,this.t,this.subscribe))}}export{s as ContextConsumer};
*/class s{constructor(t,s,i,h){var e;if(this.subscribe=!1,this.provided=!1,this.value=void 0,this.t=(t,s)=>{this.unsubscribe&&(this.unsubscribe!==s&&(this.provided=!1,this.unsubscribe()),this.subscribe||this.unsubscribe()),this.value=t,this.host.requestUpdate(),this.provided&&!this.subscribe||(this.provided=!0,this.callback&&this.callback(t,s)),this.unsubscribe=s},this.host=t,void 0!==s.context){const t=s;this.context=t.context,this.callback=t.callback,this.subscribe=null!==(e=t.subscribe)&&void 0!==e&&e}else this.context=s,this.callback=i,this.subscribe=null!=h&&h;this.host.addController(this)}hostConnected(){this.dispatchRequest()}hostDisconnected(){this.unsubscribe&&(this.unsubscribe(),this.unsubscribe=void 0)}dispatchRequest(){this.host.dispatchEvent(new t(this.context,this.t,this.subscribe))}}export{s as ContextConsumer};
//# sourceMappingURL=context-consumer.js.map

@@ -6,3 +6,3 @@ import{ValueNotifier as t}from"../value-notifier.js";

* SPDX-License-Identifier: BSD-3-Clause
*/class s extends Event{constructor(t){super("context-provider",{bubbles:!0,composed:!0}),this.context=t,Object.setPrototypeOf(this,new.target.prototype)}}class e extends t{constructor(t,s,e){super(void 0!==s.context?s.initialValue:e),this.onContextRequest=t=>{t.context===this.context&&t.composedPath()[0]!==this.host&&(t.stopPropagation(),this.addCallback(t.callback,t.subscribe))},this.host=t,void 0!==s.context?this.context=s.context:this.context=s,this.attachListeners(),this.host.addController(this)}attachListeners(){this.host.addEventListener("context-request",this.onContextRequest)}hostConnected(){this.host.dispatchEvent(new s(this.context))}}export{e as ContextProvider,s as ContextProviderEvent};
*/class s extends Event{constructor(t){super("context-provider",{bubbles:!0,composed:!0}),this.context=t}}class e extends t{constructor(t,s,e){super(void 0!==s.context?s.initialValue:e),this.onContextRequest=t=>{t.context===this.context&&t.composedPath()[0]!==this.host&&(t.stopPropagation(),this.addCallback(t.callback,t.subscribe))},this.host=t,void 0!==s.context?this.context=s.context:this.context=s,this.attachListeners(),this.host.addController(this)}attachListeners(){this.host.addEventListener("context-request",this.onContextRequest)}hostConnected(){this.host.dispatchEvent(new s(this.context))}}export{e as ContextProvider,s as ContextProviderEvent};
//# sourceMappingURL=context-provider.js.map

@@ -9,3 +9,3 @@ /**

*/
export type Context<KeyType, ValueType> = KeyType & {
export declare type Context<KeyType, ValueType> = KeyType & {
__context__: ValueType;

@@ -16,7 +16,7 @@ };

*/
export type ContextKey<KeyType, ValueType> = Context<KeyType, ValueType>;
export declare type ContextKey<KeyType, ValueType> = Context<KeyType, ValueType>;
/**
* A helper type which can extract a Context value type from a Context type
*/
export type ContextType<Key extends Context<unknown, unknown>> = Key extends Context<unknown, infer ValueType> ? ValueType : never;
export declare type ContextType<Key extends Context<unknown, unknown>> = Key extends Context<unknown, infer ValueType> ? ValueType : never;
/**

@@ -23,0 +23,0 @@ * Creates a typed Context.

@@ -33,6 +33,24 @@ /**

*/
export declare function consume<ValueType>({ context: context, subscribe, }: {
export declare function consume<ValueType>({ context, subscribe, }: {
context: Context<unknown, ValueType>;
subscribe?: boolean;
}): <K extends PropertyKey>(protoOrDescriptor: ReactiveElement & Partial<Record<K, ValueType>>, name?: K) => void | any;
}): ConsumerDecorator<ValueType>;
declare type ConsumerDecorator<ValueType> = {
<K extends PropertyKey, Proto extends ReactiveElement>(protoOrDescriptor: Proto, name?: K): FieldMustMatchProvidedType<Proto, K, ValueType>;
};
declare type DecoratorReturn = void | any;
declare type FieldMustMatchProvidedType<Obj, Key extends PropertyKey, ProvidedType> = Obj extends Record<Key, infer ConsumingType> ? [
ProvidedType
] extends [ConsumingType] ? DecoratorReturn : {
message: 'provided type not assignable to consuming field';
provided: ProvidedType;
consuming: ConsumingType;
} : Obj extends Partial<Record<Key, infer ConsumingType>> ? [
ProvidedType
] extends [ConsumingType | undefined] ? DecoratorReturn : {
message: 'provided type not assignable to consuming field';
provided: ProvidedType;
consuming: ConsumingType | undefined;
} : DecoratorReturn;
export {};
//# sourceMappingURL=consume.d.ts.map

@@ -37,3 +37,21 @@ /**

context: Context<unknown, ValueType>;
}): <K extends PropertyKey>(protoOrDescriptor: ReactiveElement & Record<K, ValueType>, name?: K) => void | any;
}): ProvideDecorator<ValueType>;
declare type ProvideDecorator<ContextType> = {
<K extends PropertyKey, Proto extends ReactiveElement>(protoOrDescriptor: Proto, name?: K): FieldMustMatchContextType<Proto, K, ContextType>;
};
declare type DecoratorReturn = void | any;
declare type FieldMustMatchContextType<Obj, Key extends PropertyKey, ContextType> = Obj extends Record<Key, infer ProvidingType> ? [
ProvidingType
] extends [ContextType] ? DecoratorReturn : {
message: 'providing field not assignable to context';
context: ContextType;
provided: ProvidingType;
} : Obj extends Partial<Record<Key, infer Providing>> ? [
Providing | undefined
] extends [ContextType] ? DecoratorReturn : {
message: 'providing field not assignable to context';
context: ContextType;
consuming: Providing | undefined;
} : DecoratorReturn;
export {};
//# sourceMappingURL=provide.d.ts.map

@@ -6,3 +6,3 @@ import{decorateProperty as t}from"@lit/reactive-element/decorators/base.js";import{ContextProvider as e}from"../controllers/context-provider.js";

* SPDX-License-Identifier: BSD-3-Clause
*/function o({context:o}){return t({finisher:(t,n)=>{const r=new WeakMap;t.addInitializer((t=>{r.set(t,new e(t,{context:o}))}));const c=Object.getOwnPropertyDescriptor(t.prototype,n),i=c?.set,s={...c,set:function(t){r.get(this)?.setValue(t),i&&i.call(this,t)}};Object.defineProperty(t.prototype,n,s)}})}export{o as provide};
*/function o({context:o}){return t({finisher:(t,n)=>{const r=new WeakMap;t.addInitializer((t=>{r.set(t,new e(t,{context:o}))}));const i=Object.getOwnPropertyDescriptor(t.prototype,n),c=null==i?void 0:i.set,s={...i,set:function(t){var e;null===(e=r.get(this))||void 0===e||e.setValue(t),c&&c.call(this,t)}};Object.defineProperty(t.prototype,n,s)}})}export{o as provide};
//# sourceMappingURL=provide.js.map

@@ -6,3 +6,3 @@ /**

*/
class t{get value(){return this.o}set value(t){this.setValue(t)}setValue(t,s=!1){const i=s||!Object.is(t,this.o);this.o=t,i&&this.updateObservers()}constructor(t){this.callbacks=new Map,this.updateObservers=()=>{for(const[t,s]of this.callbacks)t(this.o,s)},void 0!==t&&(this.value=t)}addCallback(t,s){s&&(this.callbacks.has(t)||this.callbacks.set(t,(()=>{this.callbacks.delete(t)}))),t(this.value)}clearCallbacks(){this.callbacks.clear()}}export{t as ValueNotifier};
class t{constructor(t){this.callbacks=new Map,this.updateObservers=()=>{for(const[t,s]of this.callbacks)t(this.o,s)},void 0!==t&&(this.value=t)}get value(){return this.o}set value(t){this.setValue(t)}setValue(t,s=!1){const i=s||!Object.is(t,this.o);this.o=t,i&&this.updateObservers()}addCallback(t,s){s&&(this.callbacks.has(t)||this.callbacks.set(t,(()=>{this.callbacks.delete(t)}))),t(this.value)}clearCallbacks(){this.callbacks.clear()}}export{t as ValueNotifier};
//# sourceMappingURL=value-notifier.js.map
{
"name": "@lit-labs/context",
"version": "0.3.2-pre.0",
"version": "0.3.2",
"description": "Helpers and controllers for using Context protocol",

@@ -63,3 +63,2 @@ "license": "BSD-3-Clause",

"files": [
"tsconfig.json",
"src/**/*.ts"

@@ -148,9 +147,8 @@ ],

"dependencies": {
"@lit/reactive-element": "^2.0.0-pre.0",
"lit": "^3.0.0-pre.0"
"@lit/reactive-element": "^1.5.0",
"lit": "^2.7.0"
},
"devDependencies": {
"@esm-bundle/chai": "^4.3.4-fix.0",
"@lit-internal/scripts": "^1.0.1-pre.0",
"@lit-labs/testing": "^0.2.2-pre.0",
"@lit-internal/scripts": "^1.0.0",
"@lit-labs/testing": "^0.2.0",
"@types/trusted-types": "^2.0.2"

@@ -157,0 +155,0 @@ },

@@ -160,4 +160,10 @@ # @lit-labs/context

### Protected / Private Properties
You can use the `@consume` and `@provide` decorators on TypeScript `protected` and `private` properties, but be aware that there is no type checking between the type of the context and the type of the property. This is because the TypeScript compiler does not make type information for protected or private properties available to decorators. Standard `#private` properties are not supported at all.
We expect to fix all of this when we switch to standard decorators. See [#3926](https://github.com/lit/lit/issues/3926).
## Contributing
Please see [CONTRIBUTING.md](../../../CONTRIBUTING.md).

@@ -31,5 +31,2 @@ /**

super('context-provider', {bubbles: true, composed: true});
// Work around TypeScript compiler bug
// https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, new.target.prototype);
}

@@ -36,0 +33,0 @@ }

@@ -45,3 +45,3 @@ /**

export function consume<ValueType>({
context: context,
context,
subscribe,

@@ -51,9 +51,3 @@ }: {

subscribe?: boolean;
}): <K extends PropertyKey>(
// Partial<> allows for providing the value to an optional field
protoOrDescriptor: ReactiveElement & Partial<Record<K, ValueType>>,
name?: K
// Note TypeScript requires the return type to be `void|any`
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) => void | any {
}): ConsumerDecorator<ValueType> {
return decorateProperty({

@@ -74,1 +68,41 @@ finisher: (ctor: typeof ReactiveElement, name: PropertyKey) => {

}
type ConsumerDecorator<ValueType> = {
<K extends PropertyKey, Proto extends ReactiveElement>(
protoOrDescriptor: Proto,
name?: K
): FieldMustMatchProvidedType<Proto, K, ValueType>;
};
// Note TypeScript requires the return type of a decorator to be `void | any`
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type DecoratorReturn = void | any;
type FieldMustMatchProvidedType<Obj, Key extends PropertyKey, ProvidedType> =
// First we check whether the object has the property as a required field
Obj extends Record<Key, infer ConsumingType>
? // Ok, it does, just check whether it's ok to assign the
// provided type to the consuming field
[ProvidedType] extends [ConsumingType]
? DecoratorReturn
: {
message: 'provided type not assignable to consuming field';
provided: ProvidedType;
consuming: ConsumingType;
}
: // Next we check whether the object has the property as an optional field
Obj extends Partial<Record<Key, infer ConsumingType>>
? // Check assignability again. Note that we have to include undefined
// here on the consuming type because it's optional.
[ProvidedType] extends [ConsumingType | undefined]
? DecoratorReturn
: {
message: 'provided type not assignable to consuming field';
provided: ProvidedType;
consuming: ConsumingType | undefined;
}
: // Ok, the field isn't present, so either someone's using consume
// manually, i.e. not as a decorator (maybe don't do that! but if you do,
// you're on your own for your type checking, sorry), or the field is
// private, in which case we can't check it.
DecoratorReturn;

@@ -50,8 +50,3 @@ /**

context: Context<unknown, ValueType>;
}): <K extends PropertyKey>(
protoOrDescriptor: ReactiveElement & Record<K, ValueType>,
name?: K
// Note TypeScript requires the return type to be `void|any`
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) => void | any {
}): ProvideDecorator<ValueType> {
return decorateProperty({

@@ -80,1 +75,41 @@ finisher: (ctor: typeof ReactiveElement, name: PropertyKey) => {

}
type ProvideDecorator<ContextType> = {
<K extends PropertyKey, Proto extends ReactiveElement>(
protoOrDescriptor: Proto,
name?: K
): FieldMustMatchContextType<Proto, K, ContextType>;
};
// Note TypeScript requires the return type of a decorator to be `void | any`
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type DecoratorReturn = void | any;
type FieldMustMatchContextType<Obj, Key extends PropertyKey, ContextType> =
// First we check whether the object has the property as a required field
Obj extends Record<Key, infer ProvidingType>
? // Ok, it does, just check whether it's ok to assign the
// provided type to the consuming field
[ProvidingType] extends [ContextType]
? DecoratorReturn
: {
message: 'providing field not assignable to context';
context: ContextType;
provided: ProvidingType;
}
: // Next we check whether the object has the property as an optional field
Obj extends Partial<Record<Key, infer Providing>>
? // Check assignability again. Note that we have to include undefined
// here on the providing type because it's optional.
[Providing | undefined] extends [ContextType]
? DecoratorReturn
: {
message: 'providing field not assignable to context';
context: ContextType;
consuming: Providing | undefined;
}
: // Ok, the field isn't present, so either someone's using provide
// manually, i.e. not as a decorator (maybe don't do that! but if you do,
// you're on your own for your type checking, sorry), or the field is
// private, in which case we can't check it.
DecoratorReturn;

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

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

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

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