-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15760 from chenai02/test1
feat: 优化JSBridge中类实例方法过多问题
- Loading branch information
Showing
8 changed files
with
233 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/taro-platform-harmony-hybrid/src/api/apis/interface/ClassInstanceManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import native from '../NativeApi' | ||
|
||
export class ClassInstanceManager { | ||
private static INSTANCE: ClassInstanceManager | ||
private classIdMap: Map<string, number[]> | ||
constructor () { | ||
this.classIdMap = new Map<string, number[]>() | ||
} | ||
|
||
public static getInstance (): ClassInstanceManager { | ||
if (!ClassInstanceManager.INSTANCE) { | ||
ClassInstanceManager.INSTANCE = new ClassInstanceManager() | ||
} | ||
return ClassInstanceManager.INSTANCE | ||
} | ||
|
||
createInstance (className: string, option?: any) { | ||
const objectId: number = native.createInstance({ ...option, className: className }) | ||
if (!this.classIdMap.has(className)) { | ||
this.classIdMap.set(className, []) | ||
} | ||
(this.classIdMap.get(className) as Array<number>).push(objectId) | ||
return objectId | ||
} | ||
|
||
getInstanceValue (className: string, name: string, objectId: number): any { | ||
return native.callInstance({ | ||
type: 'get', | ||
className: className, | ||
property: name, | ||
objectId: objectId | ||
}) | ||
} | ||
|
||
setInstanceValue (option: any, className: string, name: string, objectId: number): any { | ||
return native.callInstance({ | ||
option: option, | ||
className: className, | ||
type: 'set', | ||
property: name, | ||
objectId: objectId | ||
}) | ||
} | ||
|
||
setInstanceFunction (option: any, className: string, name: string, objectId: number): any { | ||
return native.callInstance({ | ||
option: option, | ||
className: className, | ||
type: 'function', | ||
property: name, | ||
objectId: objectId | ||
}) | ||
} | ||
|
||
setInstanceFunctionAsync (option: any, className: string, name: string, objectId: number): any { | ||
return native.callInstanceAsync({ | ||
option: option, | ||
className: className, | ||
type: 'function', | ||
property: name, | ||
objectId: objectId | ||
}) | ||
} | ||
|
||
destroyInstance (className: string, objectId: number) { | ||
const instances = this.classIdMap.get(className) | ||
if (instances) { | ||
const index = instances.indexOf(objectId) | ||
if (index !== -1) { | ||
instances.splice(index, 1) | ||
} | ||
native.syncAndReleaseInstance({ className: className, option: this.classIdMap.get(className) }) | ||
} | ||
} | ||
} |
Oops, something went wrong.