Skip to content

Commit

Permalink
Typescript 5 support
Browse files Browse the repository at this point in the history
  • Loading branch information
VerZsuT committed Mar 29, 2023
1 parent a430ddd commit 5cfd02b
Show file tree
Hide file tree
Showing 16 changed files with 605 additions and 236 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
lib
node_modules
.idea
emr-bridge-*.tgz
emr-bridge*.tgz
.vscode
134 changes: 117 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@ provideFromMain(contextIsolation)

### Static methods and properties

Use **publicStaticMethod**/**publicStaticProperty** decorators
**Experimental decorators**

Use **publicStaticMethodExp**/**publicStaticPropertyExp**.

```ts
// Main process
import { publicStaticMethod, publicStaticProperty, Access } from 'emr-bridge'

import { publicStaticMethodExp, publicStaticPropertyExp, Access } from 'emr-bridge'

class User {
private static name = 'Name'
private static age = 20
private static money = 1000

@publicStaticProperty('userBalance')
@publicStaticPropertyExp('userBalance')
static get balance(): string {
return `${this.money}$`
}
Expand All @@ -58,20 +61,67 @@ class User {
this.money = Number(newBalance.split('$')[0])
}

@publicStaticProperty({
@publicStaticPropertyExp({
name: 'userInfo',
access: Access.get
})
static get info(): string {
return `Name: '${this.name}'; Age: '${this.age}'; Money: '${this.money}';`
}

@publicStaticMethod()
@publicStaticMethodExp()
static setName(newName: string): void {
this.name = newName
}

@publicStaticMethodExp('getUserAge')
static getAge(): number {
return this.age
}
}
```

**New decorators**

Use **publicMethod**/**publicProperty**/**publicGetter**/**publicSetter**.

```ts
// Main process

import { publicMethod, publicProperty, publicGetter, publicSetter, Access } from 'emr-bridge'

class User {
private static name = 'Name'
private static age = 20
private static money = 1000

@publicGetter('userBalance')
static get balance(): string {
return `${this.money}$`
}

@publicSetter('userBalance')
static set balance(newBalance: string) {
this.money = Number(newBalance.split('$')[0])
}

@publicGetter({
name: 'userInfo',
access: Access.get
})
static get info(): string {
return `Name: '${this.name}'; Age: '${this.age}'; Money: '${this.money}';`
}

@publicProperty()
static id = 'ID_AJWD3KLW23DK231K'

@publicMethod()
static setName(newName: string): void {
this.name = newName
}

@publicStaticMethod('getUserAge')
@publicMethod('getUserAge')
static getAge(): number {
return this.age
}
Expand All @@ -80,29 +130,79 @@ class User {

### Methods and properties

Use **providePublic** and **publicMethod**/**publicProperty** decorators.
**Experimental decorators**

Use **providePublic** and **publicMethodExp**/**publicPropertyExp**.

Without calling **providePublic**, public methods will not be accessible from renderer.

```ts
// Main process
import { providePublic, publicMethod, publicProperty, Access } from 'emr-bridge'
import { providePublic, publicMethodExp, publicPropertyExp, Access } from 'emr-bridge'

class User {
private name = 'Name'
private age = 20
private money = 1000

@publicPropertyExp()
get balance(): string {
return `${this.money}$`
}

set balance(newBalance: string) {
this.money = Number(newBalance.split('$')[0])
}

@publicPropertyExp({
name: 'userInfo',
access: Access.get
})
get info(): string {
return `Name: '${this.name}'; Age: '${this.age}'; Money: '${this.money}';`
}

@publicMethodExp()
setName(newName: string): void {
this.name = newName
}

@publicMethodExp('getUserAge')
getAge(): number {
return this.age
}
}

const user = providePublic(new User())
```

**New decorators**

Use **publicMethod**/**publicProperty**/**publicGetter**/**publicSetter**.

```ts
// Main process
import { publicMethod, publicProperty, publicGetter, publicSetter, Access } from 'emr-bridge'

class User {
private name = 'Name'
private age = 20
private money = 1000

@publicProperty()
id = 'ID_AJWD3KLW23DK231K'

@publicGetter()
get balance(): string {
return `${this.money}$`
}

@publicSetter()
set balance(newBalance: string) {
this.money = Number(newBalance.split('$')[0])
}

@publicProperty({
@publicGetter({
name: 'userInfo',
access: Access.get
})
Expand All @@ -121,7 +221,7 @@ class User {
}
}

const user = providePublic(new User())
const user = new User()
```

### Functions and variables
Expand Down Expand Up @@ -169,26 +269,26 @@ Accessing an entity outside the specified scope will result in an error being th
// Main process

