-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
203093f
commit c92161c
Showing
7 changed files
with
502 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/** | ||
* @typedef {import("./PanelStackItem.mjs").ReactComponent} ReactComponent | ||
*/ | ||
import PanelStackItem from "./PanelStackItem.mjs"; | ||
|
||
class PanelStack { | ||
/** | ||
* @param {boolean} isActive | ||
* @param {PanelStackItem<any>[]} data | ||
* @param {(f: (data: PanelStackItem<any>[]) => PanelStackItem<any>[]) => void} updater | ||
*/ | ||
constructor(isActive, data, updater) { | ||
/** @readonly @type {boolean} */ | ||
this.isActive = isActive; | ||
|
||
/** @private @readonly @type {PanelStackItem<any>[]} */ | ||
this._data = data; | ||
|
||
/** @private @readonly @type {(f: (data: PanelStackItem<any>[]) => PanelStackItem<any>[]) => void} */ | ||
this._updater = updater; | ||
} | ||
|
||
/** | ||
* @param {PanelStackItem<any>} item | ||
* @returns {void} | ||
*/ | ||
push(item) { | ||
this._updater((data) => [item, ...data]); | ||
} | ||
|
||
/** | ||
* @template T | ||
* @param {(data: PanelStackItem<T>) => PanelStackItem<T>} f | ||
* @returns {void} | ||
*/ | ||
update(f) { | ||
this._updater((data) => { | ||
if (data.length === 0) { | ||
return data; | ||
} | ||
|
||
const [head, ...tail] = data; | ||
return [f(head), ...tail]; | ||
}); | ||
} | ||
|
||
/** | ||
* @template T | ||
* @param {ReactComponent} component | ||
* @param {(data: PanelStackItem<T>) => PanelStackItem<T>} f | ||
* @returns {void} | ||
*/ | ||
updateFor(component, f) { | ||
this._updater((data) => { | ||
return data.map((item) => { | ||
return item.component === component ? f(item) : item; | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* @returns {void} | ||
*/ | ||
pop() { | ||
this._updater((data) => { | ||
if (data.length > 1) { | ||
const [_, ...tail] = data; | ||
return tail; | ||
} | ||
|
||
return data; | ||
}); | ||
} | ||
|
||
/** | ||
* @returns {void} | ||
*/ | ||
clear() { | ||
this._updater((data) => { | ||
if (data.length > 1) { | ||
const last = data[data.length - 1]; | ||
return [last]; | ||
} | ||
|
||
return data; | ||
}); | ||
} | ||
|
||
/** | ||
* @template T | ||
* @returns {PanelStackItem<T>} | ||
*/ | ||
peek() { | ||
ensureNonEmpty(this._data); | ||
|
||
return this._data[0]; | ||
} | ||
|
||
/** | ||
* @template T | ||
* @returns {PanelStackItem<T>} | ||
*/ | ||
peekLast() { | ||
ensureNonEmpty(this._data); | ||
|
||
return this._data[this._data.length - 1]; | ||
} | ||
|
||
/** | ||
* @template T | ||
* @returns {T} | ||
*/ | ||
params() { | ||
return this.peek().state; | ||
} | ||
} | ||
|
||
/** | ||
* @param {PanelStackItem<any>[]} data | ||
*/ | ||
function ensureNonEmpty(data) { | ||
if (data.length === 0) { | ||
throw Error("PanelStack is empty!"); | ||
} | ||
} | ||
|
||
export default PanelStack; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.