Skip to content

Commit

Permalink
Feature/zpf (#564)
Browse files Browse the repository at this point in the history
* iOS: fix memory leak of blur effect

* js: fix callback cause memory leak
  • Loading branch information
penfeizhou authored Dec 20, 2022
1 parent c60754c commit be4fb05
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 22 deletions.
22 changes: 15 additions & 7 deletions doric-demo/src/Counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,22 @@ class CounterView extends ViewHolder {
class CounterVM extends ViewModel<CountModel, CounterView> {
onAttached(s: CountModel, vh: CounterView) {
vh.counter.onClick = () => {
Promise.resolve(this.getState().count).then(count => {
Promise.resolve().then(() => {
this.updateState((state) => {
state.count = count + 1;
});
})
})
setInterval(() => {
// this.updateState(state => {
// state.count++
// })
this.context.callNative("demo", "test2", "sdfsf")
//loge("setInterval")
}, 1)
};
vh.number.onClick = () => {
setInterval(() => {
// this.updateState(state => {
// state.count++
// })
this.context.callNative("demo", "test")
}, 1)
}
}
onBind(s: CountModel, vh: CounterView) {
vh.number.text = `${s.count}`;
Expand Down
5 changes: 4 additions & 1 deletion doric-iOS/Example/Example/DemoLibrary.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ @interface DoricDemoPlugin : DoricNativePlugin

@implementation DoricDemoPlugin
- (void)test {
}

- (void)test2:(NSString *)val withPromise:(DoricPromise *)promise {
dispatch_async(dispatch_get_main_queue(), ^{
ShowToast(@"Test called", DoricGravityCenter);
[promise resolve:nil];
});
}
@end
Expand Down
7 changes: 4 additions & 3 deletions doric-iOS/Example/Example/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ - (void)viewDidLoad {
[Doric registerLibrary:[DemoLibrary new]];
[Doric enablePerformance:YES];
[Doric enableRenderSnapshot:YES];
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.view addSubview:[[UITableView new] also:^(UITableView *it) {
it.width = self.view.width;
it.height = self.view.height;
Expand All @@ -59,6 +56,10 @@ - (void)viewDidAppear:(BOOL)animated {
}]];
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.demoFilePaths.count;
}
Expand Down
9 changes: 6 additions & 3 deletions doric-iOS/Pod/Classes/Shader/DoricBlurEffectViewNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ @interface DoricBlurEffectView : UIVisualEffectView
@implementation DoricBlurEffectView
- (instancetype)initWithEffect:(UIVisualEffect *)effect {
if (self = [super initWithEffect:effect]) {
__weak typeof(self) weakSelf = self;
_animator = [[UIViewPropertyAnimator alloc]
initWithDuration:1
curve:UIViewAnimationCurveLinear
animations:^{
__strong typeof(weakSelf) self = weakSelf;
self.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
}];
_animator.fractionComplete = MIN(1, MAX(0, 15.0f / 200));
Expand All @@ -52,9 +54,6 @@ - (void)setRadius:(NSUInteger)radius {
self.animator.fractionComplete = MIN(1, MAX(0, (radius / 200.f)));
}

- (void)dealloc {
[self.animator stopAnimation:YES];
}
@end

@interface DoricBlurEffectViewNode ()
Expand Down Expand Up @@ -90,4 +89,8 @@ - (void)blendView:(UIView *)view forPropName:(NSString *)name propValue:(id)prop
[super blendView:view forPropName:name propValue:prop];
}
}

- (void)dealloc {
[self.visualEffectView.animator stopAnimation:YES];
};
@end
2 changes: 2 additions & 0 deletions doric-js/bundle/doric-sandbox.es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -17203,6 +17203,7 @@ var doric = (function (exports) {
}
hookBeforeNativeCall(context);
Reflect.apply(callback.resolve, context, argumentsList);
context.callbacks.delete(callbackId);
}
function jsCallReject(contextId, callbackId, args) {
var arguments$1 = arguments;
Expand All @@ -17223,6 +17224,7 @@ var doric = (function (exports) {
}
hookBeforeNativeCall(context);
Reflect.apply(callback.reject, context.entity, argumentsList);
context.callbacks.delete(callbackId);
}
var Context = /** @class */ (function () {
function Context(id) {
Expand Down
9 changes: 8 additions & 1 deletion doric-js/bundle/doric-sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,9 @@ var doric = (function (exports) {
}
hookBeforeNativeCall(context);
Reflect.apply(callback.resolve, context, argumentsList);
if (callback.retained !== true) {
context.callbacks.delete(callbackId);
}
}
function jsCallReject(contextId, callbackId, args) {
const context = gContexts.get(contextId);
Expand All @@ -1265,6 +1268,9 @@ var doric = (function (exports) {
}
hookBeforeNativeCall(context);
Reflect.apply(callback.reject, context.entity, argumentsList);
if (callback.retained !== true) {
context.callbacks.delete(callbackId);
}
}
class Context {
constructor(id) {
Expand Down Expand Up @@ -1328,7 +1334,8 @@ var doric = (function (exports) {
const functionId = uniqueId('function');
this.callbacks.set(functionId, {
resolve: func,
reject: () => { loge("This should not be called"); }
reject: () => { loge("This should not be called"); },
retained: true,
});
return functionId;
}
Expand Down
9 changes: 8 additions & 1 deletion doric-js/bundle/doric-vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,9 @@ function jsCallResolve(contextId, callbackId, args) {
}
hookBeforeNativeCall(context);
Reflect.apply(callback.resolve, context, argumentsList);
if (callback.retained !== true) {
context.callbacks.delete(callbackId);
}
}
function jsCallReject(contextId, callbackId, args) {
const context = gContexts.get(contextId);
Expand All @@ -1276,6 +1279,9 @@ function jsCallReject(contextId, callbackId, args) {
}
hookBeforeNativeCall(context);
Reflect.apply(callback.reject, context.entity, argumentsList);
if (callback.retained !== true) {
context.callbacks.delete(callbackId);
}
}
class Context {
constructor(id) {
Expand Down Expand Up @@ -1339,7 +1345,8 @@ class Context {
const functionId = uniqueId('function');
this.callbacks.set(functionId, {
resolve: func,
reject: () => { loge("This should not be called"); }
reject: () => { loge("This should not be called"); },
retained: true,
});
return functionId;
}
Expand Down
1 change: 1 addition & 0 deletions doric-js/lib/src/runtime/sandbox.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export declare class Context {
callbacks: Map<string, {
resolve: Function;
reject: Function;
retained?: boolean;
}>;
classes: Map<string, ClassType<object>>;
hookBeforeNativeCall(): void;
Expand Down
9 changes: 8 additions & 1 deletion doric-js/lib/src/runtime/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ export function jsCallResolve(contextId, callbackId, args) {
}
hookBeforeNativeCall(context);
Reflect.apply(callback.resolve, context, argumentsList);
if (callback.retained !== true) {
context.callbacks.delete(callbackId);
}
}
export function jsCallReject(contextId, callbackId, args) {
const context = gContexts.get(contextId);
Expand All @@ -63,6 +66,9 @@ export function jsCallReject(contextId, callbackId, args) {
}
hookBeforeNativeCall(context);
Reflect.apply(callback.reject, context.entity, argumentsList);
if (callback.retained !== true) {
context.callbacks.delete(callbackId);
}
}
export class Context {
constructor(id) {
Expand Down Expand Up @@ -126,7 +132,8 @@ export class Context {
const functionId = uniqueId('function');
this.callbacks.set(functionId, {
resolve: func,
reject: () => { loge("This should not be called"); }
reject: () => { loge("This should not be called"); },
retained: true,
});
return functionId;
}
Expand Down
11 changes: 9 additions & 2 deletions doric-js/src/runtime/sandbox.es5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ export function jsCallResolve(contextId: string, callbackId: string, args?: any)
}
hookBeforeNativeCall(context)
Reflect.apply(callback.resolve, context, argumentsList)
if (callback.retained !== true) {
context.callbacks.delete(callbackId)
}
}

export function jsCallReject(contextId: string, callbackId: string, args?: any) {
Expand All @@ -103,12 +106,15 @@ export function jsCallReject(contextId: string, callbackId: string, args?: any)
}
hookBeforeNativeCall(context)
Reflect.apply(callback.reject, context.entity, argumentsList)
if (callback.retained !== true) {
context.callbacks.delete(callbackId)
}
}

export class Context {
entity: any
id: string
callbacks: Map<string, { resolve: Function, reject: Function }> = new Map
callbacks: Map<string, { resolve: Function, reject: Function, retained?: boolean }> = new Map
classes: Map<string, ClassType<object>> = new Map

hookBeforeNativeCall() {
Expand Down Expand Up @@ -144,7 +150,8 @@ export class Context {
const functionId = uniqueId('function')
this.callbacks.set(functionId, {
resolve: func,
reject: () => { loge("This should not be called") }
reject: () => { loge("This should not be called") },
retained: true,
})
return functionId
}
Expand Down
11 changes: 9 additions & 2 deletions doric-js/src/runtime/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ export function jsCallResolve(contextId: string, callbackId: string, args?: any)
}
hookBeforeNativeCall(context)
Reflect.apply(callback.resolve, context, argumentsList)
if (callback.retained !== true) {
context.callbacks.delete(callbackId)
}
}

export function jsCallReject(contextId: string, callbackId: string, args?: any) {
Expand All @@ -114,12 +117,15 @@ export function jsCallReject(contextId: string, callbackId: string, args?: any)
}
hookBeforeNativeCall(context)
Reflect.apply(callback.reject, context.entity, argumentsList)
if (callback.retained !== true) {
context.callbacks.delete(callbackId)
}
}

export class Context {
entity: any
id: string
callbacks: Map<string, { resolve: Function, reject: Function }> = new Map
callbacks: Map<string, { resolve: Function, reject: Function, retained?: boolean }> = new Map
classes: Map<string, ClassType<object>> = new Map

hookBeforeNativeCall() {
Expand Down Expand Up @@ -182,7 +188,8 @@ export class Context {
const functionId = uniqueId('function')
this.callbacks.set(functionId, {
resolve: func,
reject: () => { loge("This should not be called") }
reject: () => { loge("This should not be called") },
retained: true,
})
return functionId
}
Expand Down
2 changes: 2 additions & 0 deletions doric-web/dist/index.js

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

2 changes: 1 addition & 1 deletion doric-web/dist/index.js.map

Large diffs are not rendered by default.

0 comments on commit be4fb05

Please sign in to comment.