// static and non-static
import { Scope, Access, providePublic, publicStaticMethod, publicMethod, publicProperty } from 'emr-bridge'
import { Scope, Access, providePublic, publicStaticMethodExp, publicMethodExp, publicPropertyExp } from 'emr-bridge'

class User {
private static age = 20
private name = 'Name'

@publicProperty({
@publicPropertyExp({
scope: Scope.preload,
access: Access.get
})
get count(): number {
return 30
}

@publicStaticMethod({ scope: Scope.preload })
@publicStaticMethodExp({ scope: Scope.preload })
static getAge(): number {
return this.age
}

@publicMethod({
@publicMethodExp({
name: 'getUserName',
scope: Scope.renderer
})
Expand Down Expand Up @@ -228,18 +328,18 @@ Using an entity outside the specified scope _will cause an error_.
// Main process

// static and non-static
import { publicStaticProperty, publicProperty, providePublic, Access } from 'emr-bridge'
import { publicStaticPropertyExp, publicPropertyExp, providePublic, Access } from 'emr-bridge'

class User {
private static name = 'Name'
private static surname = 'Surname'

@publicStaticProperty({ access: Access.get })
@publicStaticPropertyExp({ access: Access.get })
static get fullName(): string {
return `${this.name} ${this.surname}`
}

@publicProperty({
@publicPropertyExp({
name: 'userAge',
access: Access.get
})
Expand Down
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "emr-bridge",
"version": "1.1.2",
"version": "1.2.0",
"description": "Allows you to easily give access to main methods from the renderer process",
"main": "lib/index.js",
"scripts": {
Expand Down Expand Up @@ -47,6 +47,6 @@
"electron": ">=20.0.0"
},
"devDependencies": {
"typescript": "^4.9.4"
"typescript": "^5.0.2"
}
}
17 changes: 9 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
export { Access, Scope } from './enums'
export { publicGetter, publicMethod, publicProperty, publicSetter } from './scripts/newDecorators'
export {
providePublic, publicFunction, publicMethod,
publicProperty, publicStaticMethod,
publicStaticProperty, publicVariable
} from './scripts/publish'
export type {
IPublishMethodArgs, IPublishPropertyArgs, PublicFunction,
PublicProperty
} from './types'
providePublic,
publicMethod as publicMethodExp,
publicProperty as publicPropertyExp,
publicStaticMethod as publicStaticMethodExp,
publicStaticProperty as publicStaticPropertyExp
} from './scripts/oldDecorators'
export { publicFunction, publicVariable } from './scripts/publish'
export type { IPublishMethodArgs, IPublishPropertyArgs, PublicFunction, PublicProperty } from './types'

5 changes: 3 additions & 2 deletions src/preload.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { Main } from './scripts/Main'
export { provideFromMain } from './scripts/preloadProvider'
export { default as Main } from './scripts/Main'
export { default as provideFromMain } from './scripts/preloadProvider'

3 changes: 2 additions & 1 deletion src/renderer.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { Bridge } from './scripts/Bridge'
export { default as Bridge } from './scripts/Bridge'

6 changes: 4 additions & 2 deletions src/scripts/Bridge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Scope } from '../enums'
import type { IIPCResult } from '../types'
import { createProvider } from './provider'
import createProvider from './provider'

if (!window.__provider__)
throw new Error('Required methods not provided. Did you forget to call "provide" in preload?')
Expand All @@ -13,7 +13,7 @@ const info = provider.getInfo()
*
* _only for renderer process_
*/
export const Bridge = createProvider({
const Bridge = createProvider({
info,
scope: Scope.renderer,
callFunction(name: string, ...args): IIPCResult {
Expand All @@ -32,3 +32,5 @@ export const Bridge = createProvider({
})
}
})

export default Bridge
8 changes: 5 additions & 3 deletions src/scripts/Main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ipcRenderer } from 'electron'
import { IPCChannel, Scope } from '../enums'
import type { IInfo, IIPCResult } from '../types'
import { createProvider } from './provider'
import type { IIPCResult, IInfo } from '../types'
import createProvider from './provider'

const info: IInfo | undefined = ipcRenderer.sendSync(IPCChannel.getPublicInfo)

Expand All @@ -13,7 +13,7 @@ if (!info)
*
* _only for preload process_
*/
export const Main = createProvider({
const Main = createProvider({
info,
scope: Scope.preload,
callFunction(name: string, ...args): IIPCResult {
Expand All @@ -36,3 +36,5 @@ export const Main = createProvider({
})
}
})

export default Main
Loading

0 comments on commit 5cfd02b

Please sign in to comment.