The requested changes are too big and content was truncated. Show full diff
@@ -0,0 +1,1 | |||||
|
1 | .eslintrc.js No newline at end of file |
@@ -0,0 +1,39 | |||||
|
1 | module.exports = { | |||
|
2 | root: true, | |||
|
3 | extends: [ | |||
|
4 | "plugin:react/recommended", | |||
|
5 | "eslint:recommended", | |||
|
6 | "plugin:@typescript-eslint/eslint-recommended", | |||
|
7 | "plugin:@typescript-eslint/recommended", | |||
|
8 | "plugin:@typescript-eslint/recommended-requiring-type-checking" | |||
|
9 | ], | |||
|
10 | parser: "@typescript-eslint/parser", | |||
|
11 | parserOptions: { | |||
|
12 | ecmaFeatures: { | |||
|
13 | jsx: true | |||
|
14 | }, | |||
|
15 | ecmaVersion: 5, | |||
|
16 | tsconfigRootDir: __dirname + "/src", | |||
|
17 | project: [ | |||
|
18 | "tsconfig.eslint.json", | |||
|
19 | "*/tsconfig.json" | |||
|
20 | ] | |||
|
21 | }, | |||
|
22 | plugins: [ | |||
|
23 | "react", | |||
|
24 | "@typescript-eslint" | |||
|
25 | ], | |||
|
26 | rules: { | |||
|
27 | "react/react-in-jsx-scope": "off", | |||
|
28 | "react/no-unknown-property": "off", | |||
|
29 | "@typescript-eslint/no-empty-function": "off", | |||
|
30 | "max-classes-per-file": [ | |||
|
31 | "error", | |||
|
32 | { "ignoreExpressions": true, "max": 1 } | |||
|
33 | ], | |||
|
34 | "@typescript-eslint/prefer-readonly": ["error"], | |||
|
35 | "semi": "off", | |||
|
36 | "@typescript-eslint/semi": ["error"] | |||
|
37 | ||||
|
38 | } | |||
|
39 | } |
1 | NO CONTENT: new file 100644 |
|
NO CONTENT: new file 100644 | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: new file 100644 |
|
NO CONTENT: new file 100644 | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
@@ -1,42 +1,42 | |||||
1 | { |
|
1 | { | |
2 | "name": "@implab/djx", |
|
2 | "name": "@implab/djx", | |
3 | "version": "0.0.1-dev", |
|
3 | "version": "0.0.1-dev", | |
4 | "description": "Supports using dojo version 1 with typescript and .tsx files", |
|
4 | "description": "Supports using dojo version 1 with typescript and .tsx files", | |
5 | "keywords": [ |
|
5 | "keywords": [ | |
6 | "dojo", |
|
6 | "dojo", | |
7 | "tsx", |
|
7 | "tsx", | |
8 | "typescript", |
|
8 | "typescript", | |
9 | "widgets" |
|
9 | "widgets" | |
10 | ], |
|
10 | ], | |
11 | "author": "Implab team", |
|
11 | "author": "Implab team", | |
12 | "license": "BSD-2-Clause", |
|
12 | "license": "BSD-2-Clause", | |
13 | "repository": "https://code.implab.org/implab/implabjs-djx", |
|
13 | "repository": "https://code.implab.org/implab/implabjs-djx", | |
14 | "publishConfig": { |
|
14 | "publishConfig": { | |
15 | "access": "public" |
|
15 | "access": "public" | |
16 | }, |
|
16 | }, | |
17 | "peerDependencies": { |
|
17 | "peerDependencies": { | |
18 | "@implab/core-amd": "^1.4.0", |
|
18 | "@implab/core-amd": "^1.4.0", | |
19 | "dojo": "^1.10.0" |
|
19 | "dojo": "^1.10.0" | |
20 | }, |
|
20 | }, | |
21 | "devDependencies": { |
|
21 | "devDependencies": { | |
22 | "@implab/core-amd": "^1.4.0", |
|
22 | "@implab/core-amd": "^1.4.0", | |
23 | "@types/chai": "4.1.3", |
|
23 | "@types/chai": "4.1.3", | |
24 | "@types/requirejs": "2.1.31", |
|
24 | "@types/requirejs": "2.1.31", | |
25 | "@types/yaml": "1.2.0", |
|
25 | "@types/yaml": "1.2.0", | |
26 | "@types/tap": "15.0.7", |
|
26 | "@types/tap": "15.0.7", | |
27 | "dojo": "1.16.0", |
|
27 | "dojo": "1.16.0", | |
28 |
"@implab/dojo-typings": "1.0. |
|
28 | "@implab/dojo-typings": "1.0.3", | |
29 | "@typescript-eslint/eslint-plugin": "^5.23.0", |
|
29 | "@typescript-eslint/eslint-plugin": "^5.23.0", | |
30 | "@typescript-eslint/parser": "^5.23.0", |
|
30 | "@typescript-eslint/parser": "^5.23.0", | |
31 | "eslint": "^8.23.0", |
|
31 | "eslint": "^8.23.0", | |
32 | "eslint-config-standard": "^17.0.0", |
|
32 | "eslint-config-standard": "^17.0.0", | |
33 | "eslint-plugin-import": "^2.26.0", |
|
33 | "eslint-plugin-import": "^2.26.0", | |
34 | "eslint-plugin-n": "^15.2.0", |
|
34 | "eslint-plugin-n": "^15.2.0", | |
35 | "eslint-plugin-promise": "^6.0.0", |
|
35 | "eslint-plugin-promise": "^6.0.0", | |
36 | "eslint-plugin-react": "^7.29.4", |
|
36 | "eslint-plugin-react": "^7.29.4", | |
37 | "requirejs": "2.3.6", |
|
37 | "requirejs": "2.3.6", | |
38 | "typescript": "4.8.3", |
|
38 | "typescript": "4.8.3", | |
39 | "yaml": "~1.7.2", |
|
39 | "yaml": "~1.7.2", | |
40 | "tap": "16.3.0" |
|
40 | "tap": "16.3.0" | |
41 | } |
|
41 | } | |
42 | } |
|
42 | } |
@@ -1,73 +1,58 | |||||
1 | import { MapOf, PromiseOrValue } from "@implab/core-amd/interfaces"; |
|
1 | import { MapOf, PromiseOrValue } from "@implab/core-amd/interfaces"; | |
2 | import { argumentNotEmptyString, isPromise, mixin } from "@implab/core-amd/safe"; |
|
2 | import { argumentNotEmptyString, isPromise, mixin } from "@implab/core-amd/safe"; | |
3 |
|
3 | |||
4 | export type LocaleProvider<T> = () => PromiseOrValue<T | { default: T }>; |
|
4 | export type LocaleProvider<T> = () => PromiseOrValue<T | { default: T }>; | |
5 |
|
5 | |||
6 | type ResolveCallback<T> = () => PromiseOrValue<T>; |
|
|||
7 |
|
||||
8 | function when<T, T2>(value: PromiseOrValue<T>, cb: (v: T) => PromiseOrValue<T2>): PromiseOrValue<T2> { |
|
6 | function when<T, T2>(value: PromiseOrValue<T>, cb: (v: T) => PromiseOrValue<T2>): PromiseOrValue<T2> { | |
9 | return isPromise(value) ? |
|
7 | return isPromise(value) ? | |
10 | value.then(cb) : |
|
8 | value.then(cb) : | |
11 | cb(value); |
|
9 | cb(value); | |
12 | } |
|
10 | } | |
13 |
|
11 | |||
14 | function isCallback<T>(v: ResolveCallback<T> | PromiseOrValue<T>): v is ResolveCallback<T> { |
|
12 | const chainObjects = <T extends object, T2 extends object>(o1: T, o2: T2) => | |
15 | return typeof v === "function"; |
|
13 | mixin(Object.create(o1) as T, o2); | |
16 | } |
|
14 | export class NlsBundle<T extends object> { | |
|
15 | private readonly _locales: MapOf<LocaleProvider<Partial<T>>>; | |||
17 |
|
16 | |||
18 | function chainObjects<T extends object>(o1: T, o2: T) { |
|
17 | private readonly _default: T; | |
19 | if (!o1) |
|
|||
20 | return o2; |
|
|||
21 | if (!o2) |
|
|||
22 | return o1; |
|
|||
23 |
|
||||
24 | return mixin(Object.create(o1) as T, o2); |
|
|||
25 | } |
|
|||
26 |
|
||||
27 | export class NlsBundle<T extends object> { |
|
|||
28 | private _locales: MapOf<LocaleProvider<Partial<T>>>; |
|
|||
29 |
|
||||
30 | private _default: T; |
|
|||
31 |
|
18 | |||
32 | private _cache: MapOf<PromiseOrValue<T>>; |
|
19 | private _cache: MapOf<PromiseOrValue<T>>; | |
33 |
|
20 | |||
34 | constructor(defNls: T, locales?: MapOf<LocaleProvider<Partial<T>>>) { |
|
21 | constructor(defNls: T, locales?: MapOf<LocaleProvider<Partial<T>>>) { | |
35 | this._default = defNls; |
|
22 | this._default = defNls; | |
36 | this._locales = locales || {}; |
|
23 | this._locales = locales || {}; | |
37 | this._cache = {}; |
|
24 | this._cache = {}; | |
38 | } |
|
25 | } | |
39 |
|
26 | |||
40 | getLocale(locale: string) { |
|
27 | getLocale(locale: string) { | |
41 | argumentNotEmptyString(locale, "locale"); |
|
28 | argumentNotEmptyString(locale, "locale"); | |
42 | const _loc = locale; |
|
29 | const _loc = locale; | |
43 |
|
30 | |||
44 | // en-US => ["en", "en-US"] |
|
31 | // en-US => ["en", "en-US"] | |
45 | const locales = _loc.split(/-|_/).map((x, i, a) => a.slice(0, i + 1).join("-")); |
|
32 | const locales = _loc.split(/-|_/).map((x, i, a) => a.slice(0, i + 1).join("-")); | |
46 | return this._resolveLocale(locales); |
|
33 | return this._resolveLocale(locales); | |
47 | } |
|
34 | } | |
48 |
|
35 | |||
49 | _resolveLocale(locales: string[]): PromiseOrValue<T> { |
|
36 | _resolveLocale(locales: string[]): PromiseOrValue<T> { | |
50 | if (!locales.length) |
|
37 | if (!locales.length) | |
51 | return this._default; |
|
38 | return this._default; | |
52 |
|
39 | |||
53 | const locale = locales.pop(); |
|
40 | const locale = locales.pop(); | |
54 | if (!locale) |
|
41 | if (!locale) | |
55 | throw new Error("The locale can't be empty"); |
|
42 | throw new Error("The locale can't be empty"); | |
56 |
|
43 | |||
57 | if (this._cache[locale]) |
|
44 | if (this._cache[locale]) | |
58 | return this._cache[locale]; |
|
45 | return this._cache[locale]; | |
59 |
|
46 | |||
60 | const data = this._loadPackage(this._locales[locale]); |
|
47 | const data = this._loadPackage(this._locales[locale]); | |
61 | const parent = this._resolveLocale(locales); |
|
48 | const parent = this._resolveLocale(locales); | |
62 |
|
49 | |||
63 | return this._cache[locale] = when(data, x => { |
|
50 | return this._cache[locale] = when(data, x => { | |
64 | return when(parent, y => this._cache[locale] = chainObjects(y, x)); |
|
51 | return when(parent, y => this._cache[locale] = chainObjects(y, x)); | |
65 | }); |
|
52 | }); | |
66 | } |
|
53 | } | |
67 |
|
54 | |||
68 |
_loadPackage(localeData: |
|
55 | _loadPackage(localeData: LocaleProvider<Partial<T>>) { | |
69 | if (isCallback(localeData)) |
|
|||
70 |
|
|
56 | return when(localeData(), data => data && "default" in data ? data.default : data); | |
71 | return localeData; |
|
|||
72 | } |
|
57 | } | |
73 | } |
|
58 | } |
@@ -1,18 +1,18 | |||||
1 | import inject from "./dom-inject"; |
|
1 | import inject from "./dom-inject"; | |
2 |
|
2 | |||
3 | interface OnLoad { |
|
3 | interface OnLoad { | |
4 |
(result?: |
|
4 | (result?: unknown): void; | |
5 |
error(err: |
|
5 | error(err: unknown): void; | |
6 | } |
|
6 | } | |
7 |
|
7 | |||
8 | const plugin = { |
|
8 | const plugin = { | |
9 | load: (id: string, require: Require, cb: OnLoad, config: { isBuild?: boolean }) => { |
|
9 | load: (id: string, require: Require, cb: OnLoad, config: { isBuild?: boolean }) => { | |
10 | if (config && config.isBuild) { |
|
10 | if (config && config.isBuild) { | |
11 | cb(); |
|
11 | cb(); | |
12 | } else { |
|
12 | } else { | |
13 | const url = require.toUrl(id); |
|
13 | const url = require.toUrl(id); | |
14 | inject.injectStylesheet(url).then(() => cb({ url }), e => cb.error(e)); |
|
14 | inject.injectStylesheet(url).then(() => cb({ url }), e => cb.error(e)); | |
15 | } |
|
15 | } | |
16 | } |
|
16 | } | |
17 | }; |
|
17 | }; | |
18 | export = plugin; |
|
18 | export = plugin; |
@@ -1,254 +1,255 | |||||
1 | import declare = require("dojo/_base/declare"); |
|
1 | import declare = require("dojo/_base/declare"); | |
2 | import { each } from "@implab/core-amd/safe"; |
|
2 | import { each } from "@implab/core-amd/safe"; | |
3 | import { Constructor } from "@implab/core-amd/interfaces"; |
|
3 | import { Constructor } from "@implab/core-amd/interfaces"; | |
4 |
|
4 | |||
5 | // declare const declare: any; |
|
5 | // declare const declare: any; | |
6 |
|
6 | |||
7 | type DeclareConstructor<T> = dojo._base.DeclareConstructor<T>; |
|
7 | type DeclareConstructor<T> = dojo._base.DeclareConstructor<T>; | |
8 |
|
8 | |||
9 | export interface AbstractConstructor<T = object> { |
|
9 | export interface AbstractConstructor<T = object> { | |
10 | prototype: T; |
|
10 | prototype: T; | |
11 | } |
|
11 | } | |
12 |
|
12 | |||
13 | interface DjMockConstructor<T = object> { |
|
13 | interface DjMockConstructor<T = object> { | |
14 | new(...args: unknown[]): T; |
|
14 | new(...args: unknown[]): T; | |
15 | mock: boolean; |
|
15 | mock: boolean; | |
16 | bases: AbstractConstructor[]; |
|
16 | bases: AbstractConstructor[]; | |
17 | } |
|
17 | } | |
18 |
|
18 | |||
19 | export function djbase<T>(): DeclareConstructor<T>; |
|
19 | export function djbase<T>(): DeclareConstructor<T>; | |
20 | export function djbase<T>( |
|
20 | export function djbase<T>( | |
21 | b0: AbstractConstructor<T> |
|
21 | b0: AbstractConstructor<T> | |
22 | ): DeclareConstructor<T>; |
|
22 | ): DeclareConstructor<T>; | |
23 |
|
23 | |||
24 | export function djbase<T0, T1>( |
|
24 | export function djbase<T0, T1>( | |
25 | b0: AbstractConstructor<T0>, |
|
25 | b0: AbstractConstructor<T0>, | |
26 | b1: AbstractConstructor<T1> |
|
26 | b1: AbstractConstructor<T1> | |
27 | ): DeclareConstructor<T0 & T1>; |
|
27 | ): DeclareConstructor<T0 & T1>; | |
28 |
|
28 | |||
29 | export function djbase<T0, T1, T2>( |
|
29 | export function djbase<T0, T1, T2>( | |
30 | b0: AbstractConstructor<T0>, |
|
30 | b0: AbstractConstructor<T0>, | |
31 | b1: AbstractConstructor<T1>, |
|
31 | b1: AbstractConstructor<T1>, | |
32 | b2: AbstractConstructor<T2> |
|
32 | b2: AbstractConstructor<T2> | |
33 | ): DeclareConstructor<T0 & T1 & T2>; |
|
33 | ): DeclareConstructor<T0 & T1 & T2>; | |
34 |
|
34 | |||
35 | export function djbase<T0, T1, T2, T3>( |
|
35 | export function djbase<T0, T1, T2, T3>( | |
36 | b0: AbstractConstructor<T0>, |
|
36 | b0: AbstractConstructor<T0>, | |
37 | b1: AbstractConstructor<T1>, |
|
37 | b1: AbstractConstructor<T1>, | |
38 | b2: AbstractConstructor<T2>, |
|
38 | b2: AbstractConstructor<T2>, | |
39 | b3: AbstractConstructor<T3> |
|
39 | b3: AbstractConstructor<T3> | |
40 | ): DeclareConstructor<T0 & T1 & T2 & T3>; |
|
40 | ): DeclareConstructor<T0 & T1 & T2 & T3>; | |
41 |
|
41 | |||
42 | export function djbase<T0, T1, T2, T3, T4>( |
|
42 | export function djbase<T0, T1, T2, T3, T4>( | |
43 | b0: AbstractConstructor<T0>, |
|
43 | b0: AbstractConstructor<T0>, | |
44 | b1: AbstractConstructor<T1>, |
|
44 | b1: AbstractConstructor<T1>, | |
45 | b2: AbstractConstructor<T2>, |
|
45 | b2: AbstractConstructor<T2>, | |
46 | b3: AbstractConstructor<T3>, |
|
46 | b3: AbstractConstructor<T3>, | |
47 | b4: AbstractConstructor<T4> |
|
47 | b4: AbstractConstructor<T4> | |
48 | ): DeclareConstructor<T0 & T1 & T2 & T3 & T4>; |
|
48 | ): DeclareConstructor<T0 & T1 & T2 & T3 & T4>; | |
49 |
|
49 | |||
50 | export function djbase<T0, T1, T2, T3, T4, T5>( |
|
50 | export function djbase<T0, T1, T2, T3, T4, T5>( | |
51 | b0: AbstractConstructor<T0>, |
|
51 | b0: AbstractConstructor<T0>, | |
52 | b1: AbstractConstructor<T1>, |
|
52 | b1: AbstractConstructor<T1>, | |
53 | b2: AbstractConstructor<T2>, |
|
53 | b2: AbstractConstructor<T2>, | |
54 | b3: AbstractConstructor<T3>, |
|
54 | b3: AbstractConstructor<T3>, | |
55 | b4: AbstractConstructor<T4>, |
|
55 | b4: AbstractConstructor<T4>, | |
56 | b5: AbstractConstructor<T5> |
|
56 | b5: AbstractConstructor<T5> | |
57 | ): DeclareConstructor<T0 & T1 & T2 & T3 & T4 & T5>; |
|
57 | ): DeclareConstructor<T0 & T1 & T2 & T3 & T4 & T5>; | |
58 |
|
58 | |||
59 | export function djbase<T0, T1, T2, T3, T4, T5, T6>( |
|
59 | export function djbase<T0, T1, T2, T3, T4, T5, T6>( | |
60 | b0: AbstractConstructor<T0>, |
|
60 | b0: AbstractConstructor<T0>, | |
61 | b1: AbstractConstructor<T1>, |
|
61 | b1: AbstractConstructor<T1>, | |
62 | b2: AbstractConstructor<T2>, |
|
62 | b2: AbstractConstructor<T2>, | |
63 | b3: AbstractConstructor<T3>, |
|
63 | b3: AbstractConstructor<T3>, | |
64 | b4: AbstractConstructor<T4>, |
|
64 | b4: AbstractConstructor<T4>, | |
65 | b5: AbstractConstructor<T5>, |
|
65 | b5: AbstractConstructor<T5>, | |
66 | b6: AbstractConstructor<T6> |
|
66 | b6: AbstractConstructor<T6> | |
67 | ): DeclareConstructor<T0 & T1 & T2 & T3 & T4 & T5 & T6>; |
|
67 | ): DeclareConstructor<T0 & T1 & T2 & T3 & T4 & T5 & T6>; | |
68 |
|
68 | |||
69 | export function djbase<T0, T1, T2, T3, T4, T5, T6, T7>( |
|
69 | export function djbase<T0, T1, T2, T3, T4, T5, T6, T7>( | |
70 | b0: AbstractConstructor<T0>, |
|
70 | b0: AbstractConstructor<T0>, | |
71 | b1: AbstractConstructor<T1>, |
|
71 | b1: AbstractConstructor<T1>, | |
72 | b2: AbstractConstructor<T2>, |
|
72 | b2: AbstractConstructor<T2>, | |
73 | b3: AbstractConstructor<T3>, |
|
73 | b3: AbstractConstructor<T3>, | |
74 | b4: AbstractConstructor<T4>, |
|
74 | b4: AbstractConstructor<T4>, | |
75 | b5: AbstractConstructor<T5>, |
|
75 | b5: AbstractConstructor<T5>, | |
76 | b6: AbstractConstructor<T6>, |
|
76 | b6: AbstractConstructor<T6>, | |
77 | b7: AbstractConstructor<T7> |
|
77 | b7: AbstractConstructor<T7> | |
78 | ): DeclareConstructor<T0 & T1 & T2 & T3 & T4 & T5 & T6 & T7>; |
|
78 | ): DeclareConstructor<T0 & T1 & T2 & T3 & T4 & T5 & T6 & T7>; | |
79 |
|
79 | |||
80 | /** Создает конструктор-заглушку из списка базовых классов, используется |
|
80 | /** Создает конструктор-заглушку из списка базовых классов, используется | |
81 | * для объявления классов при помощи `dojo/_base/declare`. |
|
81 | * для объявления классов при помощи `dojo/_base/declare`. | |
82 | * |
|
82 | * | |
83 | * Создает пустой конструктор, с пустым стандартным прототипом, это нужно, |
|
83 | * Создает пустой конструктор, с пустым стандартным прототипом, это нужно, | |
84 | * поскольку в унаследованном классе конструктор обязательно должен вызвать |
|
84 | * поскольку в унаследованном классе конструктор обязательно должен вызвать | |
85 | * `super(...)`, таким образом он вызовет пустую функцию. |
|
85 | * `super(...)`, таким образом он вызовет пустую функцию. | |
86 | * |
|
86 | * | |
87 | * Созданный конструктор хранит в себе список базовых классов, который будет |
|
87 | * Созданный конструктор хранит в себе список базовых классов, который будет | |
88 | * использован декоратором `djclass`, который вернет класс, объявленный при |
|
88 | * использован декоратором `djclass`, который вернет класс, объявленный при | |
89 | * помощи `dojo/_base/declare`. |
|
89 | * помощи `dojo/_base/declare`. | |
90 | * |
|
90 | * | |
91 | * @param bases список базовых классов, от которых требуется унаследовать |
|
91 | * @param bases список базовых классов, от которых требуется унаследовать | |
92 | * новый класс. |
|
92 | * новый класс. | |
93 | * |
|
93 | * | |
94 | */ |
|
94 | */ | |
95 | export function djbase(...bases: AbstractConstructor[]): Constructor { |
|
95 | export function djbase(...bases: AbstractConstructor[]): Constructor { | |
96 |
|
96 | |||
97 | const t = class { |
|
97 | const t = class { | |
98 | static mock: boolean; |
|
98 | static mock: boolean; | |
99 | static bases: AbstractConstructor[]; |
|
99 | static bases: AbstractConstructor[]; | |
100 | }; |
|
100 | }; | |
101 |
|
101 | |||
102 | t.mock = true; |
|
102 | t.mock = true; | |
103 | t.bases = bases; |
|
103 | t.bases = bases; | |
104 |
|
104 | |||
105 | return t as Constructor; |
|
105 | return t as Constructor; | |
106 | } |
|
106 | } | |
107 |
|
107 | |||
108 | function isMockConstructor<T extends object>(v: AbstractConstructor<T>): v is DjMockConstructor<T> { |
|
108 | function isMockConstructor<T extends object>(v: AbstractConstructor<T>): v is DjMockConstructor<T> { | |
109 | return v && "mock" in v; |
|
109 | return v && "mock" in v; | |
110 | } |
|
110 | } | |
111 |
|
111 | |||
112 | /** Создает класс при помощи `dojo/_base/declare`. Для этого исходный класс |
|
112 | /** Создает класс при помощи `dojo/_base/declare`. Для этого исходный класс | |
113 | * должен быть унаследован от `djbase(...)`. |
|
113 | * должен быть унаследован от `djbase(...)`. | |
114 | * |
|
114 | * | |
115 | * @param target Класс, который нужно объявить при помощи `dojo/_base/declare` |
|
115 | * @param target Класс, который нужно объявить при помощи `dojo/_base/declare` | |
116 | */ |
|
116 | */ | |
117 | export function djclass<T extends AbstractConstructor>(target: T): T { |
|
117 | export function djclass<T extends AbstractConstructor>(target: T): T { | |
118 | // получаем базовый конструктор и его прототип |
|
118 | // получаем базовый конструктор и его прототип | |
119 | const bp = target && !!target.prototype && Object.getPrototypeOf(target.prototype) as object; |
|
119 | const bp = target && !!target.prototype && Object.getPrototypeOf(target.prototype) as object; | |
120 | const bc = bp && bp.constructor; |
|
120 | const bc = bp && bp.constructor; | |
121 |
|
121 | |||
122 | // проверка того, что класс унаследован от специальной заглушки |
|
122 | // проверка того, что класс унаследован от специальной заглушки | |
123 | if (isMockConstructor(bc)) { |
|
123 | if (isMockConstructor(bc)) { | |
124 | // bc.bases - базовый класс, объявленный при помощи dojo/_base/declare |
|
124 | // bc.bases - базовый класс, объявленный при помощи dojo/_base/declare | |
125 | const cls = declare(bc.bases, target.prototype) as unknown as T; |
|
125 | const cls = declare(bc.bases, target.prototype) as unknown as T; | |
126 |
|
126 | |||
127 | // bc - базовый класс, bc.prototype используется как super |
|
127 | // bc - базовый класс, bc.prototype используется как super | |
128 | // при вызове базовых методов. Нужно создать bc.prototype |
|
128 | // при вызове базовых методов. Нужно создать bc.prototype | |
129 | // таким образом, чтобы он вызывал this.inherited(). |
|
129 | // таким образом, чтобы он вызывал this.inherited(). | |
130 |
|
130 | |||
131 | // создаем новый прототип, он не в цепочке прототипов у текущего |
|
131 | // создаем новый прототип, он не в цепочке прототипов у текущего | |
132 | // класса, но super.some_method будет использовать именно его. |
|
132 | // класса, но super.some_method будет использовать именно его. | |
133 | // в этом объекте будут размещены прокси для переопределенных |
|
133 | // в этом объекте будут размещены прокси для переопределенных | |
134 | // методов. |
|
134 | // методов. | |
135 | const nbp = bc.prototype = Object.create(cls.prototype) as Record<string, unknown>; |
|
135 | const nbp = bc.prototype = Object.create(cls.prototype) as Record<string, unknown>; | |
136 | nbp.constructor = bc; |
|
136 | nbp.constructor = bc; | |
137 |
|
137 | |||
138 | // proxy - фабрика для создания прокси-методов, которые внутри |
|
138 | // proxy - фабрика для создания прокси-методов, которые внутри | |
139 | // себя вызовут this.inherited с правильными параметрами. |
|
139 | // себя вызовут this.inherited с правильными параметрами. | |
140 |
const proxy = (m: (...args: unknown[]) => unknown) => function (this: dojo._base.DeclareCreatedObject |
|
140 | const proxy = (m: (...args: unknown[]) => unknown) => function (this: dojo._base.DeclareCreatedObject) { | |
141 |
const f = this.getInherited({ callee: m |
|
141 | const f = this.getInherited({ callee: m } as unknown as IArguments); | |
142 | return f ? f.apply(this, args) as unknown : undefined; |
|
142 | // eslint-disable-next-line prefer-rest-params | |
|
143 | return f ? f.apply(this, arguments) as unknown : undefined; | |||
143 |
|
144 | |||
144 | // так сделать можно только dojo 1.15+ |
|
145 | // так сделать можно только dojo 1.15+ | |
145 | // return this.inherited(m, arguments); |
|
146 | // return this.inherited(m, arguments); | |
146 | }; |
|
147 | }; | |
147 |
|
148 | |||
148 | // у текущего класса прототип содержит методы, объявленные в этом |
|
149 | // у текущего класса прототип содержит методы, объявленные в этом | |
149 | // классе и его конструктор. Нужно пройти по всем методам и |
|
150 | // классе и его конструктор. Нужно пройти по всем методам и | |
150 | // создать для них прокси. |
|
151 | // создать для них прокси. | |
151 | // При этом только те, методы, которые есть в базовых классах |
|
152 | // При этом только те, методы, которые есть в базовых классах | |
152 | // могут быть переопределены. |
|
153 | // могут быть переопределены. | |
153 | each(target.prototype, (m: unknown, p: string) => { |
|
154 | each(target.prototype, (m: unknown, p: string) => { | |
154 | if (typeof m === "function" && |
|
155 | if (typeof m === "function" && | |
155 | p !== "constructor" && |
|
156 | p !== "constructor" && | |
156 | Object.prototype.hasOwnProperty.call(target, p) |
|
157 | Object.prototype.hasOwnProperty.call(target.prototype, p) | |
157 | ) { |
|
158 | ) { | |
158 | nbp[p] = proxy(m as (...args: unknown[]) => unknown); |
|
159 | nbp[p] = proxy(m as (...args: unknown[]) => unknown); | |
159 | } |
|
160 | } | |
160 | }); |
|
161 | }); | |
161 |
|
162 | |||
162 | // TODO mixin static members |
|
163 | // TODO mixin static members | |
163 | return cls; |
|
164 | return cls; | |
164 | } else { |
|
165 | } else { | |
165 | return target; |
|
166 | return target; | |
166 | } |
|
167 | } | |
167 | } |
|
168 | } | |
168 |
|
169 | |||
169 | function makeSetterName(prop: string) { |
|
170 | function makeSetterName(prop: string) { | |
170 | return [ |
|
171 | return [ | |
171 | "_set", |
|
172 | "_set", | |
172 | prop.replace(/^./, x => x.toUpperCase()), |
|
173 | prop.replace(/^./, x => x.toUpperCase()), | |
173 | "Attr" |
|
174 | "Attr" | |
174 | ].join(""); |
|
175 | ].join(""); | |
175 | } |
|
176 | } | |
176 |
|
177 | |||
177 | function makeGetterName(prop: string) { |
|
178 | function makeGetterName(prop: string) { | |
178 | return [ |
|
179 | return [ | |
179 | "_get", |
|
180 | "_get", | |
180 | prop.replace(/^./, x => x.toUpperCase()), |
|
181 | prop.replace(/^./, x => x.toUpperCase()), | |
181 | "Attr" |
|
182 | "Attr" | |
182 | ].join(""); |
|
183 | ].join(""); | |
183 | } |
|
184 | } | |
184 |
|
185 | |||
185 | interface NodeBindSpec { |
|
186 | interface NodeBindSpec { | |
186 | node: string; |
|
187 | node: string; | |
187 | type: "attribute" | "innerText" | "textContent" | "innerHTML" | "class" | "toggleClass"; |
|
188 | type: "attribute" | "innerText" | "textContent" | "innerHTML" | "class" | "toggleClass"; | |
188 | attribute?: string; |
|
189 | attribute?: string; | |
189 |
|
190 | |||
190 | className?: string; |
|
191 | className?: string; | |
191 | } |
|
192 | } | |
192 |
|
193 | |||
193 | /** |
|
194 | /** | |
194 | * Описание привязки свойства виджета к свойству внутреннего объекта. |
|
195 | * Описание привязки свойства виджета к свойству внутреннего объекта. | |
195 | */ |
|
196 | */ | |
196 | interface MemberBindSpec { |
|
197 | interface MemberBindSpec { | |
197 | /** |
|
198 | /** | |
198 | * Имя свойства со ссылкой на объект, к которому . |
|
199 | * Имя свойства со ссылкой на объект, к которому . | |
199 | */ |
|
200 | */ | |
200 | member: string; |
|
201 | member: string; | |
201 | /** |
|
202 | /** | |
202 | * Свойство объекта к которому нужно осуществить привязку. |
|
203 | * Свойство объекта к которому нужно осуществить привязку. | |
203 | */ |
|
204 | */ | |
204 | property: string; |
|
205 | property: string; | |
205 |
|
206 | |||
206 | /** |
|
207 | /** | |
207 | * Привязка осуществляется не только на запись но и на чтение свойства. |
|
208 | * Привязка осуществляется не только на запись но и на чтение свойства. | |
208 | */ |
|
209 | */ | |
209 | getter?: boolean; |
|
210 | getter?: boolean; | |
210 | } |
|
211 | } | |
211 |
|
212 | |||
212 | function isNodeBindSpec(v: object): v is NodeBindSpec { |
|
213 | function isNodeBindSpec(v: object): v is NodeBindSpec { | |
213 | return "node" in v; |
|
214 | return "node" in v; | |
214 | } |
|
215 | } | |
215 |
|
216 | |||
216 | /** Декорирует свойства виджета для привязки их к внутренним членам, либо DOM |
|
217 | /** Декорирует свойства виджета для привязки их к внутренним членам, либо DOM | |
217 | * элементам, либо свойству внутреннего объекта. |
|
218 | * элементам, либо свойству внутреннего объекта. | |
218 | * |
|
219 | * | |
219 | * @param {NodeBindSpec | MemberBindSpec} params Параметры связывания. |
|
220 | * @param {NodeBindSpec | MemberBindSpec} params Параметры связывания. | |
220 | */ |
|
221 | */ | |
221 | export function bind(params: NodeBindSpec | MemberBindSpec) { |
|
222 | export function bind(params: NodeBindSpec | MemberBindSpec) { | |
222 | if (isNodeBindSpec(params)) { |
|
223 | if (isNodeBindSpec(params)) { | |
223 | return <K extends string>(target: Record<K, unknown>, name: K) => { |
|
224 | return <K extends string>(target: Record<K, unknown>, name: K) => { | |
224 | target[makeSetterName(name) as K /** hack to not go insane) */] = params; |
|
225 | target[makeSetterName(name) as K /** hack to not go insane) */] = params; | |
225 | }; |
|
226 | }; | |
226 | } else { |
|
227 | } else { | |
227 | return <K extends string, |
|
228 | return <K extends string>(target: Record<K | "_set", unknown>, name: K) => { | |
228 | T extends { [p in K]: p extends "_set" ? (name: p, v: unknown) => void : unknown; }> (target: T, name: K) => { |
|
229 | target[name] = undefined; | |
229 | target[name] = undefined as T[K]; |
|
230 | ||
230 |
target[makeSetterName(name) as K] = function (this: |
|
231 | target[makeSetterName(name) as K] = function (this: typeof target, v: unknown) { | |
231 | this._set(name, v); |
|
232 | (this._set as (n: K, v: unknown) => void)(name, v); | |
232 | const inner = this[params.member] as Record<string, unknown>; |
|
233 | const inner = this[params.member as K] as Record<string, unknown>; | |
233 | if (typeof inner.set === "function") |
|
234 | if (typeof inner.set === "function") | |
234 | inner.set(params.property, v); |
|
235 | inner.set(params.property, v); | |
235 |
} |
|
236 | }; | |
236 | if (params.getter) |
|
237 | if (params.getter) | |
237 | target[makeGetterName(name)] = function () { |
|
238 | target[makeGetterName(name) as K] = function (this: typeof target) { | |
238 | const inner = this[params.member] as Record<string, unknown>; |
|
239 | const inner = this[params.member as K] as Record<string, unknown>; | |
239 | if (typeof inner.get === "function") |
|
240 | if (typeof inner.get === "function") | |
240 | return inner.get(params.property) as unknown; |
|
241 | return inner.get(params.property) as unknown; | |
241 | }; |
|
242 | }; | |
242 | }; |
|
243 | }; | |
243 | } |
|
244 | } | |
244 | } |
|
245 | } | |
245 |
|
246 | |||
246 | /** Создает в прототипе свойство с указанным значением. |
|
247 | /** Создает в прототипе свойство с указанным значением. | |
247 | * @param value Значение, которое будет указано в прототипе |
|
248 | * @param value Значение, которое будет указано в прототипе | |
248 | */ |
|
249 | */ | |
249 | export function prototype<T>(value: T): <P extends { [m in K]: T }, K extends keyof P>(p: P, name: K) => void; |
|
250 | export function prototype<T>(value: T): <P extends { [m in K]: T }, K extends keyof P>(p: P, name: K) => void; | |
250 | export function prototype<T>(value?: T) { |
|
251 | export function prototype<T>(value?: T) { | |
251 | return (p: { [m in string]: T | undefined }, name: string) => { |
|
252 | return (p: { [m in string]: T | undefined }, name: string) => { | |
252 | p[name] = value; |
|
253 | p[name] = value; | |
253 | }; |
|
254 | }; | |
254 | } |
|
255 | } |
@@ -1,100 +1,100 | |||||
1 | import { id as mid } from "module"; |
|
1 | import { id as mid } from "module"; | |
2 | import { TraceSource } from "@implab/core-amd/log/TraceSource"; |
|
2 | import { TraceSource } from "@implab/core-amd/log/TraceSource"; | |
3 | import { MapOf } from "@implab/core-amd/interfaces"; |
|
3 | import { MapOf } from "@implab/core-amd/interfaces"; | |
4 | import { mixin } from "@implab/core-amd/safe"; |
|
4 | import { mixin } from "@implab/core-amd/safe"; | |
5 |
|
5 | |||
6 | const trace = TraceSource.get(mid); |
|
6 | const trace = TraceSource.get(mid); | |
7 |
|
7 | |||
8 |
|
8 | |||
9 |
function on<T extends keyof HTMLElementEventMap>(node: HTMLElement, eventName: T, handler: (this: HTMLElement, ev: HTMLElementEventMap[T]) => |
|
9 | function on<T extends keyof HTMLElementEventMap>(node: HTMLElement, eventName: T, handler: (this: HTMLElement, ev: HTMLElementEventMap[T]) => unknown): () => void { | |
10 | // Add an event listener to a DOM node |
|
10 | // Add an event listener to a DOM node | |
11 | node.addEventListener(eventName, handler, false); |
|
11 | node.addEventListener(eventName, handler, false); | |
12 |
|
12 | |||
13 | return () => node.removeEventListener(eventName, handler, false); |
|
13 | return () => node.removeEventListener(eventName, handler, false); | |
14 | } |
|
14 | } | |
15 |
|
15 | |||
16 | interface NodeLoadResult { |
|
16 | interface NodeLoadResult { | |
17 | node: HTMLElement; |
|
17 | node: HTMLElement; | |
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 | class DomInject { |
|
20 | class DomInject { | |
21 | injectionPoint?: HTMLElement; |
|
21 | injectionPoint?: HTMLElement; | |
22 | injectAfter?: HTMLElement; |
|
22 | injectAfter?: HTMLElement; | |
23 |
|
23 | |||
24 | _map: MapOf<Promise<NodeLoadResult>> = {}; |
|
24 | _map: MapOf<Promise<NodeLoadResult>> = {}; | |
25 |
|
25 | |||
26 | _inject<T extends keyof HTMLElementTagNameMap>(name: T, attr: Partial<HTMLElementTagNameMap[T]>) { |
|
26 | _inject<T extends keyof HTMLElementTagNameMap>(name: T, attr: Partial<HTMLElementTagNameMap[T]>) { | |
27 | const node = document.createElement(name); |
|
27 | const node = document.createElement(name); | |
28 |
|
28 | |||
29 | return new Promise<NodeLoadResult>((ok, fail) => { |
|
29 | return new Promise<NodeLoadResult>((ok, fail) => { | |
30 |
|
30 | |||
31 | const cleanup = () => { |
|
31 | const cleanup = () => { | |
32 | noerr(); |
|
32 | noerr(); | |
33 | noload(); |
|
33 | noload(); | |
34 | }; |
|
34 | }; | |
35 |
|
35 | |||
36 | const noload = on(node, "load", () => { |
|
36 | const noload = on(node, "load", () => { | |
37 | ok({ node }); |
|
37 | ok({ node }); | |
38 | cleanup(); |
|
38 | cleanup(); | |
39 | }); |
|
39 | }); | |
40 |
|
40 | |||
41 | const noerr = on(node, "error", e => { |
|
41 | const noerr = on(node, "error", e => { | |
42 | fail({ |
|
42 | fail({ | |
43 | error: e, |
|
43 | error: e, | |
44 | node |
|
44 | node | |
45 | }); |
|
45 | }); | |
46 | cleanup(); |
|
46 | cleanup(); | |
47 | }); |
|
47 | }); | |
48 |
|
48 | |||
49 | mixin(node, attr); |
|
49 | mixin(node, attr); | |
50 |
|
50 | |||
51 | const _injectionPoint = this.injectionPoint || document.head; |
|
51 | const _injectionPoint = this.injectionPoint || document.head; | |
52 | const _injectBefore = this.injectAfter ? this.injectAfter.nextSibling : null; |
|
52 | const _injectBefore = this.injectAfter ? this.injectAfter.nextSibling : null; | |
53 |
|
53 | |||
54 | _injectionPoint.insertBefore(node, _injectBefore); |
|
54 | _injectionPoint.insertBefore(node, _injectBefore); | |
55 | }); |
|
55 | }); | |
56 | } |
|
56 | } | |
57 |
|
57 | |||
58 | async injectScript(url: string) { |
|
58 | async injectScript(url: string) { | |
59 | let d = this._map[url]; |
|
59 | let d = this._map[url]; | |
60 |
if ( |
|
60 | if (d === undefined) { | |
61 | trace.log("js {0}", url); |
|
61 | trace.log("js {0}", url); | |
62 | d = this._inject("script", { |
|
62 | d = this._inject("script", { | |
63 | type: "text/javascript", |
|
63 | type: "text/javascript", | |
64 | charset: "utf-8", |
|
64 | charset: "utf-8", | |
65 | src: url |
|
65 | src: url | |
66 | }); |
|
66 | }); | |
67 | this._map[url] = d; |
|
67 | this._map[url] = d; | |
68 | } |
|
68 | } | |
69 | try { |
|
69 | try { | |
70 | await d; |
|
70 | await d; | |
71 | trace.log("done {0}", url); |
|
71 | trace.log("done {0}", url); | |
72 | } catch (e) { |
|
72 | } catch (e) { | |
73 | trace.error("failed {0}: {1}", url, e); |
|
73 | trace.error("failed {0}: {1}", url, e); | |
74 | throw e; |
|
74 | throw e; | |
75 | } |
|
75 | } | |
76 | } |
|
76 | } | |
77 |
|
77 | |||
78 | async injectStylesheet(url: string) { |
|
78 | async injectStylesheet(url: string) { | |
79 | let d = this._map[url]; |
|
79 | let d = this._map[url]; | |
80 |
if ( |
|
80 | if (d === undefined) { | |
81 | trace.log("js {0}", url); |
|
81 | trace.log("js {0}", url); | |
82 | d = this._inject("link", { |
|
82 | d = this._inject("link", { | |
83 | type: "text/css", |
|
83 | type: "text/css", | |
84 | rel: "stylesheet", |
|
84 | rel: "stylesheet", | |
85 | href: url |
|
85 | href: url | |
86 | }); |
|
86 | }); | |
87 | this._map[url] = d; |
|
87 | this._map[url] = d; | |
88 | } |
|
88 | } | |
89 | try { |
|
89 | try { | |
90 | await d; |
|
90 | await d; | |
91 | trace.log("done {0}", url); |
|
91 | trace.log("done {0}", url); | |
92 | } catch (e) { |
|
92 | } catch (e) { | |
93 | trace.error("failed {0}: {1}", url, e); |
|
93 | trace.error("failed {0}: {1}", url, e); | |
94 | throw e; |
|
94 | throw e; | |
95 | } |
|
95 | } | |
96 | } |
|
96 | } | |
97 | } |
|
97 | } | |
98 |
|
98 | |||
99 | const instance = new DomInject(); |
|
99 | const instance = new DomInject(); | |
100 | export default instance; |
|
100 | export default instance; |
@@ -1,51 +1,51 | |||||
1 | import { id as mid} from "module"; |
|
1 | import { id as mid} from "module"; | |
2 | import { MapOf } from "@implab/core-amd/interfaces"; |
|
2 | import { MapOf } from "@implab/core-amd/interfaces"; | |
3 | import { NlsBundle } from "./NlsBundle"; |
|
3 | import { LocaleProvider, NlsBundle } from "./NlsBundle"; | |
4 | import { isPromise } from "@implab/core-amd/safe"; |
|
4 | import { isPromise } from "@implab/core-amd/safe"; | |
5 | import { locale as sysLocale } from "dojo/_base/kernel"; |
|
5 | import { locale as sysLocale } from "dojo/_base/kernel"; | |
6 | import { TraceSource } from "@implab/core-amd/log/TraceSource"; |
|
6 | import { TraceSource } from "@implab/core-amd/log/TraceSource"; | |
7 |
|
7 | |||
8 | const trace = TraceSource.get(mid); |
|
8 | const trace = TraceSource.get(mid); | |
9 |
|
9 | |||
10 | trace.debug("Current sysLocale: {0}", sysLocale); |
|
10 | trace.debug("Current sysLocale: {0}", sysLocale); | |
11 |
|
11 | |||
12 | export interface OnLoad { |
|
12 | export interface OnLoad { | |
13 |
(result?: |
|
13 | (result?: unknown): void; | |
14 |
error(err: |
|
14 | error(err: unknown): void; | |
15 | } |
|
15 | } | |
16 |
|
16 | |||
17 |
export function bundle<T extends object>(nls: T, locales?: MapOf< |
|
17 | export function bundle<T extends object>(nls: T, locales?: MapOf<LocaleProvider<object>>) { | |
18 | const nlsBundle = new NlsBundle(nls, locales); |
|
18 | const nlsBundle = new NlsBundle(nls, locales); | |
19 |
|
19 | |||
20 | const fn = (_locale?: string) => { |
|
20 | const fn = (_locale?: string) => { | |
21 | const locale = _locale || sysLocale; |
|
21 | const locale = _locale || sysLocale; | |
22 | const result = nlsBundle.getLocale(locale); |
|
22 | const result = nlsBundle.getLocale(locale); | |
23 |
|
23 | |||
24 | if (isPromise(result)) |
|
24 | if (isPromise(result)) | |
25 | throw new Error(`The bundle '${locale}' isn't loaded`); |
|
25 | throw new Error(`The bundle '${locale}' isn't loaded`); | |
26 | else |
|
26 | else | |
27 | return result; |
|
27 | return result; | |
28 | }; |
|
28 | }; | |
29 |
|
29 | |||
30 | fn.define = (pack: Partial<T>) => pack; |
|
30 | fn.define = (pack: Partial<T>) => pack; | |
31 |
fn.load = async (id: string, require: Require, cb: OnLoad, config: |
|
31 | fn.load = async (id: string, require: Require, cb: OnLoad, config: {isBuild?: boolean}) => { | |
32 | const locale = id || sysLocale; |
|
32 | const locale = id || sysLocale; | |
33 | if (config && config.isBuild) { |
|
33 | if (config && config.isBuild) { | |
34 | cb(); |
|
34 | cb(); | |
35 | } else { |
|
35 | } else { | |
36 | try { |
|
36 | try { | |
37 | await nlsBundle.getLocale(locale); |
|
37 | await nlsBundle.getLocale(locale); | |
38 | cb(); |
|
38 | cb(); | |
39 | } catch (e) { |
|
39 | } catch (e) { | |
40 | if(cb.error) { |
|
40 | if(cb.error) { | |
41 | cb.error(e); |
|
41 | cb.error(e); | |
42 | } else { |
|
42 | } else { | |
43 | // in case the loader doesn't support error reporting |
|
43 | // in case the loader doesn't support error reporting | |
44 | trace.error("Error loading {0}: {1}", locale, e); |
|
44 | trace.error("Error loading {0}: {1}", locale, e); | |
45 | } |
|
45 | } | |
46 | } |
|
46 | } | |
47 | } |
|
47 | } | |
48 | }; |
|
48 | }; | |
49 |
|
49 | |||
50 | return fn; |
|
50 | return fn; | |
51 | } |
|
51 | } |
@@ -1,147 +1,147 | |||||
1 | /** |
|
1 | /** | |
2 | * The interface for the consumer of an observable sequence |
|
2 | * The interface for the consumer of an observable sequence | |
3 | */ |
|
3 | */ | |
4 | export interface Observer<T> { |
|
4 | export interface Observer<T> { | |
5 | /** |
|
5 | /** | |
6 | * Called for the next element in the sequence |
|
6 | * Called for the next element in the sequence | |
7 | */ |
|
7 | */ | |
8 | next: (value: T) => void; |
|
8 | next: (value: T) => void; | |
9 |
|
9 | |||
10 | /** |
|
10 | /** | |
11 | * Called once when the error occurs in the sequence. |
|
11 | * Called once when the error occurs in the sequence. | |
12 | */ |
|
12 | */ | |
13 | error: (e: unknown) => void; |
|
13 | error: (e: unknown) => void; | |
14 |
|
14 | |||
15 | /** |
|
15 | /** | |
16 | * Called once at the end of the sequence. |
|
16 | * Called once at the end of the sequence. | |
17 | */ |
|
17 | */ | |
18 | complete: () => void; |
|
18 | complete: () => void; | |
19 | } |
|
19 | } | |
20 |
|
20 | |||
21 | /** |
|
21 | /** | |
22 | * The group of functions to feed an observable. This methods are provided to |
|
22 | * The group of functions to feed an observable. This methods are provided to | |
23 | * the producer to generate a stream of events. |
|
23 | * the producer to generate a stream of events. | |
24 | */ |
|
24 | */ | |
25 | export type Sink<T> = { |
|
25 | export type Sink<T> = { | |
26 | [k in keyof Observer<T>]: (this: void, ...args: Parameters<Observer<T>[k]>) => void; |
|
26 | [k in keyof Observer<T>]: (this: void, ...args: Parameters<Observer<T>[k]>) => void; | |
27 | }; |
|
27 | }; | |
28 |
|
28 | |||
29 | export type Producer<T> = (sink: Sink<T>) => (void | (() => void)); |
|
29 | export type Producer<T> = (sink: Sink<T>) => (void | (() => void)); | |
30 |
|
30 | |||
31 | export interface Unsubscribable { |
|
31 | export interface Unsubscribable { | |
32 | unsubscribe(): void; |
|
32 | unsubscribe(): void; | |
33 | } |
|
33 | } | |
34 |
|
34 | |||
35 | export const isUnsubsribable = (v: unknown): v is Unsubscribable => |
|
35 | export const isUnsubsribable = (v: unknown): v is Unsubscribable => | |
36 | v !== null && v !== undefined && typeof (v as Unsubscribable).unsubscribe === "function"; |
|
36 | v !== null && v !== undefined && typeof (v as Unsubscribable).unsubscribe === "function"; | |
37 |
|
37 | |||
38 | export const isSubsribable = <T = unknown>(v: unknown): v is Subscribable<T> => |
|
38 | export const isSubsribable = <T = unknown>(v: unknown): v is Subscribable<T> => | |
39 | v !== null && v !== undefined && typeof (v as Subscribable<unknown>).subscribe === "function"; |
|
39 | v !== null && v !== undefined && typeof (v as Subscribable<unknown>).subscribe === "function"; | |
40 |
|
40 | |||
41 | export interface Subscribable<T> { |
|
41 | export interface Subscribable<T> { | |
42 | subscribe(consumer: Partial<Observer<T>>): Unsubscribable; |
|
42 | subscribe(consumer: Partial<Observer<T>>): Unsubscribable; | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | /** The observable source of items. */ |
|
45 | /** The observable source of items. */ | |
46 | export interface Observable<T> extends Subscribable<T> { |
|
46 | export interface Observable<T> extends Subscribable<T> { | |
47 | /** Transforms elements of the sequence with the specified mapper |
|
47 | /** Transforms elements of the sequence with the specified mapper | |
48 | * |
|
48 | * | |
49 | * @param mapper The mapper used to transform the values |
|
49 | * @param mapper The mapper used to transform the values | |
50 | */ |
|
50 | */ | |
51 | map<T2>(mapper: (value: T) => T2): Observable<T2>; |
|
51 | map<T2>(mapper: (value: T) => T2): Observable<T2>; | |
52 |
|
52 | |||
53 | /** Filters elements of the sequence. The resulting sequence will |
|
53 | /** Filters elements of the sequence. The resulting sequence will | |
54 | * contain only elements which match the specified predicate. |
|
54 | * contain only elements which match the specified predicate. | |
55 | * |
|
55 | * | |
56 | * @param predicate The filter predicate. |
|
56 | * @param predicate The filter predicate. | |
57 | */ |
|
57 | */ | |
58 | filter(predicate: (value: T) => boolean): Observable<T>; |
|
58 | filter(predicate: (value: T) => boolean): Observable<T>; | |
59 |
|
59 | |||
60 | /** Applies accumulator to each value in the sequence and |
|
60 | /** Applies accumulator to each value in the sequence and | |
61 | * emits the accumulated value for each source element |
|
61 | * emits the accumulated value for each source element | |
62 | * |
|
62 | * | |
63 | * @param accumulator |
|
63 | * @param accumulator | |
64 | * @param initial |
|
64 | * @param initial | |
65 | */ |
|
65 | */ | |
66 | scan<A>(accumulator: (acc: A, value: T) => A, initial: A): Observable<A>; |
|
66 | scan<A>(accumulator: (acc: A, value: T) => A, initial: A): Observable<A>; | |
67 | } |
|
67 | } | |
68 |
|
68 | |||
69 | const noop = () => { }; |
|
69 | const noop = () => { }; | |
70 |
|
70 | |||
71 | const sink = <T>(consumer: Partial<Observer<T>>) => { |
|
71 | const sink = <T>(consumer: Partial<Observer<T>>) => { | |
72 | const { next, error, complete } = consumer; |
|
72 | const { next, error, complete } = consumer; | |
73 | return { |
|
73 | return { | |
74 | next: next ? next.bind(consumer) : noop, |
|
74 | next: next ? next.bind(consumer) : noop, | |
75 | error: error ? error.bind(consumer) : noop, |
|
75 | error: error ? error.bind(consumer) : noop, | |
76 | complete: complete ? complete.bind(consumer) : noop |
|
76 | complete: complete ? complete.bind(consumer) : noop | |
77 | } |
|
77 | }; | |
78 | }; |
|
78 | }; | |
79 |
|
79 | |||
80 | const fuse = <T>({ next, error, complete }: Sink<T>) => { |
|
80 | const fuse = <T>({ next, error, complete }: Sink<T>) => { | |
81 | let done = false; |
|
81 | let done = false; | |
82 | return { |
|
82 | return { | |
83 | next: (value: T) => { !done && next(value) }, |
|
83 | next: (value: T) => { !done && next(value); }, | |
84 | error: (e: unknown) => { !done && (done = true, error(e)) }, |
|
84 | error: (e: unknown) => { !done && (done = true, error(e)); }, | |
85 | complete: () => { !done && (done = true, complete()) } |
|
85 | complete: () => { !done && (done = true, complete()); } | |
86 | } |
|
86 | }; | |
87 | } |
|
87 | }; | |
88 |
|
88 | |||
89 | const _observe = <T>(producer: Producer<T>): Observable<T> => ({ |
|
89 | const _observe = <T>(producer: Producer<T>): Observable<T> => ({ | |
90 | subscribe: (consumer: Partial<Observer<T>>) => ({ |
|
90 | subscribe: (consumer: Partial<Observer<T>>) => ({ | |
91 | unsubscribe: producer(sink(consumer)) ?? noop |
|
91 | unsubscribe: producer(sink(consumer)) ?? noop | |
92 | }), |
|
92 | }), | |
93 | map: (mapper) => _observe(({ next, error, complete }) => |
|
93 | map: (mapper) => _observe(({ next, error, complete }) => | |
94 | producer({ |
|
94 | producer({ | |
95 | next: next !== noop ? (v: T) => next(mapper(v)) : noop, |
|
95 | next: next !== noop ? (v: T) => next(mapper(v)) : noop, | |
96 | error, |
|
96 | error, | |
97 | complete |
|
97 | complete | |
98 | }) |
|
98 | }) | |
99 | ), |
|
99 | ), | |
100 | filter: (predicate) => _observe(({ next, error, complete }) => |
|
100 | filter: (predicate) => _observe(({ next, error, complete }) => | |
101 | producer({ |
|
101 | producer({ | |
102 | next: next !== noop ? |
|
102 | next: next !== noop ? | |
103 | (v: T) => predicate(v) ? next(v) : void (0) : noop, |
|
103 | (v: T) => predicate(v) ? next(v) : void (0) : noop, | |
104 | error, |
|
104 | error, | |
105 | complete |
|
105 | complete | |
106 | }) |
|
106 | }) | |
107 | ), |
|
107 | ), | |
108 | scan: (accumulator, initial) => _observe(({ next, error, complete }) => { |
|
108 | scan: (accumulator, initial) => _observe(({ next, error, complete }) => { | |
109 | let _acc = initial; |
|
109 | let _acc = initial; | |
110 | return producer({ |
|
110 | return producer({ | |
111 | next: next !== noop ? |
|
111 | next: next !== noop ? | |
112 | (v: T) => next(_acc = accumulator(_acc, v)) : noop, |
|
112 | (v: T) => next(_acc = accumulator(_acc, v)) : noop, | |
113 | error, |
|
113 | error, | |
114 | complete |
|
114 | complete | |
115 | }); |
|
115 | }); | |
116 | }) |
|
116 | }) | |
117 | }); |
|
117 | }); | |
118 |
|
118 | |||
119 | export const observe = <T>(producer: Producer<T>): Observable<T> => ({ |
|
119 | export const observe = <T>(producer: Producer<T>): Observable<T> => ({ | |
120 | subscribe: (consumer: Partial<Observer<T>>) => ({ |
|
120 | subscribe: (consumer: Partial<Observer<T>>) => ({ | |
121 | unsubscribe: producer(fuse(sink(consumer))) ?? noop |
|
121 | unsubscribe: producer(fuse(sink(consumer))) ?? noop | |
122 | }), |
|
122 | }), | |
123 | map: (mapper) => _observe(({ next, error, complete }) => |
|
123 | map: (mapper) => _observe(({ next, error, complete }) => | |
124 | producer(fuse({ |
|
124 | producer(fuse({ | |
125 | next: next !== noop ? |
|
125 | next: next !== noop ? | |
126 | (v: T) => next(mapper(v)) : noop, |
|
126 | (v: T) => next(mapper(v)) : noop, | |
127 | error, |
|
127 | error, | |
128 | complete |
|
128 | complete | |
129 | })) |
|
129 | })) | |
130 | ), |
|
130 | ), | |
131 | filter: (predicate) => _observe(({ next, error, complete }) => |
|
131 | filter: (predicate) => _observe(({ next, error, complete }) => | |
132 | producer(fuse({ |
|
132 | producer(fuse({ | |
133 | next: next !== noop ? |
|
133 | next: next !== noop ? | |
134 | (v: T) => predicate(v) ? next(v) : void (0) : noop, |
|
134 | (v: T) => predicate(v) ? next(v) : void (0) : noop, | |
135 | error, |
|
135 | error, | |
136 | complete |
|
136 | complete | |
137 | })) |
|
137 | })) | |
138 | ), |
|
138 | ), | |
139 | scan: (accumulator, initial) => observe(({ next, error, complete }) => { |
|
139 | scan: (accumulator, initial) => observe(({ next, error, complete }) => { | |
140 | let _acc = initial; |
|
140 | let _acc = initial; | |
141 | return producer(fuse({ |
|
141 | return producer(fuse({ | |
142 | next: next !== noop ? (v: T) => next(_acc = accumulator(_acc, v)) : noop, |
|
142 | next: next !== noop ? (v: T) => next(_acc = accumulator(_acc, v)) : noop, | |
143 | error, |
|
143 | error, | |
144 | complete |
|
144 | complete | |
145 | })); |
|
145 | })); | |
146 | }) |
|
146 | }) | |
147 | }); |
|
147 | }); |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
The requested commit or file is too big and content was truncated. Show full diff |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed | ||
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 0
You need to be logged in to leave comments.
Login now