Skip to content

Commit

Permalink
🦄 refactor: convert #xxx to _xxx
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Jan 30, 2024
1 parent ab5dd26 commit 1a69daa
Show file tree
Hide file tree
Showing 33 changed files with 509 additions and 510 deletions.
30 changes: 15 additions & 15 deletions packages/chili-core/src/base/asyncController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,51 @@ export interface AsyncResult {
}

export class AsyncController implements IDisposable {
readonly #failHandles: ((state: AsyncResult) => void)[] = [];
readonly #successHandles: ((state: AsyncResult) => void)[] = [];
private readonly _failHandles: ((state: AsyncResult) => void)[] = [];
private readonly _successHandles: ((state: AsyncResult) => void)[] = [];

#result: AsyncResult | undefined;
private _result: AsyncResult | undefined;
get result() {
return this.#result;
return this._result;
}

reset() {
this.#result = undefined;
this._result = undefined;
}

fail = (message?: string) => {
this.handle(this.#failHandles, "fail", message);
this.handle(this._failHandles, "fail", message);
};

cancel = (message?: string) => {
this.handle(this.#failHandles, "cancel", message);
this.handle(this._failHandles, "cancel", message);
};

success = (message?: string) => {
this.handle(this.#successHandles, "success", message);
this.handle(this._successHandles, "success", message);
};

private handle(
handlers: ((result: AsyncResult) => void)[],
status: "success" | "cancel" | "fail",
message?: string,
) {
if (this.#result === undefined) {
this.#result = { status, message };
handlers.forEach((x) => x(this.#result!));
if (this._result === undefined) {
this._result = { status, message };
handlers.forEach((x) => x(this._result!));
}
}

onCancelled(listener: (result: AsyncResult) => void): void {
this.#failHandles.push(listener);
this._failHandles.push(listener);
}

onCompleted(listener: (result: AsyncResult) => void): void {
this.#successHandles.push(listener);
this._successHandles.push(listener);
}

dispose() {
this.#failHandles.length = 0;
this.#successHandles.length = 0;
this._failHandles.length = 0;
this._successHandles.length = 0;
}
}
58 changes: 29 additions & 29 deletions packages/chili-core/src/base/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ export interface ICollectionChanged {
}

export class ObservableCollection<T> implements ICollectionChanged, IDisposable {
#callbacks: Set<(args: CollectionChangedArgs) => void> = new Set();
#items: T[];
private _callbacks: Set<(args: CollectionChangedArgs) => void> = new Set();
private _items: T[];

constructor(...items: T[]) {
this.#items = [...items];
this._items = [...items];
}

add(...items: T[]) {
this.#items.push(...items);
this.#callbacks.forEach((callback) =>
this._items.push(...items);
this._callbacks.forEach((callback) =>
callback({
action: CollectionAction.add,
items,
Expand All @@ -53,8 +53,8 @@ export class ObservableCollection<T> implements ICollectionChanged, IDisposable
}

remove(...items: T[]) {
this.#items = this.#items.filter((item) => !items.includes(item));
this.#callbacks.forEach((callback) =>
this._items = this._items.filter((item) => !items.includes(item));
this._callbacks.forEach((callback) =>
callback({
action: CollectionAction.remove,
items,
Expand All @@ -64,10 +64,10 @@ export class ObservableCollection<T> implements ICollectionChanged, IDisposable

move(from: number, to: number) {
if (from === to) return;
if (from >= 0 && from < this.#items.length && to >= 0 && to < this.#items.length) {
let items = this.#items.splice(from, 1);
this.#items.splice(from < to ? to - 1 : to, 0, ...items);
this.#callbacks.forEach((callback) =>
if (from >= 0 && from < this._items.length && to >= 0 && to < this._items.length) {
let items = this._items.splice(from, 1);
this._items.splice(from < to ? to - 1 : to, 0, ...items);
this._callbacks.forEach((callback) =>
callback({
action: CollectionAction.move,
from,
Expand All @@ -78,9 +78,9 @@ export class ObservableCollection<T> implements ICollectionChanged, IDisposable
}

clear() {
let items = this.#items;
this.#items = [];
this.#callbacks.forEach((callback) =>
let items = this._items;
this._items = [];
this._callbacks.forEach((callback) =>
callback({
action: CollectionAction.remove,
items,
Expand All @@ -89,14 +89,14 @@ export class ObservableCollection<T> implements ICollectionChanged, IDisposable
}

get length() {
return this.#items.length;
return this._items.length;
}

replace(index: number, ...items: T[]) {
if (index >= 0 && index < this.#items.length) {
let item = this.#items[index];
this.#items.splice(index, 1, ...items);
this.#callbacks.forEach((callback) =>
if (index >= 0 && index < this._items.length) {
let item = this._items[index];
this._items.splice(index, 1, ...items);
this._callbacks.forEach((callback) =>
callback({
action: CollectionAction.replace,
item,
Expand All @@ -115,44 +115,44 @@ export class ObservableCollection<T> implements ICollectionChanged, IDisposable
}

get items() {
return [...this.#items];
return [...this._items];
}

[Symbol.iterator]() {
return this.items[Symbol.iterator]();
}

item(index: number) {
return this.#items[index];
return this._items[index];
}

at(index: number) {
return this.#items.at(index);
return this._items.at(index);
}

indexOf(item: T, fromIndex: number | undefined) {
return this.#items.indexOf(item, fromIndex);
return this._items.indexOf(item, fromIndex);
}

contains(item: T) {
return this.#items.indexOf(item) !== -1;
return this._items.indexOf(item) !== -1;
}

get count() {
return this.#items.length;
return this._items.length;
}

onCollectionChanged(callback: (args: CollectionChangedArgs) => void): void {
this.#callbacks.add(callback);
this._callbacks.add(callback);
}

removeCollectionChanged(callback: (args: CollectionChangedArgs) => void): void {
this.#callbacks.delete(callback);
this._callbacks.delete(callback);
}

dispose() {
this.#callbacks.clear();
this.#items.length = 0;
this._callbacks.clear();
this._items.length = 0;
}
}

Expand Down
28 changes: 14 additions & 14 deletions packages/chili-core/src/base/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,48 @@ export interface IHistoryRecord {
}

export class History implements IDisposable {
#undos: IHistoryRecord[] = [];
#redos: IHistoryRecord[] = [];
private _undos: IHistoryRecord[] = [];
private _redos: IHistoryRecord[] = [];

disabled: boolean = false;
undoLimits: number = 50;

dispose(): void {
this.#undos.length = 0;
this.#redos.length = 0;
this._undos.length = 0;
this._redos.length = 0;
}

add(record: IHistoryRecord) {
this.#redos.length = 0;
this.#undos.push(record);
if (this.#undos.length > this.undoLimits) {
this.#undos = this.#undos.slice(this.#undos.length - this.undoLimits);
this._redos.length = 0;
this._undos.push(record);
if (this._undos.length > this.undoLimits) {
this._undos = this._undos.slice(this._undos.length - this.undoLimits);
}
}

undoCount() {
return this.#undos.length;
return this._undos.length;
}

redoCount() {
return this.#redos.length;
return this._redos.length;
}

undo() {
this.tryOperate(() => {
let records = this.#undos.pop();
let records = this._undos.pop();
if (records === undefined) return;
records.undo();
this.#redos.push(records);
this._redos.push(records);
});
}

redo() {
this.tryOperate(() => {
let records = this.#redos.pop();
let records = this._redos.pop();
if (records === undefined) return;
records.redo();
this.#undos.push(records);
this._undos.push(records);
});
}

Expand Down
59 changes: 29 additions & 30 deletions packages/chili-core/src/base/linkedList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@ interface LinkedListNode<T> {
}

export class LinkedList<T> {
#head: LinkedListNode<T> | undefined;
private _head: LinkedListNode<T> | undefined;
get head() {
return this.#head?.data;
return this._head?.data;
}

#tail: LinkedListNode<T> | undefined;
private _tail: LinkedListNode<T> | undefined;
get tail() {
return this.#tail?.data;
return this._tail?.data;
}

#size: number = 0;
private _size: number = 0;
get size() {
return this.#size;
return this._size;
}

push(...items: T[]) {
items.forEach((item) => {
const node: LinkedListNode<T> = { data: item };
if (this.#head === undefined) {
this.#head = node;
if (this._head === undefined) {
this._head = node;
} else {
this.#tail!.next = node;
node.prev = this.#tail;
this._tail!.next = node;
node.prev = this._tail;
}
this.#tail = node;
this.#size++;
this._tail = node;
this._size++;
});
}

Expand All @@ -47,15 +47,15 @@ export class LinkedList<T> {
if (node.prev) {
node.prev.next = newNode;
} else {
this.#head = newNode;
this._head = newNode;
}
node.prev = newNode;
this.#size++;
this._size++;
}
}

remove(item: T) {
let current = this.#head;
let current = this._head;
while (current) {
if (current.data === item) {
this.removeNode(current);
Expand All @@ -74,23 +74,22 @@ export class LinkedList<T> {
if (node.prev) {
node.prev.next = node.next;
} else {
this.#head = node.next;
this._head = node.next;
}
if (node.next) {
node.next.prev = node.prev;
} else {
this.#tail = node.prev;
this._tail = node.prev;
}
this.#size--;
return;
this._size--;
}

private nodeAt(index: number) {
if (index < 0 || index >= this.#size) {
if (index < 0 || index >= this._size) {
return undefined;
}
if (index === this.#size - 1) return this.#tail;
let [current, currentIndex] = [this.#head, 0];
if (index === this._size - 1) return this._tail;
let [current, currentIndex] = [this._head, 0];
while (current) {
if (currentIndex === index) {
break;
Expand All @@ -102,26 +101,26 @@ export class LinkedList<T> {
}

clear() {
this.#head = undefined;
this.#tail = undefined;
this.#size = 0;
this._head = undefined;
this._tail = undefined;
this._size = 0;
}

reverse() {
let currentNode = this.#head;
let currentNode = this._head;
while (currentNode) {
const next = currentNode.next;
currentNode.next = currentNode.prev;
currentNode.prev = next;
currentNode = currentNode.prev;
}
const tail = this.#tail;
this.#tail = this.#head;
this.#head = tail;
const tail = this._tail;
this._tail = this._head;
this._head = tail;
}

*[Symbol.iterator](): IterableIterator<T> {
let current = this.#head;
let current = this._head;
while (current) {
yield current.data;
current = current.next;
Expand Down
Loading

0 comments on commit 1a69daa

Please sign in to comment.