Monday, August 29, 2022

Hack library

f返回值是Awaitable, 就要用xxx_aync的函数

f返回值是Awaitable<bool>, 就要用if (await f)



self::functionA

return await EntAbc::queryFromReq($req)->queryMi->gen() |> vec ($$)

return await EntAbc::queryFromReq($req)->queryMi->gen() |> Vec\filter_nulls ($$)


Data Structure (Hack)

Vec: $a = vec[1, 2]

Dict: $b = dict["foot" => 2]

keyset: $c = keyset[1, 2]

const string USER_NAME = "we are";

static::USER_NAME 

Str\joinjoin成一个字符串$a = Str\join(Vec\reverse($vec), ',');
Str\format组成一个字符串$a = Str\format('%d is num', 2);
C\countVec的个数$a = C\count($requirements)
C\is_emptyVec是否为空C\is_empty($requirement)
C\fb\any_async
C\any
Vec中任意一个对应lambda返回true就return true$has_deprecated_req = C\fb\any($requirements, async $requirement ==> {return true},);
Vec\mapVec中每个对应lambda组成的新vec$a = await Vec\map_async($requirements, async $requirement ==> {return await static::func(1)},);
Vec\filterVec中filter对应lambda组成的新vec$a = Vec\filter($requirements, aysnc $requirement ==> 1 === 1,);
int->str类型转换$a = (string) $c
Vec\concat合并两个vec变成一个$c = Vec\concat($a, $b)
Vec\diff合并两个vec变成一个$c = Vec\diff($a, $b)
Vec\intersect求交集$c =Vec\intersect($a, $b)
C\countVec的个数$a = C\count($requirements)

返回null或者属性 Shapes::ids($student, 'id')
shape('id' => 23)

->whereInstanceOf(Abc::class)
->where(P::asyncLambda(async $commit ==> await $commit ->...->genExistence),)


Ent
EntTag::genForceFromName($vc, 'pending')->getID()

Update
EntAction: EntCommitmentMutator::updateForObject($vc, $commitment,)->setLegal('abc')->genSaveX();


EntAction: 
EntCommitmentMutator::updateForObject($vc, $commitment,)->actionUpdateStatus(Status::ABC)
->addTagIds(vec[$id])->genSaveX()
1. 自定义函数方法也就是contructor,否则需要每个都要setName('n')->setId(2)
2. 一连串动作

自动生成代码
1. Spec: EntFiled定义
2. Query: where语句
3. (controller): get方法, 
4. TGraphQL~Fields: GraphQL fields, 需要再EntSchema config exposeAccessors
5. TEnt~Action: Trait内部文件,只要加入action就会产生,用于Ent~Mutator::updateForObject($vc, $ent)->addTags($data)->genSaveWithoutReload();

Create
EntCommitmentMutator::create($vc)->setName('abc')->genChangeset() // used for Ent triggers Ent
genSave, genSaveX //return null vs exception when save and load a updated Ent

EntQuery
get
gen
genOnlyValue <==> first()->gen()
queryAll: static function. Example: EntTag::queryAll($vc)
queryX: toEdge (Return edge object). Example: queryTag()->genOnlyValue()
queryFromX: fromEdge (Return self object)


得到一个Entity
1. $tag = EntTag::genForce($vc, $f-bid)
2. $tag = EntTag::qeuryAll($vc)->whereID(P::equals($f-bid))->gen()


nonnull
如果返回值为?EntTag, 用此法去掉问号$a as nonnull



GraphQL

<<GraphQLObject>>, <<GraphQLStrongObject>> 加到class中
<<GraphQLField>> 加到方法中
<<GraphQLRootField>> 加到getAllPets方法中定义GraphQL id为all_pets, 
   对应的GraphiQL:
    {
           all_pets {
               id
           }
    }

<<GraphQLMutationRootField>>
在对应的Hack函数如GraphQL~MutationCall中定义,函数输入参数包括GraphQLInputObject
   对应的GraphiQL:
    mutation {
           create_pet {
               id,
               ower
           }
    }

<<GraphQLEnum>>
<<GraphQLInputObject>> 自定义graphql类型如lat, lng
<<GraphQLInterface>>
<<GraphQLUnino>> generic返回类型

Relay

const data = usePreloadedQuery(
   graphql`
      query all_pets
   `,
   queryRef
);

queryRef = loadQuery(...)
const {node} = useLazyLoadQuery
如果只是读,不涉及写,一般只要加入到现成的GraphQL中即可。

data.post, node.sg in React

Fragment: 一部分query的alias,另其可复用
const data = useFragment(

const [addComment] = useMutation(
这是定义在叫Hook的JS文件中,包括写入到后端和onError,onComleted类似于Ajax的call

React

useState

export default function AbcComponent({
    objectID,
    showHeader = true,
}: Props): React.MixedElement {
      const [counter, setCounter] = useState<number>(10); //10是初始值
      return (
         <div>{counter}</div>
       <Another component 
           defaultProp = {counter}
       />
        <Button
            onPress={() => setCounter(counter - 1)}
        />
     );

}

绑定后端数据到State
const [counter, setCounter] = useState(data[0].pet_name);

Flow

typed javascript: number, boolean, string, Map<string, Array<string>>

只用当变量用于component property时候采用花括号,如果在JS中不需要。
<Component
  name={name}
/>