react-use-sub
Advanced tools
Comparing version 0.0.2 to 0.1.0
@@ -6,51 +6,5 @@ 'use strict'; | ||
var react = require('react'); | ||
var reactDom = require('react-dom'); | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
const _update = (D, next) => { | ||
D.data = D.keys.reduce((prev, key) => { | ||
const n = next[key]; | ||
if (n !== undefined) { | ||
const nN = n; | ||
prev[key] = nN; | ||
} | ||
return prev; | ||
}, _objectSpread({}, D.data)); | ||
}; | ||
const _dispatch = D => { | ||
const _dispatch = D => reactDom.unstable_batchedUpdates(() => { | ||
D.subs.forEach(({ | ||
@@ -64,5 +18,15 @@ mapper, | ||
if (Object.keys(nowMapped).some(prop => last[prop] !== nowMapped[prop])) { | ||
update(b => !b); | ||
update(nowMapped); | ||
} | ||
}); | ||
}); | ||
const _update = (D, next) => { | ||
const result = {}; | ||
D.keys.forEach(key => { | ||
const p = D.data[key]; | ||
const n = next[key]; | ||
result[key] = n !== undefined ? n : p; | ||
}); | ||
D.data = result; | ||
}; | ||
@@ -92,4 +56,3 @@ | ||
const useSub = mapper => { | ||
const update = react.useState(true)[1]; | ||
const mapped = mapper(D.data); | ||
const [mapped, update] = react.useState(() => mapper(D.data)); | ||
const sub = react.useRef({ | ||
@@ -96,0 +59,0 @@ mapper, |
import { useState, useRef, useEffect } from 'react'; | ||
import { unstable_batchedUpdates } from 'react-dom'; | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === 'function') { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
const _update = (D, next) => { | ||
D.data = D.keys.reduce((prev, key) => { | ||
const n = next[key]; | ||
if (n !== undefined) { | ||
const nN = n; | ||
prev[key] = nN; | ||
} | ||
return prev; | ||
}, _objectSpread({}, D.data)); | ||
}; | ||
const _dispatch = D => { | ||
const _dispatch = D => unstable_batchedUpdates(() => { | ||
D.subs.forEach(({ | ||
@@ -59,5 +13,15 @@ mapper, | ||
if (Object.keys(nowMapped).some(prop => last[prop] !== nowMapped[prop])) { | ||
update(b => !b); | ||
update(nowMapped); | ||
} | ||
}); | ||
}); | ||
const _update = (D, next) => { | ||
const result = {}; | ||
D.keys.forEach(key => { | ||
const p = D.data[key]; | ||
const n = next[key]; | ||
result[key] = n !== undefined ? n : p; | ||
}); | ||
D.data = result; | ||
}; | ||
@@ -87,4 +51,3 @@ | ||
const useSub = mapper => { | ||
const update = useState(true)[1]; | ||
const mapped = mapper(D.data); | ||
const [mapped, update] = useState(() => mapper(D.data)); | ||
const sub = useRef({ | ||
@@ -91,0 +54,0 @@ mapper, |
{ | ||
"name": "react-use-sub", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Subscription based lightweight React store", | ||
@@ -36,3 +36,3 @@ "main": "dist/cjs/index.js", | ||
"peerDependencies": { | ||
"react": ">= 16.8.0" | ||
"react": "^16.8.3" | ||
}, | ||
@@ -46,2 +46,3 @@ "dependencies": {}, | ||
"@babel/preset-react": "^7.0.0", | ||
"@testing-library/react": "^9.1.4", | ||
"babel-eslint": "^10.0.1", | ||
@@ -56,8 +57,7 @@ "babel-jest": "^24.7.1", | ||
"flow-bin": "^0.98.0", | ||
"jest": "^24.7.1", | ||
"jest": "^24.9.0", | ||
"prettier": "^1.17.0", | ||
"react": "^16.8.6", | ||
"react-dom": "^16.8.6", | ||
"react-test-renderer": "^16.8.6", | ||
"react-testing-library": "^7.0.0", | ||
"react": "^16.9.0", | ||
"react-dom": "^16.9.0", | ||
"react-test-renderer": "^16.9.0", | ||
"rollup": "^1.10.1", | ||
@@ -64,0 +64,0 @@ "rollup-plugin-babel": "^4.3.2", |
@@ -1,3 +0,4 @@ | ||
type Mapper<DATA, OP = { [prop: string]: any }> = (state: DATA) => OP; | ||
type UseSubType<DATA> = <OP>(mapper: Mapper<DATA, OP>) => OP; | ||
type NonArrayObject = {[key: string]: any; [key: number]: never}; | ||
type Mapper<DATA, OP = NonArrayObject> = (state: DATA) => OP; | ||
type UseSubType<DATA> = <OP extends NonArrayObject>(mapper: Mapper<DATA, OP>) => OP; | ||
type StoreSetArg<DATA, PD> = PD | ((prev: DATA) => PD); | ||
@@ -4,0 +5,0 @@ type StoreSet<DATA> = (update: StoreSetArg<DATA, Partial<DATA>>) => void; |
// @flow | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { unstable_batchedUpdates as batch } from 'react-dom'; | ||
type Mapper<DATA, OP = { [prop: string]: any }> = (state: DATA) => OP; | ||
type Updater = (boolean | (boolean => boolean)) => void; | ||
type Sub<DATA, OP> = { mapper: Mapper<DATA, OP>, update: Updater, last: OP }; | ||
type Updater<OP> = (OP | (OP => OP)) => void; | ||
type Sub<DATA, OP> = { mapper: Mapper<DATA, OP>, update: Updater<OP>, last: OP }; | ||
type InternalDataStore<DATA> = { | ||
@@ -19,23 +20,20 @@ data: DATA, | ||
const _update = <DATA: {}>(D: InternalDataStore<DATA>, next: $Shape<DATA>): void => { | ||
D.data = D.keys.reduce( | ||
(prev, key) => { | ||
const n = next[key]; | ||
if (n !== undefined) { | ||
const nN: any = n; | ||
prev[key] = nN; | ||
const _dispatch = <DATA: {}>(D: InternalDataStore<DATA>): void => | ||
batch(() => { | ||
D.subs.forEach(({ mapper, update, last }) => { | ||
const nowMapped = mapper(D.data); | ||
if (Object.keys(nowMapped).some((prop: string) => last[prop] !== nowMapped[prop])) { | ||
update(nowMapped); | ||
} | ||
return prev; | ||
}, | ||
{ ...D.data } | ||
); | ||
}; | ||
}); | ||
}); | ||
const _dispatch = <DATA: {}>(D: InternalDataStore<DATA>): void => { | ||
D.subs.forEach(({ mapper, update, last }) => { | ||
const nowMapped = mapper(D.data); | ||
if (Object.keys(nowMapped).some((prop: string) => last[prop] !== nowMapped[prop])) { | ||
update(b => !b); | ||
} | ||
const _update = <DATA: {}>(D: InternalDataStore<DATA>, next: $Shape<DATA>): void => { | ||
const result = ({}: any); | ||
D.keys.forEach(key => { | ||
const p = D.data[key]; | ||
const n = next[key]; | ||
result[key] = n !== undefined ? n : p; | ||
}); | ||
D.data = result; | ||
}; | ||
@@ -61,4 +59,3 @@ | ||
const useSub = <OP>(mapper: Mapper<DATA, OP>): OP => { | ||
const update = useState<boolean>(true)[1]; | ||
const mapped = mapper(D.data); | ||
const [mapped, update] = useState<OP>(() => mapper(D.data)); | ||
const sub = useRef<Sub<DATA, OP>>({ mapper, update, last: mapped }); | ||
@@ -65,0 +62,0 @@ sub.current.last = mapped; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10504
191