Skip to content

Commit 99b95bc

Browse files
authored
sync features and bugfix from 0275c2d (#1638)
1 parent 15511ad commit 99b95bc

File tree

756 files changed

+13792
-6180
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

756 files changed

+13792
-6180
lines changed

.sync

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
e8bf8f74050d43715af4216e79e297525396f39b
1+
a72216f034c9bc684506f18c4d99ed1138b70099

packages/babel-settings/babel.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const plugins = [
55
legacy: true,
66
},
77
],
8-
'@babel/plugin-proposal-function-bind',
98
[
109
'@babel/plugin-proposal-class-properties',
1110
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = require('babel-jest').createTransformer({
2+
presets: [['@babel/preset-env'], ['babel-preset-crius']],
3+
plugins: [['@babel/plugin-proposal-decorators', { legacy: true }]],
4+
ignore: [/node_modules/],
5+
rootMode: 'upward',
6+
test: './test',
7+
});

packages/babel-settings/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"@babel/plugin-proposal-decorators": "^7.8.3",
1919
"@babel/plugin-proposal-export-default-from": "^7.8.3",
2020
"@babel/plugin-proposal-export-namespace-from": "^7.8.3",
21-
"@babel/plugin-proposal-function-bind": "^7.8.3",
2221
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.8.3",
2322
"@babel/plugin-proposal-object-rest-spread": "^7.9.6",
2423
"@babel/plugin-proposal-optional-chaining": "^7.9.0",

packages/core/README.md

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ yarn add @ringcentral-integration/core
1212

1313
The decorator `storage` depends on `Storage` Module, And The decorator `globalStorage` depends on `GlobalStorage` Module.
1414

15+
> If you use `@computed(callback)`, you should make sure that the return value of its callback function is an Array of dependency collections.
16+
1517
For example:
1618

1719
```js
1820
import {
1921
RcModuleV2,
2022
state,
2123
action,
24+
computed,
2225
} from '@ringcentral-integration/core';
2326

2427
class Auth extends RcModuleV2 {
@@ -27,7 +30,12 @@ class Auth extends RcModuleV2 {
2730

2831
@action
2932
changeConnection(connected) {
30-
this.state.connected = connected;
33+
this.connected = connected;
34+
}
35+
36+
@computed(({ connected }: Auth) => [connected])
37+
get permissions() {
38+
return { writeable: this.connected, readable: true };
3139
}
3240
}
3341
```
@@ -124,36 +132,7 @@ class Auth extends RcModuleV2 {
124132

125133
@action
126134
changeConnection(connected) {
127-
this.state.connected = connected;
128-
}
129-
}
130-
```
131-
132-
### Harmony with old RcModule
133-
134-
Handle `initialize` and `constructor` with `RcModuleV2`.
135-
136-
```js
137-
class Phone extends RcModule {
138-
constructor(params) {
139-
super(params);
140-
// ...omit
141-
for (const [key, value] of Object.entries(params)) {
142-
if (value instanceof RcModuleV2) {
143-
value.parentModule = this;
144-
value.__key__ = key;
145-
}
146-
}
147-
// ...omit
148-
}
149-
150-
initialize() {
151-
// ...omit
152-
for (const value of Object.values(this)) {
153-
if (value instanceof RcModuleV2) {
154-
value.initModule();
155-
}
156-
}
135+
this.connected = connected;
157136
}
158137
}
159138
```
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { createEnum } from 'usm/lib/utils/enum';
1+
import { ObjectMap } from '../lib/ObjectMap';
22

3-
export const moduleStatuses = createEnum(
3+
export const moduleStatuses = ObjectMap.prefixKeys(
44
['pending', 'initializing', 'ready', 'resetting'],
55
'module',
66
);

packages/core/lib/ObjectMap/ObjectMap.ts

Lines changed: 66 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { find } from 'ramda';
33
const sDefinition = Symbol('definition');
44
const RUNTIME = {
55
usingFactory: false,
6+
prefixCache: new Map(),
67
};
78

89
function factory<T>(
@@ -13,7 +14,7 @@ function factory<T>(
1314
const baseFunction = descriptor.value;
1415
return {
1516
...descriptor,
16-
value(this: ThisType<T>, ...args) {
17+
value(this: ThisType<T>, ...args: any) {
1718
RUNTIME.usingFactory = true;
1819
const result = baseFunction.call(this, ...args);
1920
RUNTIME.usingFactory = false;
@@ -22,13 +23,13 @@ function factory<T>(
2223
};
2324
}
2425

25-
export type ObjectMapKey<D> = D extends ObjectMap<infer K, infer V, infer T> &
26-
infer T
26+
export type ObjectMapKey<D> = D extends ObjectMap<infer D, infer K, infer V> &
27+
infer D
2728
? K
2829
: never;
2930

30-
export type ObjectMapValue<D> = D extends ObjectMap<infer K, infer V, infer T> &
31-
infer T
31+
export type ObjectMapValue<D> = D extends ObjectMap<infer D, infer K, infer V> &
32+
infer D
3233
? V
3334
: never;
3435

@@ -37,13 +38,13 @@ export function prefixString(str: string, prefix: string = ''): string {
3738
}
3839

3940
export class ObjectMap<
40-
K extends keyof T,
41-
V extends T[K],
42-
T extends Record<string | number, any>
41+
D extends Record<string | number, any>,
42+
K extends keyof D,
43+
V extends D[K]
4344
> {
4445
private readonly [sDefinition] = new Map();
4546

46-
constructor(definition: T) {
47+
constructor(definition: D) {
4748
if (!RUNTIME.usingFactory) {
4849
throw TypeError(
4950
'Instantiating ObjectMap with `new ObjectMap(definition)` is not recommended. ' +
@@ -67,42 +68,39 @@ export class ObjectMap<
6768

6869
@factory
6970
static fromObject<
70-
K extends keyof T,
71-
V extends T[K],
72-
T extends Record<string | number, any>
73-
>(definition: T) {
74-
return new ObjectMap(definition) as ObjectMap<K, V, T> & T;
71+
D extends Record<string | number, any>,
72+
K extends keyof D,
73+
V extends D[K]
74+
>(definition: D) {
75+
return new ObjectMap(definition) as ObjectMap<D, K, V> & D;
7576
}
7677

7778
@factory
78-
static fromKeys<T extends string>(keys: T[]) {
79-
const definition = {} as Record<T, T>;
79+
static fromKeys<K extends string>(keys: K[]) {
80+
const definition = {} as Record<K, K>;
8081
for (const key of keys) {
8182
definition[key] = key;
8283
}
83-
return new ObjectMap(definition) as ObjectMap<T, T, { [K in T]: K }> &
84-
{ [K in T]: K };
84+
return new ObjectMap(definition) as ObjectMap<{ [V in K]: V }, K, K> &
85+
{ [V in K]: V };
8586
}
8687

8788
@factory
88-
static prefixKeys<T extends string, K extends T>(
89-
keys: K[],
90-
prefix: string = '',
91-
) {
89+
static prefixKeys<K extends string>(keys: K[], prefix: string = '') {
9290
const definition = {} as Record<K, string>;
9391
for (const key of keys) {
9492
definition[key] = prefixString(key, prefix);
9593
}
9694
return new ObjectMap(definition) as ObjectMap<
95+
{ [V in K]: string },
9796
K,
98-
string,
99-
{ [V in K]: string }
97+
string
10098
> &
10199
{ [V in K]: string };
102100
}
103101

104-
static getKey<K extends keyof T, V extends T[K], T>(
105-
instance: ObjectMap<K, V, T> & T,
102+
static getKey<D, K extends keyof D, V extends D[K]>(
103+
instance: ObjectMap<D, K, V> & D,
106104
value: V,
107105
): K {
108106
const [key = null] =
@@ -111,48 +109,75 @@ export class ObjectMap<
111109
return key;
112110
}
113111

114-
static entries<K extends keyof T, V extends T[K], T>(
115-
instance: ObjectMap<K, V, T> & T,
112+
static entries<D, K extends keyof D, V extends D[K]>(
113+
instance: ObjectMap<D, K, V> & D,
116114
): IterableIterator<[K, V]> {
117115
return instance[sDefinition].entries();
118116
}
119117

120-
static size<K extends keyof T, V extends T[K], T>(
121-
instance: ObjectMap<K, V, T> & T,
118+
static size<K extends keyof D, V extends D[K], D>(
119+
instance: ObjectMap<D, K, V> & D,
122120
): number {
123121
return instance[sDefinition].size;
124122
}
125123

126-
static has<K extends keyof T, V extends T[K], T>(
127-
instance: ObjectMap<K, V, T> & T,
124+
static has<K extends keyof D, V extends D[K], D>(
125+
instance: ObjectMap<D, K, V> & D,
128126
key: K,
129127
): boolean {
130128
return instance[sDefinition].has(key);
131129
}
132130

133-
static hasValue<K extends keyof T, V extends T[K], T>(
134-
instance: ObjectMap<K, V, T> & T,
131+
static hasValue<D, K extends keyof D, V extends D[K]>(
132+
instance: ObjectMap<D, K, V> & D,
135133
value: V,
136134
): boolean {
137135
return !!ObjectMap.getKey(instance, value);
138136
}
139137

140-
static keys<K extends keyof T, V extends T[K], T>(
141-
instance: ObjectMap<K, V, T> & T,
138+
static keys<D, K extends keyof D, V extends D[K]>(
139+
instance: ObjectMap<D, K, V> & D,
142140
): IterableIterator<K> {
143141
return instance[sDefinition].keys();
144142
}
145143

146-
static values<K extends keyof T, V extends T[K], T>(
147-
instance: ObjectMap<K, V, T> & T,
144+
static values<D, K extends keyof D, V extends D[K]>(
145+
instance: ObjectMap<D, K, V> & D,
148146
): IterableIterator<V> {
149147
return instance[sDefinition].values();
150148
}
151149

152-
static forEach<K extends keyof T, V extends T[K], T>(
153-
fn: (value: V, key: K, map: ObjectMap<K, V, T> & T) => void,
154-
instance: ObjectMap<K, V, T> & T,
150+
static forEach<D, K extends keyof D, V extends D[K]>(
151+
fn: (value: V, key: K, map: ObjectMap<D, K, V> & D) => void,
152+
instance: ObjectMap<D, K, V> & D,
155153
): void {
156154
return instance[sDefinition].forEach((v, k) => fn(v, k, instance));
157155
}
156+
157+
static prefixValues<
158+
D extends Record<K, string>,
159+
K extends keyof D,
160+
V extends D[K]
161+
>(instance: ObjectMap<D, K, V> & D, prefix = '') {
162+
if (prefix === '') {
163+
return instance;
164+
}
165+
if (!RUNTIME.prefixCache.has(prefix)) {
166+
RUNTIME.prefixCache.set(prefix, new Map());
167+
}
168+
if (!RUNTIME.prefixCache.get(prefix).has(instance)) {
169+
const definition = {} as Record<K, string>;
170+
ObjectMap.forEach((value, key) => {
171+
definition[key] = prefixString(value, prefix);
172+
}, instance);
173+
const prefixedInstance = ObjectMap.fromObject(definition);
174+
RUNTIME.prefixCache.get(prefix).set(instance, prefixedInstance);
175+
}
176+
return RUNTIME.prefixCache.get(prefix).get(instance) as ObjectMap<
177+
{ [V in K]: string },
178+
K,
179+
string
180+
> &
181+
{ [V in K]: string };
182+
}
158183
}

0 commit comments

Comments
 (0)