wc-context
Advanced tools
Comparing version 0.5.4 to 0.6.0
{ | ||
"name": "wc-context", | ||
"version": "0.5.4", | ||
"version": "0.6.0", | ||
"description": "Simple context for HTML custom elements", | ||
@@ -16,35 +16,4 @@ "repository": "blikblum/wc-context", | ||
], | ||
"main": "dist/wc-context.js", | ||
"module": "dist/wc-context.js", | ||
"babel": { | ||
"presets": [ | ||
[ | ||
"env", | ||
{ | ||
"targets": { | ||
"browsers": [ | ||
"ie 11", | ||
"last 2 versions", | ||
"Firefox ESR" | ||
] | ||
}, | ||
"modules": false | ||
} | ||
] | ||
], | ||
"env": { | ||
"test": { | ||
"presets": [ | ||
[ | ||
"env", | ||
{ | ||
"targets": { | ||
"node": "current" | ||
} | ||
} | ||
] | ||
] | ||
} | ||
} | ||
}, | ||
"main": "wc-context.js", | ||
"module": "wc-context.js", | ||
"jest": { | ||
@@ -59,2 +28,3 @@ "modulePathIgnorePatterns": [ | ||
"babel-jest": "^23.4.0", | ||
"babel-plugin-transform-class-properties": "^6.24.1", | ||
"babel-preset-env": "^1.7.0", | ||
@@ -61,0 +31,0 @@ "del": "^3.0.0", |
@@ -52,36 +52,2 @@ const orphanMap = {} | ||
const contextProxyHandler = { | ||
get: function (target, propName) { | ||
return target.__wcContext[propName] | ||
} | ||
} | ||
function defineContextProp (el, name) { | ||
el.__wcContext = {} | ||
Object.defineProperty(el, name, { | ||
get () { | ||
return this.__wcContextProxy || (this.__wcContextProxy = new Proxy(this, contextProxyHandler)) | ||
} | ||
}) | ||
} | ||
function defineChildContextProp (el, name) { | ||
el.__wcChildContext = {} | ||
Object.defineProperty(el, name, { | ||
get () { | ||
return this.__wcChildContext | ||
}, | ||
set (value) { | ||
const childContext = this.__wcChildContext | ||
Object.keys(value).forEach(propName => { | ||
const propValue = value[propName] | ||
if (childContext[propName] !== propValue) { | ||
updateContext(this, propName, propValue) | ||
} | ||
childContext[propName] = propValue | ||
}) | ||
} | ||
}) | ||
} | ||
function addChildContext (el, name) { | ||
@@ -110,14 +76,11 @@ const observerMap = el.__wcContextObserverMap || (el.__wcContextObserverMap = {}) | ||
function removeChildContext (el, name) { | ||
// todo: is removeContext necessary? Probably not | ||
// removeEventListener expects the listener as argument | ||
// el.removeEventListener(`context-request-${name}`) | ||
const observerMap = el.__wcContextObserverMap | ||
const observers = observerMap && observerMap[name] | ||
if (observers) { | ||
observers.forEach(observer => { | ||
observer.__wcContext[name] = undefined | ||
}) | ||
} | ||
observerMap[name] = [] | ||
function updateChildContext (el, value) { | ||
const childContext = el.__wcChildContext | ||
Object.keys(value).forEach(propName => { | ||
const propValue = value[propName] | ||
if (childContext[propName] !== propValue) { | ||
notifyContextChange(el, propName, propValue) | ||
} | ||
childContext[propName] = propValue | ||
}) | ||
} | ||
@@ -136,3 +99,3 @@ | ||
function updateContext (el, name, value) { | ||
function notifyContextChange (el, name, value) { | ||
const observerMap = el.__wcContextObserverMap | ||
@@ -153,2 +116,2 @@ const observers = observerMap && observerMap[name] | ||
export {defineContextProp, defineChildContextProp, removeChildContext, addChildContext, observeContext, unobserveContext, updateContext} | ||
export {addChildContext, updateChildContext, observeContext, unobserveContext, notifyContextChange} |
import { defineContextProp, observeContext, unobserveContext, defineChildContextProp, addChildContext, removeChildContext } from './core' | ||
import { observeContext, unobserveContext, addChildContext, updateChildContext } from './core' | ||
const withContext = (Base) => { | ||
return class extends Base { | ||
constructor () { | ||
super() | ||
defineContextProp(this, 'context') | ||
defineChildContextProp(this, 'childContext') | ||
this.__wcChildContextInitialized = false | ||
return class extends Base { | ||
__wcContext = {} | ||
__wcChildContext = {} | ||
__wcChildContextInitialized = false | ||
get context () { | ||
return this.__wcContext | ||
} | ||
get childContext () { | ||
return this.__wcChildContext | ||
} | ||
set childContext (value) { | ||
updateChildContext(this, value) | ||
} | ||
connectedCallback () { | ||
@@ -35,7 +44,2 @@ super.connectedCallback && super.connectedCallback() | ||
} | ||
const childContext = this.childContext | ||
Object.keys(childContext).forEach(key => { | ||
removeChildContext(this, key) | ||
}) | ||
} | ||
@@ -42,0 +46,0 @@ } |
/* eslint-env jest */ | ||
import { defineContextProp, addChildContext, removeChildContext, observeContext, updateContext, defineChildContextProp } from '../src/core' | ||
import {addChildContext, updateChildContext, observeContext, notifyContextChange} from '../src/core' | ||
function defineContextProp (el, name) { | ||
el.__wcContext = {} | ||
Object.defineProperty(el, name, { | ||
get () { | ||
return this.__wcContext | ||
} | ||
}) | ||
} | ||
function defineChildContextProp (el, name) { | ||
el.__wcChildContext = {} | ||
Object.defineProperty(el, name, { | ||
get () { | ||
return this.__wcChildContext | ||
}, | ||
set (value) { | ||
updateChildContext(this, value) | ||
} | ||
}) | ||
} | ||
describe('context', () => { | ||
@@ -45,6 +66,2 @@ let rootEl | ||
afterEach(() => { | ||
removeChildContext(grandfatherEl, 'key') | ||
}) | ||
test('should be acessible in all children nodes', () => { | ||
@@ -59,9 +76,2 @@ defineContextProp(parentEl, 'context') | ||
test('should not be acessible after removed', () => { | ||
defineContextProp(childEl, 'context') | ||
observeContext(childEl, 'key') | ||
removeChildContext(grandfatherEl, 'key') | ||
expect(childEl.context.key).toBeUndefined() | ||
}) | ||
test('should not be acessible in parent nodes', () => { | ||
@@ -108,6 +118,2 @@ defineContextProp(rootEl, 'context') | ||
afterEach(() => { | ||
removeChildContext(parentEl, 'key') | ||
}) | ||
test('should override parent context', () => { | ||
@@ -127,6 +133,2 @@ defineContextProp(childEl, 'context') | ||
afterEach(() => { | ||
removeChildContext(parentEl, 'key2') | ||
}) | ||
test('should not override parent context', () => { | ||
@@ -146,6 +148,2 @@ defineContextProp(childEl, 'context') | ||
afterEach(() => { | ||
removeChildContext(grandfather2El, 'key') | ||
}) | ||
test('should keep independent values', () => { | ||
@@ -177,3 +175,3 @@ defineContextProp(childEl, 'context') | ||
callback.mockClear() | ||
updateContext(grandfatherEl, 'key', 'value2') | ||
notifyContextChange(grandfatherEl, 'key', 'value2') | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
@@ -185,3 +183,3 @@ expect(callback).toHaveBeenCalledWith('key', 'value', 'value2') | ||
callback.mockClear() | ||
updateContext(grandfatherEl, 'key', 'value') | ||
notifyContextChange(grandfatherEl, 'key', 'value') | ||
expect(callback).not.toHaveBeenCalled() | ||
@@ -206,6 +204,2 @@ }) | ||
afterEach(() => { | ||
removeChildContext(grandfatherEl, 'key') | ||
}) | ||
test('should notify the observer', () => { | ||
@@ -212,0 +206,0 @@ expect(callback).toHaveBeenCalledTimes(1) |
@@ -7,2 +7,3 @@ 'use strict' | ||
const pkg = require('../package.json') | ||
const fs = require('fs') | ||
@@ -31,3 +32,19 @@ let promise = Promise.resolve() | ||
build('index.js', 'wc-context') | ||
build('skatejs.js', 'skatejs') | ||
build('lit-element.js', 'lit-element') | ||
// Copy package.json and LICENSE.txt | ||
promise = promise.then(() => { | ||
delete pkg.private | ||
delete pkg.devDependencies | ||
delete pkg.scripts | ||
delete pkg.eslintConfig | ||
delete pkg.babel | ||
delete pkg.jest | ||
fs.writeFileSync('dist/package.json', JSON.stringify(pkg, null, ' '), 'utf-8') | ||
fs.writeFileSync('dist/LICENSE.txt', fs.readFileSync('LICENSE.txt', 'utf-8'), 'utf-8') | ||
fs.writeFileSync('dist/README.md', fs.readFileSync('README.md', 'utf-8'), 'utf-8') | ||
fs.writeFileSync('dist/CHANGELOG.md', fs.readFileSync('CHANGELOG.md', 'utf-8'), 'utf-8') | ||
}) | ||
promise.catch(err => { | ||
@@ -34,0 +51,0 @@ console.error(err.stack) // eslint-disable-line no-console |
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
18
196527
10
587
2