@mui/x-internals
Version:
Utility functions for the MUI X packages (internal use only).
74 lines (69 loc) • 2.01 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Store = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _useStore = require("./useStore");
/* eslint-disable no-cond-assign */
class Store {
// HACK: `any` fixes adding listeners that accept partial state.
// Internal state to handle recursive `setState()` calls
static create(state) {
return new Store(state);
}
constructor(state) {
this.state = state;
this.listeners = new Set();
this.updateTick = 0;
}
subscribe = fn => {
this.listeners.add(fn);
return () => {
this.listeners.delete(fn);
};
};
/**
* Returns the current state snapshot. Meant for usage with `useSyncExternalStore`.
* If you want to access the state, use the `state` property instead.
*/
getSnapshot = () => {
return this.state;
};
setState(newState) {
this.state = newState;
this.updateTick += 1;
const currentTick = this.updateTick;
const it = this.listeners.values();
let result;
while (result = it.next(), !result.done) {
if (currentTick !== this.updateTick) {
// If the tick has changed, a recursive `setState` call has been made,
// and it has already notified all listeners.
return;
}
const listener = result.value;
listener(newState);
}
}
update(changes) {
for (const key in changes) {
if (!Object.is(this.state[key], changes[key])) {
this.setState((0, _extends2.default)({}, this.state, changes));
return;
}
}
}
set(key, value) {
if (!Object.is(this.state[key], value)) {
this.setState((0, _extends2.default)({}, this.state, {
[key]: value
}));
}
}
use = (selector, a1, a2, a3) => {
return (0, _useStore.useStore)(this, selector, a1, a2, a3);
};
}
exports.Store = Store;