Skip to content

Commit

Permalink
fix(@loong/core): fix checkAction not transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
沈昶 committed Jul 12, 2023
1 parent 346d5c3 commit 3850ebc
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 94 deletions.
6 changes: 2 additions & 4 deletions demo/main.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { bind, Component, Hook, Injectable, Prop, Watch } from '@loong-js/react-mobx';
import { action, observable } from 'mobx';
import { Action, bind, Component, Hook, Injectable, Prop, Watch } from '@loong-js/react';
import { useState } from 'react';
import { createRoot } from 'react-dom/client';
@Component()
Expand All @@ -10,7 +9,6 @@ const binder = bind(AppCompnent);
@Injectable()
class Service {
// @Prop()
@observable
count = 0;

@Prop('count')
Expand All @@ -20,7 +18,7 @@ class Service {
console.log('run >>>', this.countAlias);
}

@action.bound
@Action()
increase() {
this.count += 1;
}
Expand Down
11 changes: 6 additions & 5 deletions packages/component/src/__tests__/prop.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Watch } from '@loong-js/core';
import { observable, observe, unobserve } from '@loong-js/observer';
import { observable, observe, unobserve, setConfig } from '@loong-js/observer';
import { Component, Prop } from '..';
import { ComponentConstructor } from '../annotations/component';
import { ComponentRegistry } from '../component-registry';

setConfig({
strict: false,
});

