Skip to content

Commit

Permalink
fix: use Singleton Pattern of Hyphen (#2242)
Browse files Browse the repository at this point in the history
* fix: hyphen use Singleton pattern

* refactor: add getInstance

* refactor: language dectector also use singlon pattern
  • Loading branch information
Jocs committed May 15, 2024
1 parent 2739fba commit ba853df
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ export class DocumentSkeleton extends Skeleton {
private _findLiquid: Liquid = new Liquid();

// Use for hyphenation.
private _hyphen = new Hyphen();
private _hyphen = Hyphen.getInstance();

private _languageDetector = new LanguageDetector();
private _languageDetector = LanguageDetector.getInstance();

private _iteratorCount = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import type { IDisposable } from '@wendellhu/redi';
import type { Nullable } from '@univerjs/core';
import { Lang } from './lang';
import { EnUs } from './patterns/en-us';
import type { IHyphenPattern, RawHyphenPattern } from './tools';
Expand All @@ -24,6 +25,16 @@ export class Hyphen implements IDisposable {
private _patterns: Map<Lang, IHyphenPattern> = new Map();
private _hyphenCache: Map<Lang, Map<string, string[]>> = new Map();

private static _instance: Nullable<Hyphen> = null;

static getInstance(): Hyphen {
if (this._instance == null) {
this._instance = new Hyphen();
}

return this._instance;
}

constructor() {
this._preloadPatterns();
this.loadPattern(Lang.EnGb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import type { IDisposable } from '@wendellhu/redi';
import { franc } from 'franc-min';
import type { Nullable } from '@univerjs/core';
import { Lang } from './lang';

const LANG_MAP_TO_HYPHEN_LANG: Record<string, Lang> = {
Expand Down Expand Up @@ -108,6 +109,16 @@ export class LanguageDetector implements IDisposable {
// language cache for special text.
private _detectCache = new Map<string, Lang>();

private static _instance: Nullable<LanguageDetector> = null;

static getInstance(): LanguageDetector {
if (this._instance == null) {
this._instance = new LanguageDetector();
}

return this._instance;
}

detect(text: string): Lang {
let lang = this._detectCache.get(text);

Expand Down

0 comments on commit ba853df

Please sign in to comment.