Skip to content

Commit

Permalink
Merge pull request #11 from szhsin/feat/selector-devtool
Browse files Browse the repository at this point in the history
feat: selector devtool
  • Loading branch information
szhsin authored Dec 27, 2022
2 parents 736873f + ebdff7e commit b226783
Show file tree
Hide file tree
Showing 36 changed files with 386 additions and 163 deletions.
89 changes: 52 additions & 37 deletions dist/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@ var createState = function createState(_temp) {
});
}
};
if (middleware) set = middleware(set, get, config);
var subscribe = function subscribe(listener) {
listeners.add(listener);
return function () {
listeners["delete"](listener);
};
};
if (middleware) set = middleware({
set: set,
get: get,
subscribe: subscribe
}, config);
return {
get: get,
set: set,
subscribe: function subscribe(listener) {
listeners.add(listener);
return function () {
listeners["delete"](listener);
};
},
subscribe: subscribe,
actions: actionCreator && actionCreator(set, get)
};
};
Expand All @@ -42,44 +47,54 @@ var isEqual = function isEqual(args1, args2) {
}
return true;
};
var selector = function selector() {
for (var _len = arguments.length, items = new Array(_len), _key = 0; _key < _len; _key++) {
items[_key] = arguments[_key];
}
var lastIndex = items.length - 1;
var selectorFunc = items[lastIndex];
items.length = lastIndex;
var cache;
return {
get: function get() {
var args = items.map(function (item) {
return item.get();
});
if (cache && isEqual(args, cache.args)) return cache.ret;
var ret = selectorFunc.apply(void 0, args);
cache = {
args: args,
ret: ret
};
return ret;
},
subscribe: function subscribe(listener) {
var unsubscribers = items.map(function (item) {
return item.subscribe(listener);
});
return function () {
return unsubscribers.forEach(function (unsubscribe) {
return unsubscribe();
});
};
var createSelector = function createSelector(_temp) {
var _ref = _temp === void 0 ? {} : _temp,
plugin = _ref.plugin;
return function () {
for (var _len = arguments.length, items = new Array(_len), _key = 0; _key < _len; _key++) {
items[_key] = arguments[_key];
}
var length = items.length;
var cutoff = typeof items[length - 1] === 'function' ? length - 1 : length - 2;
var selectorFunc = items[cutoff];
var config = items[cutoff + 1];
items.length = cutoff;
var cache;
var selector = {
get: function get() {
var args = items.map(function (item) {
return item.get();
});
if (cache && isEqual(args, cache.args)) return cache.ret;
var ret = selectorFunc.apply(void 0, args);
cache = {
args: args,
ret: ret
};
return ret;
},
subscribe: function subscribe(listener) {
var unsubscribers = items.map(function (item) {
return item.subscribe(listener);
});
return function () {
return unsubscribers.forEach(function (unsubscribe) {
return unsubscribe();
});
};
}
};
plugin == null ? void 0 : plugin(selector, config);
return selector;
};
};
var selector = /*#__PURE__*/createSelector();

var useSnapshot = function useSnapshot(state) {
return shim.useSyncExternalStore(state.subscribe, state.get, state.get);
};

exports.createSelector = createSelector;
exports.createState = createState;
exports.selector = selector;
exports.state = state;
Expand Down
2 changes: 1 addition & 1 deletion dist/es/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { createState, state } from './vanilla/state.js';
export { selector } from './vanilla/selector.js';
export { createSelector, selector } from './vanilla/selector.js';
export { useSnapshot } from './react/useSnapshot.js';
71 changes: 40 additions & 31 deletions dist/es/vanilla/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,47 @@ var isEqual = function isEqual(args1, args2) {
}
return true;
};
var selector = function selector() {
for (var _len = arguments.length, items = new Array(_len), _key = 0; _key < _len; _key++) {
items[_key] = arguments[_key];
}
var lastIndex = items.length - 1;
var selectorFunc = items[lastIndex];
items.length = lastIndex;
var cache;
return {
get: function get() {
var args = items.map(function (item) {
return item.get();
});
if (cache && isEqual(args, cache.args)) return cache.ret;
var ret = selectorFunc.apply(void 0, args);
cache = {
args: args,
ret: ret
};
return ret;
},
subscribe: function subscribe(listener) {
var unsubscribers = items.map(function (item) {
return item.subscribe(listener);
});
return function () {
return unsubscribers.forEach(function (unsubscribe) {
return unsubscribe();
});
};
var createSelector = function createSelector(_temp) {
var _ref = _temp === void 0 ? {} : _temp,
plugin = _ref.plugin;
return function () {
for (var _len = arguments.length, items = new Array(_len), _key = 0; _key < _len; _key++) {
items[_key] = arguments[_key];
}
var length = items.length;
var cutoff = typeof items[length - 1] === 'function' ? length - 1 : length - 2;
var selectorFunc = items[cutoff];
var config = items[cutoff + 1];
items.length = cutoff;
var cache;
var selector = {
get: function get() {
var args = items.map(function (item) {
return item.get();
});
if (cache && isEqual(args, cache.args)) return cache.ret;
var ret = selectorFunc.apply(void 0, args);
cache = {
args: args,
ret: ret
};
return ret;
},
subscribe: function subscribe(listener) {
var unsubscribers = items.map(function (item) {
return item.subscribe(listener);
});
return function () {
return unsubscribers.forEach(function (unsubscribe) {
return unsubscribe();
});
};
}
};
plugin == null ? void 0 : plugin(selector, config);
return selector;
};
};
var selector = /*#__PURE__*/createSelector();

export { selector };
export { createSelector, selector };
19 changes: 12 additions & 7 deletions dist/es/vanilla/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ var createState = function createState(_temp) {
});
}
};
if (middleware) set = middleware(set, get, config);
var subscribe = function subscribe(listener) {
listeners.add(listener);
return function () {
listeners["delete"](listener);
};
};
if (middleware) set = middleware({
set: set,
get: get,
subscribe: subscribe
}, config);
return {
get: get,
set: set,
subscribe: function subscribe(listener) {
listeners.add(listener);
return function () {
listeners["delete"](listener);
};
},
subscribe: subscribe,
actions: actionCreator && actionCreator(set, get)
};
};
Expand Down
42 changes: 32 additions & 10 deletions dist/middleware/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,36 @@

var immer$1 = require('immer');

function _extends() {
_extends = Object.assign ? Object.assign.bind() : function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}

var applyMiddleware = function applyMiddleware() {
for (var _len = arguments.length, middlewares = new Array(_len), _key = 0; _key < _len; _key++) {
middlewares[_key] = arguments[_key];
}
return function (set, get, config) {
return middlewares.reduceRight(function (prev, curr) {
return curr(prev, get, config);
}, set);
return function (api, config) {
return middlewares.reduceRight(function (set, middleware) {
return middleware(_extends({}, api, {
set: set
}), config);
}, api.set);
};
};

var immer = function immer(set) {
var immer = function immer(_ref) {
var set = _ref.set;
return function (value) {
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
rest[_key - 1] = arguments[_key];
Expand All @@ -30,7 +48,9 @@ var persist = function persist(_temp) {
return localStorage;
} : _ref$getStorage;
var states = [];
var middleware = function middleware(set, get, config) {
var middleware = function middleware(_ref2, config) {
var set = _ref2.set,
get = _ref2.get;
var key = (config == null ? void 0 : config.key) || '';
if (!key) throw new Error('[reactish-state] state should be provided with a string `key` in the config object when the `persist` middleware is used.');
if (prefix) key = prefix + key;
Expand All @@ -41,9 +61,9 @@ var persist = function persist(_temp) {
};
};
middleware.hydrate = function () {
states.forEach(function (_ref2) {
var key = _ref2[0],
set = _ref2[1];
states.forEach(function (_ref3) {
var key = _ref3[0],
set = _ref3[1];
var value = getStorage().getItem(key);
value && set(JSON.parse(value), 'HYDRATE');
});
Expand All @@ -52,7 +72,9 @@ var persist = function persist(_temp) {
return middleware;
};

var reduxDevtools = function reduxDevtools(set, get, config) {
var reduxDevtools = function reduxDevtools(_ref, config) {
var set = _ref.set,
get = _ref.get;
if (typeof window === 'undefined' || !window.__REDUX_DEVTOOLS_EXTENSION__) return set;
var devtools = window.__REDUX_DEVTOOLS_EXTENSION__.connect({
name: config == null ? void 0 : config.key
Expand Down
16 changes: 16 additions & 0 deletions dist/middleware/es/_virtual/_rollupPluginBabelHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function _extends() {
_extends = Object.assign ? Object.assign.bind() : function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}

export { _extends as extends };
12 changes: 8 additions & 4 deletions dist/middleware/es/applyMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { extends as _extends } from './_virtual/_rollupPluginBabelHelpers.js';

var applyMiddleware = function applyMiddleware() {
for (var _len = arguments.length, middlewares = new Array(_len), _key = 0; _key < _len; _key++) {
middlewares[_key] = arguments[_key];
}
return function (set, get, config) {
return middlewares.reduceRight(function (prev, curr) {
return curr(prev, get, config);
}, set);
return function (api, config) {
return middlewares.reduceRight(function (set, middleware) {
return middleware(_extends({}, api, {
set: set
}), config);
}, api.set);
};
};

Expand Down
3 changes: 2 additions & 1 deletion dist/middleware/es/immer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { produce } from 'immer';

var immer = function immer(set) {
var immer = function immer(_ref) {
var set = _ref.set;
return function (value) {
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
rest[_key - 1] = arguments[_key];
Expand Down
10 changes: 6 additions & 4 deletions dist/middleware/es/persist.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ var persist = function persist(_temp) {
return localStorage;
} : _ref$getStorage;
var states = [];
var middleware = function middleware(set, get, config) {
var middleware = function middleware(_ref2, config) {
var set = _ref2.set,
get = _ref2.get;
var key = (config == null ? void 0 : config.key) || '';
if (!key) throw new Error('[reactish-state] state should be provided with a string `key` in the config object when the `persist` middleware is used.');
if (prefix) key = prefix + key;
Expand All @@ -17,9 +19,9 @@ var persist = function persist(_temp) {
};
};
middleware.hydrate = function () {
states.forEach(function (_ref2) {
var key = _ref2[0],
set = _ref2[1];
states.forEach(function (_ref3) {
var key = _ref3[0],
set = _ref3[1];
var value = getStorage().getItem(key);
value && set(JSON.parse(value), 'HYDRATE');
});
Expand Down
4 changes: 3 additions & 1 deletion dist/middleware/es/reduxDevtools.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var reduxDevtools = function reduxDevtools(set, get, config) {
var reduxDevtools = function reduxDevtools(_ref, config) {
var set = _ref.set,
get = _ref.get;
if (typeof window === 'undefined' || !window.__REDUX_DEVTOOLS_EXTENSION__) return set;
var devtools = window.__REDUX_DEVTOOLS_EXTENSION__.connect({
name: config == null ? void 0 : config.key
Expand Down
Loading

0 comments on commit b226783

Please sign in to comment.