@Component()
class TestComponent {
@Prop()
Expand Down Expand Up @@ -71,10 +75,7 @@ describe('Prop', () => {
}

const module = (TestComponent as ComponentConstructor).create?.({
observable: (value) =>
observable(value, {
strict: false,
}),
observable,
observe: (observeFunction: (...args: any[]) => any) => {
const runner = observe(observeFunction);
return () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@loong-js/core",
"version": "1.1.0-alpha.42",
"version": "1.1.0-alpha.43",
"description": "> TODO: description",
"types": "dist/index.d.ts",
"main": "dist/index.js",
Expand Down
26 changes: 9 additions & 17 deletions packages/core/src/__tests__/watch.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Injectable } from '@loong-js/core';
import { observable, observe, unobserve } from '@loong-js/observer';
import { observable, observe, unobserve, setConfig } from '@loong-js/observer';
import { Module, ModuleConstructor } from '../annotations/module';
import { Watch } from '../annotations/watch';
import { ModuleRegistry } from '../module-registry';

setConfig({
strict: false,
});

describe('Watch', () => {
test('pass in the property name string of the current class (module or service)', () => {
@Module()
Expand All @@ -17,10 +21,7 @@ describe('Watch', () => {
}

const module = (TestModule as ModuleConstructor).create?.({
observable: (value) =>
observable(value, {
strict: false,
}),
observable,
observe: (observeFunction: (...args: any[]) => any) => {
const runner = observe(observeFunction);
return () => {
Expand Down Expand Up @@ -52,10 +53,7 @@ describe('Watch', () => {
}

const module = (TestModule as ModuleConstructor).create?.({
observable: (value) =>
observable(value, {
strict: false,
}),
observable,
observe: (observeFunction: (...args: any[]) => any) => {
const runner = observe(observeFunction);
return () => {
Expand Down Expand Up @@ -91,10 +89,7 @@ describe('Watch', () => {
}

const module = (TestModule as ModuleConstructor).create?.({
observable: (value) =>
observable(value, {
strict: false,
}),
observable,
observe: (observeFunction: (...args: any[]) => any) => {
const runner = observe(observeFunction);
return () => {
Expand Down Expand Up @@ -131,10 +126,7 @@ describe('Watch', () => {
}

const module = (TestModule as ModuleConstructor).create?.({
observable: (value) =>
observable(value, {
strict: false,
}),
observable,
observe: (observeFunction: (...args: any[]) => any) => {
const runner = observe(observeFunction);
return () => {
Expand Down
52 changes: 24 additions & 28 deletions packages/observer/src/__tests__/observable.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObservable, observable, observe } from '..';
import { isObservable, observable, observe, setConfig } from '..';

describe('observable', () => {
test('should throw an error when no argument is provided or an unqualified value is provided', () => {
Expand Down Expand Up @@ -91,20 +91,18 @@ describe('observable', () => {
});

test('should only use generator functions to update data when configured strict = true', () => {
const result = observable(
{
count: 1,
setCount() {
this.count = 2;
},
setConfig({
strict: true,
checkAction(result) {
return typeof result === 'function';
},
{
strict: true,
checkAction(result) {
return typeof result === 'function';
},
}
);
});
const result = observable({
count: 1,
setCount() {
this.count = 2;
},
});

observe(() => {
console.log(result.count);
Expand All @@ -122,21 +120,19 @@ describe('observable', () => {
});

test('should automatically bind this in generator functions when strict = true and autoBind = true', () => {
const result = observable(
{
count: 1,
setCount() {
this.count = 2;
},
setConfig({
strict: true,
autoBind: true,
checkAction(result) {
return typeof result === 'function';
},
{
strict: true,
autoBind: true,
checkAction(result) {
return typeof result === 'function';
},
}
);
});
const result = observable({
count: 1,
setCount() {
this.count = 2;
},
});
const { setCount } = result;

observe(() => {
Expand Down
9 changes: 4 additions & 5 deletions packages/observer/src/action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { endBatch, startBatch } from './batch-updates';
import { ACTIONS, OPTIONS } from './constants/key-cache';
import { config } from './config';
import { ACTIONS } from './constants/key-cache';
import { rawToProxyMap } from './observable';
import { disableAllowStateUpdates, enableAllowStateUpdates } from './state-updates';
import { getKeyCache, setKeyCache } from './utils/key-cache';
Expand All @@ -11,16 +12,14 @@ export function createAction(target: object, key: unknown, action: GeneratorFunc
return result;
}

const autoBind = getKeyCache<boolean>(target, OPTIONS, 'autoBind');
const strict = getKeyCache<boolean>(target, OPTIONS, 'strict');
let scope: any;

if (autoBind !== false) {
if (config.autoBind !== false) {
const proxy = rawToProxyMap.get(target);
scope = proxy;
}
function internalAction(...args: any[]) {
return executeAction(action, scope, strict !== false, ...args);
return executeAction(action, scope, config.strict !== false, ...args);
}

setKeyCache(target, key, {
Expand Down
18 changes: 18 additions & 0 deletions packages/observer/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Key } from './reaction';

export type CheckActionType = (result: Function, target: object, key: Key) => boolean;

interface IConfig {
checkAction?: CheckActionType;
autoBind?: boolean;
strict?: boolean;
}

export let config: IConfig = {};

export function setConfig(newConfig: IConfig) {
config = {
...config,
...newConfig,
};
}
1 change: 0 additions & 1 deletion packages/observer/src/constants/key-cache.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export const EFFECTS = Symbol('effects');
export const ACTIONS = Symbol('actions');
export const OPTIONS = Symbol('options');
8 changes: 3 additions & 5 deletions packages/observer/src/handlers/base-handler.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { Handler } from '.';
import { createAction } from '../action';
import { OPTIONS } from '../constants/key-cache';
import { config } from '../config';
import { TrackOperationType, TriggerOperationType } from '../constants/operation-type';
import { CheckActionType, proxyToRawMap } from '../observable';
import { proxyToRawMap } from '../observable';
import { Key, track, trigger } from '../reaction';
import { findObservable } from '../utils/find-observable';
import { hasChanged } from '../utils/has-changed';
import { hasOwnProperty } from '../utils/has-own-property';
import { isObject } from '../utils/is-object';
import { getKeyCache } from '../utils/key-cache';

export const baseHandler: Handler = {
get(target, key: Key, receiver: object) {
const result = Reflect.get(target, key, receiver);
const checkAction = getKeyCache<CheckActionType>(target, OPTIONS, 'checkAction');

track(target, TrackOperationType.GET, key);

Expand All @@ -23,7 +21,7 @@ export const baseHandler: Handler = {
return result;
}

if (checkAction && checkAction(result, target, key)) {
if (config.checkAction?.(result, target, key)) {
return createAction(target, key, result);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/observer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ export { observable } from './observable';
export { observe, unobserve } from './observer';
export { isObservable } from './utils/is-observable';
export { raw } from './utils/raw';
export { setConfig } from './config';

export type { IObserveRunner } from './observer';
export type { IObservableOptions } from './observable';
21 changes: 3 additions & 18 deletions packages/observer/src/observable.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
import { OPTIONS } from './constants/key-cache';
import { getTargetType, TargetType } from './constants/target-type';
import { baseHandler, collectionHandler } from './handlers';
import { Key } from './reaction';
import { isObject } from './utils/is-object';
import { isObservable } from './utils/is-observable';
import { setKeyCache } from './utils/key-cache';
import { warn } from './utils/warn';

export const rawToProxyMap = new WeakMap<any, any>();

export const proxyToRawMap = new WeakMap<any, any>();

export type CheckActionType = (result: Function, target: object, key: Key) => boolean;

export interface IObservableOptions {
autoBind?: boolean;
strict?: boolean;
checkAction?: CheckActionType;
}

export function observable<T extends object>(target: T, options?: IObservableOptions): T {
export function observable<T extends object>(target: T): T {
// If it is already observable, return the target directly.
if (isObservable(target)) {
return target;
}

return createObservable(target, options);
return createObservable(target);
}

function createObservable(target: object, options?: IObservableOptions) {
function createObservable(target: object) {
if (!isObject(target)) {
warn(`${target} cannot be made observable`);
return target;
Expand All @@ -53,9 +42,5 @@ function createObservable(target: object, options?: IObservableOptions) {
rawToProxyMap.set(target, proxy);
proxyToRawMap.set(proxy, target);

if (options) {
setKeyCache(target, OPTIONS, options);
}

return proxy;
}
5 changes: 3 additions & 2 deletions packages/observer/src/reaction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { error } from './utils/error';
import { ITERATION_KEY } from './constants/iteration-key';
import { EFFECTS, OPTIONS } from './constants/key-cache';
import { EFFECTS } from './constants/key-cache';
import { TrackOperationType, TriggerOperationType } from './constants/operation-type';
import { hasAllowStateUpdates } from './state-updates';
import { getKeyCache, setKeyCache } from './utils/key-cache';
import { schedule } from './utils/schedule';
import { config } from './config';

export type Key = string | symbol;

Expand Down Expand Up @@ -116,7 +117,7 @@ export function trigger(target: object, type: TriggerOperationType, key?: unknow

// If it is a strict mode and is not executed during the action,
// the update is not allowed.
if (getKeyCache(target, OPTIONS, 'strict') === true && !hasAllowStateUpdates()) {
if (config.strict === true && !hasAllowStateUpdates()) {
error('data can only be updated in action.');
}

Expand Down
14 changes: 7 additions & 7 deletions packages/react/src/bind.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { observable, observe, unobserve } from '@loong-js/observer';
import { IObservableOptions } from '@loong-js/observer';
import { observable, observe, setConfig, unobserve } from '@loong-js/observer';
import { createBind } from '@loong-js/react-pure';
import { checkAction } from './check-action';
import { observer } from './observer';

setConfig({
checkAction,
});

export const bind = createBind({
view: observer,
observable(target: any, options?: IObservableOptions) {
return observable(target, {
checkAction,
...options,
});
observable(target: any) {
return observable(target);
},
observe<T = any>(observeFunction: (...args: any[]) => T) {
const runner = observe(observeFunction);
Expand Down

0 comments on commit 3850ebc

Please sign in to comment.