Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lzxb committed Jul 19, 2024
1 parent 8d764d4 commit ba5c91b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.4.4

- fix: multiple inheritance unbinding object errors

## 1.4.3

- fix: memory leak caused by unused `@Setup` decorator
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-class-setup",
"version": "1.4.3",
"version": "1.4.4",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
"types": "dist/index.d.ts",
Expand Down
14 changes: 8 additions & 6 deletions src/setup-reference.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
let count = 0;
let isOpen = false;
let isBind = false;

export function add() {
if (isOpen) {
export function addCount() {
// 如果还是处于绑定状态,说明上一次解绑的过程中程序执行报错了,需要重置
if (isBind) {
isBind = false;
count = 1;
} else {
isOpen = true;
count++;
}
}

const weakMap = new WeakMap<object, number>();

export function popTarget(target: object): boolean {
export function unBindTarget(target: object): boolean {
let count = weakMap.get(target);
if (typeof count === 'number') {
count--;
Expand All @@ -21,7 +22,7 @@ export function popTarget(target: object): boolean {
return false;
} else {
weakMap.delete(target);
isOpen = false;
isBind = false;
return true;
}
}
Expand All @@ -32,6 +33,7 @@ export function bindTarget(target: object) {
if (count > 0) {
weakMap.set(target, count);
count = 0;
isBind = true;
} else {
console.warn(`The instance did not use the '@Setup' decorator`);
}
Expand Down
6 changes: 3 additions & 3 deletions src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { initComputed } from './computed';
import { getOptions, getSetupOptions } from './options';
import { initDefine } from './define';
import { add, popTarget } from './setup-reference';
import { addCount, unBindTarget } from './setup-reference';
import { getPropertyDescriptors } from './property-descriptors';

export type TargetConstructor = {
Expand Down Expand Up @@ -68,9 +68,9 @@ function Setup<T extends TargetConstructor>(Target: T) {
public static [SETUP_PROPERTY_DESCRIPTOR] =
getPropertyDescriptors(Target);
public constructor(...args: any[]) {
add();
addCount();
super(...args);
if (popTarget(this)) {
if (unBindTarget(this)) {
// Vue3 needs to return, vue2 does not need to return
return initHook(reactive(this));
}
Expand Down

0 comments on commit ba5c91b

Please sign in to comment.