UNPKG

@mui/x-internals

Version:

Utility functions for the MUI X packages (internal use only).

50 lines (49 loc) 1.25 kB
"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")); class Store { static create(state) { return new Store(state); } constructor(state) { this.state = void 0; this.listeners = void 0; this.subscribe = fn => { this.listeners.add(fn); return () => { this.listeners.delete(fn); }; }; this.getSnapshot = () => { return this.state; }; this.update = newState => { if (this.state !== newState) { this.state = newState; this.listeners.forEach(l => l(newState)); } }; this.state = state; this.listeners = new Set(); } apply(changes) { for (const key in changes) { if (!Object.is(this.state[key], changes[key])) { this.update((0, _extends2.default)({}, this.state, changes)); return; } } } set(key, value) { if (!Object.is(this.state[key], value)) { this.update((0, _extends2.default)({}, this.state, { [key]: value })); } } } exports.Store = Store;