123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- {
- class Store {
- #reducer;
- #state;
- #cbs = []
- constructor(reducer){
- this.#reducer = reducer
- this.#state = reducer(undefined, {})
- }
- getState(){
- return this.#state
- }
- subscribe(cb){
- (this.#cbs.push(cb), () => this.#cbs = this.#cbs.filter(c => c !== cb))
- }
- dispatch(action){
- let newState = this.#reducer (this.#state,action)
- if (newState !== this.#state) {
- this.#state = newState
- for ( let cb of this.#cbs) {
- cb ()
- }
- }
- }
- get state () {
- return this.#state
- }
- }
- class StoreThunk extends Store {
- dispatch(action) {
-
- if (typeof action === 'function') {
-
- return action(this.dispatch.bind(this), this.getState.bind(this))
- } else {
- super.dispatch(action)
- }
- }
- }
- }
- class RGB {
- #r;
- #g;
- #b;
- set r (newR) {
- if (typeof newR !="number" || !(newR>= 0 && newR<=255)) {
- throw new RangeError('Неправильный формат или диапазон')
- } else {
- this.#r = newR
- }
- }
- set g (newG) {
- if (typeof newG !="number" || !(newG>= 0 && newG<=255)) {
- throw new RangeError('Ошибка в типе значений')
- } else {
- this.#g = newG
- }
- }
- set b (newB) {
- if (typeof newB != 'number' || !(newB>= 0 && newB<=255)) {
- throw new RangeError('Ошибка в типе значений')
- } else {
- this.#b = newB
- }
- }
- }
|